Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -120,6 +120,49 @@ async def play(websocket, game, player, connected):
|
|
120 |
}
|
121 |
websockets.broadcast(connected, json.dumps(event))
|
122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
async def start(websocket,events):
|
125 |
"""
|
|
|
120 |
}
|
121 |
websockets.broadcast(connected, json.dumps(event))
|
122 |
|
123 |
+
async def exe(websocket,connected):
|
124 |
+
"""
|
125 |
+
Receive and process moves from a player.
|
126 |
+
|
127 |
+
"""
|
128 |
+
async for message in websocket:
|
129 |
+
# Parse a "play" event from the UI.
|
130 |
+
event = json.loads(message)
|
131 |
+
assert event["type"] == "cmd"
|
132 |
+
# command = event["command"]
|
133 |
+
|
134 |
+
try:
|
135 |
+
if "command" in event:
|
136 |
+
# Second player joins an existing game.
|
137 |
+
await execute_command(websocket, event["project_name"], event["command"])
|
138 |
+
elif "write" in event:
|
139 |
+
# Spectator watches an existing game.
|
140 |
+
await watch(websocket, event["watch"]) #Todo
|
141 |
+
|
142 |
+
else:
|
143 |
+
# First player starts a new game.
|
144 |
+
await start(websocket, message)
|
145 |
+
except RuntimeError as exc:
|
146 |
+
# Send an "error" event if the move was illegal.
|
147 |
+
await error(websocket, str(exc))
|
148 |
+
continue
|
149 |
+
|
150 |
+
# Send a "play" event to update the UI.
|
151 |
+
event = {
|
152 |
+
"type": "play",
|
153 |
+
"player": player,
|
154 |
+
"column": column,
|
155 |
+
"row": row,
|
156 |
+
}
|
157 |
+
websockets.broadcast(connected, json.dumps(event))
|
158 |
+
|
159 |
+
# If move is winning, send a "win" event.
|
160 |
+
if game.winner is not None:
|
161 |
+
event = {
|
162 |
+
"type": "win",
|
163 |
+
"player": game.winner,
|
164 |
+
}
|
165 |
+
websockets.broadcast(connected, json.dumps(event))
|
166 |
|
167 |
async def start(websocket,events):
|
168 |
"""
|