From 45d5f5238838099250edc4f7397dbda2ba98b7cf Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 26 Aug 2022 23:53:17 +0200 Subject: [PATCH] restore betting --- .../assets/js/comments+submission_listing.js | 3 +- files/classes/polls.py | 4 +- files/classes/submission.py | 46 ++++++++--- files/routes/admin.py | 82 ++++++++++--------- files/routes/comments.py | 8 +- files/routes/polls.py | 15 +++- files/routes/posts.py | 38 ++++----- files/templates/poll_votes.html | 2 +- files/templates/util/assetcache.html | 2 +- schema.sql | 4 +- 10 files changed, 120 insertions(+), 84 deletions(-) diff --git a/files/assets/js/comments+submission_listing.js b/files/assets/js/comments+submission_listing.js index 2546c2020..66d6bd041 100644 --- a/files/assets/js/comments+submission_listing.js +++ b/files/assets/js/comments+submission_listing.js @@ -116,6 +116,7 @@ function bet_vote(cid) { var scoretext = document.getElementById('bet-' + cid); var score = Number(scoretext.textContent); scoretext.textContent = score + 1; - post('/bet/' + cid); + post(`/vote/post/option/${cid}`); + document.getElementById("user-coins-amount").innerText = parseInt(document.getElementById("user-coins-amount").innerText) - 200; } \ No newline at end of file diff --git a/files/classes/polls.py b/files/classes/polls.py index bebd43365..066b3006d 100644 --- a/files/classes/polls.py +++ b/files/classes/polls.py @@ -11,7 +11,7 @@ class SubmissionOption(Base): id = Column(Integer, primary_key=True) submission_id = Column(Integer, ForeignKey("submissions.id")) body_html = Column(Text) - exclusive = Column(Boolean) + exclusive = Column(Integer) votes = relationship("SubmissionOptionVote") post = relationship("Submission", back_populates="options") @@ -56,7 +56,7 @@ class CommentOption(Base): id = Column(Integer, primary_key=True) comment_id = Column(Integer, ForeignKey("comments.id")) body_html = Column(Text) - exclusive = Column(Boolean) + exclusive = Column(Integer) votes = relationship("CommentOptionVote") comment = relationship("Comment", back_populates="options") diff --git a/files/classes/submission.py b/files/classes/submission.py index b15acd34a..adaf2da03 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -310,6 +310,14 @@ class Submission(Base): if v and v.controversial: url += "&sort=controversial" return url + + @lazy + def total_bet_voted(self, v): + if "closed" in self.body.lower(): return True + if v: + for o in self.options: + if o.exclusive == 2 and o.voted(v): return True + return False @lazy def realbody(self, v, listing=False): @@ -339,19 +347,33 @@ class Submission(Base): else: curr = '' body += f'' - for c in self.options: - if c.exclusive: - body += f'''
' + for o in self.options: + if o.exclusive == 2: + body += f'''
= 200) or self.total_bet_voted(v): body += " disabled " + + body += f'''>" + if v and v.admin_level > 2: + body += f'''''' + body += "
" + + elif o.exclusive == 1: + body += f'''
' else: - body += f'
' + body += f'
' diff --git a/files/routes/admin.py b/files/routes/admin.py index ce4330da1..3d6558c32 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -183,52 +183,60 @@ def remove_admin(v, username): return {"message": "Admin removed!"} -# @app.post("/distribute/") -# @limiter.limit("1/second;30/minute;200/hour;1000/day") -# @admin_level_required(3) -# def distribute(v, comment): -# autobetter = get_account(AUTOBETTER_ID) -# if autobetter.coins == 0: return {"error": "@AutoBetter has 0 coins"} +@app.post("/distribute/") +@limiter.limit("1/second;30/minute;200/hour;1000/day") +@admin_level_required(3) +def distribute(v, option_id): + autojanny = get_account(AUTOJANNY_ID) + if autojanny.coins == 0: return {"error": "@AutoJanny has 0 coins"} -# try: comment = int(comment) -# except: abort(400) -# post = g.db.query(Comment.parent_submission).filter_by(id=comment).one_or_none()[0] -# post = get_post(post) + try: option_id = int(option_id) + except: abort(400) -# pool = 0 -# for option in post.bet_options: pool += option.upvotes -# pool *= 200 + try: option = g.db.get(SubmissionOption, option_id) + except: abort(404) -# autobetter.coins -= pool -# if autobetter.coins < 0: autobetter.coins = 0 -# g.db.add(autobetter) + post = option.post -# votes = g.db.query(CommentVote).filter_by(comment_id=comment) -# coinsperperson = int(pool / votes.count()) + pool = 0 + for o in post.options: + if o.exclusive == 2: pool += o.upvotes + pool *= 200 -# cid = notif_comment(f"You won {coinsperperson} coins betting on [{post.title}]({post.shortlink}) :marseyparty:") -# for vote in votes: -# u = vote.user -# u.coins += coinsperperson -# add_notif(cid, u.id) + autojanny.coins -= pool + if autojanny.coins < 0: autojanny.coins = 0 + g.db.add(autojanny) -# cid = notif_comment(f"You lost the 200 coins you bet on [{post.title}]({post.shortlink}) :marseylaugh:") -# cids = [x.id for x in post.bet_options] -# cids.remove(comment) -# votes = g.db.query(CommentVote).filter(CommentVote.comment_id.in_(cids)).all() -# for vote in votes: add_notif(cid, vote.user.id) + votes = option.votes + coinsperperson = int(pool / len(votes)) -# post.body += '\n\nclosed' -# g.db.add(post) + cid = notif_comment(f"You won {coinsperperson} coins betting on [{post.title}]({post.shortlink}) :marseyparty:") + for vote in votes: + u = vote.user + u.coins += coinsperperson + add_notif(cid, u.id) + + + cid = notif_comment(f"You lost the 200 coins you bet on [{post.title}]({post.shortlink}) :marseylaugh:") + losing_voters = [] + for o in post.options: + if o.exclusive == 2 and o.id != option_id: + losing_voters.extend([x.user_id for x in o.votes]) + for uid in losing_voters: + add_notif(cid, uid) + + + post.body += '\n\nclosed' + g.db.add(post) -# ma = ModAction( -# kind="distribute", -# user_id=v.id, -# target_comment_id=comment -# ) -# g.db.add(ma) + ma = ModAction( + kind="distribute", + user_id=v.id, + target_submission_id=post.id + ) + g.db.add(ma) -# return {"message": f"Each winner has received {coinsperperson} coins!"} + return {"message": f"Each winner has received {coinsperperson} coins!"} @app.post("/@/revert_actions") @limiter.limit("1/second;30/minute;200/hour;1000/day") diff --git a/files/routes/comments.py b/files/routes/comments.py index 0aea556ee..bf6e57b54 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -377,7 +377,7 @@ def comment(v): option = CommentOption( comment_id=c.id, body_html=filter_emojis_only(option), - exclusive=False + exclusive=0 ) g.db.add(option) @@ -385,7 +385,7 @@ def comment(v): choice = CommentOption( comment_id=c.id, body_html=filter_emojis_only(choice), - exclusive=True + exclusive=1 ) g.db.add(choice) @@ -668,7 +668,7 @@ def edit_comment(cid, v): option = CommentOption( comment_id=c.id, body_html=filter_emojis_only(i.group(1)), - exclusive = False + exclusive = 0 ) g.db.add(option) @@ -677,7 +677,7 @@ def edit_comment(cid, v): option = CommentOption( comment_id=c.id, body_html=filter_emojis_only(i.group(1)), - exclusive = True + exclusive = 1 ) g.db.add(option) diff --git a/files/routes/polls.py b/files/routes/polls.py index ad890a69c..5e1e6c9db 100644 --- a/files/routes/polls.py +++ b/files/routes/polls.py @@ -16,12 +16,21 @@ def vote_option(option_id, v): if not option: abort(404) + if option.exclusive == 2: + if v.coins < 200: return {"error": "You don't have 200 coins!"} + v.coins -= 200 + g.db.add(v) + autojanny = get_account(AUTOJANNY_ID) + autojanny.coins += 200 + g.db.add(autojanny) + if option.exclusive: vote = g.db.query(SubmissionOptionVote).join(SubmissionOption).filter( SubmissionOptionVote.user_id==v.id, SubmissionOptionVote.submission_id==option.submission_id, - SubmissionOption.exclusive==True).one_or_none() + SubmissionOption.exclusive==option.exclusive).one_or_none() if vote: + if option.exclusive == 2: return {"error": "You already voted on this bet!"} g.db.delete(vote) existing = g.db.query(SubmissionOptionVote).filter_by(option_id=option_id, user_id=v.id).one_or_none() @@ -32,7 +41,7 @@ def vote_option(option_id, v): submission_id=option.submission_id, ) g.db.add(vote) - elif existing: + elif existing and not option.exclusive: g.db.delete(existing) return "", 204 @@ -72,7 +81,7 @@ def vote_option_comment(option_id, v): vote = g.db.query(CommentOptionVote).join(CommentOption).filter( CommentOptionVote.user_id==v.id, CommentOptionVote.comment_id==option.comment_id, - CommentOption.exclusive==True).one_or_none() + CommentOption.exclusive==1).one_or_none() if vote: g.db.delete(vote) diff --git a/files/routes/posts.py b/files/routes/posts.py index 702b05327..232edcc41 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -432,7 +432,7 @@ def edit_post(pid, v): option = SubmissionOption( submission_id=p.id, body_html=filter_emojis_only(i.group(1)), - exclusive = False + exclusive = 0 ) g.db.add(option) @@ -441,7 +441,7 @@ def edit_post(pid, v): option = SubmissionOption( submission_id=p.id, body_html=filter_emojis_only(i.group(1)), - exclusive = True + exclusive = 1 ) g.db.add(option) @@ -879,11 +879,11 @@ def submit_post(v, sub=None): if len(url) > 2048: return error("There's a 2048 character limit for URLs.") - # if v and v.admin_level > 2: - # bet_options = [] - # for i in bet_regex.finditer(body): - # bet_options.append(i.group(1)) - # body = body.replace(i.group(0), "") + if v and v.admin_level > 2: + bets = [] + for i in bet_regex.finditer(body): + bets.append(i.group(1)) + body = body.replace(i.group(0), "") options = [] for i in poll_regex.finditer(body): @@ -951,23 +951,11 @@ def submit_post(v, sub=None): g.db.add(v) send_repeatable_notification(CARP_ID, post.permalink) - # if v and v.admin_level > 2: - # for option in bet_options: - # bet_option = Comment(author_id=AUTOBETTER_ID, - # parent_submission=post.id, - # level=1, - # body_html=filter_emojis_only(option), - # upvotes=0, - # is_bot=True - # ) - - # g.db.add(bet_option) - for option in options: option = SubmissionOption( submission_id=post.id, body_html=filter_emojis_only(option), - exclusive=False + exclusive=0 ) g.db.add(option) @@ -975,10 +963,18 @@ def submit_post(v, sub=None): choice = SubmissionOption( submission_id=post.id, body_html=filter_emojis_only(choice), - exclusive=True + exclusive=1 ) g.db.add(choice) + for bet in bets: + bet = SubmissionOption( + submission_id=post.id, + body_html=filter_emojis_only(bet), + exclusive=2 + ) + g.db.add(bet) + vote = Vote(user_id=v.id, vote_type=1, submission_id=post.id diff --git a/files/templates/poll_votes.html b/files/templates/poll_votes.html index d8221f78f..69aa71f2a 100644 --- a/files/templates/poll_votes.html +++ b/files/templates/poll_votes.html @@ -13,7 +13,7 @@ -

{{thing.body_html | safe}} - {{ups | length}} votes

+

{{thing.body_html | safe}} - {{ups | length}} {% if thing.exclusive == 2 %}bets{% else %}votes{% endif %}

diff --git a/files/templates/util/assetcache.html b/files/templates/util/assetcache.html index dc05aab7a..97b3ac4e6 100644 --- a/files/templates/util/assetcache.html +++ b/files/templates/util/assetcache.html @@ -19,7 +19,7 @@ set CACHE_VER = { 'js/award_modal.js': 255, 'js/bootstrap.js': 279, 'js/category_modal.js': 200, - 'js/comments+submission_listing.js': 267, + 'js/comments+submission_listing.js': 269, 'js/submission_listing.js': 261, 'js/emoji_modal.js': 314, 'js/formatting.js': 240, diff --git a/schema.sql b/schema.sql index e2050c538..336cf3e6d 100644 --- a/schema.sql +++ b/schema.sql @@ -299,7 +299,7 @@ CREATE TABLE public.comment_options ( id integer DEFAULT nextval('public.comment_option_id_seq'::regclass) NOT NULL, comment_id integer NOT NULL, body_html character varying(500) NOT NULL, - exclusive boolean NOT NULL + exclusive integer NOT NULL ); @@ -695,7 +695,7 @@ CREATE TABLE public.submission_options ( id integer DEFAULT nextval('public.submission_option_id_seq'::regclass) NOT NULL, submission_id integer NOT NULL, body_html character varying(500) NOT NULL, - exclusive boolean NOT NULL + exclusive integer NOT NULL );