Move more stuff to proper location

remotes/1693045480750635534/spooky-22
Outrun Colors, LLC 2022-05-29 19:49:14 -05:00
parent d1f0f7a164
commit a0cc7e1cf6
6 changed files with 66 additions and 36 deletions

View File

@ -173,6 +173,7 @@ if SITE in {'rdrama.net','devrama.xyz'}:
"6": "947236351445725204",
"7": "886781932430565418",
}
elif SITE == "pcmemes.net":
HOLE_COST = 10000
NOTIFICATIONS_ID = 1046
@ -1013,3 +1014,20 @@ procoins_li = (0,2500,5000,10000,25000,50000,125000,250000)
linefeeds_regex = re.compile("([^\n])\n([^\n])", flags=re.A)
def make_name(*args, **kwargs): return request.base_url
# Lottery
LOTTERY_TICKET_COST = 12
# The amount of dramacoins permanently removed from the economy to reduce expected value
LOTTERY_SINK_RATE = 3
# The amount of dramacoins the lottery founders receive
LOTTERY_ROYALTY_RATE = 1
# The account in which royalties are to be deposited
LOTTERY_ROYALTY_ACCOUNT_ID = 9
# LOTTERY_ROYALTY_ACCOUNT_ID = 8239 # (McCoxmaul)
# The account in which the prize is held to be accessed by anyone
LOTTERY_MANAGER_ACCOUNT_ID = 3
# LOTTERY_MANAGER_ACCOUNT_ID = 11651 (Lottershe)

View File

@ -3,50 +3,39 @@ from random import choice
from sqlalchemy import *
from files.helpers.alerts import *
from files.helpers.wrappers import *
LOTTERY_TICKET_COST = 12
# The amount of dramacoins permanently removed from the economy to reduce expected value
SINK_RATE = 3
# The amount of dramacoins the lottery founders receive
ROYALTY_RATE = 1
# The account in which royalties are to be deposited
ROYALTY_ACCOUNT_ID = 9
# The account in which the prize is held to be accessed by anyone
MANAGER_ACCOUNT_ID = 3
from flask import g
from .const import *
def get_active_lottery(g):
def get_active_lottery():
return g.db.query(Lottery).order_by(Lottery.id.desc()).filter(Lottery.is_active).one_or_none()
def get_users_participating_in_lottery(g):
def get_users_participating_in_lottery():
return g.db.query(User).filter(User.currently_held_lottery_tickets > 0).all()
def get_active_lottery_stats(g):
active_lottery = get_active_lottery(g)
participating_users = get_users_participating_in_lottery(g)
def get_active_lottery_stats():
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)
def end_lottery_session(g):
active_lottery = get_active_lottery(g)
def end_lottery_session():
active_lottery = get_active_lottery()
if (active_lottery is None):
return False, "There is no active lottery."
participating_users = get_users_participating_in_lottery(g)
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
@ -59,7 +48,7 @@ def end_lottery_session(g):
active_lottery.is_active = False
manager = g.db.query(User).get(MANAGER_ACCOUNT_ID)
manager = g.db.query(User).get(LOTTERY_MANAGER_ACCOUNT_ID)
manager.coins -= active_lottery.prize
g.db.commit()
@ -67,24 +56,25 @@ def end_lottery_session(g):
return True, f'{winning_user.username} won {active_lottery.prize} dramacoins!'
def start_new_lottery_session(g):
end_lottery_session(g)
def start_new_lottery_session():
end_lottery_session()
lottery = Lottery()
epoch_time = int(time.time())
one_week_from_now = epoch_time + 60 * 60 * 24 * 7
lottery.ends_at = one_week_from_now
lottery.is_active = True
lottery.winner_id = 1
g.db.add(lottery)
g.db.commit()
def purchase_lottery_ticket(g, v):
def purchase_lottery_ticket(v):
if (v.coins < LOTTERY_TICKET_COST):
return False, f'Lottery tickets cost {LOTTERY_TICKET_COST} dramacoins each.'
most_recent_lottery = get_active_lottery(g)
most_recent_lottery = get_active_lottery()
if (most_recent_lottery is None):
return False, "There is no active lottery."
@ -92,15 +82,16 @@ def purchase_lottery_ticket(g, v):
v.currently_held_lottery_tickets += 1
v.total_held_lottery_tickets += 1
net_ticket_value = LOTTERY_TICKET_COST - SINK_RATE - ROYALTY_RATE
net_ticket_value = LOTTERY_TICKET_COST - \
LOTTERY_SINK_RATE - LOTTERY_ROYALTY_RATE
most_recent_lottery.prize += net_ticket_value
most_recent_lottery.tickets_sold += 1
manager = g.db.query(User).get(MANAGER_ACCOUNT_ID)
manager = g.db.query(User).get(LOTTERY_MANAGER_ACCOUNT_ID)
manager.coins += net_ticket_value
beneficiary = g.db.query(User).get(ROYALTY_ACCOUNT_ID)
beneficiary.coins += ROYALTY_RATE
beneficiary = g.db.query(User).get(LOTTERY_ROYALTY_ACCOUNT_ID)
beneficiary.coins += LOTTERY_ROYALTY_RATE
g.db.commit()

