Skip to content

Commit

Permalink
Issue/#789 Always compute team results for rabbitmq messages (#991)
Browse files Browse the repository at this point in the history
* Always compute team outcomes

* Add tests for missing validity states
  • Loading branch information
Askaholic authored Dec 30, 2023
1 parent d0152da commit 224661c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
31 changes: 16 additions & 15 deletions server/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,20 +466,19 @@ async def resolve_game_results(self) -> EndedGameInfo:
]

team_outcomes = [GameOutcome.UNKNOWN for _ in basic_info.teams]
team_player_partial_outcomes = [
{self.get_player_outcome(player) for player in team}
for team in basic_info.teams
]

if self.validity is ValidityState.VALID:
team_player_partial_outcomes = [
{self.get_player_outcome(player) for player in team}
for team in basic_info.teams
]

try:
# TODO: Remove override once game result messages are reliable
team_outcomes = (
self._outcome_override_hook()
or resolve_game(team_player_partial_outcomes)
)
except GameResolutionError:
try:
# TODO: Remove override once game result messages are reliable
team_outcomes = (
self._outcome_override_hook()
or resolve_game(team_player_partial_outcomes)
)
except GameResolutionError:
if self.validity is ValidityState.VALID:
await self.mark_invalid(ValidityState.UNKNOWN_RESULT)

try:
Expand Down Expand Up @@ -650,6 +649,8 @@ async def validate_game_mode_settings(self):
await self.mark_invalid(ValidityState.UNEVEN_TEAMS_NOT_RANKED)
return

# TODO: This validity state seems to be impossible to get because it is
# already covered by UNEVEN_TEAMS_NOT_RANKED above.
if len(self.players) < 2:
await self.mark_invalid(ValidityState.SINGLE_PLAYER)
return
Expand Down Expand Up @@ -707,8 +708,8 @@ async def update_game_stats(self):
assert self.host is not None

async with self._db.acquire() as conn:
# Determine if the map is blacklisted, and invalidate the game for ranking purposes if
# so, and grab the map id at the same time.
# Determine if the map is blacklisted, and invalidate the game for
# ranking purposes if so, and grab the map id at the same time.
result = await conn.execute(
"SELECT id, ranked FROM map_version "
"WHERE lower(filename) = lower(:filename)",
Expand Down
8 changes: 4 additions & 4 deletions tests/integration_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,8 @@ async def test_galactic_war_2v1_game_ended_broadcasts_army_results(lobby_server,
"sim_mod_ids": [],
"teams": [
{
"outcome": "VICTORY",
"player_ids": [1, 3],
"army_results": [
{
"player_id": 1,
Expand All @@ -1230,10 +1232,10 @@ async def test_galactic_war_2v1_game_ended_broadcasts_army_results(lobby_server,
"metadata": ["recall"],
},
],
"outcome": "UNKNOWN",
"player_ids": [1, 3]
},
{
"outcome": "DEFEAT",
"player_ids": [6],
"army_results": [
{
"player_id": 6,
Expand All @@ -1242,8 +1244,6 @@ async def test_galactic_war_2v1_game_ended_broadcasts_army_results(lobby_server,
"metadata": ["recall"],
},
],
"outcome": "UNKNOWN",
"player_ids": [6]
},
],
"validity": "UNEVEN_TEAMS_NOT_RANKED",
Expand Down
56 changes: 40 additions & 16 deletions tests/unit_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ async def test_ffa_not_rated(game, game_add_players):
game_add_players(game, 5, team=1)
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.FFA_NOT_RANKED
assert game.validity is ValidityState.FFA_NOT_RANKED


async def test_generated_map_is_rated(game, game_add_players):
Expand All @@ -174,7 +174,7 @@ async def test_generated_map_is_rated(game, game_add_players):
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.VALID
assert game.validity is ValidityState.VALID


async def test_unranked_generated_map_not_rated(game, game_add_players):
Expand All @@ -185,17 +185,28 @@ async def test_unranked_generated_map_not_rated(game, game_add_players):
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.BAD_MAP
assert game.validity is ValidityState.BAD_MAP


async def test_unranked_mod_not_rated(game, game_add_players):
game.state = GameState.LOBBY
game_add_players(game, 2, team=1)
game.mods = {"mod-id": "Some mod"}
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity is ValidityState.BAD_MOD


async def test_two_player_ffa_is_rated(game, game_add_players):
game.state = GameState.LOBBY
game_add_players(game, 2, team=1)
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.VALID
assert game.validity is ValidityState.VALID


async def test_multi_team_not_rated(game, game_add_players):
Expand All @@ -205,9 +216,9 @@ async def test_multi_team_not_rated(game, game_add_players):
game_add_players(game, 2, team=4)
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.MULTI_TEAM
assert game.validity is ValidityState.MULTI_TEAM


async def test_has_ai_players_not_rated(game, game_add_players):
Expand All @@ -227,9 +238,9 @@ async def test_has_ai_players_not_rated(game, game_add_players):
}
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.HAS_AI_PLAYERS
assert game.validity is ValidityState.HAS_AI_PLAYERS


async def test_uneven_teams_not_rated(game, game_add_players):
Expand All @@ -238,19 +249,30 @@ async def test_uneven_teams_not_rated(game, game_add_players):
game_add_players(game, 3, team=3)
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.UNEVEN_TEAMS_NOT_RANKED
assert game.validity is ValidityState.UNEVEN_TEAMS_NOT_RANKED


async def test_single_team_not_rated(game, game_add_players):
n_players = 4
game.state = GameState.LOBBY
game_add_players(game, n_players, team=2)
print(game._player_options)
await game.launch()
game.launched_at = time.time() - 60 * 20
for i in range(n_players):
await game.add_result(0, i + 1, "victory", 5)
await game.add_result(0, i, "victory", 5)
await game.on_game_finish()
assert game.validity is ValidityState.UNEVEN_TEAMS_NOT_RANKED


async def test_single_player_not_rated(game, game_add_players):
game.state = GameState.LOBBY
game_add_players(game, 1, team=1)
await game.launch()
game.launched_at = time.time() - 60 * 20
await game.add_result(0, 0, "victory", 5)
await game.on_game_finish()
assert game.validity is ValidityState.UNEVEN_TEAMS_NOT_RANKED

Expand Down Expand Up @@ -449,7 +471,9 @@ async def test_game_marked_dirty_when_timed_out(game: Game, game_service):


async def test_clear_slot(
game: Game, mock_game_connection: GameConnection, player_factory
game: Game,
mock_game_connection: GameConnection,
player_factory,
):
game.state = GameState.LOBBY
players = [
Expand Down Expand Up @@ -1056,7 +1080,7 @@ async def test_army_results_present_for_invalid_games(game: Game, game_add_playe
game_results = await game.resolve_game_results()
result_dict = game_results.to_dict()

assert result_dict["teams"][0]["outcome"] == "UNKNOWN"
assert result_dict["teams"][0]["outcome"] == "VICTORY"
assert result_dict["teams"][0]["army_results"] == [
{
"player_id": 1,
Expand All @@ -1071,7 +1095,7 @@ async def test_army_results_present_for_invalid_games(game: Game, game_add_playe
"metadata": [],
},
]
assert result_dict["teams"][1]["outcome"] == "UNKNOWN"
assert result_dict["teams"][1]["outcome"] == "DEFEAT"
assert result_dict["teams"][1]["army_results"] == [
{
"player_id": 3,
Expand Down

0 comments on commit 224661c

Please sign in to comment.