diff --git a/MultiServer.py b/MultiServer.py index 7d9610be..514290a0 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -331,7 +331,7 @@ class Context(Node): await on_client_disconnected(self, endpoint) -# separated out, due to compatibilty between clients +# separated out, due to compatibility between clients def notify_hints(ctx: Context, team: int, hints: typing.List[Utils.Hint]): cmd = ctx.dumper([{"cmd": "Hint", "hints" : hints}]) texts = ([format_hint(ctx, team, hint)] for hint in hints) @@ -369,8 +369,6 @@ async def server(websocket, path, ctx: Context): await ctx.disconnect(client) - - async def on_client_connected(ctx: Context, client: Client): await ctx.send_msgs(client, [{ 'cmd': 'RoomInfo', diff --git a/NetUtils.py b/NetUtils.py index 4c73fc83..e26dc0ce 100644 --- a/NetUtils.py +++ b/NetUtils.py @@ -13,6 +13,26 @@ class JSONMessagePart(typing.TypedDict): color: typing.Optional[str] text: typing.Optional[str] +class CLientStatus(enum.IntEnum): + CLIENT_UNKNOWN = 0 + # CLIENT_CONNECTED = 5 maybe? + CLIENT_READY = 10 + CLIENT_PLAYING = 20 + CLIENT_GOAL = 30 + + +class NetworkPlayer(typing.NamedTuple): + team: int + slot: int + alias: str + name: str + + +class NetworkItem(typing.NamedTuple): + item: int + location: int + player: int + def _scan_for_TypedTuples(obj: typing.Any) -> typing.Any: if isinstance(obj, tuple) and hasattr(obj, "_fields"): # NamedTuple is not actually a parent class @@ -36,14 +56,18 @@ def encode(obj): return _encode(_scan_for_TypedTuples(obj)) from Utils import Version # for object hook -whitelist = {"NetworkPlayer", "NetworkItem", "Version"} +whitelist = {"NetworkPlayer": NetworkPlayer, + "NetworkItem": NetworkItem, + "Version": Version} def _object_hook(o: typing.Any) -> typing.Any: if isinstance(o, dict): - cls = o.get("class", None) - if cls in whitelist: - del (o["class"]) - return globals()[cls](**o) + cls = whitelist.get(o.get("class", None), None) + if cls: + for key in tuple(o): + if key not in cls._fields: + del(o[key]) + return cls(**o) return o @@ -166,24 +190,3 @@ def color_code(*args): def color(text, *args): return color_code(*args) + text + color_code('reset') - - -class CLientStatus(enum.IntEnum): - CLIENT_UNKNOWN = 0 - # CLIENT_CONNECTED = 5 maybe? - CLIENT_READY = 10 - CLIENT_PLAYING = 20 - CLIENT_GOAL = 30 - - -class NetworkPlayer(typing.NamedTuple): - team: int - slot: int - alias: str - name: str - - -class NetworkItem(typing.NamedTuple): - item: int - location: int - player: int