fix (data): Cap coin updates.
Fixes an issue where the session system could overflow `members.coins`.
This commit is contained in:
@@ -74,7 +74,7 @@ def add_pending(pending):
|
||||
"""
|
||||
UPDATE members
|
||||
SET
|
||||
coins = coins + t.coin_diff
|
||||
coins = LEAST(coins + t.coin_diff, 2147483647)
|
||||
FROM
|
||||
(VALUES %s)
|
||||
AS
|
||||
|
||||
@@ -77,6 +77,7 @@ class hourly_reward(settings.Integer, settings.GuildSetting):
|
||||
desc = "Number of LionCoins given per hour of study."
|
||||
|
||||
_default = 50
|
||||
_max = 32767
|
||||
|
||||
long_desc = (
|
||||
"Each spent in a voice channel will reward this number of LionCoins."
|
||||
@@ -99,6 +100,7 @@ class hourly_live_bonus(settings.Integer, settings.GuildSetting):
|
||||
desc = "Number of extra LionCoins given for a full hour of streaming (via go live or video)."
|
||||
|
||||
_default = 10
|
||||
_max = 32767
|
||||
|
||||
long_desc = (
|
||||
"LionCoin bonus earnt for every hour a member streams in a voice channel, including video. "
|
||||
|
||||
@@ -1,7 +1,66 @@
|
||||
DROP VIEW current_sessions_totals CASCADE;
|
||||
-- Add coin cap to close_study_session
|
||||
DROP FUNCTION close_study_session(_guildid BIGINT, _userid BIGINT);
|
||||
|
||||
CREATE FUNCTION close_study_session(_guildid BIGINT, _userid BIGINT)
|
||||
RETURNS SETOF members
|
||||
AS $$
|
||||
BEGIN
|
||||
RETURN QUERY
|
||||
WITH
|
||||
current_sesh AS (
|
||||
DELETE FROM current_sessions
|
||||
WHERE guildid=_guildid AND userid=_userid
|
||||
RETURNING
|
||||
*,
|
||||
EXTRACT(EPOCH FROM (NOW() - start_time)) AS total_duration,
|
||||
stream_duration + COALESCE(EXTRACT(EPOCH FROM (NOW() - stream_start)), 0) AS total_stream_duration,
|
||||
video_duration + COALESCE(EXTRACT(EPOCH FROM (NOW() - video_start)), 0) AS total_video_duration,
|
||||
live_duration + COALESCE(EXTRACT(EPOCH FROM (NOW() - live_start)), 0) AS total_live_duration
|
||||
), bonus_userid AS (
|
||||
SELECT COUNT(boostedTimestamp),
|
||||
CASE WHEN EXISTS (
|
||||
SELECT 1 FROM Topgg
|
||||
WHERE Topgg.userid=_userid AND EXTRACT(EPOCH FROM (NOW() - boostedTimestamp)) < 12.5*60*60
|
||||
) THEN
|
||||
(array_agg(
|
||||
CASE WHEN boostedTimestamp <= current_sesh.start_time THEN
|
||||
1.25
|
||||
ELSE
|
||||
(((current_sesh.total_duration - EXTRACT(EPOCH FROM (boostedTimestamp - current_sesh.start_time)))/current_sesh.total_duration)*0.25)+1
|
||||
END))[1]
|
||||
ELSE
|
||||
1
|
||||
END
|
||||
AS bonus
|
||||
FROM Topgg, current_sesh
|
||||
WHERE Topgg.userid=_userid AND EXTRACT(EPOCH FROM (NOW() - boostedTimestamp)) < 12.5*60*60
|
||||
ORDER BY (array_agg(boostedTimestamp))[1] DESC LIMIT 1
|
||||
), saved_sesh AS (
|
||||
INSERT INTO session_history (
|
||||
guildid, userid, channelid, rating, tag, channel_type, start_time,
|
||||
duration, stream_duration, video_duration, live_duration,
|
||||
coins_earned
|
||||
) SELECT
|
||||
guildid, userid, channelid, rating, tag, channel_type, start_time,
|
||||
total_duration, total_stream_duration, total_video_duration, total_live_duration,
|
||||
((total_duration * hourly_coins + live_duration * hourly_live_coins) * bonus_userid.bonus )/ 3600
|
||||
FROM current_sesh, bonus_userid
|
||||
RETURNING *
|
||||
)
|
||||
UPDATE members
|
||||
SET
|
||||
tracked_time=(tracked_time + saved_sesh.duration),
|
||||
coins=LEAST(coins + saved_sesh.coins_earned, 2147483647)
|
||||
FROM saved_sesh
|
||||
WHERE members.guildid=saved_sesh.guildid AND members.userid=saved_sesh.userid
|
||||
RETURNING members.*;
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
|
||||
-- Rebuild study data views
|
||||
DROP VIEW current_sessions_totals CASCADE;
|
||||
|
||||
CREATE VIEW current_sessions_totals AS
|
||||
SELECT
|
||||
*,
|
||||
|
||||
@@ -552,7 +552,7 @@ AS $$
|
||||
UPDATE members
|
||||
SET
|
||||
tracked_time=(tracked_time + saved_sesh.duration),
|
||||
coins=(coins + saved_sesh.coins_earned)
|
||||
coins=LEAST(coins + saved_sesh.coins_earned, 2147483647)
|
||||
FROM saved_sesh
|
||||
WHERE members.guildid=saved_sesh.guildid AND members.userid=saved_sesh.userid
|
||||
RETURNING members.*;
|
||||
|
||||
Reference in New Issue
Block a user