blob: 196c3dc6798a9f42d3bb7d7e7069c94d14cb0de2 (
plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#!/usr/bin/env python3
import asyncio
import websockets
import swiftstory.GameManager
from swiftstory.Client import Client
from swiftstory.Status import error
import json
game_manager = swiftstory.GameManager.GameManager()
async def message_received_handler(client, websocket, message):
try:
json_msg = json.loads(message)
except JSONDecodeError:
res = error('badly formatted json')
else:
op = json_msg['op']
if op == 'join_game':
try:
game_name = json_msg['game_name']
except KeyError:
res = error('field `game_name\' is required')
else:
lang = json_msg.get('lang')
res = await client.join_game(game_name, lang)
elif op == 'view_player_cards':
res = client.view_player_cards()
elif op == 'view_black_card':
res = client.view_black_card()
elif op == 'view_played_cards':
res = client.view_played_cards()
elif op == 'pick_black_card':
res = await client.pick_black_card()
elif op == 'designate_card':
card_id = None
try:
card_id = int(json_msg['card_id'])
except (KeyError, TypeError):
pass
finally:
res = await client.designate_card(card_id)
elif op == 'play_white_card':
try:
card_id = int(json_msg['card_id'])
except KeyError:
res = error('field `card_id\' is required')
else:
res = await client.play_white_card(card_id)
elif op == 'collect_cards':
res = await client.collect_cards()
else:
res = error('invalid command')
await websocket.send(res)
async def swiftstory(websocket, path):
client = Client(websocket, game_manager)
async for message in websocket:
await message_received_handler(client, websocket, message)
await client.disconnect()
def main():
start_server = websockets.serve(swiftstory, '0.0.0.0', 1236)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
if __name__ == '__main__':
main()
|