Skip to content

Commit

Permalink
fix(game): Not subtracting from pending bounty when game ends due to …
Browse files Browse the repository at this point in the history
…inactivity before wagers are collected
  • Loading branch information
Nickelza committed Nov 6, 2023
1 parent d3436c5 commit 2e59ebc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
23 changes: 10 additions & 13 deletions src/service/bounty_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
import traceback
from math import ceil

from peewee import Case, fn
Expand Down Expand Up @@ -281,12 +282,10 @@ async def add_or_remove_bounty(user: User, amount: int = None, context: ContextT
user.pending_bounty += (amount if pending_belly_amount is None else pending_belly_amount)

if user.bounty < 0 and previous_bounty >= 0:
try:
raise ValueError(f'User {user.id} has negative bounty: {user.bounty} after removing '
f'{amount} bounty in event '
f'{update.to_dict() if update is not None else "None"}')
except ValueError as ve:
logging.exception(ve)
logging.exception(f'User {user.id} has negative bounty: {user.bounty} after removing '
f'{amount} bounty in event '
f'{update.to_dict() if update is not None else "None"}'
f'\n{traceback.print_stack()}')

if should_save:
user.save()
Expand All @@ -296,13 +295,11 @@ async def add_or_remove_bounty(user: User, amount: int = None, context: ContextT
user.pending_bounty -= (amount if pending_belly_amount is None else pending_belly_amount)

if user.pending_bounty < 0 and previous_pending_bounty >= 0:
try:
raise ValueError(f'User {user.id} has negative pending bounty: {user.pending_bounty}'
f'(previous was {previous_pending_bounty} after removing'
f' {amount} pending bounty in event '
f'{update.to_dict() if update is not None else "None"}')
except ValueError as ve:
logging.exception(ve)
logging.exception(f'User {user.id} has negative pending bounty: {user.pending_bounty}'
f'(previous was {previous_pending_bounty} after removing'
f' {amount} pending bounty in event '
f'{update.to_dict() if update is not None else "None"}'
f'\n{traceback.print_stack()}')

if should_save:
user.save()
Expand Down
14 changes: 8 additions & 6 deletions src/service/game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async def end_game(game: Game, game_outcome: GameOutcome, is_forced_end: bool =
challenger: User = game.challenger
opponent: User = game.opponent
half_wager: int = game.wager / 2
previous_status: GameStatus = game.get_status()

bounty_for_challenger = bounty_for_opponent = 0
pending_bounty_for_challenger = pending_bounty_for_opponent = game.wager / 2
Expand All @@ -81,13 +82,14 @@ async def end_game(game: Game, game_outcome: GameOutcome, is_forced_end: bool =
bounty_for_opponent = half_wager
game.status = GameStatus.FORCED_END if is_forced_end else GameStatus.DRAW

await add_or_remove_bounty(challenger, bounty_for_challenger,
pending_belly_amount=pending_bounty_for_challenger, update=update,
tax_event_type=IncomeTaxEventType.GAME, event_id=game.id)
if not previous_status.no_wager_was_collected():
await add_or_remove_bounty(challenger, bounty_for_challenger,
pending_belly_amount=pending_bounty_for_challenger, update=update,
tax_event_type=IncomeTaxEventType.GAME, event_id=game.id)

if opponent is not None:
await add_or_remove_bounty(opponent, bounty_for_opponent, pending_belly_amount=pending_bounty_for_opponent,
update=update, tax_event_type=IncomeTaxEventType.GAME, event_id=game.id)
if opponent is not None:
await add_or_remove_bounty(opponent, bounty_for_opponent, pending_belly_amount=pending_bounty_for_opponent,
update=update, tax_event_type=IncomeTaxEventType.GAME, event_id=game.id)

# Refresh
game.challenger = challenger
Expand Down

0 comments on commit 2e59ebc

Please sign in to comment.