diff --git a/files/classes/lottery.py b/files/classes/lottery.py index 35d163278..0142a7166 100644 --- a/files/classes/lottery.py +++ b/files/classes/lottery.py @@ -6,27 +6,27 @@ from files.helpers.const import * class Lottery(Base): - __tablename__ = "lotteries" + __tablename__ = "lotteries" - id = Column(Integer, primary_key=True) - is_active = Column(Boolean, default=False) - ends_at = Column(Integer) - prize = Column(Integer, default=0) - tickets_sold = Column(Integer, default=0) - winner_id = Column(Integer, ForeignKey("users.id")) + id = Column(Integer, primary_key=True) + is_active = Column(Boolean, default=False) + ends_at = Column(Integer) + prize = Column(Integer, default=0) + tickets_sold = Column(Integer, default=0) + winner_id = Column(Integer, ForeignKey("users.id")) - @property - @lazy - def timeleft(self): - if not self.is_active: - return 0 + @property + @lazy + def timeleft(self): + if not self.is_active: + return 0 - epoch_time = int(time.time()) - remaining_time = self.ends_at - epoch_time + epoch_time = int(time.time()) + remaining_time = self.ends_at - epoch_time - return 0 if remaining_time < 0 else remaining_time + return 0 if remaining_time < 0 else remaining_time - @property - @lazy - def stats(self): - return {"active": self.is_active, "timeLeft": self.timeleft, "prize": self.prize, "ticketsSoldThisSession": self.tickets_sold,} + @property + @lazy + def stats(self): + return {"active": self.is_active, "timeLeft": self.timeleft, "prize": self.prize, "ticketsSoldThisSession": self.tickets_sold,} diff --git a/files/helpers/actions.py b/files/helpers/actions.py index 1f4872021..d22b823ff 100644 --- a/files/helpers/actions.py +++ b/files/helpers/actions.py @@ -4,7 +4,7 @@ from files.classes.badges import Badge, BadgeDef # TODO: More sanity checks on passed parameters. # TODO: Add `replace=False` parameter which, when set true, removes any -# existing badge with identical id & user and replaces with new one. +# existing badge with identical id & user and replaces with new one. def badge_grant(user_id, badge_id, desc='', url='', commit=True): user = g.db.query(User).filter(User.id == int(user_id)).one_or_none() if not user: diff --git a/files/helpers/const.py b/files/helpers/const.py index af8c0690c..c8e6eeaf0 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -525,13 +525,13 @@ AWARDS = { "price": 500 }, "glowie": { - "kind": "glowie", - "title": "Glowie", - "description": "Indicates that the recipient can be seen when driving. Just run them over.", - "icon": "fas fa-user-secret", - "color": "text-green", - "price": 500 - }, + "kind": "glowie", + "title": "Glowie", + "description": "Indicates that the recipient can be seen when driving. Just run them over.", + "icon": "fas fa-user-secret", + "color": "text-green", + "price": 500 + }, "rehab": { "kind": "rehab", "title": "Rehab", @@ -541,13 +541,13 @@ AWARDS = { "price": 777 }, "beano": { - "kind": "beano", - "title": "Beano", - "description": "Stops you from embarrassing yourself with your flatulence", - "icon": "fas fa-gas-pump-slash", - "color": "text-green", - "price": 1000 - }, + "kind": "beano", + "title": "Beano", + "description": "Stops you from embarrassing yourself with your flatulence", + "icon": "fas fa-gas-pump-slash", + "color": "text-green", + "price": 1000 + }, "progressivestack": { "kind": "progressivestack", "title": "Progressive Stack", diff --git a/files/helpers/lottery.py b/files/helpers/lottery.py index 6a3ca0945..ab1e2a18c 100644 --- a/files/helpers/lottery.py +++ b/files/helpers/lottery.py @@ -10,120 +10,120 @@ from .const import * LOTTERY_WINNER_BADGE_ID = 137 def get_active_lottery(): - return g.db.query(Lottery).order_by(Lottery.id.desc()).filter(Lottery.is_active).one_or_none() + return g.db.query(Lottery).order_by(Lottery.id.desc()).filter(Lottery.is_active).one_or_none() def get_users_participating_in_lottery(): - return g.db.query(User) \ - .filter(User.currently_held_lottery_tickets > 0) \ - .order_by(User.currently_held_lottery_tickets.desc()).all() + return g.db.query(User) \ + .filter(User.currently_held_lottery_tickets > 0) \ + .order_by(User.currently_held_lottery_tickets.desc()).all() def get_active_lottery_stats(): - active_lottery = get_active_lottery() - participating_users = get_users_participating_in_lottery() + active_lottery = get_active_lottery() + participating_users = get_users_participating_in_lottery() - return None if active_lottery is None else active_lottery.stats, len(participating_users) + return None if active_lottery is None else active_lottery.stats, len(participating_users) def end_lottery_session(): - active_lottery = get_active_lottery() + active_lottery = get_active_lottery() - if (active_lottery is None): - return False, "There is no active lottery." + if (active_lottery is None): + return False, "There is no active lottery." - participating_users = get_users_participating_in_lottery() - raffle = [] - for user in participating_users: - for _ in range(user.currently_held_lottery_tickets): - raffle.append(user.id) + participating_users = get_users_participating_in_lottery() + raffle = [] + for user in participating_users: + for _ in range(user.currently_held_lottery_tickets): + raffle.append(user.id) - winner = choice(raffle) - active_lottery.winner_id = winner - winning_user = next(filter(lambda x: x.id == winner, participating_users)) - winning_user.coins += active_lottery.prize - winning_user.total_lottery_winnings += active_lottery.prize - badge_grant(winner, LOTTERY_WINNER_BADGE_ID) + winner = choice(raffle) + active_lottery.winner_id = winner + winning_user = next(filter(lambda x: x.id == winner, participating_users)) + winning_user.coins += active_lottery.prize + winning_user.total_lottery_winnings += active_lottery.prize + badge_grant(winner, LOTTERY_WINNER_BADGE_ID) - for user in participating_users: - chance_to_win = user.currently_held_lottery_tickets / len(raffle) * 100 - if user.id == winner: - notification_text = f'You won {active_lottery.prize} coins in the lottery! ' \ - + f'Congratulations!\nYour odds of winning were: {chance_to_win}%' - else: - notification_text = f'You did not win the lottery. Better luck next time!\n' \ - + f'Your odds of winning were: {chance_to_win}%\nWinner: @{winning_user.username} (won {active_lottery.prize} coins)' - send_repeatable_notification(user.id, notification_text) - user.currently_held_lottery_tickets = 0 + for user in participating_users: + chance_to_win = user.currently_held_lottery_tickets / len(raffle) * 100 + if user.id == winner: + notification_text = f'You won {active_lottery.prize} coins in the lottery! ' \ + + f'Congratulations!\nYour odds of winning were: {chance_to_win}%' + else: + notification_text = f'You did not win the lottery. Better luck next time!\n' \ + + f'Your odds of winning were: {chance_to_win}%\nWinner: @{winning_user.username} (won {active_lottery.prize} coins)' + send_repeatable_notification(user.id, notification_text) + user.currently_held_lottery_tickets = 0 - active_lottery.is_active = False + active_lottery.is_active = False - g.db.commit() + g.db.commit() - return True, f'{winning_user.username} won {active_lottery.prize} coins!' + return True, f'{winning_user.username} won {active_lottery.prize} coins!' def start_new_lottery_session(): - end_lottery_session() + end_lottery_session() - lottery = Lottery() - epoch_time = int(time.time()) - # Subtract 4 minutes from one week so cronjob interval doesn't cause the - # time to drift toward over multiple weeks. - one_week_from_now = epoch_time + 60 * 60 * 24 * 7 - (4 * 60) - lottery.ends_at = one_week_from_now - lottery.is_active = True + lottery = Lottery() + epoch_time = int(time.time()) + # Subtract 4 minutes from one week so cronjob interval doesn't cause the + # time to drift toward over multiple weeks. + one_week_from_now = epoch_time + 60 * 60 * 24 * 7 - (4 * 60) + lottery.ends_at = one_week_from_now + lottery.is_active = True - g.db.add(lottery) - g.db.commit() + g.db.add(lottery) + g.db.commit() def check_if_end_lottery_task(): - active_lottery = get_active_lottery() + active_lottery = get_active_lottery() - if active_lottery is None: - return False - elif active_lottery.timeleft > 0: - return False + if active_lottery is None: + return False + elif active_lottery.timeleft > 0: + return False - start_new_lottery_session() - return True + start_new_lottery_session() + return True def lottery_ticket_net_value(): - return LOTTERY_TICKET_COST - LOTTERY_SINK_RATE + return LOTTERY_TICKET_COST - LOTTERY_SINK_RATE def purchase_lottery_tickets(v, quantity=1): - if quantity < 1: - return False, "Must purchase one or more lottery tickets." - elif (v.coins < LOTTERY_TICKET_COST * quantity): - return False, f'Lottery tickets cost {LOTTERY_TICKET_COST} coins each.' + if quantity < 1: + return False, "Must purchase one or more lottery tickets." + elif (v.coins < LOTTERY_TICKET_COST * quantity): + return False, f'Lottery tickets cost {LOTTERY_TICKET_COST} coins each.' - most_recent_lottery = get_active_lottery() - if (most_recent_lottery is None): - return False, "There is no active lottery." + most_recent_lottery = get_active_lottery() + if (most_recent_lottery is None): + return False, "There is no active lottery." - v.coins -= LOTTERY_TICKET_COST * quantity - v.currently_held_lottery_tickets += quantity - v.total_held_lottery_tickets += quantity + v.coins -= LOTTERY_TICKET_COST * quantity + v.currently_held_lottery_tickets += quantity + v.total_held_lottery_tickets += quantity - net_ticket_value = lottery_ticket_net_value() * quantity - most_recent_lottery.prize += net_ticket_value - most_recent_lottery.tickets_sold += quantity + net_ticket_value = lottery_ticket_net_value() * quantity + most_recent_lottery.prize += net_ticket_value + most_recent_lottery.tickets_sold += quantity - g.db.commit() + g.db.commit() - if quantity == 1: return True, f'Successfully purchased {quantity} lottery ticket!' - return True, f'Successfully purchased {quantity} lottery tickets!' + if quantity == 1: return True, f'Successfully purchased {quantity} lottery ticket!' + return True, f'Successfully purchased {quantity} lottery tickets!' def grant_lottery_tickets_to_user(v, quantity): - active_lottery = get_active_lottery() - prize_value = lottery_ticket_net_value() * quantity + active_lottery = get_active_lottery() + prize_value = lottery_ticket_net_value() * quantity - if active_lottery: - v.currently_held_lottery_tickets += quantity - v.total_held_lottery_tickets += quantity + if active_lottery: + v.currently_held_lottery_tickets += quantity + v.total_held_lottery_tickets += quantity - active_lottery.prize += prize_value - active_lottery.tickets_sold += quantity + active_lottery.prize += prize_value + active_lottery.tickets_sold += quantity - g.db.commit() + g.db.commit() diff --git a/files/helpers/stats.py b/files/helpers/stats.py index db5e1f719..9000ce336 100644 --- a/files/helpers/stats.py +++ b/files/helpers/stats.py @@ -63,13 +63,13 @@ def chart(kind, site): plt.rcParams['figure.figsize'] = (chart_width, 20) signup_chart = plt.subplot2grid((chart_width, 20), ( 0, 0), rowspan=6, colspan=chart_width) - posts_chart = plt.subplot2grid((chart_width, 20), (10, 0), rowspan=6, colspan=chart_width) + posts_chart = plt.subplot2grid((chart_width, 20), (10, 0), rowspan=6, colspan=chart_width) comments_chart = plt.subplot2grid((chart_width, 20), (20, 0), rowspan=6, colspan=chart_width) signup_chart.grid(), posts_chart.grid(), comments_chart.grid() signup_chart.plot (daily_times, daily_signups, color='red') - posts_chart.plot (daily_times, post_stats, color='blue') + posts_chart.plot (daily_times, post_stats, color='blue') comments_chart.plot(daily_times, comment_stats, color='purple') signup_chart.set_ylim(ymin=0) diff --git a/files/routes/lottery.py b/files/routes/lottery.py index 59d9fd8b8..0d05991cd 100644 --- a/files/routes/lottery.py +++ b/files/routes/lottery.py @@ -11,16 +11,16 @@ import requests @admin_level_required(3) @lottery_required def lottery_end(v): - success, message = end_lottery_session() - return {"message": message} if success else {"error": message} + success, message = end_lottery_session() + return {"message": message} if success else {"error": message} @app.post("/lottery/start") @admin_level_required(3) @lottery_required def lottery_start(v): - start_new_lottery_session() - return {"message": "Lottery started."} + start_new_lottery_session() + return {"message": "Lottery started."} @app.post("/lottery/buy") @@ -28,15 +28,15 @@ def lottery_start(v): @auth_required @lottery_required def lottery_buy(v): - quantity = int(request.values.get("quantity")) - success, message = purchase_lottery_tickets(v, quantity) - lottery, participants = get_active_lottery_stats() + quantity = int(request.values.get("quantity")) + success, message = purchase_lottery_tickets(v, quantity) + lottery, participants = get_active_lottery_stats() - if success: - return {"message": message, "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}} - else: - return {"error": message, "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}} + if success: + return {"message": message, "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}} + else: + return {"error": message, "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}} @app.get("/lottery/active") @@ -44,20 +44,20 @@ def lottery_buy(v): @auth_required @lottery_required def lottery_active(v): - lottery, participants = get_active_lottery_stats() + lottery, participants = get_active_lottery_stats() - return {"message": "", "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}} + return {"message": "", "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}} @app.get("/lottery") @auth_required @lottery_required def lottery(v): - lottery_stats, participant_stats = get_active_lottery_stats() - return render_template("lottery.html", v=v, lottery_stats=lottery_stats, participant_stats=participant_stats) + lottery_stats, participant_stats = get_active_lottery_stats() + return render_template("lottery.html", v=v, lottery_stats=lottery_stats, participant_stats=participant_stats) @app.get("/admin/lottery/participants") @admin_level_required(2) @lottery_required def lottery_admin(v): - participants = get_users_participating_in_lottery() - return render_template("admin/lottery.html", v=v, participants=participants) + participants = get_users_participating_in_lottery() + return render_template("admin/lottery.html", v=v, participants=participants) diff --git a/files/templates/header.html b/files/templates/header.html index 6879fac70..fca4daa0d 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -44,11 +44,11 @@ } {% endif %} -
{% endif %}