forked from rDrama/rDrama
restore betting
parent
ac98f54902
commit
45d5f52388
|
@ -116,6 +116,7 @@ function bet_vote(cid) {
|
||||||
var scoretext = document.getElementById('bet-' + cid);
|
var scoretext = document.getElementById('bet-' + cid);
|
||||||
var score = Number(scoretext.textContent);
|
var score = Number(scoretext.textContent);
|
||||||
scoretext.textContent = score + 1;
|
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;
|
document.getElementById("user-coins-amount").innerText = parseInt(document.getElementById("user-coins-amount").innerText) - 200;
|
||||||
}
|
}
|
|
@ -11,7 +11,7 @@ class SubmissionOption(Base):
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
submission_id = Column(Integer, ForeignKey("submissions.id"))
|
submission_id = Column(Integer, ForeignKey("submissions.id"))
|
||||||
body_html = Column(Text)
|
body_html = Column(Text)
|
||||||
exclusive = Column(Boolean)
|
exclusive = Column(Integer)
|
||||||
|
|
||||||
votes = relationship("SubmissionOptionVote")
|
votes = relationship("SubmissionOptionVote")
|
||||||
post = relationship("Submission", back_populates="options")
|
post = relationship("Submission", back_populates="options")
|
||||||
|
@ -56,7 +56,7 @@ class CommentOption(Base):
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
comment_id = Column(Integer, ForeignKey("comments.id"))
|
comment_id = Column(Integer, ForeignKey("comments.id"))
|
||||||
body_html = Column(Text)
|
body_html = Column(Text)
|
||||||
exclusive = Column(Boolean)
|
exclusive = Column(Integer)
|
||||||
|
|
||||||
votes = relationship("CommentOptionVote")
|
votes = relationship("CommentOptionVote")
|
||||||
comment = relationship("Comment", back_populates="options")
|
comment = relationship("Comment", back_populates="options")
|
||||||
|
|
|
@ -311,6 +311,14 @@ class Submission(Base):
|
||||||
|
|
||||||
return url
|
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
|
@lazy
|
||||||
def realbody(self, v, listing=False):
|
def realbody(self, v, listing=False):
|
||||||
if self.club and not (v and (v.paid_dues or v.id == self.author_id)): return f"<p>{CC} ONLY</p>"
|
if self.club and not (v and (v.paid_dues or v.id == self.author_id)): return f"<p>{CC} ONLY</p>"
|
||||||
|
@ -339,19 +347,33 @@ class Submission(Base):
|
||||||
else: curr = ''
|
else: curr = ''
|
||||||
body += f'<input class="d-none" id="current-{self.id}"{curr}>'
|
body += f'<input class="d-none" id="current-{self.id}"{curr}>'
|
||||||
|
|
||||||
for c in self.options:
|
for o in self.options:
|
||||||
if c.exclusive:
|
if o.exclusive == 2:
|
||||||
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')"'''
|
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 c.voted(v): body += " checked "
|
if o.voted(v): body += " checked "
|
||||||
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html}<span class="presult-{self.id}'''
|
if not (v and v.coins >= 200) or self.total_bet_voted(v): body += " disabled "
|
||||||
body += f'"> - <a href="/votes/post/option/{c.id}"><span id="choice-{c.id}">{c.upvotes}</span> votes</a></span></label></div>'
|
|
||||||
|
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:
|
else:
|
||||||
body += f'<div class="custom-control"><input type="checkbox" class="custom-control-input" id="{c.id}" name="option"'
|
body += f'<div class="custom-control"><input type="checkbox" class="custom-control-input" id="{o.id}" name="option"'
|
||||||
if c.voted(v): body += " checked"
|
if o.voted(v): body += " checked"
|
||||||
if v: body += f''' onchange="poll_vote('{c.id}', 'post')"'''
|
if v: body += f''' onchange="poll_vote('{o.id}', 'post')"'''
|
||||||
else: body += f''' onchange="poll_vote_no_v('{c.id}', '{self.id}')"'''
|
else: body += f''' onchange="poll_vote_no_v('{o.id}', '{self.id}')"'''
|
||||||
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html}<span class="presult-{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/{c.id}"><span id="poll-{c.id}">{c.upvotes}</span> votes</a></span></label></div>'
|
body += f'"> - <a href="/votes/post/option/{o.id}"><span id="poll-{o.id}">{o.upvotes}</span> votes</a></span></label></div>'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -183,52 +183,60 @@ def remove_admin(v, username):
|
||||||
|
|
||||||
return {"message": "Admin removed!"}
|
return {"message": "Admin removed!"}
|
||||||
|
|
||||||
# @app.post("/distribute/<comment>")
|
@app.post("/distribute/<option_id>")
|
||||||
# @limiter.limit("1/second;30/minute;200/hour;1000/day")
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
||||||
# @admin_level_required(3)
|
@admin_level_required(3)
|
||||||
# def distribute(v, comment):
|
def distribute(v, option_id):
|
||||||
# autobetter = get_account(AUTOBETTER_ID)
|
autojanny = get_account(AUTOJANNY_ID)
|
||||||
# if autobetter.coins == 0: return {"error": "@AutoBetter has 0 coins"}
|
if autojanny.coins == 0: return {"error": "@AutoJanny has 0 coins"}
|
||||||
|
|
||||||
# try: comment = int(comment)
|
try: option_id = int(option_id)
|
||||||
# except: abort(400)
|
except: abort(400)
|
||||||
# post = g.db.query(Comment.parent_submission).filter_by(id=comment).one_or_none()[0]
|
|
||||||
# post = get_post(post)
|
|
||||||
|
|
||||||
# pool = 0
|
try: option = g.db.get(SubmissionOption, option_id)
|
||||||
# for option in post.bet_options: pool += option.upvotes
|
except: abort(404)
|
||||||
# pool *= 200
|
|
||||||
|
|
||||||
# autobetter.coins -= pool
|
post = option.post
|
||||||
# if autobetter.coins < 0: autobetter.coins = 0
|
|
||||||
# g.db.add(autobetter)
|
|
||||||
|
|
||||||
# votes = g.db.query(CommentVote).filter_by(comment_id=comment)
|
pool = 0
|
||||||
# coinsperperson = int(pool / votes.count())
|
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:")
|
autojanny.coins -= pool
|
||||||
# for vote in votes:
|
if autojanny.coins < 0: autojanny.coins = 0
|
||||||
# u = vote.user
|
g.db.add(autojanny)
|
||||||
# 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:")
|
votes = option.votes
|
||||||
# cids = [x.id for x in post.bet_options]
|
coinsperperson = int(pool / len(votes))
|
||||||
# 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)
|
|
||||||
|
|
||||||
# post.body += '\n\nclosed'
|
cid = notif_comment(f"You won {coinsperperson} coins betting on [{post.title}]({post.shortlink}) :marseyparty:")
|
||||||
# g.db.add(post)
|
for vote in votes:
|
||||||
|
u = vote.user
|
||||||
|
u.coins += coinsperperson
|
||||||
|
add_notif(cid, u.id)
|
||||||
|
|
||||||
# ma = ModAction(
|
|
||||||
# kind="distribute",
|
|
||||||
# user_id=v.id,
|
|
||||||
# target_comment_id=comment
|
|
||||||
# )
|
|
||||||
# g.db.add(ma)
|
|
||||||
|
|
||||||
# return {"message": f"Each winner has received {coinsperperson} coins!"}
|
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_submission_id=post.id
|
||||||
|
)
|
||||||
|
g.db.add(ma)
|
||||||
|
|
||||||
|
return {"message": f"Each winner has received {coinsperperson} coins!"}
|
||||||
|
|
||||||
@app.post("/@<username>/revert_actions")
|
@app.post("/@<username>/revert_actions")
|
||||||
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
||||||
|
|
|
@ -377,7 +377,7 @@ def comment(v):
|
||||||
option = CommentOption(
|
option = CommentOption(
|
||||||
comment_id=c.id,
|
comment_id=c.id,
|
||||||
body_html=filter_emojis_only(option),
|
body_html=filter_emojis_only(option),
|
||||||
exclusive=False
|
exclusive=0
|
||||||
)
|
)
|
||||||
g.db.add(option)
|
g.db.add(option)
|
||||||
|
|
||||||
|
@ -385,7 +385,7 @@ def comment(v):
|
||||||
choice = CommentOption(
|
choice = CommentOption(
|
||||||
comment_id=c.id,
|
comment_id=c.id,
|
||||||
body_html=filter_emojis_only(choice),
|
body_html=filter_emojis_only(choice),
|
||||||
exclusive=True
|
exclusive=1
|
||||||
)
|
)
|
||||||
g.db.add(choice)
|
g.db.add(choice)
|
||||||
|
|
||||||
|
@ -668,7 +668,7 @@ def edit_comment(cid, v):
|
||||||
option = CommentOption(
|
option = CommentOption(
|
||||||
comment_id=c.id,
|
comment_id=c.id,
|
||||||
body_html=filter_emojis_only(i.group(1)),
|
body_html=filter_emojis_only(i.group(1)),
|
||||||
exclusive = False
|
exclusive = 0
|
||||||
)
|
)
|
||||||
g.db.add(option)
|
g.db.add(option)
|
||||||
|
|
||||||
|
@ -677,7 +677,7 @@ def edit_comment(cid, v):
|
||||||
option = CommentOption(
|
option = CommentOption(
|
||||||
comment_id=c.id,
|
comment_id=c.id,
|
||||||
body_html=filter_emojis_only(i.group(1)),
|
body_html=filter_emojis_only(i.group(1)),
|
||||||
exclusive = True
|
exclusive = 1
|
||||||
)
|
)
|
||||||
g.db.add(option)
|
g.db.add(option)
|
||||||
|
|
||||||
|
|
|
@ -16,12 +16,21 @@ def vote_option(option_id, v):
|
||||||
|
|
||||||
if not option: abort(404)
|
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:
|
if option.exclusive:
|
||||||
vote = g.db.query(SubmissionOptionVote).join(SubmissionOption).filter(
|
vote = g.db.query(SubmissionOptionVote).join(SubmissionOption).filter(
|
||||||
SubmissionOptionVote.user_id==v.id,
|
SubmissionOptionVote.user_id==v.id,
|
||||||
SubmissionOptionVote.submission_id==option.submission_id,
|
SubmissionOptionVote.submission_id==option.submission_id,
|
||||||
SubmissionOption.exclusive==True).one_or_none()
|
SubmissionOption.exclusive==option.exclusive).one_or_none()
|
||||||
if vote:
|
if vote:
|
||||||
|
if option.exclusive == 2: return {"error": "You already voted on this bet!"}
|
||||||
g.db.delete(vote)
|
g.db.delete(vote)
|
||||||
|
|
||||||
existing = g.db.query(SubmissionOptionVote).filter_by(option_id=option_id, user_id=v.id).one_or_none()
|
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,
|
submission_id=option.submission_id,
|
||||||
)
|
)
|
||||||
g.db.add(vote)
|
g.db.add(vote)
|
||||||
elif existing:
|
elif existing and not option.exclusive:
|
||||||
g.db.delete(existing)
|
g.db.delete(existing)
|
||||||
|
|
||||||
return "", 204
|
return "", 204
|
||||||
|
@ -72,7 +81,7 @@ def vote_option_comment(option_id, v):
|
||||||
vote = g.db.query(CommentOptionVote).join(CommentOption).filter(
|
vote = g.db.query(CommentOptionVote).join(CommentOption).filter(
|
||||||
CommentOptionVote.user_id==v.id,
|
CommentOptionVote.user_id==v.id,
|
||||||
CommentOptionVote.comment_id==option.comment_id,
|
CommentOptionVote.comment_id==option.comment_id,
|
||||||
CommentOption.exclusive==True).one_or_none()
|
CommentOption.exclusive==1).one_or_none()
|
||||||
if vote:
|
if vote:
|
||||||
g.db.delete(vote)
|
g.db.delete(vote)
|
||||||
|
|
||||||
|
|
|
@ -432,7 +432,7 @@ def edit_post(pid, v):
|
||||||
option = SubmissionOption(
|
option = SubmissionOption(
|
||||||
submission_id=p.id,
|
submission_id=p.id,
|
||||||
body_html=filter_emojis_only(i.group(1)),
|
body_html=filter_emojis_only(i.group(1)),
|
||||||
exclusive = False
|
exclusive = 0
|
||||||
)
|
)
|
||||||
g.db.add(option)
|
g.db.add(option)
|
||||||
|
|
||||||
|
@ -441,7 +441,7 @@ def edit_post(pid, v):
|
||||||
option = SubmissionOption(
|
option = SubmissionOption(
|
||||||
submission_id=p.id,
|
submission_id=p.id,
|
||||||
body_html=filter_emojis_only(i.group(1)),
|
body_html=filter_emojis_only(i.group(1)),
|
||||||
exclusive = True
|
exclusive = 1
|
||||||
)
|
)
|
||||||
g.db.add(option)
|
g.db.add(option)
|
||||||
|
|
||||||
|
@ -879,11 +879,11 @@ def submit_post(v, sub=None):
|
||||||
if len(url) > 2048:
|
if len(url) > 2048:
|
||||||
return error("There's a 2048 character limit for URLs.")
|
return error("There's a 2048 character limit for URLs.")
|
||||||
|
|
||||||
# if v and v.admin_level > 2:
|
if v and v.admin_level > 2:
|
||||||
# bet_options = []
|
bets = []
|
||||||
# for i in bet_regex.finditer(body):
|
for i in bet_regex.finditer(body):
|
||||||
# bet_options.append(i.group(1))
|
bets.append(i.group(1))
|
||||||
# body = body.replace(i.group(0), "")
|
body = body.replace(i.group(0), "")
|
||||||
|
|
||||||
options = []
|
options = []
|
||||||
for i in poll_regex.finditer(body):
|
for i in poll_regex.finditer(body):
|
||||||
|
@ -951,23 +951,11 @@ def submit_post(v, sub=None):
|
||||||
g.db.add(v)
|
g.db.add(v)
|
||||||
send_repeatable_notification(CARP_ID, post.permalink)
|
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:
|
for option in options:
|
||||||
option = SubmissionOption(
|
option = SubmissionOption(
|
||||||
submission_id=post.id,
|
submission_id=post.id,
|
||||||
body_html=filter_emojis_only(option),
|
body_html=filter_emojis_only(option),
|
||||||
exclusive=False
|
exclusive=0
|
||||||
)
|
)
|
||||||
g.db.add(option)
|
g.db.add(option)
|
||||||
|
|
||||||
|
@ -975,10 +963,18 @@ def submit_post(v, sub=None):
|
||||||
choice = SubmissionOption(
|
choice = SubmissionOption(
|
||||||
submission_id=post.id,
|
submission_id=post.id,
|
||||||
body_html=filter_emojis_only(choice),
|
body_html=filter_emojis_only(choice),
|
||||||
exclusive=True
|
exclusive=1
|
||||||
)
|
)
|
||||||
g.db.add(choice)
|
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 = Vote(user_id=v.id,
|
||||||
vote_type=1,
|
vote_type=1,
|
||||||
submission_id=post.id
|
submission_id=post.id
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
</pre>
|
</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">
|
<div class="overflow-x-auto mt-5">
|
||||||
<table class="table table-striped mb-5">
|
<table class="table table-striped mb-5">
|
||||||
<thead class="bg-primary text-white">
|
<thead class="bg-primary text-white">
|
||||||
|
|
|
@ -19,7 +19,7 @@ set CACHE_VER = {
|
||||||
'js/award_modal.js': 255,
|
'js/award_modal.js': 255,
|
||||||
'js/bootstrap.js': 279,
|
'js/bootstrap.js': 279,
|
||||||
'js/category_modal.js': 200,
|
'js/category_modal.js': 200,
|
||||||
'js/comments+submission_listing.js': 267,
|
'js/comments+submission_listing.js': 269,
|
||||||
'js/submission_listing.js': 261,
|
'js/submission_listing.js': 261,
|
||||||
'js/emoji_modal.js': 314,
|
'js/emoji_modal.js': 314,
|
||||||
'js/formatting.js': 240,
|
'js/formatting.js': 240,
|
||||||
|
|
|
@ -299,7 +299,7 @@ CREATE TABLE public.comment_options (
|
||||||
id integer DEFAULT nextval('public.comment_option_id_seq'::regclass) NOT NULL,
|
id integer DEFAULT nextval('public.comment_option_id_seq'::regclass) NOT NULL,
|
||||||
comment_id integer NOT NULL,
|
comment_id integer NOT NULL,
|
||||||
body_html character varying(500) 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,
|
id integer DEFAULT nextval('public.submission_option_id_seq'::regclass) NOT NULL,
|
||||||
submission_id integer NOT NULL,
|
submission_id integer NOT NULL,
|
||||||
body_html character varying(500) NOT NULL,
|
body_html character varying(500) NOT NULL,
|
||||||
exclusive boolean NOT NULL
|
exclusive integer NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue