restore betting

remotes/1693045480750635534/spooky-22
Aevann1 2022-08-26 23:53:17 +02:00
parent ac98f54902
commit 45d5f52388
10 changed files with 120 additions and 84 deletions

View File

@ -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;
}

View File

@ -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")

View File

@ -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'<input class="d-none" id="current-{self.id}"{curr}>'
for c in self.options:
if c.exclusive:
body += f'''<div class="custom-control"><input name="choice-{self.id}" autocomplete="off" class="custom-control-input" type="radio" id="{c.id}" onchange="choice_vote('{c.id}','{self.id}','post')"'''
if c.voted(v): body += " checked "
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html}<span class="presult-{self.id}'''
body += f'"> - <a href="/votes/post/option/{c.id}"><span id="choice-{c.id}">{c.upvotes}</span> votes</a></span></label></div>'
for o in self.options:
if o.exclusive == 2:
body += f'''<div class="custom-control"><input name="bet-{self.id}" autocomplete="off" class="custom-control-input bet" type="radio" id="{o.id}" onchange="bet_vote('{o.id}','{self.id}')"'''
if o.voted(v): body += " checked "
if not (v and v.coins >= 200) or self.total_bet_voted(v): body += " disabled "
body += f'''><label class="custom-control-label" for="{o.id}">{o.body_html}<span class="presult-{self.id}'''
body += f'"> - <a href="/votes/post/option/{o.id}"><span id="bet-{o.id}">{o.upvotes}</span> bets</a>'
if not self.total_bet_voted(v):
body += '''<span class="cost"> (cost of entry: 200 coins)</span>'''
body += "</label>"
if v and v.admin_level > 2:
body += f'''<button class="btn btn-primary px-2 mx-2" style="font-size:10px;padding:2px" onclick="post_toast(this,'/distribute/{o.id}')">Declare winner</button>'''
body += "</div>"
elif o.exclusive == 1:
body += f'''<div class="custom-control"><input name="choice-{self.id}" autocomplete="off" class="custom-control-input" type="radio" id="{o.id}" onchange="choice_vote('{o.id}','{self.id}','post')"'''
if o.voted(v): body += " checked "
body += f'''><label class="custom-control-label" for="{o.id}">{o.body_html}<span class="presult-{self.id}'''
body += f'"> - <a href="/votes/post/option/{o.id}"><span id="choice-{o.id}">{o.upvotes}</span> votes</a></span></label></div>'
else:
body += f'<div class="custom-control"><input type="checkbox" class="custom-control-input" id="{c.id}" name="option"'
if c.voted(v): body += " checked"
if v: body += f''' onchange="poll_vote('{c.id}', 'post')"'''
else: body += f''' onchange="poll_vote_no_v('{c.id}', '{self.id}')"'''
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html}<span class="presult-{self.id}'''
body += f'"> - <a href="/votes/post/option/{c.id}"><span id="poll-{c.id}">{c.upvotes}</span> votes</a></span></label></div>'
body += f'<div class="custom-control"><input type="checkbox" class="custom-control-input" id="{o.id}" name="option"'
if o.voted(v): body += " checked"
if v: body += f''' onchange="poll_vote('{o.id}', 'post')"'''
else: body += f''' onchange="poll_vote_no_v('{o.id}', '{self.id}')"'''
body += f'''><label class="custom-control-label" for="{o.id}">{o.body_html}<span class="presult-{self.id}'''
body += f'"> - <a href="/votes/post/option/{o.id}"><span id="poll-{o.id}">{o.upvotes}</span> votes</a></span></label></div>'

View File

@ -183,52 +183,60 @@ def remove_admin(v, username):
return {"message": "Admin removed!"}
# @app.post("/distribute/<comment>")
# @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/<option_id>")
@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("/@<username>/revert_actions")
@limiter.limit("1/second;30/minute;200/hour;1000/day")

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -13,7 +13,7 @@
</pre>
<h3>{{thing.body_html | safe}} - {{ups | length}} votes</h3>
<h3>{{thing.body_html | safe}} - {{ups | length}} {% if thing.exclusive == 2 %}bets{% else %}votes{% endif %}</h3>
<div class="overflow-x-auto mt-5">
<table class="table table-striped mb-5">
<thead class="bg-primary text-white">

View File

@ -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,

View File

@ -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
);