fix(market): per-event bet loss+win

This commit is contained in:
2026-10-07 21:44:33 +02:00
parent 6a5a224d3b
commit 382ba01f3e
8 changed files with 107 additions and 26 deletions
+18
View File
@@ -1,10 +1,28 @@
import uuid
from django.db import models
from django.db.models import Sum
from events.models import Event
from events.scoping import EventScopedManager
# Each event's market starts every user with this balance
STARTING_POINTS = 1000
def get_event_points(user, event) -> int:
"""User's market balance for an event: starting points + the event's point changes.
BET_LOST rows are informational (the stake was already deducted by BET_PLACED).
"""
total = (
UserPointChange.objects.filter(user=user, market__event=event)
.exclude(reason=UserPointChange.Reason.BET_LOST)
.aggregate(total=Sum("amount"))["total"]
)
return STARTING_POINTS + (total or 0)
class BaseModel(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
created_at = models.DateTimeField(auto_now_add=True)