fix end_game

This commit is contained in:
Jannes Höke 2016-05-19 21:28:04 +02:00
parent c9f7c09a46
commit 424219d825
2 changed files with 39 additions and 3 deletions

View file

@ -127,16 +127,18 @@ class GameManager(object):
# Clear game # Clear game
for player_in_game in game.players: 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) this_users_players.remove(player_in_game)
if this_users_players: if this_users_players:
self.userid_current[player.user.id] = this_users_players[0] self.userid_current[player.user.id] = this_users_players[0]
else: else:
del self.userid_players[player.user.id] del self.userid_players[player_in_game.user.id]
del self.userid_current[player.user.id] del self.userid_current[player_in_game.user.id]
self.chatid_games[chat.id].remove(game) 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): def player_for_user_in_chat(self, user, chat):
players = self.userid_players.get(user.id, list()) players = self.userid_players.get(user.id, list())

View file

@ -71,3 +71,37 @@ class Test(unittest.TestCase):
self.assertRaises(AlreadyJoinedError, self.assertRaises(AlreadyJoinedError,
self.gm.join_game, self.gm.join_game,
*(self.user1, self.chat0)) *(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)