Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -259,62 +259,6 @@ async def wirte_file(websocket, key,project_name, path, content, connected):
|
|
259 |
"data": e,
|
260 |
}
|
261 |
websockets.broadcast(connected,json.dumps(event))
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
async def execute_command(websocket, key,project_name, command, connected):
|
266 |
-
base_path = os.path.join(os.getcwd(), 'projects', key,project_name)
|
267 |
-
print(base_path)
|
268 |
-
mod_command = f'cd {base_path}&& {command}'
|
269 |
-
|
270 |
-
try:
|
271 |
-
process = await asyncio.create_subprocess_shell(
|
272 |
-
mod_command,
|
273 |
-
# cwd=base_path,
|
274 |
-
stdin=asyncio.subprocess.PIPE,
|
275 |
-
stdout=asyncio.subprocess.PIPE,
|
276 |
-
stderr=asyncio.subprocess.PIPE,
|
277 |
-
# text=True,
|
278 |
-
)
|
279 |
-
|
280 |
-
async def send_message(message):
|
281 |
-
print('sending msg')
|
282 |
-
# await websocket.send(f'data: {message}')
|
283 |
-
websockets.broadcast(connected,json.dumps(message) )
|
284 |
-
|
285 |
-
# async for line in process.stdout:
|
286 |
-
# print('sending line')
|
287 |
-
|
288 |
-
# event = {
|
289 |
-
# "type": "terminal-data",
|
290 |
-
# "data": line.strip().decode('utf-8'),
|
291 |
-
# }
|
292 |
-
# await send_message(event)
|
293 |
-
|
294 |
-
|
295 |
-
# async for line in process.stderr:
|
296 |
-
# print(f'error:{line.strip()}')
|
297 |
-
# event = {
|
298 |
-
# "type": "terminal-error",
|
299 |
-
# "data": line.strip().decode('utf-8'),
|
300 |
-
# }
|
301 |
-
# await send_message(event)
|
302 |
-
|
303 |
-
await asyncio.gather(
|
304 |
-
handle_user_input(websocket, process),
|
305 |
-
read_process_output(process, websocket)
|
306 |
-
)
|
307 |
-
|
308 |
-
return_code = await process.wait()
|
309 |
-
if return_code == 0:
|
310 |
-
# await send_message('Code executed successfully')
|
311 |
-
pass
|
312 |
-
else:
|
313 |
-
# await send_message(f'error:Execution failed with return code {return_code}')
|
314 |
-
pass
|
315 |
-
|
316 |
-
except Exception as e:
|
317 |
-
await error(websocket, str(e))
|
318 |
|
319 |
async def read_process_output(process, websocket):
|
320 |
async for line in process.stdout:
|
@@ -520,61 +464,6 @@ async def error(websocket, message):
|
|
520 |
await websocket.send(json.dumps(event))
|
521 |
|
522 |
|
523 |
-
async def replay(websocket, game):
|
524 |
-
"""
|
525 |
-
Send previous moves.
|
526 |
-
|
527 |
-
"""
|
528 |
-
# Make a copy to avoid an exception if game.moves changes while iteration
|
529 |
-
# is in progress. If a move is played while replay is running, moves will
|
530 |
-
# be sent out of order but each move will be sent once and eventually the
|
531 |
-
# UI will be consistent.
|
532 |
-
for player, column, row in game.moves.copy():
|
533 |
-
event = {
|
534 |
-
"type": "play",
|
535 |
-
"player": player,
|
536 |
-
"column": column,
|
537 |
-
"row": row,
|
538 |
-
}
|
539 |
-
await websocket.send(json.dumps(event))
|
540 |
-
|
541 |
-
|
542 |
-
async def play(websocket, game, player, connected):
|
543 |
-
"""
|
544 |
-
Receive and process moves from a player.
|
545 |
-
|
546 |
-
"""
|
547 |
-
async for message in websocket:
|
548 |
-
# Parse a "play" event from the UI.
|
549 |
-
event = json.loads(message)
|
550 |
-
assert event["type"] == "play"
|
551 |
-
column = event["column"]
|
552 |
-
|
553 |
-
try:
|
554 |
-
# Play the move.
|
555 |
-
row = game.play(player, column)
|
556 |
-
except RuntimeError as exc:
|
557 |
-
# Send an "error" event if the move was illegal.
|
558 |
-
await error(websocket, str(exc))
|
559 |
-
continue
|
560 |
-
|
561 |
-
# Send a "play" event to update the UI.
|
562 |
-
event = {
|
563 |
-
"type": "play",
|
564 |
-
"player": player,
|
565 |
-
"column": column,
|
566 |
-
"row": row,
|
567 |
-
}
|
568 |
-
websockets.broadcast(connected, json.dumps(event))
|
569 |
-
|
570 |
-
# If move is winning, send a "win" event.
|
571 |
-
if game.winner is not None:
|
572 |
-
event = {
|
573 |
-
"type": "win",
|
574 |
-
"player": game.winner,
|
575 |
-
}
|
576 |
-
websockets.broadcast(connected, json.dumps(event))
|
577 |
-
|
578 |
async def exe(websocket,connected,key):
|
579 |
"""
|
580 |
Receive and process moves from a player.
|
@@ -760,38 +649,15 @@ async def exe(websocket,connected,key):
|
|
760 |
await error(websocket, str(exc))
|
761 |
continue
|
762 |
|
763 |
-
# # Send a "play" event to update the UI.
|
764 |
-
# event = {
|
765 |
-
# "type": "play",
|
766 |
-
# "player": player,
|
767 |
-
# "column": column,
|
768 |
-
# "row": row,
|
769 |
-
# }
|
770 |
-
# websockets.broadcast(connected, json.dumps(event))
|
771 |
-
|
772 |
-
# # If move is winning, send a "win" event.
|
773 |
-
# if game.winner is not None:
|
774 |
-
# event = {
|
775 |
-
# "type": "win",
|
776 |
-
# "player": game.winner,
|
777 |
-
# }
|
778 |
-
# websockets.broadcast(connected, json.dumps(event))
|
779 |
|
780 |
async def start(websocket,events):
|
781 |
-
"""
|
782 |
-
Handle a connection from the first player: start a new game.
|
783 |
|
784 |
-
|
785 |
-
# Initialize a Connect Four game, the set of WebSocket connections
|
786 |
-
# receiving moves from this game, and secret access tokens.
|
787 |
-
game = User()
|
788 |
connected = {websocket}
|
789 |
|
790 |
join_key = secrets.token_urlsafe(12)
|
791 |
JOIN[join_key] = game, connected
|
792 |
|
793 |
-
watch_key = secrets.token_urlsafe(12)
|
794 |
-
WATCH[watch_key] = game, connected
|
795 |
|
796 |
try:
|
797 |
# Send the secret access tokens to the browser of the first player,
|
@@ -813,7 +679,7 @@ async def start(websocket,events):
|
|
813 |
await exe(websocket,connected,join_key)
|
814 |
finally:
|
815 |
del JOIN[join_key]
|
816 |
-
|
817 |
|
818 |
|
819 |
async def join(websocket, key):
|
@@ -823,7 +689,7 @@ async def join(websocket, key):
|
|
823 |
"""
|
824 |
# Find the Connect Four game.
|
825 |
try:
|
826 |
-
|
827 |
except KeyError:
|
828 |
await error(websocket, "collabration not found.")
|
829 |
return
|
@@ -831,35 +697,6 @@ async def join(websocket, key):
|
|
831 |
# Register to receive moves from this game.
|
832 |
connected.add(websocket)
|
833 |
try:
|
834 |
-
# base_path = os.path.join(os.getcwd(), 'projects', key,'your_project_name')
|
835 |
-
# mod_command = f'cd {base_path} && ls'
|
836 |
-
# process = await asyncio.create_subprocess_shell(
|
837 |
-
# mod_command,
|
838 |
-
# # cwd=base_path,
|
839 |
-
# stdin=asyncio.subprocess.PIPE,
|
840 |
-
# stdout=asyncio.subprocess.PIPE,
|
841 |
-
# stderr=asyncio.subprocess.PIPE,
|
842 |
-
# # text=True,
|
843 |
-
# )
|
844 |
-
# project_name = []
|
845 |
-
# async for line in process.stdout:
|
846 |
-
# project_name.append(line.strip().decode('utf-8'))
|
847 |
-
|
848 |
-
# return_code = await process.wait()
|
849 |
-
|
850 |
-
# current_directory = f'projects/{key}/your_project_name/{project_name[0]}'
|
851 |
-
# print(current_directory)
|
852 |
-
# print("printed") # Send the first move, in case the first player already played it.
|
853 |
-
# file_structure = await generate_file_structure(current_directory)
|
854 |
-
# # Receive and process moves from the second player.
|
855 |
-
# event = {
|
856 |
-
# "type": "join",
|
857 |
-
# "file_structure": json.dumps(file_structure),
|
858 |
-
|
859 |
-
# }
|
860 |
-
# print('in join')
|
861 |
-
# print(event)
|
862 |
-
# await websocket.send(json.dumps(event))
|
863 |
event = {
|
864 |
"type": "sendDir",
|
865 |
}
|
@@ -868,30 +705,6 @@ async def join(websocket, key):
|
|
868 |
finally:
|
869 |
connected.remove(websocket)
|
870 |
|
871 |
-
|
872 |
-
async def watch(websocket, watch_key):
|
873 |
-
"""
|
874 |
-
Handle a connection from a spectator: watch an existing game.
|
875 |
-
|
876 |
-
"""
|
877 |
-
# Find the Connect Four game.
|
878 |
-
try:
|
879 |
-
game, connected = WATCH[watch_key]
|
880 |
-
except KeyError:
|
881 |
-
await error(websocket, "Game not found.")
|
882 |
-
return
|
883 |
-
|
884 |
-
# Register to receive moves from this game.
|
885 |
-
connected.add(websocket)
|
886 |
-
try:
|
887 |
-
# Send previous moves, in case the game already started.
|
888 |
-
await replay(websocket, game)
|
889 |
-
# Keep the connection open, but don't receive any messages.
|
890 |
-
await websocket.wait_closed()
|
891 |
-
finally:
|
892 |
-
connected.remove(websocket)
|
893 |
-
|
894 |
-
|
895 |
async def handler(websocket):
|
896 |
"""
|
897 |
Handle a connection and dispatch it according to who is connecting.
|
@@ -907,10 +720,6 @@ async def handler(websocket):
|
|
907 |
if "join" in event:
|
908 |
# Second player joins an existing game.
|
909 |
await join(websocket, event["join"])
|
910 |
-
elif "watch" in event:
|
911 |
-
# Spectator watches an existing game.
|
912 |
-
await watch(websocket, event["watch"])
|
913 |
-
|
914 |
else:
|
915 |
# First player starts a new game.
|
916 |
await start(websocket, message)
|
@@ -919,19 +728,6 @@ async def handler(websocket):
|
|
919 |
# Execute a command in the project folder.
|
920 |
await execute_command(websocket, event["project_name"], event["command"])
|
921 |
|
922 |
-
# assert event["type"] == "cmd"
|
923 |
-
|
924 |
-
# if "command" in event:
|
925 |
-
# # Second player joins an existing game.
|
926 |
-
# await execute_command(websocket, project_name, event["command"])
|
927 |
-
# elif "watch" in event:
|
928 |
-
# # Spectator watches an existing game.
|
929 |
-
# await watch(websocket, event["watch"])
|
930 |
-
# else:
|
931 |
-
# # First player starts a new game.
|
932 |
-
# await start(websocket, message)
|
933 |
-
|
934 |
-
|
935 |
async def main():
|
936 |
# Set the stop condition when receiving SIGTERM.
|
937 |
loop = asyncio.get_running_loop()
|
|
|
259 |
"data": e,
|
260 |
}
|
261 |
websockets.broadcast(connected,json.dumps(event))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
async def read_process_output(process, websocket):
|
264 |
async for line in process.stdout:
|
|
|
464 |
await websocket.send(json.dumps(event))
|
465 |
|
466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
467 |
async def exe(websocket,connected,key):
|
468 |
"""
|
469 |
Receive and process moves from a player.
|
|
|
649 |
await error(websocket, str(exc))
|
650 |
continue
|
651 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
652 |
|
653 |
async def start(websocket,events):
|
|
|
|
|
654 |
|
655 |
+
user = User()
|
|
|
|
|
|
|
656 |
connected = {websocket}
|
657 |
|
658 |
join_key = secrets.token_urlsafe(12)
|
659 |
JOIN[join_key] = game, connected
|
660 |
|
|
|
|
|
661 |
|
662 |
try:
|
663 |
# Send the secret access tokens to the browser of the first player,
|
|
|
679 |
await exe(websocket,connected,join_key)
|
680 |
finally:
|
681 |
del JOIN[join_key]
|
682 |
+
|
683 |
|
684 |
|
685 |
async def join(websocket, key):
|
|
|
689 |
"""
|
690 |
# Find the Connect Four game.
|
691 |
try:
|
692 |
+
user, connected = JOIN[key]
|
693 |
except KeyError:
|
694 |
await error(websocket, "collabration not found.")
|
695 |
return
|
|
|
697 |
# Register to receive moves from this game.
|
698 |
connected.add(websocket)
|
699 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
700 |
event = {
|
701 |
"type": "sendDir",
|
702 |
}
|
|
|
705 |
finally:
|
706 |
connected.remove(websocket)
|
707 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
708 |
async def handler(websocket):
|
709 |
"""
|
710 |
Handle a connection and dispatch it according to who is connecting.
|
|
|
720 |
if "join" in event:
|
721 |
# Second player joins an existing game.
|
722 |
await join(websocket, event["join"])
|
|
|
|
|
|
|
|
|
723 |
else:
|
724 |
# First player starts a new game.
|
725 |
await start(websocket, message)
|
|
|
728 |
# Execute a command in the project folder.
|
729 |
await execute_command(websocket, event["project_name"], event["command"])
|
730 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
731 |
async def main():
|
732 |
# Set the stop condition when receiving SIGTERM.
|
733 |
loop = asyncio.get_running_loop()
|