Make object hook more dynamic; ignoring unknown arguments
This commit is contained in:
parent
612c3c23c0
commit
1849693353
|
@ -331,7 +331,7 @@ class Context(Node):
|
||||||
await on_client_disconnected(self, endpoint)
|
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]):
|
def notify_hints(ctx: Context, team: int, hints: typing.List[Utils.Hint]):
|
||||||
cmd = ctx.dumper([{"cmd": "Hint", "hints" : hints}])
|
cmd = ctx.dumper([{"cmd": "Hint", "hints" : hints}])
|
||||||
texts = ([format_hint(ctx, team, hint)] for hint in 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)
|
await ctx.disconnect(client)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def on_client_connected(ctx: Context, client: Client):
|
async def on_client_connected(ctx: Context, client: Client):
|
||||||
await ctx.send_msgs(client, [{
|
await ctx.send_msgs(client, [{
|
||||||
'cmd': 'RoomInfo',
|
'cmd': 'RoomInfo',
|
||||||
|
|
55
NetUtils.py
55
NetUtils.py
|
@ -13,6 +13,26 @@ class JSONMessagePart(typing.TypedDict):
|
||||||
color: typing.Optional[str]
|
color: typing.Optional[str]
|
||||||
text: 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:
|
def _scan_for_TypedTuples(obj: typing.Any) -> typing.Any:
|
||||||
if isinstance(obj, tuple) and hasattr(obj, "_fields"): # NamedTuple is not actually a parent class
|
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))
|
return _encode(_scan_for_TypedTuples(obj))
|
||||||
|
|
||||||
from Utils import Version # for object hook
|
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:
|
def _object_hook(o: typing.Any) -> typing.Any:
|
||||||
if isinstance(o, dict):
|
if isinstance(o, dict):
|
||||||
cls = o.get("class", None)
|
cls = whitelist.get(o.get("class", None), None)
|
||||||
if cls in whitelist:
|
if cls:
|
||||||
del (o["class"])
|
for key in tuple(o):
|
||||||
return globals()[cls](**o)
|
if key not in cls._fields:
|
||||||
|
del(o[key])
|
||||||
|
return cls(**o)
|
||||||
|
|
||||||
return o
|
return o
|
||||||
|
|
||||||
|
@ -166,24 +190,3 @@ def color_code(*args):
|
||||||
|
|
||||||
def color(text, *args):
|
def color(text, *args):
|
||||||
return color_code(*args) + text + color_code('reset')
|
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
|
|
||||||
|
|
Loading…
Reference in New Issue