(leaderboards): Update to support sessions.

Use `member_totals` to generate leaderboards instead of `members`.
Fix typo in data coin conversion.
This commit is contained in:
2021-12-04 11:36:52 +02:00
parent bb181d9039
commit 179b6ebf4e
5 changed files with 24 additions and 31 deletions

View File

@@ -46,19 +46,15 @@ async def cmd_topcoin(ctx):
exclude.update(ctx.client.objects['blacklisted_users']) exclude.update(ctx.client.objects['blacklisted_users'])
exclude.update(ctx.client.objects['ignored_members'][ctx.guild.id]) exclude.update(ctx.client.objects['ignored_members'][ctx.guild.id])
args = {
'guildid': ctx.guild.id,
'select_columns': ('userid', 'total_coins::INTEGER'),
'_extra': "AND total_coins > 0 ORDER BY total_coins DESC " + ("LIMIT 100" if top100 else "")
}
if exclude: if exclude:
user_data = tables.lions.select_where( args['userid'] = data.NOT(list(exclude))
guildid=ctx.guild.id,
userid=data.NOT(list(exclude)), user_data = tables.members_totals.select_where(**args)
select_columns=('userid', 'coins'),
_extra="AND coins > 0 ORDER BY coins DESC " + ("LIMIT 100" if top100 else "")
)
else:
user_data = tables.lions.select_where(
guildid=ctx.guild.id,
select_columns=('userid', 'coins'),
_extra="AND coins > 0 ORDER BY coins DESC " + ("LIMIT 100" if top100 else "")
)
# Quit early if the leaderboard is empty # Quit early if the leaderboard is empty
if not user_data: if not user_data:

View File

@@ -38,27 +38,20 @@ async def cmd_top(ctx):
) )
top100 = (ctx.args == "100" or ctx.alias == "top100") top100 = (ctx.args == "100" or ctx.alias == "top100")
# Flush any pending coin transactions
Lion.sync()
# Fetch the leaderboard # Fetch the leaderboard
exclude = set(m.id for m in ctx.guild_settings.unranked_roles.members) exclude = set(m.id for m in ctx.guild_settings.unranked_roles.members)
exclude.update(ctx.client.objects['blacklisted_users']) exclude.update(ctx.client.objects['blacklisted_users'])
exclude.update(ctx.client.objects['ignored_members'][ctx.guild.id]) exclude.update(ctx.client.objects['ignored_members'][ctx.guild.id])
args = {
'guildid': ctx.guild.id,
'select_columns': ('userid', 'total_tracked_time::INTEGER'),
'_extra': "AND total_tracked_time > 0 ORDER BY total_tracked_time DESC " + ("LIMIT 100" if top100 else "")
}
if exclude: if exclude:
user_data = tables.lions.select_where( args['userid'] = data.NOT(list(exclude))
guildid=ctx.guild.id,
userid=data.NOT(list(exclude)), user_data = tables.members_totals.select_where(**args)
select_columns=('userid', 'tracked_time'),
_extra="AND tracked_time > 0 ORDER BY tracked_time DESC " + ("LIMIT 100" if top100 else "")
)
else:
user_data = tables.lions.select_where(
guildid=ctx.guild.id,
select_columns=('userid', 'tracked_time'),
_extra="AND tracked_time > 0 ORDER BY tracked_time DESC " + ("LIMIT 100" if top100 else "")
)
# Quit early if the leaderboard is empty # Quit early if the leaderboard is empty
if not user_data: if not user_data:
@@ -68,6 +61,7 @@ async def cmd_top(ctx):
author_index = None author_index = None
entries = [] entries = []
for i, (userid, time) in enumerate(user_data): for i, (userid, time) in enumerate(user_data):
time = int(time)
member = ctx.guild.get_member(userid) member = ctx.guild.get_member(userid)
name = member.display_name if member else str(userid) name = member.display_name if member else str(userid)
name = name.replace('*', ' ').replace('_', ' ') name = name.replace('*', ' ').replace('_', ' ')

View File

@@ -57,3 +57,6 @@ def study_time_since(guildid, userid, timestamp):
cursor.callproc('study_time_since', (guildid, userid, timestamp)) cursor.callproc('study_time_since', (guildid, userid, timestamp))
rows = cursor.fetchall() rows = cursor.fetchall()
return (rows[0][0] if rows else None) or 0 return (rows[0][0] if rows else None) or 0
members_totals = Table('members_totals')

View File

@@ -77,7 +77,7 @@ AS $$
) SELECT ) SELECT
guildid, userid, channelid, channel_type, start_time, guildid, userid, channelid, channel_type, start_time,
total_duration, total_stream_duration, total_video_duration, total_live_duration, total_duration, total_stream_duration, total_video_duration, total_live_duration,
(total_duration * hourly_coins + live_duration * hourly_live_coins) / 60 (total_duration * hourly_coins + live_duration * hourly_live_coins) / 3600
FROM current_sesh FROM current_sesh
RETURNING * RETURNING *
) )
@@ -108,7 +108,7 @@ CREATE VIEW members_totals AS
*, *,
sesh.start_time AS session_start, sesh.start_time AS session_start,
tracked_time + COALESCE(sesh.total_duration, 0) AS total_tracked_time, tracked_time + COALESCE(sesh.total_duration, 0) AS total_tracked_time,
coins + COALESCE((sesh.total_duration * sesh.hourly_coins + sesh.live_duration * sesh.hourly_live_coins) / 60, 0) AS total_coins coins + COALESCE((sesh.total_duration * sesh.hourly_coins + sesh.live_duration * sesh.hourly_live_coins) / 3600, 0) AS total_coins
FROM members FROM members
LEFT JOIN current_sessions_totals sesh USING (guildid, userid); LEFT JOIN current_sessions_totals sesh USING (guildid, userid);

View File

@@ -514,7 +514,7 @@ AS $$
) SELECT ) SELECT
guildid, userid, channelid, channel_type, start_time, guildid, userid, channelid, channel_type, start_time,
total_duration, total_stream_duration, total_video_duration, total_live_duration, total_duration, total_stream_duration, total_video_duration, total_live_duration,
(total_duration * hourly_coins + live_duration * hourly_live_coins) / 60 (total_duration * hourly_coins + live_duration * hourly_live_coins) / 3600
FROM current_sesh FROM current_sesh
RETURNING * RETURNING *
) )
@@ -544,7 +544,7 @@ CREATE VIEW members_totals AS
*, *,
sesh.start_time AS session_start, sesh.start_time AS session_start,
tracked_time + COALESCE(sesh.total_duration, 0) AS total_tracked_time, tracked_time + COALESCE(sesh.total_duration, 0) AS total_tracked_time,
coins + COALESCE((sesh.total_duration * sesh.hourly_coins + sesh.live_duration * sesh.hourly_live_coins) / 60, 0) AS total_coins coins + COALESCE((sesh.total_duration * sesh.hourly_coins + sesh.live_duration * sesh.hourly_live_coins) / 3600, 0) AS total_coins
FROM members FROM members
LEFT JOIN current_sessions_totals sesh USING (guildid, userid); LEFT JOIN current_sessions_totals sesh USING (guildid, userid);