diff --git a/integration_tests/fafclient.py b/integration_tests/fafclient.py index 716af5407..43e456cdc 100644 --- a/integration_tests/fafclient.py +++ b/integration_tests/fafclient.py @@ -17,6 +17,7 @@ def __init__(self, user_agent="faf-client", version="1.0.0-dev"): # TODO: make this configurable via a command line arg? self.faf_uid_path = "faf-uid" self.player_id = None + self.player_name = None def is_connected(self): return self.proto and self.proto.is_connected() @@ -52,6 +53,16 @@ async def read_until(self, predicate, timeout=60): async def read_until_command(self, command, timeout=60): return await read_until_command(self.proto, command, timeout=timeout) + async def read_until_game_launch(self, uid): + return await self.read_until( + lambda msg: ( + msg.get("command") == "game_info" and + msg["host"] == "test" and + msg["launched_at"] is not None + and msg["uid"] == uid + ) + ) + # Commonly used functionality here async def ping(self): await self.send_command("ping") @@ -82,6 +93,7 @@ async def login(self, username, password): }) msg = await self.read_until_command("welcome") self.player_id = msg["id"] + self.player_name = msg["login"] return msg def get_unique_id(self, session): @@ -150,7 +162,12 @@ async def send_player_options(self, player_id, **options): "args": [player_id, option, value] }) - async def get_player_ratings(self, *names, timeout=30): + async def get_player_ratings( + self, + *names, + rating_type="global", + timeout=30 + ): """ Wait for `player_info` messages until all player names have been found. Then return a dictionary containing all those players ratings @@ -159,7 +176,7 @@ async def get_player_ratings(self, *names, timeout=30): while set(ratings.keys()) != set(names): msg = await self.read_until_command("player_info", timeout=timeout) ratings.update({ - player_info["login"]: player_info["global_rating"] + player_info["login"]: player_info["ratings"][rating_type]["rating"] for player_info in msg["players"] }) return ratings diff --git a/integration_tests/test_game.py b/integration_tests/test_game.py index 6d9c567ab..cdb8199bf 100644 --- a/integration_tests/test_game.py +++ b/integration_tests/test_game.py @@ -23,13 +23,7 @@ async def simulate_game_launch(host, *guests): await host.send_gpg_command("GameState", "Launching") for client in all_clients: - await client.read_until( - lambda msg: ( - msg.get("command") == "game_info" and - msg["host"] == "test" and - msg["launched_at"] is not None - ) - ) + await client.read_until_game_launch(game_id) async def simulate_result_reports(host, *guests, results=[]): diff --git a/integration_tests/test_matchmaking.py b/integration_tests/test_matchmaking.py index 5059c9a31..f114069af 100644 --- a/integration_tests/test_matchmaking.py +++ b/integration_tests/test_matchmaking.py @@ -1,5 +1,10 @@ +import asyncio +import random + import pytest +from .test_game import simulate_result_reports + # All test coroutines will be treated as marked. pytestmark = pytest.mark.asyncio @@ -61,3 +66,74 @@ async def test_ladder_1v1_match(client_factory): "enforce_rating_range": False, "teams": {} } + + +async def test_ladder_1v1_game(client_factory): + """More or less the same as the regression test version""" + client1, _ = await client_factory.login("test") + client2, _ = await client_factory.login("test2") + + await client1.read_until_command("game_info") + await client2.read_until_command("game_info") + + ratings = await client1.get_player_ratings( + "test", + "test2", + rating_type="ladder_1v1" + ) + + await client1.send_message({ + "command": "game_matchmaking", + "state": "start", + "faction": "uef" + }) + + await client2.send_message({ + "command": "game_matchmaking", + "state": "start", + "faction": "seraphim" + }) + + player_positions = {} + + async def handle_game_launch(client): + msg = await client.read_until_command("game_launch") + await client.open_fa() + + player_positions[client.player_name] = msg["map_position"] + if msg["map_position"] == 1: + # player is host + peer_msg = await client.read_until_command("ConnectToPeer") + peer_id = peer_msg["args"][1] + await client.configure_joining_player(peer_id, 2) + await client.send_gpg_command("GameState", "Launching") + await client.read_until_game_launch(msg["uid"]) + + await asyncio.gather( + handle_game_launch(client1), + handle_game_launch(client2) + ) + + if random.random() < 0.5: + winner = client1.player_name + loser = client2.player_name + else: + winner = client2.player_name + loser = client1.player_name + + await simulate_result_reports(client1, client2, results=[ + [player_positions[winner], "victory 10"], + [player_positions[loser], "defeat -10"] + ]) + + for client in (client1, client2): + await client.send_gpg_command("GameState", "Ended") + + new_ratings = await client1.get_player_ratings( + "test", + "test2", + rating_type="ladder_1v1" + ) + + assert ratings[winner][0] < new_ratings[winner][0] + assert ratings[loser][0] > new_ratings[loser][0]