From d869353fdff4c517bccbad817b99fefff41a3f27 Mon Sep 17 00:00:00 2001 From: TLSM Date: Sun, 23 Oct 2022 18:23:51 -0400 Subject: [PATCH] Track coins awarded with votes. Supports more complicated double XP shenanigans without creating any cycles of user actions that create positive DC. --- files/classes/votes.py | 2 ++ files/routes/votes.py | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/files/classes/votes.py b/files/classes/votes.py index 897776986..89e72956e 100644 --- a/files/classes/votes.py +++ b/files/classes/votes.py @@ -14,6 +14,7 @@ class Vote(Base): vote_type = Column(Integer) app_id = Column(Integer, ForeignKey("oauth_apps.id")) real = Column(Boolean, default=True) + coins = Column(Integer, default=1, nullable=False) created_utc = Column(Integer) user = relationship("User") @@ -44,6 +45,7 @@ class CommentVote(Base): vote_type = Column(Integer) app_id = Column(Integer, ForeignKey("oauth_apps.id")) real = Column(Boolean, default=True) + coins = Column(Integer, default=1, nullable=False) created_utc = Column(Integer) user = relationship("User") diff --git a/files/routes/votes.py b/files/routes/votes.py index 8b6fce640..033f1c4a5 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -72,22 +72,21 @@ def vote_post_comment(target_id, new, v, cls, vote_cls): abort(400) existing = existing.one_or_none() - if DOUBLE_XP_ENABLED > 0: - if not existing and int(time.time()) > DOUBLE_XP_ENABLED: - coin_mult = 2 - elif existing and existing.created_utc > DOUBLE_XP_ENABLED: - coin_mult = 2 + if DOUBLE_XP_ENABLED > 0 and int(time.time()) > DOUBLE_XP_ENABLED: + coin_mult = 2 + coin_value = coin_delta * coin_mult if existing and existing.vote_type == new: return "", 204 if existing: if existing.vote_type == 0 and new != 0: - target.author.coins += coin_delta * coin_mult + target.author.coins += coin_value target.author.truecoins += coin_delta g.db.add(target.author) existing.vote_type = new + existing.coins = coin_value g.db.add(existing) elif existing.vote_type != 0 and new == 0: - target.author.charge_account('coins', coin_delta * coin_mult) + target.author.charge_account('coins', existing.coins) target.author.truecoins -= coin_delta g.db.add(target.author) g.db.delete(existing) @@ -95,7 +94,7 @@ def vote_post_comment(target_id, new, v, cls, vote_cls): existing.vote_type = new g.db.add(existing) elif new != 0: - target.author.coins += coin_delta * coin_mult + target.author.coins += coin_value target.author.truecoins += coin_delta g.db.add(target.author) @@ -106,14 +105,16 @@ def vote_post_comment(target_id, new, v, cls, vote_cls): vote_type=new, submission_id=target_id, app_id=v.client.application.id if v.client else None, - real = real + real=real, + coins=coin_value ) elif vote_cls == CommentVote: vote = CommentVote(user_id=v.id, vote_type=new, comment_id=target_id, app_id=v.client.application.id if v.client else None, - real=real + real=real, + coins=coin_value ) g.db.add(vote) g.db.flush()