Skip to content

Commit

Permalink
Add test to fully simulate ladder game
Browse files Browse the repository at this point in the history
This should generate entries in matchmaker_queue_game
  • Loading branch information
Askaholic committed Oct 6, 2020
1 parent d5edd67 commit b022f33
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
21 changes: 19 additions & 2 deletions integration_tests/fafclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 1 addition & 7 deletions integration_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[]):
Expand Down
76 changes: 76 additions & 0 deletions integration_tests/test_matchmaking.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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]

0 comments on commit b022f33

Please sign in to comment.