78 lines
4.0 KiB
Python
78 lines
4.0 KiB
Python
"""What one player sees of the game right now (WEB-20 .. WEB-22): their funds, their bet in the round, and how
|
|||
|
|
their last round ended. One function feeds the page and `/app/stato/`, like `public_state` does for the home."""
|
||
|
|
|
||
|
|
from datetime import datetime
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from django.utils import timezone
|
||
|
|
|
||
|
|
from .balances import balance_of
|
||
|
|
from .format import plm_text
|
||
|
|
from .models import Participation, Round
|
||
|
|
from .status import public_state
|
||
|
|
|
||
|
|
PARTICIPATION_LABELS = {
|
||
|
|
Participation.Status.PENDING: ("In attesa di conferma", "La tua puntata è stata inviata e aspetta la conferma. "
|
||
|
|
"Fino ad allora non partecipi ancora all'estrazione."),
|
||
|
|
Participation.Status.CONFIRMED: ("Confermata", "Partecipi all'estrazione di questo round."),
|
||
|
|
Participation.Status.REJECTED: ("Rifiutata", "La puntata non è stata accettata: i fondi sono tornati disponibili."),
|
||
|
|
Participation.Status.EXCLUDED: ("Esclusa dall'estrazione", "La puntata è stata esclusa dall'estrazione e l'importo ti è stato rimborsato."),
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def _participation_block(participation: Participation) -> dict[str, Any]:
|
||
|
|
label, message = PARTICIPATION_LABELS[participation.status]
|
||
|
|
return {"status": participation.status, "label": label, "message": message, "round_id": participation.round_id,
|
||
|
|
"amount_text": plm_text(participation.bet_amount_sats)}
|
||
|
|
|
||
|
|
|
||
|
|
def _last_result(user, active_round_id: int | None) -> str:
|
||
|
|
"""How the player's most recent finished round ended, in one sentence ('' if there is nothing to say)."""
|
||
|
|
last = (Participation.objects.filter(user=user).exclude(round_id=active_round_id).select_related("round")
|
||
|
|
.order_by("-round_id").first())
|
||
|
|
if last is None or last.round.status != Round.Status.CLOSED:
|
||
|
|
return ""
|
||
|
|
if last.status == Participation.Status.CONFIRMED:
|
||
|
|
if last.round.winner_id == user.pk:
|
||
|
|
return f"Round {last.round_id}: hai vinto {plm_text(last.round.winner_amount_sats or 0)}."
|
||
|
|
return f"Round {last.round_id}: non hai vinto questa volta."
|
||
|
|
if last.status == Participation.Status.REJECTED:
|
||
|
|
return f"Round {last.round_id}: la tua puntata è stata rifiutata e i fondi ti sono tornati."
|
||
|
|
if last.status == Participation.Status.EXCLUDED:
|
||
|
|
return f"Round {last.round_id}: la tua puntata è stata esclusa e rimborsata."
|
||
|
|
return ""
|
||
|
|
|
||
|
|
|
||
|
|
def player_state(user, now: datetime | None = None, public: dict[str, Any] | None = None) -> dict[str, Any]:
|
||
|
|
now = now or timezone.now()
|
||
|
|
public = public or public_state(now)
|
||
|
|
balance = balance_of(user)
|
||
|
|
participation = (Participation.objects.filter(user=user, round_id=public["round_id"]).first()
|
||
|
|
if public["round_id"] else None)
|
||
|
|
|
||
|
|
can_bet = public["can_participate"] and participation is None and balance.spendable_sats >= public["bet_amount_sats"]
|
||
|
|
blocker = ""
|
||
|
|
if not can_bet:
|
||
|
|
if participation is not None:
|
||
|
|
blocker = "" # the bet's own status already says it
|
||
|
|
elif not public["can_participate"]:
|
||
|
|
blocker = public["message"] if public["state"] != "open" else ""
|
||
|
|
elif balance.total_sats >= public["bet_amount_sats"]:
|
||
|
|
blocker = "Il saldo include fondi ancora in conferma: potrai puntare quando saranno confermati."
|
||
|
|
else:
|
||
|
|
blocker = f"Per puntare servono {public['bet_amount_text']} spendibili: deposita dal portafoglio."
|
||
|
|
|
||
|
|
return {
|
||
|
|
"spendable_sats": balance.spendable_sats, "spendable_text": plm_text(balance.spendable_sats),
|
||
|
|
"incoming_sats": balance.incoming_sats, "incoming_text": plm_text(balance.incoming_sats),
|
||
|
|
"participation": _participation_block(participation) if participation else None,
|
||
|
|
"can_bet": can_bet, "bet_blocker": blocker,
|
||
|
|
"last_result": _last_result(user, public["round_id"]),
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def full_state(user, now: datetime | None = None) -> dict[str, Any]:
|
||
|
|
"""The public state plus the player's own block, for `/app/stato/`."""
|
||
|
|
now = now or timezone.now()
|
||
|
|
public = public_state(now)
|
||
|
|
return {**public, "me": player_state(user, now, public)}
|