Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -16,7 +16,24 @@ JOIN = {}
|
|
16 |
|
17 |
WATCH = {}
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
async def error(websocket, message):
|
21 |
"""
|
22 |
Send an error message.
|
@@ -85,7 +102,7 @@ async def play(websocket, game, player, connected):
|
|
85 |
websockets.broadcast(connected, json.dumps(event))
|
86 |
|
87 |
|
88 |
-
async def start(websocket):
|
89 |
"""
|
90 |
Handle a connection from the first player: start a new game.
|
91 |
|
@@ -110,8 +127,14 @@ async def start(websocket):
|
|
110 |
"watch": watch_key,
|
111 |
}
|
112 |
await websocket.send(json.dumps(event))
|
|
|
|
|
|
|
|
|
|
|
113 |
# Receive and process moves from the first player.
|
114 |
-
await play(websocket, game, PLAYER1, connected)
|
|
|
115 |
finally:
|
116 |
del JOIN[join_key]
|
117 |
del WATCH[watch_key]
|
@@ -181,7 +204,7 @@ async def handler(websocket):
|
|
181 |
await watch(websocket, event["watch"])
|
182 |
else:
|
183 |
# First player starts a new game.
|
184 |
-
await start(websocket)
|
185 |
|
186 |
|
187 |
async def main():
|
|
|
16 |
|
17 |
WATCH = {}
|
18 |
|
19 |
+
async def create_file_structure(websocket, data, base_path='.'):
|
20 |
+
if data['type'] == 'folder':
|
21 |
+
folder_path = os.path.join(base_path, data['name'])
|
22 |
+
os.makedirs(folder_path, exist_ok=True)
|
23 |
+
for child in data['children']:
|
24 |
+
await create_file_structure(child, base_path=folder_path)
|
25 |
+
elif data['type'] == 'file':
|
26 |
+
file_path = os.path.join(base_path, data['name'])
|
27 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
28 |
+
file.write(data['content'])
|
29 |
|
30 |
+
event = {
|
31 |
+
"type": "msg",
|
32 |
+
"message": "project created",
|
33 |
+
}
|
34 |
+
await websocket.send(json.dumps(event))
|
35 |
+
|
36 |
+
|
37 |
async def error(websocket, message):
|
38 |
"""
|
39 |
Send an error message.
|
|
|
102 |
websockets.broadcast(connected, json.dumps(event))
|
103 |
|
104 |
|
105 |
+
async def start(websocket,events):
|
106 |
"""
|
107 |
Handle a connection from the first player: start a new game.
|
108 |
|
|
|
127 |
"watch": watch_key,
|
128 |
}
|
129 |
await websocket.send(json.dumps(event))
|
130 |
+
|
131 |
+
js = json.loads(events)
|
132 |
+
assert js["type"] == "init"
|
133 |
+
base_path = os.path.join(os.getcwd(), 'projects', js["project_name"])
|
134 |
+
data=js["file_structure"]
|
135 |
# Receive and process moves from the first player.
|
136 |
+
# await play(websocket, game, PLAYER1, connected)
|
137 |
+
await create_file_structure(websocket,data, base_path=base_path)
|
138 |
finally:
|
139 |
del JOIN[join_key]
|
140 |
del WATCH[watch_key]
|
|
|
204 |
await watch(websocket, event["watch"])
|
205 |
else:
|
206 |
# First player starts a new game.
|
207 |
+
await start(websocket, event)
|
208 |
|
209 |
|
210 |
async def main():
|