fix payment for already found hints

This commit is contained in:
Fabian Dill 2020-04-22 15:50:14 +02:00
parent e02025c534
commit 9842399d8b
2 changed files with 32 additions and 29 deletions

View File

@ -85,14 +85,16 @@ class Context:
self.commandprocessor = ServerCommandProcessor(self) self.commandprocessor = ServerCommandProcessor(self)
def get_save(self) -> dict: def get_save(self) -> dict:
return { d = {
"rom_names": list(self.rom_names.items()), "rom_names": list(self.rom_names.items()),
"received_items": tuple((k, v) for k, v in self.received_items.items()), "received_items": tuple((k, v) for k, v in self.received_items.items()),
"hints_used": tuple((key, value) for key, value in self.hints_used.items()), "hints_used": tuple((key, value) for key, value in self.hints_used.items()),
"hints": tuple((key, value) for key, value in self.hints.items()), "hints": tuple((key, list(value)) for key, value in self.hints.items()),
"location_checks": tuple((key, tuple(value)) for key, value in self.location_checks.items()) "location_checks": tuple((key, tuple(value)) for key, value in self.location_checks.items())
} }
return d
def set_save(self, savedata: dict): def set_save(self, savedata: dict):
rom_names = savedata["rom_names"] rom_names = savedata["rom_names"]
received_items = {tuple(k): [ReceivedItem(*i) for i in v] for k, v in savedata["received_items"]} received_items = {tuple(k): [ReceivedItem(*i) for i in v] for k, v in savedata["received_items"]}
@ -557,38 +559,36 @@ class ClientMessageProcessor(CommandProcessor):
hints = collect_hints_location(self.ctx, self.client.team, self.client.slot, item_name) hints = collect_hints_location(self.ctx, self.client.team, self.client.slot, item_name)
if hints: if hints:
if item_name in self.ctx.hints[self.client.team, self.client.slot]: new_hints = set(hints) - self.ctx.hints[self.client.team, self.client.slot]
notify_hints(self.ctx, self.client.team, hints) old_hints = set(hints) - new_hints
self.output("Hint was previously used, no points deducted.") if old_hints:
return True notify_hints(self.ctx, self.client.team, list(old_hints))
else: if not new_hints:
found = 0 self.output("Hint was previously used, no points deducted.")
for hint in hints: if new_hints:
found += 1 - hint.found found_hints = sum(not hint.found for hint in new_hints)
if not found: if not found_hints: # everything's been found, no need to pay
notify_hints(self.ctx, self.client.team, hints) can_pay = True
self.output("No new items found, no points deducted.") elif self.ctx.hint_cost:
return False can_pay = points_available // (self.ctx.hint_cost * found_hints) >= 1
else: else:
if self.ctx.hint_cost: can_pay = True
can_pay = points_available // (self.ctx.hint_cost * found) >= 1
else:
can_pay = True
if can_pay: if can_pay:
self.ctx.hints_used[self.client.team, self.client.slot] += found self.ctx.hints_used[self.client.team, self.client.slot] += found_hints
for hint in hints: for hint in new_hints:
if not hint.found:
self.ctx.hints[self.client.team, hint.finding_player].add(hint) self.ctx.hints[self.client.team, hint.finding_player].add(hint)
self.ctx.hints[self.client.team, hint.receiving_player].add(hint) self.ctx.hints[self.client.team, hint.receiving_player].add(hint)
notify_hints(self.ctx, self.client.team, hints) notify_hints(self.ctx, self.client.team, list(new_hints))
save(self.ctx) save(self.ctx)
else: else:
notify_client(self.client, f"You can't afford the hint. " notify_client(self.client, f"You can't afford the hint. "
f"You have {points_available} points and need at least " f"You have {points_available} points and need at least "
f"{self.ctx.hint_cost}, " f"{self.ctx.hint_cost}, "
f"more if multiple items are still to be found.") f"more if multiple items are still to be found.")
return True return True
else: else:
self.output("Nothing found. Item/Location may not exist.") self.output("Nothing found. Item/Location may not exist.")
return False return False

View File

@ -168,6 +168,9 @@ class Hint(typing.NamedTuple):
return Hint(self.receiving_player, self.finding_player, self.location, self.item, found) return Hint(self.receiving_player, self.finding_player, self.location, self.item, found)
return self return self
def __hash__(self):
return hash((self.receiving_player, self.finding_player, self.location, self.item))
def get_public_ipv4() -> str: def get_public_ipv4() -> str:
import socket import socket
import urllib.request import urllib.request