Skip to content

CommonClient: use rich text for /received #2715

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

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions CommonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
Utils.init_logging("TextClient", exception_logger="Client")

from MultiServer import CommandProcessor
from NetUtils import Endpoint, decode, NetworkItem, encode, JSONtoTextParser, \
ClientStatus, Permission, NetworkSlot, RawJSONtoTextParser
from NetUtils import (Endpoint, decode, NetworkItem, encode, JSONtoTextParser, ClientStatus, Permission, NetworkSlot,
RawJSONtoTextParser, add_json_text, add_json_location, add_json_item, JSONTypes)
from Utils import Version, stream_input, async_start
from worlds import network_data_package, AutoWorldRegister
import os
Expand Down Expand Up @@ -72,9 +72,16 @@ def _cmd_disconnect(self) -> bool:

def _cmd_received(self) -> bool:
"""List all received items"""
self.output(f'{len(self.ctx.items_received)} received items:')
item: NetworkItem
self.output(f'{len(self.ctx.items_received)} received items, sorted by time:')
for index, item in enumerate(self.ctx.items_received, 1):
self.output(f"{self.ctx.item_names[item.item]} from {self.ctx.player_names[item.player]}")
parts = []
add_json_item(parts, item.item, self.ctx.slot, item.flags)
add_json_text(parts, " from ")
add_json_location(parts, item.location, item.player)
add_json_text(parts, " by ")
add_json_text(parts, str(item.player), type=JSONTypes.player_id)
Copy link
Contributor

@MatthewMarinets MatthewMarinets Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why cast item.player to a string? it seems to be an int ...

You mean here? This is the text parameter, it immediately gets stringified one level down within add_json_text():

def add_json_text(parts: list, text: typing.Any, **kwargs) -> None:
    parts.append({"text": str(text), **kwargs})

I don't see an issue with this. Maybe some explicit typing on the type parameter and some typing @overload magic can make the type-checking better, (ie associating certain types for text with certain values of type) but I'd consider that out of scope for this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, i only mentioned it because MultiServer's json_format_send_event() does the same thing without casting it. No personal preference just wanted to point out the discrepancy (and if one was actually worse get it seen so that one could be fixed)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, the str() should be removed. This now runs str(str()) and also it's confusing why the player number would be passed in as str if the function takes Any. That being said, I feel like add_json_player would be an even better solution because it abstracts away that text is used for player, and on the other end we already have an explicit handler for player.

self.ctx.on_print_json({"data": parts, "cmd": "PrintJSON"})
return True

def _cmd_missing(self, filter_text = "") -> bool:
Expand Down
4 changes: 2 additions & 2 deletions NetUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ def add_json_item(parts: list, item_id: int, player: int = 0, item_flags: int =
parts.append({"text": str(item_id), "player": player, "flags": item_flags, "type": JSONTypes.item_id, **kwargs})


def add_json_location(parts: list, item_id: int, player: int = 0, **kwargs) -> None:
parts.append({"text": str(item_id), "player": player, "type": JSONTypes.location_id, **kwargs})
def add_json_location(parts: list, location_id: int, player: int = 0, **kwargs) -> None:
parts.append({"text": str(location_id), "player": player, "type": JSONTypes.location_id, **kwargs})


class Hint(typing.NamedTuple):
Expand Down