rewrite: New Scheduled Session System.
This commit is contained in:
@@ -37,6 +37,8 @@ class VoiceTrackerCog(LionCog):
|
||||
self.babel = babel
|
||||
|
||||
# State
|
||||
# Flag indicating whether local voice sessions have been initialised
|
||||
self.initialised = asyncio.Event()
|
||||
self.handle_events = False
|
||||
self.tracking_lock = asyncio.Lock()
|
||||
|
||||
@@ -92,6 +94,7 @@ class VoiceTrackerCog(LionCog):
|
||||
|
||||
logger.debug("Disabling voice state event handling.")
|
||||
self.handle_events = False
|
||||
self.initialised.clear()
|
||||
# Read and save the tracked voice states of all visible voice channels
|
||||
voice_members = {} # (guildid, userid) -> TrackedVoiceState
|
||||
voice_guilds = set()
|
||||
@@ -252,6 +255,7 @@ class VoiceTrackerCog(LionCog):
|
||||
for row in rows:
|
||||
VoiceSession.from_ongoing(self.bot, row, expiries[(row.guildid, row.userid)])
|
||||
logger.info(f"Started {len(rows)} new voice sessions from voice channels!")
|
||||
self.initialised.set()
|
||||
|
||||
@LionCog.listener("on_voice_state_update")
|
||||
@log_wrap(action='Voice Track')
|
||||
@@ -259,7 +263,6 @@ class VoiceTrackerCog(LionCog):
|
||||
"""
|
||||
Spawns the correct tasks from members joining, leaving, and changing live state.
|
||||
"""
|
||||
# TODO: Logging context
|
||||
if not self.handle_events:
|
||||
# Rely on initialisation to handle current state
|
||||
return
|
||||
@@ -505,7 +508,7 @@ class VoiceTrackerCog(LionCog):
|
||||
delay = (tomorrow - now).total_seconds()
|
||||
else:
|
||||
start_time = now
|
||||
delay = 60
|
||||
delay = 20
|
||||
|
||||
expiry = start_time + dt.timedelta(seconds=cap)
|
||||
if expiry >= tomorrow:
|
||||
|
||||
@@ -173,6 +173,7 @@ class VoiceSession:
|
||||
live_video=state.video,
|
||||
hourly_coins=self.hourly_rate
|
||||
)
|
||||
self.bot.dispatch('voice_session_start', self.data)
|
||||
self.start_task = None
|
||||
|
||||
def schedule_expiry(self, expire_time):
|
||||
@@ -230,7 +231,11 @@ class VoiceSession:
|
||||
"""
|
||||
if self.activity is SessionState.ONGOING:
|
||||
# End the ongoing session
|
||||
await self.data.close_study_session_at(self.guildid, self.userid, utc_now())
|
||||
now = utc_now()
|
||||
await self.data.close_study_session_at(self.guildid, self.userid, now)
|
||||
|
||||
# TODO: Something a bit saner/safer.. dispatch the finished session instead?
|
||||
self.bot.dispatch('voice_session_end', self.data, now)
|
||||
|
||||
# Rank update
|
||||
# TODO: Change to broadcasted event?
|
||||
|
||||
Reference in New Issue
Block a user