summaryrefslogtreecommitdiff
path: root/swiftstory/SwiftStory.py
diff options
context:
space:
mode:
Diffstat (limited to 'swiftstory/SwiftStory.py')
-rw-r--r--swiftstory/SwiftStory.py51
1 files changed, 28 insertions, 23 deletions
diff --git a/swiftstory/SwiftStory.py b/swiftstory/SwiftStory.py
index 9e3051a..196c3dc 100644
--- a/swiftstory/SwiftStory.py
+++ b/swiftstory/SwiftStory.py
@@ -1,21 +1,18 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
+
+import asyncio
+import websockets
-from websocket_server import WebsocketServer
import swiftstory.GameManager
-import swiftstory.Client
+from swiftstory.Client import Client
from swiftstory.Status import error
import json
game_manager = swiftstory.GameManager.GameManager()
-def new_client_handler(client, server):
- client['client'] = swiftstory.Client.Client(server, client, game_manager)
-
-def client_left_handler(client, server):
- client['client'].disconnect();
-def message_received_handler(client, server, message):
+async def message_received_handler(client, websocket, message):
try:
json_msg = json.loads(message)
except JSONDecodeError:
@@ -29,15 +26,15 @@ def message_received_handler(client, server, message):
res = error('field `game_name\' is required')
else:
lang = json_msg.get('lang')
- res = client['client'].join_game(game_name, lang)
+ res = await client.join_game(game_name, lang)
elif op == 'view_player_cards':
- res = client['client'].view_player_cards()
+ res = client.view_player_cards()
elif op == 'view_black_card':
- res = client['client'].view_black_card()
+ res = client.view_black_card()
elif op == 'view_played_cards':
- res = client['client'].view_played_cards()
+ res = client.view_played_cards()
elif op == 'pick_black_card':
- res = client['client'].pick_black_card()
+ res = await client.pick_black_card()
elif op == 'designate_card':
card_id = None
try:
@@ -45,28 +42,36 @@ def message_received_handler(client, server, message):
except (KeyError, TypeError):
pass
finally:
- res = client['client'].designate_card(card_id)
+ 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 = client['client'].play_white_card(card_id)
+ res = await client.play_white_card(card_id)
elif op == 'collect_cards':
- res = client['client'].collect_cards()
+ res = await client.collect_cards()
else:
res = error('invalid command')
- server.send_message(client, res)
+ 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():
- server = WebsocketServer(1236, '0.0.0.0')
- server.set_fn_new_client(new_client_handler)
- server.set_fn_client_left(client_left_handler)
- server.set_fn_message_received(message_received_handler)
+ start_server = websockets.serve(swiftstory, '0.0.0.0', 1236)
- server.run_forever()
+ asyncio.get_event_loop().run_until_complete(start_server)
+ asyncio.get_event_loop().run_forever()
if __name__ == '__main__':
main()