View File

@ -669,6 +669,7 @@ def api_comment(v):
parent_post.comment_count += 1
g.db.add(parent_post)
v.coins += 20000
g.db.commit()
if request.headers.get("Authorization"): return c.json

View File

@ -10,7 +10,7 @@ from files.helpers.lottery import *
@auth_required
def lottery_end(v):
if v.admin_level > 2:
success, message = end_lottery_session(g)
success, message = end_lottery_session()
return {"message": message} if success else {"error": message}
else:
return {"error": "JL3+ or higher required to start and end lotteries."}, 401
@ -20,7 +20,7 @@ def lottery_end(v):
@auth_required
def lottery_start(v):
if v.admin_level > 2:
start_new_lottery_session(g)
start_new_lottery_session()
return {"message": "Lottery started."}
else:
return {"error": "JL3+ or higher required to start and end lotteries."}, 401
@ -30,8 +30,8 @@ def lottery_start(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def lottery_buy(v):
success, message = purchase_lottery_ticket(g, v)
lottery, participants = get_active_lottery_stats(g)
success, message = purchase_lottery_ticket(v)
lottery, participants = get_active_lottery_stats()
if success:
return {"message": message, "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}}
@ -43,5 +43,6 @@ def lottery_buy(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def lottery_active(v):
lottery, participants = get_active_lottery_stats(g)
lottery, participants = get_active_lottery_stats()
return {"message": "", "stats": {"user": v.lottery_stats, "lottery": lottery, "participants": participants}}

View File

@ -112,6 +112,7 @@
<a class="nav-link" href="/random_post/" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Random post"><i class="fas fa-random"></i></a>
</li>
{% if v %}
<li class="nav-item d-flex align-items-center justify-content-center text-center mx-1">
<a
href="#"
@ -123,6 +124,7 @@
<i class="fas fa-ticket"></i>
</a>
</li>
{% endif %}
<li class="nav-item d-flex align-items-center justify-content-center text-center mx-1">
<a class="nav-link" href="/chat/" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Chat"><i class="fas fa-messages"></i></a>

View File

@ -1850,3 +1850,20 @@ ALTER TABLE ONLY public.votes
ALTER TABLE ONLY public.votes
ADD CONSTRAINT vote_user_fkey FOREIGN KEY (user_id) REFERENCES public.users(id);
CREATE TABLE public.lotteries (
id SERIAL PRIMARY KEY,
is_active boolean DEFAULT false NOT NULL,
ends_at integer DEFAULT 0 NOT NULL,
prize integer DEFAULT 0 NOT NULL,
tickets_sold integer DEFAULT 0 NOT NULL,
winner_id integer DEFAULT 0 NOT NULL
);
ALTER TABLE ONLY public.lotteries
ADD CONSTRAINT fk_winner FOREIGN KEY (winner_id) REFERENCES public.users(id);
ALTER TABLE public.users
ADD currently_held_lottery_tickets integer DEFAULT 0 NOT NULL,
ADD total_held_lottery_tickets integer DEFAULT 0 NOT NULL,
ADD total_lottery_winnings integer DEFAULT 0 NOT NULL;