-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
27 lines (20 loc) · 745 Bytes
/
server.py
File metadata and controls
27 lines (20 loc) · 745 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import asyncio
from websockets import server
from compute_actions import compute
import random
from game_adapter import json_of_game_state
from projectl import ProjectLGame
async def handler(websocket: server.WebSocketServerProtocol):
game = ProjectLGame(2)
while True:
await websocket.send(json_of_game_state(game.extract_state()))
possible_actions = compute(game)
if len(possible_actions) == 0:
return
(random_action, _) = random.choice(possible_actions)
game.step(random_action)
async def main():
async with server.serve(handler, "localhost", 8765):
print("started server on ws://localhost:8765")
await asyncio.Future() # run forever
asyncio.run(main())