-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbriscola_service.py
65 lines (55 loc) · 1.87 KB
/
briscola_service.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import asyncio
import json
import random
import string
import time
from threading import Thread
import gevent
import websockets as ws
from briscola.game import Game
from briscola.game_server import GameServer
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
class BriscolaService:
'''
this class is responsible for creating and managing games
'''
def __init__(self):
self.games = {}
self.game_tasks = {}
#self.current_id = 1000
self.group = asyncio.sleep(1)
async def consumer_handler(self, websocket):
"""Async websocket message handler"""
async for message in websocket:
time.sleep(1)
reply = self.consumer(message)
await websocket.send(reply)
def consumer(self, message):
"""Synchronous message handler"""
print(message)
message_json = json.loads(message)
reply = ''
if message_json['message_type'] == 'create':
game_id = id_generator()
while game_id in self.games.keys():
game_id = id_generator()
print(game_id)
new_game = GameServer(game_id)
self.games[game_id] = new_game
self.game_tasks[game_id] = asyncio.create_task(new_game.main())
reply = json.dumps(
{
'message_type' : 'create',
'result' : 'success',
'game_id' : game_id
}
)
return reply
async def main(self):
# Setup websocket connection
async with ws.connect('ws://localhost:8000/ws/gameservice/') as websocket:
# Call websocket message handler
await self.consumer_handler(websocket)
if __name__ == "__main__":
asyncio.run(BriscolaService().main())