async def status(self,ctx): '''Display the current lottory status''' lottory_id = db.get_current_lottory() tickets = db.get_lottory_tickets(lottory_id) num_tickets = len(tickets) progressive = db.get_lottory_jackpot_prog(lottory_id) jackpot = payout_table[True][4] + progressive await ctx.send("Lottory {} is in progress, currently {:,} tickets sold, current jackpot is {:,}".format(lottory_id,num_tickets,jackpot))
async def draw(self,ctx): '''Start the next drawing''' lottory_id = db.get_current_lottory() progressive = db.get_lottory_jackpot_prog(lottory_id) total_jackpot = progressive + payout_table[True][4] ticket_list = db.get_lottory_tickets(lottory_id) #List of tuples (user_id, ticket_value) if len(ticket_list) < 1: #Verify there is at least 1 ticket sold before allowing drawing await ctx.send("There are no tickets sold for this drawing yet!") return db.add_lottory() #increment current when drawing starts winning_numbers = quickpick()[0] #Choose winning numbers balls = {0:'First', 1:'Second', 2:'Third', 3:'Fourth', 4:'MEGA'} embed_dict = {'colour':discord.Colour(0x006400), 'author_name':"Drawing for lottory {}! Jackpot is currently {:,}".format(lottory_id,total_jackpot), 'fields': {} } lottory_message = await ctx.send(embed=build_embed(embed_dict)) async with ctx.typing(): winning_ticket_display = [] for ball_number, ball_name in balls.items(): await asyncio.sleep(3) winning_ticket_display.append(str(winning_numbers[ball_number])) embed_dict['fields'][1] = {'name': "{} Ball".format(ball_name), 'value': winning_numbers[ball_number], 'inline': True} winning_numbers_value = "-".join(winning_ticket_display) if len(winning_ticket_display) < 5 else Ticket(winning_numbers) embed_dict['fields'][2] = {'name': 'Winning Numbers' , 'value': winning_numbers_value, 'inline': True} await lottory_message.edit(embed=build_embed(embed_dict)) num_tickets = len(ticket_list) progressive_split = [] winner_dict = {} loser_dict = {} total_payout = 0 async with ctx.typing(): for ticket_tuple in ticket_list: ticket_value = eval(ticket_tuple[0]) #ticket value stored as a string, convert back to list user_id = ticket_tuple[1] mega, match = parse_ticket(winning_numbers, ticket_value) ticket_payout = determine_payout(mega, match) if ticket_payout != 0: winner_dict = add_ticket_to_dict(winner_dict, user_id, ticket_value, ticket_payout) else: loser_dict = add_ticket_to_dict(loser_dict, user_id, ticket_value, ticket_payout) results = {} async with ctx.typing(): for user_id, list_of_winning_tickets in winner_dict.items(): balance_modifier = 0 for ticket_tuple in list_of_winning_tickets: ticket_value = Ticket(ticket_tuple[0]) ticket_payout = ticket_tuple[1] if ticket_payout == payout_table[True][4]: progressive_split.append([user_id, ticket_value]) else: balance_modifier += ticket_payout new_user_balance = db.modify_user_balance(user_id, balance_modifier) results[user_id] = [balance_modifier, new_user_balance, list_of_winning_tickets] total_payout += balance_modifier jackpot_results = {} if len(progressive_split) > 0: jackpot_progressive_share = round(progressive / len(progressive_split), 2) jackpot_payout = round(payout_table[True][4] + jackpot_progressive_share, 2) for ticket_tuple in progressive_split: user_id = ticket_tuple[0] ticket_value = ticket_tuple[1] total_payout += jackpot_payout new_user_balance = db.modify_user_balance(user_id, jackpot_payout) if user_id not in jackpot_results: jackpot_results[user_id] = [jackpot_payout, new_user_balance, [ticket_value], jackpot_progressive_share] else: jackpot_results[user_id][0] += jackpot_payout jackpot_results[user_id][1] = new_user_balance jackpot_results[user_id][2].append(ticket_value) jackpot_results[user_id][3] += jackpot_progressive_share split_won = 'won' if len(jackpot_results) == 1 else 'split' await ctx.send("------------JACKPOT WINNAR!!!!!!-------------") for user_id, result in jackpot_results.items(): jackpot_payout = result[0] new_user_balance = result[1] ticket_values = result[2] if len(result[2]) <= 10 else len(result[2]) progressive_split = result[3] user = await self.bot.get_user_info(user_id) await ctx.send('{} {} the Jackpot! Payout {:,}, your share of the progressive is {:,}! with {} tickets!!'.format(user.name, split_won, round(jackpot_payout,2), round(progressive_split,2), ticket_values)) await user.send('You {} the Jackpot for lottory {} with ticket {}! {:,} has been deposited into your account. Your new balance is {}.'.format(split_won, lottory_id, ticket_value, round(jackpot_payout,2), new_user_balance)) for user_id, result in results.items(): jackpot_balance_modifier = jackpot_results[user_id][0] if user_id in jackpot_results else 0 balance_modifier = result[0] + jackpot_balance_modifier new_user_balance = result[1] winning_tickets = result[2] user = await self.bot.get_user_info(user_id) embed_dict['fields'][user_id] = {'name': user.name, 'value': "Won a total of {:,} on {:,} winning tickers!".format(balance_modifier, len(winning_tickets)), 'inline': False} await user.send("Lottory {} Results: You won {:,}. Your new balance is {:,}.".format(lottory_id, balance_modifier, new_user_balance)) if len(winning_tickets) < 100: for n in range(0, len(winning_tickets), 50): await user.send("Your winnings tickets for Lottory {}: Winning Numbers:{} Your winners: {}".format(lottory_id, winning_numbers, winning_tickets[n:n+50])) await lottory_message.edit(embed=build_embed(embed_dict)) income = ticket_cost * num_tickets payout_ratio = 100 * (total_payout - income) / income db.update_lottory_stats(lottory_id, income, total_payout) embed_dict['author_name'] = "Lottory {} ended!".format(lottory_id) embed_dict['fields'][0] = {"name": "{:,} tickets were sold for {:,}".format(num_tickets, income), 'value':"{:,} was paid out for a payout ratio of {}%".format(round(total_payout, 2), round(payout_ratio, 2))} await lottory_message.edit(embed=build_embed(embed_dict)) if len(progressive_split) == 0: lottory_id = db.get_current_lottory() #Add progressive to next lottory db.modify_lottory_jackpot_prog(lottory_id, progressive) else: await ctx.send("Jackpot has been reseeded to {:,}".format(payout_table[True][4]))