diff options
-rwxr-xr-x[l---------] | cao-server | 73 | ||||
-rwxr-xr-x | server.py | 72 |
2 files changed, 72 insertions, 73 deletions
diff --git a/cao-server b/cao-server index 48c45cb..162cd5d 120000..100755 --- a/cao-server +++ b/cao-server @@ -1 +1,72 @@ -server.py
\ No newline at end of file +#!/usr/bin/env python + +from websocket_server import WebsocketServer +import CAO.GameManager +import CAO.Client +from CAO.Status import cao_error + +import json + +game_manager = CAO.GameManager.GameManager() + +def new_client_handler(client, server): + client['cao_client'] = CAO.Client.Client(server, client, game_manager) + +def client_left_handler(client, server): + client['cao_client'].disconnect(); + +def message_received_handler(client, server, message): + try: + json_msg = json.loads(message) + except: + res = cao_error('badly formatted json') + else: + op = json_msg['op'] + if op == 'join_game': + try: + game_name = json_msg['game_name'] + except KeyError: + res = cao_error('field `game_name\' is required') + else: + lang = json_msg.get('lang') + res = client['cao_client'].join_game(game_name, lang) + elif op == 'view_player_cards': + res = client['cao_client'].view_player_cards() + elif op == 'view_black_card': + res = client['cao_client'].view_black_card() + elif op == 'view_played_cards': + res = client['cao_client'].view_played_cards() + elif op == 'pick_black_card': + res = client['cao_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 = client['cao_client'].designate_card(card_id) + elif op == 'play_white_card': + try: + card_id = int(json_msg['card_id']) + except KeyError: + res = cao_error('field `card_id\' is required') + else: + res = client['cao_client'].play_white_card(card_id) + elif op == 'collect_cards': + res = client['cao_client'].collect_cards() + else: + res = cao_error('invalid command') + + server.send_message(client, res) + +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) + + server.run_forever() + +if __name__ == '__main__': + main() diff --git a/server.py b/server.py deleted file mode 100755 index 162cd5d..0000000 --- a/server.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env python - -from websocket_server import WebsocketServer -import CAO.GameManager -import CAO.Client -from CAO.Status import cao_error - -import json - -game_manager = CAO.GameManager.GameManager() - -def new_client_handler(client, server): - client['cao_client'] = CAO.Client.Client(server, client, game_manager) - -def client_left_handler(client, server): - client['cao_client'].disconnect(); - -def message_received_handler(client, server, message): - try: - json_msg = json.loads(message) - except: - res = cao_error('badly formatted json') - else: - op = json_msg['op'] - if op == 'join_game': - try: - game_name = json_msg['game_name'] - except KeyError: - res = cao_error('field `game_name\' is required') - else: - lang = json_msg.get('lang') - res = client['cao_client'].join_game(game_name, lang) - elif op == 'view_player_cards': - res = client['cao_client'].view_player_cards() - elif op == 'view_black_card': - res = client['cao_client'].view_black_card() - elif op == 'view_played_cards': - res = client['cao_client'].view_played_cards() - elif op == 'pick_black_card': - res = client['cao_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 = client['cao_client'].designate_card(card_id) - elif op == 'play_white_card': - try: - card_id = int(json_msg['card_id']) - except KeyError: - res = cao_error('field `card_id\' is required') - else: - res = client['cao_client'].play_white_card(card_id) - elif op == 'collect_cards': - res = client['cao_client'].collect_cards() - else: - res = cao_error('invalid command') - - server.send_message(client, res) - -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) - - server.run_forever() - -if __name__ == '__main__': - main() |