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

Issue/#482 Add TypedDict definition for protocol messages #966

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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 Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sqlalchemy = ">=2.0.0"
trueskill = "*"
twilio = ">=7.0.0"
uvloop = {version = "*", markers = "sys_platform != 'win32'"}
typing-extensions = "*"

[dev-packages]
hypothesis = "*" # Versions between 6.47.1 and 6.56.4 added a prerelease dependency. See https://github.com/pypa/pipenv/issues/1760
Expand Down
4 changes: 2 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 49 additions & 28 deletions server/lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from .player_service import PlayerService
from .players import Player, PlayerState
from .protocol import DisconnectedError, Protocol
from .protocol.types import client
from .rating import InclusiveRange, RatingType
from .rating_service import RatingService
from .types import Address, GameLaunchOptions
Expand Down Expand Up @@ -203,16 +204,16 @@ async def on_message_received(self, message):
self._logger.exception(ex)
await self.abort("Error processing command")

async def command_ping(self, msg):
async def command_ping(self, msg: client.Ping):
await self.send({"command": "pong"})

async def command_pong(self, msg):
async def command_pong(self, msg: client.Pong):
pass

async def command_create_account(self, message):
async def command_create_account(self, message: dict):
raise ClientError("FAF no longer supports direct registration. Please use the website to register.", recoverable=True)

async def command_coop_list(self, message):
async def command_coop_list(self, message: client.CoopList):
"""Request for coop map list"""
async with self._db.acquire() as conn:
result = await conn.stream(select(coop_map))
Expand Down Expand Up @@ -240,10 +241,13 @@ async def command_coop_list(self, message):
"featured_mod": "coop"
})

async def command_matchmaker_info(self, message):
async def command_matchmaker_info(self, message: client.MatchmakerInfo):
await self.send({
"command": "matchmaker_info",
"queues": [queue.to_dict() for queue in self.ladder_service.queues.values()]
"queues": [
queue.to_dict()
for queue in self.ladder_service.queues.values()
]
})

async def send_game_list(self):
Expand All @@ -255,7 +259,7 @@ async def send_game_list(self):
]
})

async def command_social_remove(self, message):
async def command_social_remove(self, message: client.SocialRemove):
if "friend" in message:
subject_id = message["friend"]
player_attr = self.player.friends
Expand All @@ -275,7 +279,7 @@ async def command_social_remove(self, message):
with contextlib.suppress(KeyError):
player_attr.remove(subject_id)

async def command_social_add(self, message):
async def command_social_add(self, message: client.SocialAdd):
if "friend" in message:
status = "FRIEND"
subject_id = message["friend"]
Expand Down Expand Up @@ -309,7 +313,7 @@ async def send_updated_achievements(self, updated_achievements):
"updated_achievements": updated_achievements
})

async def command_admin(self, message):
async def command_admin(self, message: client.Admin):
action = message["action"]

if action == "closeFA":
Expand Down Expand Up @@ -492,7 +496,7 @@ async def check_policy_conformity(self, player_id, uid_hash, session, ignore_res

return response.get("result", "") == "honest"

async def command_auth(self, message):
async def command_auth(self, message: client.Auth):
token = message["token"]
unique_id = message["unique_id"]
player_id = await self.oauth_service.get_player_id_from_token(token)
Expand Down Expand Up @@ -521,7 +525,7 @@ async def command_auth(self, message):
player_id, username, new_irc_password, unique_id, auth_method
)

async def command_hello(self, message):
async def command_hello(self, message: client.Hello):
login = message["login"].strip()
password = message["password"]
unique_id = message["unique_id"]
Expand Down Expand Up @@ -695,21 +699,29 @@ async def update_irc_password(self, conn, login, password):
except (OperationalError, ProgrammingError):
self._logger.error("Failure updating NickServ password for %s", login)

async def command_restore_game_session(self, message):
async def command_restore_game_session(
self,
message: client.RestoreGameSession
):
assert self.player is not None

game_id = int(message.get("game_id"))
game_id = int(message["game_id"])

# Restore the player's game connection, if the game still exists and is live
if not game_id or game_id not in self.game_service:
await self.send_warning("The game you were connected to does no longer exist")
await self.send_warning("The game you were connected to no longer exists")
return

game = self.game_service[game_id] # type: Game
game: Game = self.game_service[game_id]
if game.state is not GameState.LOBBY and game.state is not GameState.LIVE:
await self.send_warning("The game you were connected to is no longer available")
return

