feat: Add data to channel payload.
Add entire goal list to payload. Add contributions leaderboard to payload. Add raw sub point total to payload. Add raw bits total to payload. Add subpoint and bits total computations to ActiveSubathon.
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
from asyncio import Event
|
||||
from datetime import datetime, timedelta
|
||||
from data.queries import JOINTYPE
|
||||
from utils.lib import utc_now, strfdelta
|
||||
from modules.profiles.profiles.profiles import ProfilesRegistry
|
||||
|
||||
from . import logger
|
||||
from .data import SubathonData, Subathon, RunningSubathon, SubathonContribution, SubathonGoal
|
||||
from .data import (
|
||||
SubathonData,
|
||||
Subathon,
|
||||
RunningSubathon,
|
||||
SubathonContribution,
|
||||
SubathonGoal,
|
||||
)
|
||||
|
||||
|
||||
class ActiveSubathon:
|
||||
def __init__(self, subathondata: Subathon, runningdata: RunningSubathon | None):
|
||||
@@ -17,7 +25,7 @@ class ActiveSubathon:
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.subathondata.name or 'Subathon'
|
||||
return self.subathondata.name or "Subathon"
|
||||
|
||||
async def check_cap(self):
|
||||
if not (cap := self.subathondata.timecap):
|
||||
@@ -32,7 +40,7 @@ class ActiveSubathon:
|
||||
"""
|
||||
Return True if the subathon duration has exceeded its earned time.
|
||||
"""
|
||||
return (await self.get_remaining() <= 0)
|
||||
return await self.get_remaining() <= 0
|
||||
|
||||
async def pause(self):
|
||||
"""
|
||||
@@ -53,10 +61,14 @@ class ActiveSubathon:
|
||||
async def resume(self):
|
||||
if self.running:
|
||||
raise ValueError("This subathon is already running!")
|
||||
self.runningdata = await RunningSubathon.create(subathon_id=self.subathondata.subathon_id)
|
||||
self.runningdata = await RunningSubathon.create(
|
||||
subathon_id=self.subathondata.subathon_id
|
||||
)
|
||||
|
||||
async def get_score(self) -> float:
|
||||
rows = await SubathonContribution.fetch_where(subathon_id=self.subathondata.subathon_id)
|
||||
rows = await SubathonContribution.fetch_where(
|
||||
subathon_id=self.subathondata.subathon_id
|
||||
)
|
||||
return sum(row.score for row in rows)
|
||||
|
||||
def get_score_time(self, score: float) -> int:
|
||||
@@ -75,7 +87,7 @@ class ActiveSubathon:
|
||||
# Add running duration if required
|
||||
if self.runningdata:
|
||||
now = utc_now()
|
||||
added = int( (now - self.runningdata.last_started).total_seconds() )
|
||||
added = int((now - self.runningdata.last_started).total_seconds())
|
||||
duration += added
|
||||
earned = await self.get_earned()
|
||||
|
||||
@@ -111,23 +123,108 @@ class ActiveSubathon:
|
||||
remaining = await self.get_remaining()
|
||||
return now + timedelta(seconds=remaining)
|
||||
|
||||
async def add_contribution(self, profileid: int | None, score: float, event_id: int | None) -> SubathonContribution:
|
||||
async def add_contribution(
|
||||
self, profileid: int | None, score: float, event_id: int | None
|
||||
) -> SubathonContribution:
|
||||
return await SubathonContribution.create(
|
||||
subathon_id=self.subathondata.subathon_id,
|
||||
profileid=profileid, score=score, event_id=event_id
|
||||
profileid=profileid,
|
||||
score=score,
|
||||
event_id=event_id,
|
||||
)
|
||||
|
||||
def fetch_contributions(self, **kwargs):
|
||||
query = SubathonContribution.fetch_where(
|
||||
subathon_id=self.subathondata.subathon_id,
|
||||
**kwargs
|
||||
).order_by('created_at')
|
||||
subathon_id=self.subathondata.subathon_id, **kwargs
|
||||
).order_by("created_at")
|
||||
return query
|
||||
|
||||
async def get_goals(self) -> list[SubathonGoal]:
|
||||
goals = await SubathonGoal.fetch_where(subathon_id=self.subathondata.subathon_id).order_by('required_score')
|
||||
goals = await SubathonGoal.fetch_where(
|
||||
subathon_id=self.subathondata.subathon_id
|
||||
).order_by("required_score")
|
||||
return goals
|
||||
|
||||
async def get_total_subs(self) -> int:
|
||||
"""
|
||||
Get the total number of sub points
|
||||
contributed to this subathon.
|
||||
"""
|
||||
pointcol = (
|
||||
"CASE "
|
||||
"WHEN tier = 1000 THEN 1 "
|
||||
"WHEN tier = 2000 THEN 2 "
|
||||
"WHEN tier = 3000 THEN 6 "
|
||||
"END"
|
||||
)
|
||||
total_points = 0
|
||||
|
||||
result = await (
|
||||
SubathonContribution.table.select_where(
|
||||
subathoid=self.subathondata.subathon_id
|
||||
)
|
||||
.join(
|
||||
"subscribe_events",
|
||||
using=("eventid",),
|
||||
join_type=JOINTYPE.INNER,
|
||||
)
|
||||
.select(total=f"SUM({pointcol})")
|
||||
.with_no_adapter()
|
||||
)
|
||||
|
||||
total_points += result["total"]
|
||||
|
||||
result = await (
|
||||
SubathonContribution.table.select_where(
|
||||
subathoid=self.subathondata.subathon_id
|
||||
)
|
||||
.join(
|
||||
"subscribe_message_events",
|
||||
using=("eventid",),
|
||||
join_type=JOINTYPE.INNER,
|
||||
)
|
||||
.select(total=f"SUM({pointcol})")
|
||||
.with_no_adapter()
|
||||
)
|
||||
|
||||
total_points += result["total"]
|
||||
|
||||
result = await (
|
||||
SubathonContribution.table.select_where(
|
||||
subathoid=self.subathondata.subathon_id
|
||||
)
|
||||
.join(
|
||||
"subscribe_gift_events",
|
||||
using=("eventid",),
|
||||
join_type=JOINTYPE.INNER,
|
||||
)
|
||||
.select(total=f"SUM(gifted_count * ({pointcol}))")
|
||||
.with_no_adapter()
|
||||
)
|
||||
|
||||
total_points += result["total"]
|
||||
|
||||
return total_points
|
||||
|
||||
async def get_total_bits(self) -> int:
|
||||
"""
|
||||
Get the total number of bits
|
||||
contributed to this subathon.
|
||||
"""
|
||||
result = await (
|
||||
SubathonContribution.table.select_where(
|
||||
subathoid=self.subathondata.subathon_id
|
||||
)
|
||||
.join(
|
||||
"bits_events",
|
||||
using=("eventid",),
|
||||
join_type=JOINTYPE.INNER,
|
||||
)
|
||||
.select(total="SUM(bits)")
|
||||
.with_no_adapter()
|
||||
)
|
||||
return result["total"]
|
||||
|
||||
|
||||
class SubathonRegistry:
|
||||
def __init__(self, data: SubathonData, profiler: ProfilesRegistry):
|
||||
@@ -144,5 +241,3 @@ class SubathonRegistry:
|
||||
running = await RunningSubathon.fetch(subathondata.subathon_id)
|
||||
subba = ActiveSubathon(subathondata, running)
|
||||
return subba
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user