From 424219d825458c2d5fbb32240ef3850910b4265f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Thu, 19 May 2016 21:28:04 +0200 Subject: [PATCH] fix end_game --- game_manager.py | 8 +++++--- test/test_game_manager.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/game_manager.py b/game_manager.py index 1f87f28..c26e150 100644 --- a/game_manager.py +++ b/game_manager.py @@ -127,16 +127,18 @@ class GameManager(object): # Clear game for player_in_game in game.players: - this_users_players = self.userid_players[player.user.id] + this_users_players = self.userid_players[player_in_game.user.id] this_users_players.remove(player_in_game) if this_users_players: self.userid_current[player.user.id] = this_users_players[0] else: - del self.userid_players[player.user.id] - del self.userid_current[player.user.id] + del self.userid_players[player_in_game.user.id] + del self.userid_current[player_in_game.user.id] self.chatid_games[chat.id].remove(game) + if not self.chatid_games[chat.id]: + del self.chatid_games[chat.id] def player_for_user_in_chat(self, user, chat): players = self.userid_players.get(user.id, list()) diff --git a/test/test_game_manager.py b/test/test_game_manager.py index e06882e..ab9c913 100644 --- a/test/test_game_manager.py +++ b/test/test_game_manager.py @@ -71,3 +71,37 @@ class Test(unittest.TestCase): self.assertRaises(AlreadyJoinedError, self.gm.join_game, *(self.user1, self.chat0)) + + def test_leave_game(self): + g0 = self.gm.new_game(self.chat0) + + self.gm.join_game(self.user0, self.chat0) + self.gm.join_game(self.user1, self.chat0) + + self.assertRaises(NotEnoughPlayersError, + self.gm.leave_game, + *(self.user1, self.chat0)) + + self.gm.join_game(self.user2, self.chat0) + self.gm.leave_game(self.user0, self.chat0) + + self.assertRaises(NoGameInChatError, + self.gm.leave_game, + *(self.user0, self.chat0)) + + def test_end_game(self): + g0 = self.gm.new_game(self.chat0) + + self.gm.join_game(self.user0, self.chat0) + self.gm.join_game(self.user1, self.chat0) + + self.assertEqual(len(self.gm.userid_players[0]), 1) + + g1 = self.gm.new_game(self.chat0) + self.gm.join_game(self.user2, self.chat0) + + self.gm.end_game(self.chat0, self.user0) + self.assertEqual(len(self.gm.chatid_games[0]), 1) + + self.gm.end_game(self.chat0, self.user2) + self.assertFalse(0 in self.gm.chatid_games)