Add a way to show found items on client/server (#52)

* Add a way to show found items on client/server

* item show stuff:
use less ram and cpu, rename a few things

* Add UpdateTags server command.

Co-authored-by: Fabian Dill <fabian.dill@web.de>
This commit is contained in:
CaitSith2
2020-03-23 02:47:07 -07:00
committed by GitHub
parent 2bec95b4f8
commit 7b620e94f7
2 changed files with 46 additions and 10 deletions

View File

@@ -6,7 +6,6 @@ import logging
import zlib
import collections
import typing
import os
import ModuleUpdate
@@ -39,6 +38,10 @@ class Client:
self.tags = []
self.version = [0, 0, 0]
@property
def wants_item_notification(self):
return self.auth and "FoundItems" in self.tags
class Context:
def __init__(self, host: str, port: int, password: str, location_check_points: int, hint_cost: int,
@@ -135,7 +138,6 @@ def notify_hints(ctx: Context, team: int, hints: typing.List[Utils.Hint]):
payload = texts
asyncio.create_task(send_msgs(client.socket, payload))
async def server(websocket, path, ctx: Context):
client = Client(websocket)
ctx.clients.append(client)
@@ -210,7 +212,7 @@ def get_connected_players_string(ctx: Context):
return f'{len(auth_clients)} players of {len(ctx.player_names)} connected ' + text[:-1]
def get_received_items(ctx: Context, team: int, player: int):
def get_received_items(ctx: Context, team: int, player: int) -> typing.List[ReceivedItem]:
return ctx.received_items.setdefault((team, player), [])
@@ -235,7 +237,7 @@ def forfeit_player(ctx: Context, team: int, slot: int):
def register_location_checks(ctx: Context, team: int, slot: int, locations):
ctx.location_checks[team, slot] |= set(locations)
found_items = False
for location in locations:
@@ -254,8 +256,17 @@ def register_location_checks(ctx: Context, team: int, slot: int, locations):
recvd_items.append(new_item)
if slot != target_player:
broadcast_team(ctx, team, [['ItemSent', (slot, location, target_player, target_item)]])
logging.info('(Team #%d) %s sent %s to %s (%s)' % (team+1, ctx.player_names[(team, slot)], get_item_name_from_id(target_item), ctx.player_names[(team, target_player)], get_location_name_from_address(location)))
logging.info('(Team #%d) %s sent %s to %s (%s)' % (
team + 1, ctx.player_names[(team, slot)], get_item_name_from_id(target_item),
ctx.player_names[(team, target_player)], get_location_name_from_address(location)))
found_items = True
elif target_player == slot: # local pickup, notify clients of the pickup
if location not in ctx.location_checks[team, slot]:
for client in ctx.clients:
if client.team == team and client.wants_item_notification:
asyncio.create_task(
send_msgs(client.socket, [['ItemFound', (target_item, location, slot)]]))
ctx.location_checks[team, slot] |= set(locations)
send_new_items(ctx)
if found_items:
@@ -398,6 +409,12 @@ async def process_client_cmd(ctx: Context, client: Client, cmd, args):
logging.info(f"{client.name} in team {client.team+1} scouted {', '.join([l[0] for l in locs])}")
await send_msgs(client.socket, [['LocationInfo', [l[1:] for l in locs]]])
if cmd == 'UpdateTags':
if not args or type(args) is not list:
await send_msgs(client.socket, [['InvalidArguments', 'UpdateTags']])
return
client.tags = args
if cmd == 'Say':
if type(args) is not str or not args.isprintable():
await send_msgs(client.socket, [['InvalidArguments', 'Say']])