From 82cd09e010783b81ec58145c7682f4b8da49ac85 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 23 Dec 2021 15:27:43 +0100 Subject: Use lowercase module names Signed-off-by: Olivier Gayot --- swiftstory/Board.py | 55 ------------ swiftstory/Cards.py | 16 ---- swiftstory/Client.py | 76 ---------------- swiftstory/Game.py | 218 --------------------------------------------- swiftstory/GameManager.py | 37 -------- swiftstory/Player.py | 38 -------- swiftstory/Status.py | 8 -- swiftstory/__main__.py | 8 +- swiftstory/board.py | 55 ++++++++++++ swiftstory/cards.py | 16 ++++ swiftstory/client.py | 76 ++++++++++++++++ swiftstory/game.py | 218 +++++++++++++++++++++++++++++++++++++++++++++ swiftstory/game_manager.py | 37 ++++++++ swiftstory/player.py | 38 ++++++++ swiftstory/status.py | 8 ++ tests/test_board.py | 2 +- tests/test_cards.py | 2 +- tests/test_status.py | 2 +- tests/test_swiftstory.py | 2 +- 19 files changed, 456 insertions(+), 456 deletions(-) delete mode 100644 swiftstory/Board.py delete mode 100644 swiftstory/Cards.py delete mode 100644 swiftstory/Client.py delete mode 100644 swiftstory/Game.py delete mode 100644 swiftstory/GameManager.py delete mode 100644 swiftstory/Player.py delete mode 100644 swiftstory/Status.py create mode 100644 swiftstory/board.py create mode 100644 swiftstory/cards.py create mode 100644 swiftstory/client.py create mode 100644 swiftstory/game.py create mode 100644 swiftstory/game_manager.py create mode 100644 swiftstory/player.py create mode 100644 swiftstory/status.py diff --git a/swiftstory/Board.py b/swiftstory/Board.py deleted file mode 100644 index c9b2aee..0000000 --- a/swiftstory/Board.py +++ /dev/null @@ -1,55 +0,0 @@ -import random - - -class Board: - ''' This class automatically handles the reshuffling of different deck/heap - of cards ''' - - def __init__(self): - # List of tuples: - # (card_id, card_desc) - self.white_pick = list() - self.black_pick = list() - self.white_recycled = list() - self.black_recycled = list() - - self.current_black_card = None - - # List of tuples: - # ((card_id, card_desc), player) - self.played_cards = list() - - def reveal_next_black_card(self): - if self.current_black_card is not None: - self.black_recycled.append(self.current_black_card) - self.current_black_card = None - - if not self.black_pick: - # If we have no more cards in the deck, we need to reform one using - # the cards that have been recycled. - random.shuffle(self.black_recycled) - - self.black_pick, self.black_recycled = self.black_recycled, list() - - self.current_black_card = self.black_pick.pop() - - def pick_white_card(self): - if not self.white_pick: - # If we have no more cards in the deck, we need to reform one using - # the cards that have been recycled. - random.shuffle(self.white_recycled) - - self.white_pick, self.white_recycled = self.white_recycled, list() - - return self.white_pick.pop() - - def play_card(self, player, card): - self.played_cards.append((card, player)) - - def shuffle_played_cards(self): - random.shuffle(self.played_cards) - - def recycle_played_cards(self): - self.white_recycled += [i[0] for i in self.played_cards] - - self.played_cards = [] diff --git a/swiftstory/Cards.py b/swiftstory/Cards.py deleted file mode 100644 index d570f87..0000000 --- a/swiftstory/Cards.py +++ /dev/null @@ -1,16 +0,0 @@ -import json - - -class Cards: - @staticmethod - def get_white_cards(lang): - ''' Read the file containing the white cards and return a list of cards ''' - with open(f'usr/share/swiftstory/lang/{lang}/cards/white.json', encoding='utf-8') as fh: - return json.load(fh) - - @staticmethod - def get_black_cards(lang): - ''' Read the file containing the black cards and return a list of cards ''' - - with open(f'usr/share/swiftstory/lang/{lang}/cards/black.json', encoding='utf-8') as fh: - return json.load(fh) diff --git a/swiftstory/Client.py b/swiftstory/Client.py deleted file mode 100644 index a84783d..0000000 --- a/swiftstory/Client.py +++ /dev/null @@ -1,76 +0,0 @@ -import asyncio -import logging - -from swiftstory.exception import WrongAction, UnsupportedLanguage, JoinError - - -class Client: - def __init__(self, socket, game_manager): - self.game = None - self.game_manager = game_manager - - self.socket = socket - self.player = None - - def join_game(self, game_name, lang): - if self.game is not None: - raise WrongAction('You are already in a game') - - if lang is None: - lang = 'en' - - try: - game = self.game_manager.join_game(game_name, lang) - except UnsupportedLanguage as e: - raise JoinError(f"unsupported language: {str(e)}") from e - # XXX self.game will be assigned by game.try_join() - - return game.try_join(self) - - def play_white_card(self, card_id): - if self.game is None: - raise WrongAction('You have to join a game first') - return self.game.try_play_card(self.player, card_id) - - def pick_black_card(self): - if self.game is None: - raise WrongAction('You have to join a game first') - return self.game.try_become_judge(self.player) - - def collect_cards(self): - if self.game is None: - raise WrongAction('You have to join a game first') - return self.game.try_collect_cards(self.player) - - def designate_card(self, card_id): - if self.game is None: - raise WrongAction('You have to join a game first') - return self.game.try_designate_card(self.player, card_id) - - def view_player_cards(self): - if self.game is None: - raise WrongAction('You have to join a game first') - return self.game.try_view_player_cards(self.player) - - def view_played_cards(self): - if self.game is None: - raise WrongAction('You have to join a game first') - return self.game.try_view_played_cards(self.player) - - def view_black_card(self): - if self.game is None: - raise WrongAction('You have to join a game first') - return self.game.try_view_black_card(self.player) - - def register_notification(self, message): - async def f(): - try: - await self.socket.send(message) - except websockets.exceptions.ConnectionClosed: - logging.warning("Recipient has disconnected.") - - asyncio.create_task(f()) - - def disconnect(self): - if self.player is not None: - self.game.disconnect(self.player) diff --git a/swiftstory/Game.py b/swiftstory/Game.py deleted file mode 100644 index ea0582a..0000000 --- a/swiftstory/Game.py +++ /dev/null @@ -1,218 +0,0 @@ -import random - -from swiftstory.exception import WrongAction, JoinError -from swiftstory.Player import Player -from swiftstory.Board import Board -from swiftstory.Status import error, success - - -class Game: - WAITING_NEW_JUDGE = 0 - WAITING_COLLECTION = 1 - WAITING_DESIGNATION = 2 - - def __init__(self, white_desc, black_desc): - white_pick = list(enumerate(white_desc)) - black_pick = list(enumerate(black_desc)) - - self.state = self.WAITING_NEW_JUDGE - - self.players = [] - - self.judge = None - - self.board = Board() - self.board.white_pick = white_pick - self.board.black_pick = black_pick - - random.shuffle(self.board.white_pick) - random.shuffle(self.board.black_pick) - - def try_join(self, client): - if len(self.players) >= 10: - raise JoinError('too many players in this game') - - cards = [] - - try: - for _ in range(10): - cards.append(self.board.pick_white_card()) - except IndexError: - raise JoinError('not enough white cards for player') - - player = Player(client) - - for card in cards: - player.receive_card(card) - - client.player = player - client.game = self - - self.players.append(player) - - for p in self.players: - if p is not player: - p.register_notification({'op': 'player_joined_game'}) - - cards = [(idx, desc) for idx, (_, desc) in player.cards.items()] - - if self.state is self.WAITING_NEW_JUDGE: - state = 'waiting_judge' - elif self.state is self.WAITING_COLLECTION: - state = 'waiting_collection' - else: - state = 'waiting_designation' - - return success({'cards': cards, 'game_state': state}) - - - def try_become_judge(self, player): - if self.state is not self.WAITING_NEW_JUDGE: - # TODO what if the judge has quit ? - raise WrongAction('Someone is judge already') - - self.judge = player - self.board.reveal_next_black_card() - - self.state = self.WAITING_COLLECTION - - for p in self.players: - if p is not player: - p.register_notification({'op': 'judge_designed'}) - - return self.try_view_black_card(player) - - - def try_play_card(self, player, card_id): - if self.state is not self.WAITING_COLLECTION: - raise WrongAction('Who asked you to play now ?!') - - if self.judge is player: - raise WrongAction('You\'re the judge, you silly') - elif player.has_played: - raise WrongAction('You already played, you dumb ass') - - try: - card = player.pop_card(card_id) - except IndexError: - raise WrongAction('Invalid card id') - - player.has_played = True - - self.board.play_card(player, card) - - self.judge.register_notification({'op': 'card_played'}) - - return success({'card_id': card_id}) - - - def try_collect_cards(self, player): - if self.state is not self.WAITING_COLLECTION: - raise WrongAction("Do you think it's the moment for collection !?") - - if self.judge is not player: - raise WrongAction('You\'re not the judge, you fool!') - - self.board.shuffle_played_cards() - - # we prevent the others to play - self.state = self.WAITING_DESIGNATION - - for p in self.players: - if p is not player: - p.register_notification({'op': 'cards_collected'}) - - return self.try_view_played_cards(player) - - - def try_designate_card(self, player, card_id): - if self.state is not self.WAITING_DESIGNATION: - raise WrongAction('Not now, moron !') - - if self.judge is not player: - raise WrongAction('Who do you think you are !?') - - if card_id is None and len(self.board.played_cards) > 0: - raise WrongAction('There are cards on the board, pick one !') - - if card_id is not None or len(self.board.played_cards) > 0: - # if there are cards on the board - # TODO check exception - try: - card, winner = self.board.played_cards[card_id] - except IndexError: - return error('Invalid card') - - winner.inc_score() - - # put the cards back in the deck - self.board.recycle_played_cards() - - # reset the state of the players - for p in self.players: - if p.has_played: - idx = p.receive_card(self.board.pick_white_card()) - - p.register_notification({ - 'op': 'received_card', - 'content': { - 'card': { - 'id': p.cards[idx][0], - 'desc': p.cards[idx][1], - }, - }, - }) - p.has_played = False - - self.judge = None - - for p in self.players: - if p is not player: - p.register_notification({'op': 'judge_needed'}) - - self.state = self.WAITING_NEW_JUDGE - - return success(None) - - def try_view_player_cards(self, player): - return success([(idx, desc) for idx, (_, desc) in player.cards.items()]) - - def try_view_played_cards(self, player): - if self.state is not self.WAITING_DESIGNATION: - raise WrongAction('Not now, moron !') - - return success([desc for (_, desc), _ in self.board.played_cards]) - - def try_view_black_card(self, player): - card = self.board.current_black_card - - if card is not None: - return success(card[1]) - - raise WrongAction('The black card has not been revealed yet') - - def disconnect(self, player): - player.client = None - - if self.judge is player: - self.judge = None - - for p in self.players: - p.register_notification({'op': 'judge_needed'}) - - for card, p in self.board.played_cards: - p.receive_card(card) - - p.register_notification({ - 'op': 'received_card', - 'content': { - 'card': { - 'id': card[0], - 'desc': card[1], - }, - }, - }) - p.has_played = False - - self.board.played_cards = [] - self.state = self.WAITING_NEW_JUDGE diff --git a/swiftstory/GameManager.py b/swiftstory/GameManager.py deleted file mode 100644 index 52ce554..0000000 --- a/swiftstory/GameManager.py +++ /dev/null @@ -1,37 +0,0 @@ -import logging -import os - -from swiftstory.exception import UnsupportedLanguage -from swiftstory.Game import Game -from swiftstory.Cards import Cards - - -class GameManager: - def __init__(self): - self.langs = {} - - for filename in next(os.walk('usr/share/swiftstory/lang'))[1]: - self.langs[filename] = {} - - for lang in self.langs: - self.langs[lang]['black_cards'] = Cards.get_black_cards(lang) - self.langs[lang]['white_cards'] = Cards.get_white_cards(lang) - - self.langs[lang]['games'] = {} - - def join_game(self, game_name, lang): - if self.langs.get(lang) is None: - raise UnsupportedLanguage(lang) - - games = self.langs[lang]['games'] - black_cards = self.langs[lang]['black_cards'] - white_cards = self.langs[lang]['white_cards'] - - game = games.get(game_name) - - if game is None: - logging.info("Starting new game: %s (lang: %s)", game_name, lang) - - game = games[game_name] = Game(white_cards, black_cards) - - return game diff --git a/swiftstory/Player.py b/swiftstory/Player.py deleted file mode 100644 index 7193b64..0000000 --- a/swiftstory/Player.py +++ /dev/null @@ -1,38 +0,0 @@ -import json - - -class Player: - def __init__(self, client): - self.cards = {} - self.next_idx = 0 - - self.client = client - - self.score = 0 - - self.has_played = False - - self.name = 'default' - - def pop_card(self, card_id): - return self.cards.pop(card_id) - - def inc_score(self): - self.score += 1 - self.register_notification({ - 'op': 'updated_score', - 'content': self.score, - }) - - def receive_card(self, card): - self.cards[self.next_idx] = card - self.next_idx += 1 - return self.next_idx - 1 - - def register_notification(self, obj): - if self.client is None: - return - - message = json.dumps({'type': 'notification', 'content': obj}) - - self.client.register_notification(message) diff --git a/swiftstory/Status.py b/swiftstory/Status.py deleted file mode 100644 index fb8347b..0000000 --- a/swiftstory/Status.py +++ /dev/null @@ -1,8 +0,0 @@ -import json - - -def error(msg, code=255): - return json.dumps({'type': 'response', 'content': {'status': code, 'info': msg}}) - -def success(obj): - return json.dumps({'type': 'response', 'content': {'status': 0, 'result': obj}}) diff --git a/swiftstory/__main__.py b/swiftstory/__main__.py index 0b5d7c7..00c6e39 100644 --- a/swiftstory/__main__.py +++ b/swiftstory/__main__.py @@ -9,13 +9,13 @@ import json import logging import websockets -import swiftstory.GameManager +import swiftstory.game_manager from swiftstory.exception import WrongAction, JoinError -from swiftstory.Client import Client -from swiftstory.Status import error +from swiftstory.client import Client +from swiftstory.status import error -game_manager = swiftstory.GameManager.GameManager() +game_manager = swiftstory.game_manager.GameManager() def message_received_handler(client, message): diff --git a/swiftstory/board.py b/swiftstory/board.py new file mode 100644 index 0000000..c9b2aee --- /dev/null +++ b/swiftstory/board.py @@ -0,0 +1,55 @@ +import random + + +class Board: + ''' This class automatically handles the reshuffling of different deck/heap + of cards ''' + + def __init__(self): + # List of tuples: + # (card_id, card_desc) + self.white_pick = list() + self.black_pick = list() + self.white_recycled = list() + self.black_recycled = list() + + self.current_black_card = None + + # List of tuples: + # ((card_id, card_desc), player) + self.played_cards = list() + + def reveal_next_black_card(self): + if self.current_black_card is not None: + self.black_recycled.append(self.current_black_card) + self.current_black_card = None + + if not self.black_pick: + # If we have no more cards in the deck, we need to reform one using + # the cards that have been recycled. + random.shuffle(self.black_recycled) + + self.black_pick, self.black_recycled = self.black_recycled, list() + + self.current_black_card = self.black_pick.pop() + + def pick_white_card(self): + if not self.white_pick: + # If we have no more cards in the deck, we need to reform one using + # the cards that have been recycled. + random.shuffle(self.white_recycled) + + self.white_pick, self.white_recycled = self.white_recycled, list() + + return self.white_pick.pop() + + def play_card(self, player, card): + self.played_cards.append((card, player)) + + def shuffle_played_cards(self): + random.shuffle(self.played_cards) + + def recycle_played_cards(self): + self.white_recycled += [i[0] for i in self.played_cards] + + self.played_cards = [] diff --git a/swiftstory/cards.py b/swiftstory/cards.py new file mode 100644 index 0000000..d570f87 --- /dev/null +++ b/swiftstory/cards.py @@ -0,0 +1,16 @@ +import json + + +class Cards: + @staticmethod + def get_white_cards(lang): + ''' Read the file containing the white cards and return a list of cards ''' + with open(f'usr/share/swiftstory/lang/{lang}/cards/white.json', encoding='utf-8') as fh: + return json.load(fh) + + @staticmethod + def get_black_cards(lang): + ''' Read the file containing the black cards and return a list of cards ''' + + with open(f'usr/share/swiftstory/lang/{lang}/cards/black.json', encoding='utf-8') as fh: + return json.load(fh) diff --git a/swiftstory/client.py b/swiftstory/client.py new file mode 100644 index 0000000..a84783d --- /dev/null +++ b/swiftstory/client.py @@ -0,0 +1,76 @@ +import asyncio +import logging + +from swiftstory.exception import WrongAction, UnsupportedLanguage, JoinError + + +class Client: + def __init__(self, socket, game_manager): + self.game = None + self.game_manager = game_manager + + self.socket = socket + self.player = None + + def join_game(self, game_name, lang): + if self.game is not None: + raise WrongAction('You are already in a game') + + if lang is None: + lang = 'en' + + try: + game = self.game_manager.join_game(game_name, lang) + except UnsupportedLanguage as e: + raise JoinError(f"unsupported language: {str(e)}") from e + # XXX self.game will be assigned by game.try_join() + + return game.try_join(self) + + def play_white_card(self, card_id): + if self.game is None: + raise WrongAction('You have to join a game first') + return self.game.try_play_card(self.player, card_id) + + def pick_black_card(self): + if self.game is None: + raise WrongAction('You have to join a game first') + return self.game.try_become_judge(self.player) + + def collect_cards(self): + if self.game is None: + raise WrongAction('You have to join a game first') + return self.game.try_collect_cards(self.player) + + def designate_card(self, card_id): + if self.game is None: + raise WrongAction('You have to join a game first') + return self.game.try_designate_card(self.player, card_id) + + def view_player_cards(self): + if self.game is None: + raise WrongAction('You have to join a game first') + return self.game.try_view_player_cards(self.player) + + def view_played_cards(self): + if self.game is None: + raise WrongAction('You have to join a game first') + return self.game.try_view_played_cards(self.player) + + def view_black_card(self): + if self.game is None: + raise WrongAction('You have to join a game first') + return self.game.try_view_black_card(self.player) + + def register_notification(self, message): + async def f(): + try: + await self.socket.send(message) + except websockets.exceptions.ConnectionClosed: + logging.warning("Recipient has disconnected.") + + asyncio.create_task(f()) + + def disconnect(self): + if self.player is not None: + self.game.disconnect(self.player) diff --git a/swiftstory/game.py b/swiftstory/game.py new file mode 100644 index 0000000..cda3ddd --- /dev/null +++ b/swiftstory/game.py @@ -0,0 +1,218 @@ +import random + +from swiftstory.exception import WrongAction, JoinError +from swiftstory.player import Player +from swiftstory.board import Board +from swiftstory.status import error, success + + +class Game: + WAITING_NEW_JUDGE = 0 + WAITING_COLLECTION = 1 + WAITING_DESIGNATION = 2 + + def __init__(self, white_desc, black_desc): + white_pick = list(enumerate(white_desc)) + black_pick = list(enumerate(black_desc)) + + self.state = self.WAITING_NEW_JUDGE + + self.players = [] + + self.judge = None + + self.board = Board() + self.board.white_pick = white_pick + self.board.black_pick = black_pick + + random.shuffle(self.board.white_pick) + random.shuffle(self.board.black_pick) + + def try_join(self, client): + if len(self.players) >= 10: + raise JoinError('too many players in this game') + + cards = [] + + try: + for _ in range(10): + cards.append(self.board.pick_white_card()) + except IndexError: + raise JoinError('not enough white cards for player') + + player = Player(client) + + for card in cards: + player.receive_card(card) + + client.player = player + client.game = self + + self.players.append(player) + + for p in self.players: + if p is not player: + p.register_notification({'op': 'player_joined_game'}) + + cards = [(idx, desc) for idx, (_, desc) in player.cards.items()] + + if self.state is self.WAITING_NEW_JUDGE: + state = 'waiting_judge' + elif self.state is self.WAITING_COLLECTION: + state = 'waiting_collection' + else: + state = 'waiting_designation' + + return success({'cards': cards, 'game_state': state}) + + + def try_become_judge(self, player): + if self.state is not self.WAITING_NEW_JUDGE: + # TODO what if the judge has quit ? + raise WrongAction('Someone is judge already') + + self.judge = player + self.board.reveal_next_black_card() + + self.state = self.WAITING_COLLECTION + + for p in self.players: + if p is not player: + p.register_notification({'op': 'judge_designed'}) + + return self.try_view_black_card(player) + + + def try_play_card(self, player, card_id): + if self.state is not self.WAITING_COLLECTION: + raise WrongAction('Who asked you to play now ?!') + + if self.judge is player: + raise WrongAction('You\'re the judge, you silly') + elif player.has_played: + raise WrongAction('You already played, you dumb ass') + + try: + card = player.pop_card(card_id) + except IndexError: + raise WrongAction('Invalid card id') + + player.has_played = True + + self.board.play_card(player, card) + + self.judge.register_notification({'op': 'card_played'}) + + return success({'card_id': card_id}) + + + def try_collect_cards(self, player): + if self.state is not self.WAITING_COLLECTION: + raise WrongAction("Do you think it's the moment for collection !?") + + if self.judge is not player: + raise WrongAction('You\'re not the judge, you fool!') + + self.board.shuffle_played_cards() + + # we prevent the others to play + self.state = self.WAITING_DESIGNATION + + for p in self.players: + if p is not player: + p.register_notification({'op': 'cards_collected'}) + + return self.try_view_played_cards(player) + + + def try_designate_card(self, player, card_id): + if self.state is not self.WAITING_DESIGNATION: + raise WrongAction('Not now, moron !') + + if self.judge is not player: + raise WrongAction('Who do you think you are !?') + + if card_id is None and len(self.board.played_cards) > 0: + raise WrongAction('There are cards on the board, pick one !') + + if card_id is not None or len(self.board.played_cards) > 0: + # if there are cards on the board + # TODO check exception + try: + card, winner = self.board.played_cards[card_id] + except IndexError: + return error('Invalid card') + + winner.inc_score() + + # put the cards back in the deck + self.board.recycle_played_cards() + + # reset the state of the players + for p in self.players: + if p.has_played: + idx = p.receive_card(self.board.pick_white_card()) + + p.register_notification({ + 'op': 'received_card', + 'content': { + 'card': { + 'id': p.cards[idx][0], + 'desc': p.cards[idx][1], + }, + }, + }) + p.has_played = False + + self.judge = None + + for p in self.players: + if p is not player: + p.register_notification({'op': 'judge_needed'}) + + self.state = self.WAITING_NEW_JUDGE + + return success(None) + + def try_view_player_cards(self, player): + return success([(idx, desc) for idx, (_, desc) in player.cards.items()]) + + def try_view_played_cards(self, player): + if self.state is not self.WAITING_DESIGNATION: + raise WrongAction('Not now, moron !') + + return success([desc for (_, desc), _ in self.board.played_cards]) + + def try_view_black_card(self, player): + card = self.board.current_black_card + + if card is not None: + return success(card[1]) + + raise WrongAction('The black card has not been revealed yet') + + def disconnect(self, player): + player.client = None + + if self.judge is player: + self.judge = None + + for p in self.players: + p.register_notification({'op': 'judge_needed'}) + + for card, p in self.board.played_cards: + p.receive_card(card) + + p.register_notification({ + 'op': 'received_card', + 'content': { + 'card': { + 'id': card[0], + 'desc': card[1], + }, + }, + }) + p.has_played = False + + self.board.played_cards = [] + self.state = self.WAITING_NEW_JUDGE diff --git a/swiftstory/game_manager.py b/swiftstory/game_manager.py new file mode 100644 index 0000000..a87f67e --- /dev/null +++ b/swiftstory/game_manager.py @@ -0,0 +1,37 @@ +import logging +import os + +from swiftstory.exception import UnsupportedLanguage +from swiftstory.game import Game +from swiftstory.cards import Cards + + +class GameManager: + def __init__(self): + self.langs = {} + + for filename in next(os.walk('usr/share/swiftstory/lang'))[1]: + self.langs[filename] = {} + + for lang in self.langs: + self.langs[lang]['black_cards'] = Cards.get_black_cards(lang) + self.langs[lang]['white_cards'] = Cards.get_white_cards(lang) + + self.langs[lang]['games'] = {} + + def join_game(self, game_name, lang): + if self.langs.get(lang) is None: + raise UnsupportedLanguage(lang) + + games = self.langs[lang]['games'] + black_cards = self.langs[lang]['black_cards'] + white_cards = self.langs[lang]['white_cards'] + + game = games.get(game_name) + + if game is None: + logging.info("Starting new game: %s (lang: %s)", game_name, lang) + + game = games[game_name] = Game(white_cards, black_cards) + + return game diff --git a/swiftstory/player.py b/swiftstory/player.py new file mode 100644 index 0000000..7193b64 --- /dev/null +++ b/swiftstory/player.py @@ -0,0 +1,38 @@ +import json + + +class Player: + def __init__(self, client): + self.cards = {} + self.next_idx = 0 + + self.client = client + + self.score = 0 + + self.has_played = False + + self.name = 'default' + + def pop_card(self, card_id): + return self.cards.pop(card_id) + + def inc_score(self): + self.score += 1 + self.register_notification({ + 'op': 'updated_score', + 'content': self.score, + }) + + def receive_card(self, card): + self.cards[self.next_idx] = card + self.next_idx += 1 + return self.next_idx - 1 + + def register_notification(self, obj): + if self.client is None: + return + + message = json.dumps({'type': 'notification', 'content': obj}) + + self.client.register_notification(message) diff --git a/swiftstory/status.py b/swiftstory/status.py new file mode 100644 index 0000000..fb8347b --- /dev/null +++ b/swiftstory/status.py @@ -0,0 +1,8 @@ +import json + + +def error(msg, code=255): + return json.dumps({'type': 'response', 'content': {'status': code, 'info': msg}}) + +def success(obj): + return json.dumps({'type': 'response', 'content': {'status': 0, 'result': obj}}) diff --git a/tests/test_board.py b/tests/test_board.py index 6b0fefd..5344122 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -1,6 +1,6 @@ import unittest -from swiftstory.Board import Board +from swiftstory.board import Board class TestBoard(unittest.TestCase): diff --git a/tests/test_cards.py b/tests/test_cards.py index ea49847..a5e4437 100644 --- a/tests/test_cards.py +++ b/tests/test_cards.py @@ -1,6 +1,6 @@ import unittest -from swiftstory.Cards import Cards +from swiftstory.cards import Cards class TestCards(unittest.TestCase): diff --git a/tests/test_status.py b/tests/test_status.py index 5c48cd0..9fa65d0 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -1,7 +1,7 @@ import json import unittest -import swiftstory.Status as Status +import swiftstory.status as Status class TestStatus(unittest.TestCase): diff --git a/tests/test_swiftstory.py b/tests/test_swiftstory.py index 5c1c2bc..aac1865 100644 --- a/tests/test_swiftstory.py +++ b/tests/test_swiftstory.py @@ -1,7 +1,7 @@ import unittest import swiftstory.__main__ as SwiftStory -from swiftstory.Status import error +from swiftstory.status import error class TestSwiftStory(unittest.TestCase): -- cgit v1.2.3