2022-05-29 03:33:44 +00:00
import time
2022-05-29 04:23:20 +00:00
from random import choice
2022-05-29 03:33:44 +00:00
from sqlalchemy import *
2022-05-29 04:23:20 +00:00
from files . helpers . alerts import *
2022-05-29 03:33:44 +00:00
from files . helpers . wrappers import *
2022-05-30 00:49:14 +00:00
from flask import g
from . const import *
2022-05-29 03:33:44 +00:00
2022-05-30 00:49:14 +00:00
def get_active_lottery ( ) :
2022-05-29 04:23:20 +00:00
return g . db . query ( Lottery ) . order_by ( Lottery . id . desc ( ) ) . filter ( Lottery . is_active ) . one_or_none ( )
2022-05-29 03:33:44 +00:00
2022-05-29 06:01:45 +00:00
2022-05-30 00:49:14 +00:00
def get_users_participating_in_lottery ( ) :
2022-05-29 06:01:45 +00:00
return g . db . query ( User ) . filter ( User . currently_held_lottery_tickets > 0 ) . all ( )
2022-05-30 00:49:14 +00:00
def get_active_lottery_stats ( ) :
active_lottery = get_active_lottery ( )
participating_users = get_users_participating_in_lottery ( )
2022-05-29 06:01:45 +00:00
return None if active_lottery is None else active_lottery . stats , len ( participating_users )
2022-05-29 03:33:44 +00:00
2022-05-30 00:49:14 +00:00
def end_lottery_session ( ) :
active_lottery = get_active_lottery ( )
2022-05-29 03:33:44 +00:00
2022-05-29 04:23:20 +00:00
if ( active_lottery is None ) :
return False , " There is no active lottery. "
2022-05-29 03:33:44 +00:00
2022-05-30 00:49:14 +00:00
participating_users = get_users_participating_in_lottery ( )
2022-05-29 04:23:20 +00:00
raffle = [ ]
for user in participating_users :
for _ in range ( user . currently_held_lottery_tickets ) :
raffle . append ( user . id )
2022-05-29 03:33:44 +00:00
2022-05-29 04:23:20 +00:00
winner = choice ( raffle )
2022-05-30 00:49:14 +00:00
active_lottery . winner_id = winner
2022-05-29 04:23:20 +00:00
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
2022-05-29 03:33:44 +00:00
2022-05-29 04:23:20 +00:00
for user in participating_users :
2022-05-29 06:01:45 +00:00
chance_to_win = user . currently_held_lottery_tickets / len ( raffle ) * 100
notification_text = f ' You won { active_lottery . prize } dramacoins in the lottery! Congratulations! \n Odds of winning: { chance_to_win } % ' if user . id == winner else " You did not win the lottery. Better luck next time! \n Odds of winning: {chance_to_win} % "
send_repeatable_notification ( user . id , notification_text )
2022-05-29 04:23:20 +00:00
user . currently_held_lottery_tickets = 0
2022-05-29 03:33:44 +00:00
2022-05-29 04:23:20 +00:00
active_lottery . is_active = False
2022-05-30 00:49:14 +00:00
manager = g . db . query ( User ) . get ( LOTTERY_MANAGER_ACCOUNT_ID )
2022-05-29 04:23:20 +00:00
manager . coins - = active_lottery . prize
2022-05-29 03:33:44 +00:00
g . db . commit ( )
2022-05-29 04:23:20 +00:00
return True , f ' { winning_user . username } won { active_lottery . prize } dramacoins! '
2022-05-29 03:33:44 +00:00
2022-05-30 00:49:14 +00:00
def start_new_lottery_session ( ) :
end_lottery_session ( )
2022-05-29 03:33:44 +00:00
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
2022-05-30 00:49:14 +00:00
lottery . winner_id = 1
2022-05-29 03:33:44 +00:00
g . db . add ( lottery )
g . db . commit ( )
2022-05-30 00:49:14 +00:00
def purchase_lottery_ticket ( v ) :
2022-05-29 03:33:44 +00:00
if ( v . coins < LOTTERY_TICKET_COST ) :
2022-05-29 06:06:39 +00:00
return False , f ' Lottery tickets cost { LOTTERY_TICKET_COST } dramacoins each. '
2022-05-29 03:33:44 +00:00
2022-05-30 00:49:14 +00:00
most_recent_lottery = get_active_lottery ( )
2022-05-29 04:23:20 +00:00
if ( most_recent_lottery is None ) :
2022-05-29 06:06:39 +00:00
return False , " There is no active lottery. "
2022-05-29 03:33:44 +00:00
v . coins - = LOTTERY_TICKET_COST
v . currently_held_lottery_tickets + = 1
v . total_held_lottery_tickets + = 1
2022-05-30 00:49:14 +00:00
net_ticket_value = LOTTERY_TICKET_COST - \
LOTTERY_SINK_RATE - LOTTERY_ROYALTY_RATE
2022-05-29 03:33:44 +00:00
most_recent_lottery . prize + = net_ticket_value
most_recent_lottery . tickets_sold + = 1
2022-05-30 00:49:14 +00:00
manager = g . db . query ( User ) . get ( LOTTERY_MANAGER_ACCOUNT_ID )
2022-05-29 03:33:44 +00:00
manager . coins + = net_ticket_value
2022-05-30 00:49:14 +00:00
beneficiary = g . db . query ( User ) . get ( LOTTERY_ROYALTY_ACCOUNT_ID )
beneficiary . coins + = LOTTERY_ROYALTY_RATE
2022-05-29 03:33:44 +00:00
g . db . commit ( )
2022-05-29 06:06:39 +00:00
return True , ' Successfully purchased a lottery ticket! '