public static void EndGiveAway(int giveAwayID)
        {
            GiveAway giveAway = GiveAways[giveAwayID - 1];

            List <UserDataSet> ticketPool = new List <UserDataSet>();

            foreach (TicketData ticket in giveAway.tickets)
            {
                for (int i = 0; i < ticket.ticketAmount; i++)
                {
                    ticketPool.Add(ticket.userDataSet);
                }
            }
            ExtentionMethods.Shuffle(ticketPool);

            foreach (GiveAwayReward reward in giveAway.rewards)
            {
                Random WinnerSelector = new Random();
                reward.winner = ticketPool[WinnerSelector.Next(0, ticketPool.Count)];
                ticketPool.Remove(reward.winner);
            }

            giveAway.state = GiveAwayState.Ended;
            UpdateGiveAwayMessage(giveAway);
        }
        public static void getTickets(int GiveAwayID, ICommandContext context)
        {
            GiveAway giveAway = GiveAways[GiveAwayID - 1];
            int      amount   = 0;

            amount = giveAway.tickets.FirstOrDefault(x => x.userDataSet.userID == context.User.Id).ticketAmount;
            context.Channel.SendMessageAsync($"You own {amount} Tickets of the pool of {giveAway.GetTotalTickets()} Tickets.");
        }
        public static void BuyTickets(ICommandContext context, int id, int amountToBuy)
        {
            GiveAway giveAway = GiveAways[id - 1];

            switch (giveAway.state)
            {
            case GiveAwayState.Cancelled:
                context.Channel.SendMessageAsync("Sorry, That giveaway is cancelled! You cannot buy tickets.");
                return;

            case GiveAwayState.Ended:
                context.Channel.SendMessageAsync("Sorry, That giveaway has aleady ended! You cannot buy tickets.");
                return;

            case GiveAwayState.Pending:
                context.Channel.SendMessageAsync("Sorry, That giveaway has not yet started and is in pending status! You cannot buy tickets yet. Wait untill the status is `open`.");
                return;
            }

            UserDataSet userDataSet = UserDataService.UserData.FirstOrDefault(x => x.userID == context.User.Id);

            if (userDataSet.drak - (amountToBuy * giveAway.ticketEntryCost) < 0)
            {
                context.Channel.SendMessageAsync("Sorry, you don't have enough Draks to complete this purchase."); return;
            }
            if (giveAway.tickets.Exists(x => x.userDataSet.userID == userDataSet.userID))
            {
                userDataSet.drak -= giveAway.ticketEntryCost * amountToBuy;

                TicketData ticketData = giveAway.tickets.FirstOrDefault(x => x.userDataSet.userID == userDataSet.userID);
                ticketData.userDataSet   = userDataSet;
                ticketData.ticketAmount += amountToBuy;
                context.Channel.SendMessageAsync("Tickets have been successfully bought!");
            }
            else
            {
                userDataSet.drak -= giveAway.ticketEntryCost * amountToBuy;

                TicketData ticketData = new TicketData();
                ticketData.userDataSet   = userDataSet;
                ticketData.ticketAmount += amountToBuy;

                giveAway.tickets.Add(ticketData);
                context.Channel.SendMessageAsync("Tickets have been successfully bought!");
            }
            UserDataService.SaveUserData();
            SaveGiveAways();
            UpdateGiveAwayMessage(giveAway);
        }
        public static void CreateGiveAway(float entryCost, int endsInDays, string[] rewardStrings)
        {
            GiveAway newGiveAway = new GiveAway();

            newGiveAway.id = GiveAways.Count() + 1;
            foreach (string rewardString in rewardStrings)
            {
                GiveAwayReward reward = new GiveAwayReward();
                reward.rewardString = rewardString;
                reward.winner       = null;
                newGiveAway.rewards.Add(reward);
            }
            newGiveAway.ticketEntryCost = entryCost;
            newGiveAway.endDateTime     = DateTime.Now.AddDays(endsInDays);

            IUserMessage userMessage = (Program.guild.GetChannel(giveawayChannelID) as IMessageChannel).SendMessageAsync("", false, CreateMessageEmbed(newGiveAway).Build()).Result;

            newGiveAway.giveAwayMessageId = userMessage.Id;
            GiveAways.Add(newGiveAway);
            SaveGiveAways();
        }
 public static void UpdateGiveAwayMessage(GiveAway giveAway)
 {
     ((Program.guild.GetChannel(giveawayChannelID) as IMessageChannel).GetMessageAsync(giveAway.giveAwayMessageId).Result as IUserMessage).ModifyAsync(x => x.Embed = CreateMessageEmbed(giveAway).Build());
 }
        static EmbedBuilder CreateMessageEmbed(GiveAway newGiveAway)
        {
            string statusString = "";
            Color  statusColor  = new Color();

            switch (newGiveAway.state)
            {
            case GiveAwayState.Cancelled:
                statusString = "Cancelled";
                statusColor  = Color.Red;
                break;

            case GiveAwayState.Ended:
                statusString = "Ended";
                statusColor  = Color.LightGrey;
                break;

            case GiveAwayState.Open:
                statusString = "Open";
                statusColor  = Color.Green;
                break;

            case GiveAwayState.Pending:
                statusString = "Pending";
                statusColor  = Color.LightOrange;
                break;
            }

            EmbedBuilder embed = new EmbedBuilder()
            {
                Color = statusColor,
                Title = $"GiveAway #{newGiveAway.id}"
            };

            embed.AddField(new EmbedFieldBuilder()
            {
                Name = "Ticket Cost:", Value = newGiveAway.ticketEntryCost + " Ξ"
            });
            embed.AddField(new EmbedFieldBuilder()
            {
                Name = "\u200B", Value = "\u200B"
            });
            embed.AddField(new EmbedFieldBuilder()
            {
                Name = "__**Rewards:**__", Value = "\u200B"
            });
            int rewardIndex = 1;

            foreach (GiveAwayReward reward in newGiveAway.rewards)
            {
                string valueString = reward.rewardString;
                if (reward.winner != null)
                {
                    valueString += $" - **Winner: {Program.guild.GetUser(reward.winner.userID).Mention}**";
                }
                embed.AddField(new EmbedFieldBuilder()
                {
                    Name = $"Reward {rewardIndex}:", Value = valueString
                });
                rewardIndex++;
            }
            embed.AddField(new EmbedFieldBuilder()
            {
                Name = "\u200B", Value = "\u200B"
            });

            embed.AddField(new EmbedFieldBuilder()
            {
                Name = "Status:", Value = statusString
            });
            embed.AddField(new EmbedFieldBuilder()
            {
                Name = "Total Entries:", Value = newGiveAway.GetTotalTickets()
            });
            embed.AddField(new EmbedFieldBuilder()
            {
                Name = "End Date:", Value = newGiveAway.endDateTime.Date.ToString("dd/MM/yy")
            });

            embed.Footer = new EmbedFooterBuilder()
            {
                Text = "Giveaway created: " + newGiveAway.openDateTime.Date.ToString("dd/MM/yy")
            };

            return(embed);
        }