public void PrizeLottery() { var rnd = new Random(); var lottery = new Lottery <Prizes>(Lottery <Prizes> .DrawResponse.RemovePrize); for (int i = 0; i < 10000; i++) { var dicResposta = new Dictionary <Prizes, int> { { Prizes.Prize1, 0 }, { Prizes.Prize2, 0 }, { Prizes.Prize3, 0 }, { Prizes.Prize4, 0 } }; int moedas1 = rnd.Next(1, 50); int moedas2 = rnd.Next(1, 50); int powerUp = rnd.Next(1, 50); int powerUpeMoedas = rnd.Next(1, 50); lottery.AddTickets(Prizes.Prize1, moedas1); lottery.AddTickets(Prizes.Prize2, moedas2); lottery.AddTickets(Prizes.Prize3, powerUp); lottery.AddTickets(Prizes.Prize4, powerUpeMoedas); do { var winner = lottery.Draw(); dicResposta[winner]++; } while (lottery.NumberOfPrizes > 0); Assert.Equal(1, dicResposta[Prizes.Prize1]); Assert.Equal(1, dicResposta[Prizes.Prize2]); Assert.Equal(1, dicResposta[Prizes.Prize3]); Assert.Equal(1, dicResposta[Prizes.Prize4]); } }
public void TicketLottery() { var rnd = new Random(); var lottery = new Lottery <Prizes>(Lottery <Prizes> .DrawResponse.RemoveTicket); for (int i = 0; i < 10000; i++) { var answers = new Dictionary <Prizes, int> { { Prizes.Prize1, 0 }, { Prizes.Prize2, 0 }, { Prizes.Prize3, 0 }, { Prizes.Prize4, 0 } }; int prize1 = rnd.Next(1, 50); int prize2 = rnd.Next(1, 50); int prize3 = rnd.Next(1, 50); int prize4 = rnd.Next(1, 50); lottery.AddTickets(Prizes.Prize1, prize1); lottery.AddTickets(Prizes.Prize2, prize2); lottery.AddTickets(Prizes.Prize3, prize3); lottery.AddTickets(Prizes.Prize4, prize4); do { var winner = lottery.Draw(); answers[winner]++; } while (lottery.NumberOfTickets > 0); Assert.Equal(prize1, answers[Prizes.Prize1]); Assert.Equal(prize2, answers[Prizes.Prize2]); Assert.Equal(prize3, answers[Prizes.Prize3]); Assert.Equal(prize4, answers[Prizes.Prize4]); } }
static async Task OnMessageRecieve(PlanetMessage message) { string dictkey = $"{message.Author_Id}-{message.Planet_Id}"; PlanetMember ClientUser = null; ClientUser = await message.GetAuthorAsync(); if (MessagesThisMinute.ContainsKey(ClientUser.Id)) { MessagesThisMinute[ClientUser.Id] += 1; } else { MessagesThisMinute.Add(ClientUser.Id, 1); } if (message.Author_Id == ulong.Parse(Client.Config.BotId)) { return; } User user = await Context.Users.FirstOrDefaultAsync(x => x.UserId == message.Author_Id && x.PlanetId == message.Planet_Id); if (lotterycache.ContainsKey(message.Planet_Id)) { if (lotterycache[message.Planet_Id].Type == "message") { Lottery lottery = await Context.Lotteries.FirstOrDefaultAsync(x => x.PlanetId == message.Planet_Id); lottery.Jackpot += lottery.JackpotIncreasePerMesage; await lottery.AddTickets(message.Author_Id, 1, message.Planet_Id, Context); } } if (message.Content.Substring(0, 1) == Client.Config.CommandSign) { string command = message.Content.Split(" ")[0]; command = command.Replace("\n", ""); List <string> ops = message.Content.Split(" ").ToList(); command = command.Replace(Client.Config.CommandSign, ""); if (command == "roll") { if (ops.Count < 3) { await PostMessage(message.Channel_Id, message.Planet_Id, "Command Format: /roll <from> <to>"); return; } int from = int.Parse(ops[1]); int to = int.Parse(ops[2]); int num = rnd.Next(from, to); await PostMessage(message.Channel_Id, message.Planet_Id, $"Roll: {num}"); } if (command == "eco") { if (ops.Count == 1) { ops.Add(""); } switch (ops[1]) { case "cap": int total = 0; foreach (User USER in Context.Users) { total += (int)USER.Coins; } await PostMessage(message.Channel_Id, message.Planet_Id, $"Eco cap: {total} coins"); break; default: await PostMessage(message.Channel_Id, message.Planet_Id, "Available Commands: /eco cap"); break; } } if (command == "forcerolepayout") { if (await ClientUser.IsOwner() != true) { await PostMessage(message.Channel_Id, message.Planet_Id, $"Only the owner of this server can use this command!"); return; } await Context.UpdateRoleIncomes(planets, true, Context); await PostMessage(message.Channel_Id, message.Planet_Id, "Successfully forced a role payout."); } if (command == "lottery") { if (ops.Count() == 1) { ops.Add(""); } switch (ops[1]) { case "timeleft": Lottery lottery = await Context.Lotteries.FirstOrDefaultAsync(x => x.PlanetId == message.Planet_Id); if (lottery == null) { await PostMessage(message.Channel_Id, message.Planet_Id, $"There is no lottery currently going on!"); break; } TimeSpan timeleft = lottery.EndDate.Subtract(DateTime.UtcNow); await PostMessage(message.Channel_Id, message.Planet_Id, $"{timeleft.Hours} hours left"); break; case "tickets": LotteryTicket ticket = await Context.LotteryTickets.FirstOrDefaultAsync(x => x.PlanetId == message.Planet_Id && x.UserId == message.Author_Id); if (ticket == null) { await PostMessage(message.Channel_Id, message.Planet_Id, $"There is no lottery currently going on!"); break; } await PostMessage(message.Channel_Id, message.Planet_Id, $"You have {ticket.Tickets} tickets"); break; case "jackpot": lottery = await Context.Lotteries.FirstOrDefaultAsync(x => x.PlanetId == message.Planet_Id); if (lottery == null) { await PostMessage(message.Channel_Id, message.Planet_Id, $"There is no lottery currently going on!"); break; } await PostMessage(message.Channel_Id, message.Planet_Id, $"The current jackpot is {lottery.Jackpot}"); break; case "create": if (await ClientUser.IsOwner() != true) { await PostMessage(message.Channel_Id, message.Planet_Id, $"Only the owner of this server can use this command!"); break; } if (ops.Count() < 4) { await PostMessage(message.Channel_Id, message.Planet_Id, "Command Format: /lottery create coin <hours> or /lottery create message <how many coins will each message add> <hours>"); break; } string type = ops[2]; int HoursToLast = 0; if (type == "message") { if (ops.Count() < 5) { await PostMessage(message.Channel_Id, message.Planet_Id, "Command Format: /lottery create message <how many coins will each message add> <hours>"); break; } HoursToLast = int.Parse(ops[4]); } else { HoursToLast = int.Parse(ops[3]); } if (HoursToLast > 24) { await PostMessage(message.Channel_Id, message.Planet_Id, "You can not have a lottery last more than 24 hours!"); break; } if (type != "coin" && type != "message") { await PostMessage(message.Channel_Id, message.Planet_Id, "Type must either by coin or message!"); break; } // check if the planet is areadly doing a lottery lottery = await Context.Lotteries.FirstOrDefaultAsync(x => x.PlanetId == message.Planet_Id); if (lottery != null) { await PostMessage(message.Channel_Id, message.Planet_Id, "You can not have two lotteries running at the same time!"); break; } lottery = new Lottery(); lottery.StartDate = DateTime.UtcNow; lottery.Type = type; lottery.PlanetId = message.Planet_Id; lottery.EndDate = DateTime.UtcNow.AddHours(HoursToLast); lottery.Jackpot = 0; lottery.ChannelId = message.Channel_Id; if (type == "message") { lottery.JackpotIncreasePerMesage = (double)int.Parse(ops[3]); } else { lottery.JackpotIncreasePerMesage = 0; } await Context.Lotteries.AddAsync(lottery); lotterycache.Add(lottery.PlanetId, lottery); await PostMessage(message.Channel_Id, message.Planet_Id, "Successfully created a lottery."); await Context.SaveChangesAsync(); break; default: await PostMessage(message.Channel_Id, message.Planet_Id, "Command Format: /lottery create coin <hours> or /lottery create message <how many coins will each message add> <hours>"); break; } } } Console.WriteLine(message.Content); }