Skip to content

Commit

Permalink
Fixes #105 Show All Ties for Final Winners
Browse files Browse the repository at this point in the history
Doesn't do ties for inner rounds still.
Adjusts Test Cases.
  • Loading branch information
hawkerm committed Jun 5, 2017
1 parent 731683d commit 51e550f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 22 deletions.
33 changes: 20 additions & 13 deletions SBA_Serv/Game/Tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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

Expand All @@ -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):
"""
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions SBA_Serv/SBA_Serv.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
<Content Include="basicgame_combatexercise.cfg" />
<Content Include="Tests\test_server.cfg" />
<Content Include="Tests\test_singlegroup.cfg" />
<Content Include="Tests\test_tournament_short.cfg" />
<Content Include="Tests\test_tournament_wild_ties.cfg" />
<Content Include="Tests\test_tournament_wild.cfg" />
<Content Include="Tests\test_tournament.cfg" />
Expand Down
75 changes: 72 additions & 3 deletions SBA_Serv/Tests/HungryHungryBaublesTestCases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions SBA_Serv/Tests/ServerTestCases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions SBA_Serv/Tests/SurvivorTestCases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions SBA_Serv/Tests/test_tournament_short.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Server]
disconnect_on_idle = false

[Game]
auto_start = false
allow_after_start = false

[Tournament]
tournament = true
round_time = 15

0 comments on commit 51e550f

Please sign in to comment.