summaryrefslogtreecommitdiff
path: root/swiftstory
diff options
context:
space:
mode:
Diffstat (limited to 'swiftstory')
-rw-r--r--swiftstory/Client.py17
-rw-r--r--swiftstory/Game.py25
-rw-r--r--swiftstory/SwiftStory.py3
-rw-r--r--swiftstory/exception.py2
4 files changed, 27 insertions, 20 deletions
diff --git a/swiftstory/Client.py b/swiftstory/Client.py
index 93aa900..b1a6ae7 100644
--- a/swiftstory/Client.py
+++ b/swiftstory/Client.py
@@ -1,6 +1,7 @@
import asyncio
import logging
+from swiftstory.exception import WrongAction
from swiftstory.Status import error
@@ -14,7 +15,7 @@ class Client:
def join_game(self, game_name, lang):
if self.game is not None:
- return error('You are already in a game')
+ raise WrongAction('You are already in a game')
if lang is None:
lang = 'en'
@@ -29,37 +30,37 @@ class Client:
def play_white_card(self, card_id):
if self.game is None:
- return error('You have to join a game first')
+ 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:
- return error('You have to join a game first')
+ 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:
- return error('You have to join a game first')
+ 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:
- return error('You have to join a game first')
+ 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:
- return error('You have to join a game first')
+ 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:
- return error('You have to join a game first')
+ 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:
- return error('You have to join a game first')
+ raise WrongAction('You have to join a game first')
return self.game.try_view_black_card(self.player)
def register_notification(self, message):
diff --git a/swiftstory/Game.py b/swiftstory/Game.py
index cb02a1d..0df2e16 100644
--- a/swiftstory/Game.py
+++ b/swiftstory/Game.py
@@ -1,5 +1,6 @@
import random
+from swiftstory.exception import WrongAction
from swiftstory.Player import Player
from swiftstory.Board import Board
from swiftstory.Status import error, success
@@ -68,7 +69,7 @@ class Game:
def try_become_judge(self, player):
if self.state is not self.WAITING_NEW_JUDGE:
# TODO what if the judge has quit ?
- return error('Someone is judge already')
+ raise WrongAction('Someone is judge already')
self.judge = player
self.board.reveal_next_black_card()
@@ -84,17 +85,17 @@ class Game:
def try_play_card(self, player, card_id):
if self.state is not self.WAITING_COLLECTION:
- return error('Who asked you to play now ?!')
+ raise WrongAction('Who asked you to play now ?!')
if self.judge is player:
- return error('You\'re the judge, you silly')
+ raise WrongAction('You\'re the judge, you silly')
elif player.has_played:
- return error('You already played, you dumb ass')
+ raise WrongAction('You already played, you dumb ass')
try:
card = player.pop_card(card_id)
except IndexError:
- return error('Invalid card id')
+ raise WrongAction('Invalid card id')
player.has_played = True
@@ -107,10 +108,10 @@ class Game:
def try_collect_cards(self, player):
if self.state is not self.WAITING_COLLECTION:
- return error('Do you think it\'s the moment for colletion !?')
+ raise WrongAction('Do you think it\'s the moment for colletion !?')
if self.judge is not player:
- return error('You\'re not the judge, you fool!')
+ raise WrongAction('You\'re not the judge, you fool!')
self.board.shuffle_played_cards()
@@ -126,13 +127,13 @@ class Game:
def try_designate_card(self, player, card_id):
if self.state is not self.WAITING_DESIGNATION:
- return error('Not now, moron !')
+ raise WrongAction('Not now, moron !')
if self.judge is not player:
- return error('Who do you think you are !?')
+ raise WrongAction('Who do you think you are !?')
if card_id is None and len(self.board.played_cards) > 0:
- return error('There are cards on the board, pick one !')
+ 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
@@ -178,7 +179,7 @@ class Game:
def try_view_played_cards(self, player):
if self.state is not self.WAITING_DESIGNATION:
- return error('Not now, moron !')
+ raise WrongAction('Not now, moron !')
return success([desc for (_, desc), _ in self.board.played_cards])
@@ -188,7 +189,7 @@ class Game:
if card is not None:
return success(card[1])
- return error('The black card has not been revealed yet')
+ raise WrongAction('The black card has not been revealed yet')
def disconnect(self, player):
player.client = None
diff --git a/swiftstory/SwiftStory.py b/swiftstory/SwiftStory.py
index f9799ef..b81b0c3 100644
--- a/swiftstory/SwiftStory.py
+++ b/swiftstory/SwiftStory.py
@@ -8,6 +8,7 @@ import logging
import websockets
import swiftstory.GameManager
+from swiftstory.exception import WrongAction
from swiftstory.Client import Client
from swiftstory.Status import error
@@ -62,6 +63,8 @@ def message_received_handler(client, message):
return opcodes_map[json_msg["op"]]()
except (KeyError, TypeError):
return error('invalid command')
+ except WrongAction as e:
+ return error(str(e))
async def connection_handler(websocket, path):
diff --git a/swiftstory/exception.py b/swiftstory/exception.py
new file mode 100644
index 0000000..107e9fe
--- /dev/null
+++ b/swiftstory/exception.py
@@ -0,0 +1,2 @@
+class WrongAction(Exception):
+ pass