thaihipster commited on
Commit
7ea622a
·
verified ·
1 Parent(s): 17cbccb

Upload client.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. client.py +65 -0
client.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 2048 Game Environment Client.
3
+
4
+ Connects to a running Game2048Environment server via WebSocket.
5
+ """
6
+
7
+ from typing import Dict
8
+
9
+ # Support both in-repo and standalone imports
10
+ try:
11
+ from openenv.core.client_types import StepResult
12
+ from openenv.core.env_client import EnvClient
13
+ from openenv.core.env_server.types import State
14
+
15
+ from .models import Game2048Action, Game2048Observation, Game2048State
16
+ except ImportError:
17
+ from openenv_core.client_types import StepResult
18
+ from openenv_core.env_client import EnvClient
19
+ from openenv_core.env_server.types import State
20
+
21
+ from models import Game2048Action, Game2048Observation, Game2048State
22
+
23
+
24
+ class Game2048Env(EnvClient[Game2048Action, Game2048Observation, Game2048State]):
25
+ """
26
+ Client for the 2048 Game Environment.
27
+
28
+ Example:
29
+ >>> with Game2048Env(base_url="http://localhost:8000").sync() as env:
30
+ ... result = env.reset()
31
+ ... print(result.observation.board_text)
32
+ ... result = env.step(Game2048Action(action=2)) # move left
33
+ ... print(result.observation.score)
34
+ """
35
+
36
+ def _step_payload(self, action: Game2048Action) -> Dict:
37
+ return {"action": action.action}
38
+
39
+ def _parse_result(self, payload: Dict) -> StepResult[Game2048Observation]:
40
+ obs_data = payload.get("observation", {})
41
+ observation = Game2048Observation(
42
+ board=obs_data.get("board", []),
43
+ score=obs_data.get("score", 0),
44
+ legal_actions=obs_data.get("legal_actions", []),
45
+ max_tile=obs_data.get("max_tile", 0),
46
+ board_text=obs_data.get("board_text", ""),
47
+ done=payload.get("done", False),
48
+ reward=payload.get("reward", 0.0),
49
+ metadata=obs_data.get("metadata", {}),
50
+ )
51
+ return StepResult(
52
+ observation=observation,
53
+ reward=payload.get("reward", 0.0),
54
+ done=payload.get("done", False),
55
+ )
56
+
57
+ def _parse_state(self, payload: Dict) -> Game2048State:
58
+ return Game2048State(
59
+ episode_id=payload.get("episode_id"),
60
+ step_count=payload.get("step_count", 0),
61
+ done=payload.get("done", False),
62
+ won=payload.get("won", False),
63
+ score=payload.get("score", 0),
64
+ max_tile=payload.get("max_tile", 0),
65
+ )