From e46420f4a9f0676a5f93db573515f73b0e4c3778 Mon Sep 17 00:00:00 2001 From: Zach Parks Date: Fri, 24 Nov 2023 17:14:07 -0600 Subject: [PATCH] MultiServer: Create read-only data storage key for client statuses. (#2412) --- MultiServer.py | 11 ++++++++++- docs/network protocol.md | 13 +++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/MultiServer.py b/MultiServer.py index bd9d2446af65..9d2e9b564e75 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -2,8 +2,8 @@ import argparse import asyncio -import copy import collections +import copy import datetime import functools import hashlib @@ -417,6 +417,8 @@ def _load(self, decoded_obj: dict, game_data_packages: typing.Dict[str, typing.A self.player_name_lookup[slot_info.name] = 0, slot_id self.read_data[f"hints_{0}_{slot_id}"] = lambda local_team=0, local_player=slot_id: \ list(self.get_rechecked_hints(local_team, local_player)) + self.read_data[f"client_status_{0}_{slot_id}"] = lambda local_team=0, local_player=slot_id: \ + self.client_game_state[local_team, local_player] self.seed_name = decoded_obj["seed_name"] self.random.seed(self.seed_name) @@ -712,6 +714,12 @@ def on_new_hint(self, team: int, slot: int): "hint_points": get_slot_points(self, team, slot) }]) + def on_client_status_change(self, team: int, slot: int): + key: str = f"_read_client_status_{team}_{slot}" + targets: typing.Set[Client] = set(self.stored_data_notification_clients[key]) + if targets: + self.broadcast(targets, [{"cmd": "SetReply", "key": key, "value": self.client_game_state[team, slot]}]) + def update_aliases(ctx: Context, team: int): cmd = ctx.dumper([{"cmd": "RoomUpdate", @@ -1819,6 +1827,7 @@ def update_client_status(ctx: Context, client: Client, new_status: ClientStatus) ctx.on_goal_achieved(client) ctx.client_game_state[client.team, client.slot] = new_status + ctx.on_client_status_change(client.team, client.slot) ctx.save() diff --git a/docs/network protocol.md b/docs/network protocol.md index c17cc74a8ac7..199f96f48131 100644 --- a/docs/network protocol.md +++ b/docs/network protocol.md @@ -380,11 +380,12 @@ Additional arguments sent in this package will also be added to the [Retrieved]( Some special keys exist with specific return data, all of them have the prefix `_read_`, so `hints_{team}_{slot}` is `_read_hints_{team}_{slot}`. -| Name | Type | Notes | -|-------------------------------|--------------------------|---------------------------------------------------| -| hints_{team}_{slot} | list\[[Hint](#Hint)\] | All Hints belonging to the requested Player. | -| slot_data_{slot} | dict\[str, any\] | slot_data belonging to the requested slot. | -| item_name_groups_{game_name} | dict\[str, list\[str\]\] | item_name_groups belonging to the requested game. | +| Name | Type | Notes | +|------------------------------|-------------------------------|---------------------------------------------------| +| hints_{team}_{slot} | list\[[Hint](#Hint)\] | All Hints belonging to the requested Player. | +| slot_data_{slot} | dict\[str, any\] | slot_data belonging to the requested slot. | +| item_name_groups_{game_name} | dict\[str, list\[str\]\] | item_name_groups belonging to the requested game. | +| client_status_{team}_{slot} | [ClientStatus](#ClientStatus) | The current game status of the requested player. | ### Set Used to write data to the server's data storage, that data can then be shared across worlds or just saved for later. Values for keys in the data storage can be retrieved with a [Get](#Get) package, or monitored with a [SetNotify](#SetNotify) package. @@ -558,7 +559,7 @@ Color options: `player` marks owning player id for location/item, `flags` contains the [NetworkItem](#NetworkItem) flags that belong to the item -### Client States +### ClientStatus An enumeration containing the possible client states that may be used to inform the server in [StatusUpdate](#StatusUpdate). The MultiServer automatically sets the client state to `ClientStatus.CLIENT_CONNECTED` on the first active connection