diff --git a/SBA_Serv/Game/Tournaments.py b/SBA_Serv/Game/Tournaments.py index 080a98a..245b6e0 100644 --- a/SBA_Serv/Game/Tournaments.py +++ b/SBA_Serv/Game/Tournaments.py @@ -12,8 +12,10 @@ def __init__(self, cfg, leaderfunc): self._currentgroup = 0 self._finalgroup = [] self._finalround = False - self._finalwinner = None + self._finalwinners = None self._leaderfunc = leaderfunc + self._primary_victory = cfg.get("Game", "primary_victory_attr") + self._secondary_victory = cfg.get("Game", "secondary_victory_attr") def is_initialized(self): return self._initialized @@ -39,6 +41,12 @@ def initialize(self, players): #next self._initialized = True + def check_score_equal(self, player1, player2): + """ + Checks to see if two player scores are equal according to the current game rules. + """ + return getattr(player1, self._primary_victory) == getattr(player2, self._primary_victory) and getattr(player1, self._secondary_victory) == getattr(player2, self._secondary_victory) + def check_results(self, players, stats): """ Get the end results, pre-sorted (top players first in list) @@ -51,7 +59,12 @@ def check_results(self, players, stats): #next else: logging.info("Final Round Winner %s stats: %s", players[0].name, stats[0]) - self._finalwinner = players[0] + self._finalwinners = [players[0]] + i = 1 + # Check for ties + while i < len(players) and self.check_score_equal(players[0], players[i]): + self._finalwinners.append(players[i]) + i += 1 def next_round(self): """ @@ -115,8 +128,10 @@ def gui_draw_tournament_bracket(self, screen, flags, trackplayer): pygame.draw.line(screen, (192, 192, 192), (810, py), (810, y)) pygame.draw.line(screen, (192, 192, 192), (800, y), (810, y)) - if self._finalwinner: - screen.blit(self._tfont.render(self._finalwinner.name, False, (128, 255, 255)), (835, py + (y - py) / 2)) + if self._finalwinners: + for player in self._finalwinners: + screen.blit(self._tfont.render(player.name, False, (128, 255, 255)), (835, py + (y - py) / 2)) + y += 36 return y @@ -129,8 +144,6 @@ def __init__(self, cfg, leaderfunc): self._toplay = cfg.getint("Wildcard", "number") self._andties = cfg.getboolean("Wildcard", "take_ties") self._wildround = False - self._primary_victory = cfg.get("Game", "primary_victory_attr") - self._secondary_victory = cfg.get("Game", "secondary_victory_attr") def check_results(self, players, stats): """ @@ -149,17 +162,11 @@ def check_wildcard(self): # do check before, so we have 'next' player after appending last to check ties if x >= self._toplay and \ (not self._andties or - (len(self._wildgroup) >= 1 and not self.__check_score_equal(player, self._wildgroup[-1]))): + (len(self._wildgroup) >= 1 and not self.check_score_equal(player, self._wildgroup[-1]))): break x += 1 self._wildgroup.append(player) - def __check_score_equal(self, player1, player2): - """ - Checks to see if two player scores are equal according to the current game rules. - """ - return getattr(player1, self._primary_victory) == getattr(player2, self._primary_victory) and getattr(player1, self._secondary_victory) == getattr(player2, self._secondary_victory) - def next_round(self): """ Setup a subsequent round of players. diff --git a/SBA_Serv/SBA_Serv.pyproj b/SBA_Serv/SBA_Serv.pyproj index 48b60da..f3706cc 100644 --- a/SBA_Serv/SBA_Serv.pyproj +++ b/SBA_Serv/SBA_Serv.pyproj @@ -183,6 +183,7 @@ + diff --git a/SBA_Serv/Tests/HungryHungryBaublesTestCases.py b/SBA_Serv/Tests/HungryHungryBaublesTestCases.py index 7d4b605..72c3f38 100644 --- a/SBA_Serv/Tests/HungryHungryBaublesTestCases.py +++ b/SBA_Serv/Tests/HungryHungryBaublesTestCases.py @@ -110,7 +110,7 @@ class HungryHungryBaublesTournamentTestCases(SBAGUITestCase): Test cases for Hungry Hungry Baubles game w/ tournamnet. """ def get_config_filename(self): - return "test_hungryhungrybaubles.cfg", "test_tournament.cfg" + return "test_hungryhungrybaubles.cfg", "test_tournament_short.cfg" def test_ship_not_added_until_after_start(self): """ @@ -131,6 +131,74 @@ def test_ship_not_added_until_after_start(self): self.assertTrue(ship in self.game.world, "Ship didn't get added to world") self.assertEqual(len(self.game.world), 2, "Not Ship+Bauble in world") + def test_small_tournament_with_ties(self): + ships = [] + numships = 8 + groups = self.cfg.getint("Tournament", "groups") + + for x in xrange(numships): + ships.append(AIShip_SetList("Move", self.game.world.mid_point(-500 + x * 100, -500 + x * 100), self.game, [ + "IdleCommand(self, random.randint(1, 5))", + ])) + + for x in xrange(groups + 1): + self.assertEqual(len(self.game.world), 0, "Objects in World before round") + self.assertFalse(self.game.round_get_has_started(), "Game Timer Running") + + self.game.round_start() + + time.sleep(2.0) + + if x == groups: + # Final Round + self.assertEqual(len(self.game.world), 8, "Found more than 4 ships + baubles in world") + + for player in self.game._tmanager._finalgroup: + self.assertTrue(player.object in self.game.world, "Player's Ship not in final tournament") + self.assertLess(player.score, 1, "Players shouldn't have score entering final round") + + for i in xrange(x + 2): + self.game.world.append(Bauble(intpos(self.game.game_get_current_player_list()[i % 2].object.body.position), 1)) + time.sleep(0.5) + + self.assertTrue(self.game.round_get_has_started(), "Game Timer NOT Running") + + time.sleep(7.5) + + leader = None + for player in self.game.game_get_current_leader_list(): + print player.name, player.score + if leader == None: + leader = player + #self.assertGreater(player.score, 10, "Each player should have scored") + + time.sleep(7) + + self.assertFalse(self.game.round_get_has_started(), "Game Timer Running After") + + # Round should end + self.assertEqual(len(self.game.world), 0, "Objects in World after round") + + # No player should have died + self.assertEqual(self.game.game_get_current_player_list()[0].deaths, 0, "Player died") + self.assertEqual(self.game.game_get_current_player_list()[1].deaths, 0, "Player died") + + # the leader should still have points + self.assertGreater(leader.score, 0, "Leader should have scored points") + + if x < groups: + self.assertEqual(len(self.game._tmanager._finalgroup), x+1, "Ship not added to final group") + self.assertIn(leader, self.game._tmanager._finalgroup, "Correct player not added to final group") + else: + # final round + self.assertIsNotNone(self.game._tmanager._finalwinners, "Final Winner not marked") + self.assertIn(leader, self.game._tmanager._finalwinners, "Incorrect leader chosen") + pass + #eif + #next round + + time.sleep(3) + class HungryHungryBaublesTournamentWildTestCases(SBAGUITestCase): """ @@ -161,6 +229,7 @@ def test_full_wild_tournment_with_ties(self): time.sleep(2.0) if x == groups: + # Final Round self.assertEqual(len(self.game.world), 12, "Found more than 6 ships + baubles in world") for player in self.game._tmanager._finalgroup: @@ -201,8 +270,8 @@ def test_full_wild_tournment_with_ties(self): self.assertIn(leader, self.game._tmanager._finalgroup, "Correct player not added to final group") else: # final round - self.assertIsNotNone(self.game._tmanager._finalwinner, "Final Winner not marked") - self.assertEqual(self.game._tmanager._finalwinner, leader, "Incorrect leader chosen") + self.assertIsNotNone(self.game._tmanager._finalwinners, "Final Winner not marked") + self.assertIn(leader, self.game._tmanager._finalwinners, "Incorrect leader chosen") pass #eif #next round diff --git a/SBA_Serv/Tests/ServerTestCases.py b/SBA_Serv/Tests/ServerTestCases.py index b7e8383..a8174ff 100644 --- a/SBA_Serv/Tests/ServerTestCases.py +++ b/SBA_Serv/Tests/ServerTestCases.py @@ -562,8 +562,8 @@ def test_running_network_tournament(self): self.assertIn(leader, self.game._tmanager._finalgroup, "Correct player not added to final group") else: # final round - self.assertIsNotNone(self.game._tmanager._finalwinner, "Final Winner not marked") - self.assertEqual(self.game._tmanager._finalwinner, leader, "Incorrect leader chosen") + self.assertIsNotNone(self.game._tmanager._finalwinners, "Final Winner not marked") + self.assertIn(leader, self.game._tmanager._finalwinner, "Incorrect leader chosen") pass #eif #next round diff --git a/SBA_Serv/Tests/SurvivorTestCases.py b/SBA_Serv/Tests/SurvivorTestCases.py index dd5511b..095bbf7 100644 --- a/SBA_Serv/Tests/SurvivorTestCases.py +++ b/SBA_Serv/Tests/SurvivorTestCases.py @@ -97,8 +97,8 @@ def test_full_tournament(self): self.assertIn(leader, self.game._tmanager._finalgroup, "Correct player not added to final group") else: # final round - self.assertIsNotNone(self.game._tmanager._finalwinner, "Final Winner not marked") - self.assertEqual(self.game._tmanager._finalwinner, leader, "Incorrect leader chosen") + self.assertIsNotNone(self.game._tmanager._finalwinners, "Final Winner not marked") + self.assertIn(leader, self.game._tmanager._finalwinners, "Incorrect leader chosen") #eif #next round @@ -166,8 +166,8 @@ def test_full_wild_tournament(self): self.assertIn(leader, self.game._tmanager._finalgroup, "Correct player not added to final group") else: # final round - #self.assertIsNotNone(self.game._tmanager._finalwinner, "Final Winner not marked") - #self.assertEqual(self.game._tmanager._finalwinner, leader, "Incorrect leader chosen") + #self.assertIsNotNone(self.game._tmanager._finalwinners, "Final Winner not marked") + #self.assertIn(leader, self.game._tmanager._finalwinners, "Incorrect leader chosen") pass #eif #next round diff --git a/SBA_Serv/Tests/test_tournament_short.cfg b/SBA_Serv/Tests/test_tournament_short.cfg new file mode 100644 index 0000000..ab3386d --- /dev/null +++ b/SBA_Serv/Tests/test_tournament_short.cfg @@ -0,0 +1,10 @@ +[Server] +disconnect_on_idle = false + +[Game] +auto_start = false +allow_after_start = false + +[Tournament] +tournament = true +round_time = 15