if self.player.id not in game._players_at_launch:
await self.send_warning("You are not part of this game")
# TODO: Implement me
return

self._logger.debug("Restoring game session of player %s to game %s", self.player, game)
self.game_connection = GameConnection(
database=self._db,
Expand All @@ -725,14 +737,14 @@ async def command_restore_game_session(self, message):
self.player.state = PlayerState.PLAYING
self.player.game = game

async def command_ask_session(self, message):
async def command_ask_session(self, message: client.AskSession):
user_agent = message.get("user_agent")
version = message.get("version")
self._set_user_agent_and_version(user_agent, version)
await self._check_user_agent()
await self.send({"command": "session", "session": self.session})

async def command_avatar(self, message):
async def command_avatar(self, message: client.Avatar):
action = message["action"]

if action == "list_avatar":
Expand Down Expand Up @@ -839,7 +851,7 @@ async def wrapper(self, message):

@ice_only
@player_idle("join a game")
async def command_game_join(self, message):
async def command_game_join(self, message: client.GameJoin):
"""
We are going to join a game.
"""
Expand Down Expand Up @@ -887,7 +899,7 @@ async def command_game_join(self, message):
await self.launch_game(game, is_host=False)

@ice_only
async def command_game_matchmaking(self, message):
async def command_game_matchmaking(self, message: client.GameMatchmaking):
queue_name = str(
message.get("queue_name") or message.get("mod", "ladder1v1")
)
Expand Down Expand Up @@ -940,7 +952,7 @@ async def command_game_matchmaking(self, message):

@ice_only
@player_idle("host a game")
async def command_game_host(self, message):
async def command_game_host(self, message: client.GameHost):
assert isinstance(self.player, Player)

await self.abort_connection_if_banned()
Expand Down Expand Up @@ -978,7 +990,7 @@ async def command_game_host(self, message):
)
await self.launch_game(game, is_host=True)

async def command_match_ready(self, message):
async def command_match_ready(self, message: client.MatchReady):
"""
Replace with full implementation when implemented in client, see:
https://github.com/FAForever/downlords-faf-client/issues/1783
Expand Down Expand Up @@ -1063,7 +1075,7 @@ def _prepare_launch_game(

return {k: v for k, v in cmd.items() if v is not None}

async def command_modvault(self, message):
async def command_modvault(self, message: client.ModVault):
type = message["type"]

async with self._db.acquire() as conn:
Expand Down Expand Up @@ -1137,7 +1149,7 @@ async def command_modvault(self, message):
else:
raise ValueError("invalid type argument")

async def command_ice_servers(self, message):
async def command_ice_servers(self, message: client.IceServers):
if not self.player:
return

Expand All @@ -1157,7 +1169,7 @@ async def command_ice_servers(self, message):
})

@player_idle("invite a player")
async def command_invite_to_party(self, message):
async def command_invite_to_party(self, message: client.InviteToParty):
recipient = self.player_service.get_player(message["recipient_id"])
if recipient is None:
# TODO: Client localized message
Expand All @@ -1169,7 +1181,10 @@ async def command_invite_to_party(self, message):
self.party_service.invite_player_to_party(self.player, recipient)

@player_idle("join a party")
async def command_accept_party_invite(self, message):
async def command_accept_party_invite(
self,
message: client.AcceptPartyInvite
):
sender = self.player_service.get_player(message["sender_id"])
if sender is None:
# TODO: Client localized message
Expand All @@ -1178,19 +1193,25 @@ async def command_accept_party_invite(self, message):
await self.party_service.accept_invite(self.player, sender)

@player_idle("kick a player")
async def command_kick_player_from_party(self, message):
async def command_kick_player_from_party(
self,
message: client.KickPlayerFromParty
):
kicked_player = self.player_service.get_player(message["kicked_player_id"])
if kicked_player is None:
# TODO: Client localized message
raise ClientError("The kicked player doesn't exist", recoverable=True)

await self.party_service.kick_player_from_party(self.player, kicked_player)

async def command_leave_party(self, _message):
async def command_leave_party(self, message: client.LeaveParty):
self.ladder_service.cancel_search(self.player)
await self.party_service.leave_party(self.player)

async def command_set_party_factions(self, message):
async def command_set_party_factions(
self,
message: client.SetPartyFactions
):
factions = set(Faction.from_value(v) for v in message["factions"])

if not factions:
Expand Down
Empty file.
Loading