fix(market): per-event bet loss+win
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user