Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hosted_at field to game_info message #986

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e_tests/test_matchmaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async def test_ladder_1v1_match(client_factory):
"sim_mods": {},
"num_players": 0,
"max_players": 2,
"hosted_at": None,
"launched_at": None,
"rating_type": "ladder_1v1",
"rating_min": None,
Expand Down
4 changes: 4 additions & 0 deletions server/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
resolve_game
)
from server.rating import InclusiveRange, RatingType
from server.timing import datetime_now

from ..players import Player, PlayerState
from .typedefs import (
Expand Down Expand Up @@ -78,6 +79,7 @@ def __init__(
self._game_stats_service = game_stats_service
self.game_service = game_service
self._player_options: dict[int, dict[str, Any]] = defaultdict(dict)
self.hosted_at = None
self.launched_at = None
self.finished = False
self._logger = logging.getLogger(
Expand Down Expand Up @@ -264,6 +266,7 @@ def get_team_sets(self) -> list[set[Player]]:

def set_hosted(self):
self._hosted_future.set_result(None)
self.hosted_at = datetime_now()

async def add_result(
self,
Expand Down Expand Up @@ -894,6 +897,7 @@ def to_dict(self):
"host": self.host.login if self.host else "",
"num_players": len(connected_players),
"max_players": self.max_players,
"hosted_at": self.hosted_at.isoformat() if self.hosted_at else None,
"launched_at": self.launched_at,
"rating_type": self.rating_type,
"rating_min": self.displayed_rating_range.lo,
Expand Down
69 changes: 68 additions & 1 deletion tests/integration_tests/test_game.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import asyncio
import gc
import json
import time
from collections import defaultdict
from datetime import datetime

import pytest
from sqlalchemy import select

from server.db.models import game_player_stats
from server.games.game_results import GameOutcome
from server.protocol import Protocol
from server.timing import datetime_now
from tests.utils import fast_forward

from .conftest import (
Expand Down Expand Up @@ -99,7 +102,7 @@ def predecate(cmd) -> bool:

return cmd["launched_at"] is not None

await read_until(proto, predecate, timeout=timeout)
return await read_until(proto, predecate, timeout=timeout)


async def client_response(proto, timeout=10):
Expand Down Expand Up @@ -261,6 +264,70 @@ async def send_player_options(proto, *options):
})


@fast_forward(60)
async def test_game_info_messages(lobby_server):
host_id, _, host_proto = await connect_and_sign_in(
("test", "test_password"), lobby_server
)
guest_id, _, guest_proto = await connect_and_sign_in(
("Rhiza", "puff_the_magic_dragon"), lobby_server
)
await read_until_command(guest_proto, "game_info")
await read_until_command(host_proto, "game_info")

# Host game
await host_proto.send_message({
"command": "game_host",
"mod": "faf",
"visibility": "public",
})
msg = await read_until_command(host_proto, "game_launch")
game_id = int(msg["uid"])

msg = await read_until_command(host_proto, "game_info")

assert msg["hosted_at"] is None
assert msg["launched_at"] is None

await open_fa(host_proto)
await read_until_command(host_proto, "HostGame", target="game")

msg = await read_until_command(host_proto, "game_info")
hosted_at = msg["hosted_at"]
assert datetime.fromisoformat(hosted_at) <= datetime_now()
assert msg["launched_at"] is None

# Join a player
await join_game(guest_proto, game_id)

# Set player options
await send_player_options(
host_proto,
[host_id, "Army", 1],
[host_id, "Team", 1],
[host_id, "StartSpot", 1],
[host_id, "Faction", 1],
[host_id, "Color", 1],
[guest_id, "Army", 2],
[guest_id, "Team", 2],
[guest_id, "StartSpot", 2],
[guest_id, "Faction", 2],
[guest_id, "Color", 2],
)

# Launch game
await host_proto.send_message({
"target": "game",
"command": "GameState",
"args": ["Launching"]
})

msg = await read_until_launched(host_proto, game_id)

assert msg["hosted_at"] == hosted_at
assert msg["launched_at"] <= time.time()


@fast_forward(60)
async def test_game_ended_rates_game(lobby_server):
host_id, _, host_proto = await connect_and_sign_in(
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ async def test_to_dict(game, player_factory):
"host": game.host.login,
"num_players": len(game.players),
"max_players": game.max_players,
"hosted_at": None,
"launched_at": game.launched_at,
"rating_type": game.rating_type,
"rating_min": game.displayed_rating_range.lo,
Expand Down