(Lion): Update to account for current session.

Remove `time` pending and syncing logic.
Update `time` and `coins` to account for current session.
Add `Session.duration` for current session duration.
Add `Session.coins_earned` for current session coins.
This commit is contained in:
2021-12-04 10:24:11 +02:00
parent cffdfb693b
commit 144ccf9e81
4 changed files with 50 additions and 22 deletions

View File

@@ -57,7 +57,7 @@ lion_ranks = Table('member_ranks', attach_as='lion_ranks')
def add_pending(pending): def add_pending(pending):
""" """
pending: pending:
List of tuples of the form `(userid, pending_coins, pending_time)`. List of tuples of the form `(guildid, userid, pending_coins)`.
""" """
with lions.conn: with lions.conn:
cursor = lions.conn.cursor() cursor = lions.conn.cursor()
@@ -66,12 +66,11 @@ def add_pending(pending):
""" """
UPDATE members UPDATE members
SET SET
coins = coins + t.coin_diff, coins = coins + t.coin_diff
tracked_time = tracked_time + t.time_diff
FROM FROM
(VALUES %s) (VALUES %s)
AS AS
t (guildid, userid, coin_diff, time_diff) t (guildid, userid, coin_diff)
WHERE WHERE
members.guildid = t.guildid members.guildid = t.guildid
AND AND

View File

@@ -12,7 +12,7 @@ class Lion:
Mostly acts as a transparent interface to the corresponding Row, Mostly acts as a transparent interface to the corresponding Row,
but also adds some transaction caching logic to `coins` and `tracked_time`. but also adds some transaction caching logic to `coins` and `tracked_time`.
""" """
__slots__ = ('guildid', 'userid', '_pending_coins', '_pending_time', '_member') __slots__ = ('guildid', 'userid', '_pending_coins', '_member')
# Members with pending transactions # Members with pending transactions
_pending = {} # userid -> User _pending = {} # userid -> User
@@ -25,7 +25,6 @@ class Lion:
self.userid = userid self.userid = userid
self._pending_coins = 0 self._pending_coins = 0
self._pending_time = 0
self._member = None self._member = None
@@ -93,16 +92,33 @@ class Lion:
@property @property
def time(self): def time(self):
""" """
Amount of time the user has spent studying, accounting for pending values. Amount of time the user has spent studying, accounting for a current session.
""" """
return int(self.data.tracked_time + self._pending_time) # Base time from cached member data
time = self.data.tracked_time
# Add current session time if it exists
if session := self.session:
time += session.duration
return int(time)
@property @property
def coins(self): def coins(self):
""" """
Number of coins the user has, accounting for the pending value. Number of coins the user has, accounting for the pending value and current session.
""" """
return int(self.data.coins + self._pending_coins) # Base coin amount from cached member data
coins = self.data.coins
# Add pending coin amount
coins += self._pending_coins
# Add current session coins if applicable
if session := self.session:
coins += session.coins_earned
return int(coins)
@property @property
def session(self): def session(self):
@@ -176,15 +192,6 @@ class Lion:
if flush: if flush:
self.flush() self.flush()
def addTime(self, amount, flush=True):
"""
Add time to a user (in seconds), optionally storing the transaction in pending.
"""
self._pending_time += amount
self._pending[self.key] = self
if flush:
self.flush()
def flush(self): def flush(self):
""" """
Flush any pending transactions to the database. Flush any pending transactions to the database.
@@ -202,7 +209,7 @@ class Lion:
if lions: if lions:
# Build userid to pending coin map # Build userid to pending coin map
pending = [ pending = [
(lion.guildid, lion.userid, int(lion._pending_coins), int(lion._pending_time)) (lion.guildid, lion.userid, int(lion._pending_coins))
for lion in lions for lion in lions
] ]
@@ -212,5 +219,4 @@ class Lion:
# Cleanup pending users # Cleanup pending users
for lion in lions: for lion in lions:
lion._pending_coins -= int(lion._pending_coins) lion._pending_coins -= int(lion._pending_coins)
lion._pending_time -= int(lion._pending_time)
cls._pending.pop(lion.key, None) cls._pending.pop(lion.key, None)

View File

@@ -128,8 +128,31 @@ class Session:
@property @property
def data(self): def data(self):
"""
Row of the `current_sessions` table corresponding to this session.
"""
return current_sessions.fetch(self.key) return current_sessions.fetch(self.key)
@property
def duration(self):
"""
Current duration of the session.
"""
return (utc_now() - self.data.start_time).total_seconds()
@property
def coins_earned(self):
"""
Number of coins earned so far.
"""
data = self.data
coins = self.duration * data.hourly_coins
coins += data.live_duration * data.hourly_live_coins
if data.live_start:
coins += (utc_now() - data.live_start).total_seconds() * data.hourly_live_coins
return coins // 3600
def activate(self): def activate(self):
""" """
Activate the study session. Activate the study session.