Example #1
0
 public int CompareTo(PlayerScore item2)
 {
     return(item2.Money.CompareTo(Money));
 }
Example #2
0
        public async Task ProcessCommand(ulong discordId, SocketTextChannel replyChannel, string command, IList <string> args)
        {
            if (!Scoreboard.ContainsKey(discordId))
            {
                string name = ResolvePlayerName(discordId);
                Scoreboard[discordId] = new PlayerScore(discordId, name);
                Scoreboard.SaveToFile(SavePath);
                Console.WriteLine($"New player registered: {name} (DiscordID {discordId})");
            }

            command = command.ToLower().Trim();
            switch (command)
            {
            // Singlet commands
            case "score":
            case "money":
            case "myscore":
                if (Scoreboard.ContainsKey(discordId))
                {
                    var myScore = Scoreboard[discordId];
                    await SayInChannel(replyChannel, $"Score for {myScore.Name}:\n{myScore.Money:c} after {myScore.BetCount} bets ({myScore.MoneyFromBets:+$0.00;-$0.00;$0}). " +
                                       $"Prediction record: {myScore.PredictionsRight} - {myScore.PredictionsWrong} (+${myScore.MoneyFromPredictions})");
                }
                else
                {
                    await SayInChannel(replyChannel, $"You are not a registered player yet. Place a Bet or Prediction to start. " +
                                       $"You'll start with ${PlayerScore.STARTING_MONEY}.");
                }
                break;

            case "scores":
            case "allscores":
            case "scoreboard":
                if (Scoreboard.Count == 0)
                {
                    await SayInChannel(replyChannel, "The scoreboard is empty - there are no registered players yet.");
                }
                else
                {
                    string scoreMessage = "Scoreboard:\n";
                    var    sortedScores = Scoreboard.Values.ToList();
                    sortedScores.Sort((item1, item2) => item1.CompareTo(item2));
                    int place = 1;
                    foreach (var playerScore in sortedScores)
                    {
                        scoreMessage += $"{place++}. {playerScore.Name}: {playerScore.Money:c} ({playerScore.BetCount} bets, {playerScore.MoneyFromBets:+$0.00;-$0.00;$0}) " +
                                        $"Predictions: {playerScore.PredictionsRight} - {playerScore.PredictionsWrong} (+${playerScore.MoneyFromPredictions})\n";
                    }
                    await SayInChannel(replyChannel, scoreMessage);
                }
                break;

            case "games":
            case "upcoming":
            case "schedule":
                var futureGames = Schedule.Where(match => DateTime.Now < match.ScheduledTime + TimeSpan.FromMinutes(10));
                if (futureGames.Count() <= 0)
                {
                    await SayInChannel(replyChannel, "There are no games left in the season.");
                }
                else
                {
                    string scheduleMessage = "Upcoming LCS games:\n";
                    foreach (var futureGame in futureGames.Take(futureGames.Count() >= 5 ? 5 : futureGames.Count()))
                    {
                        scheduleMessage += futureGame.ToString() + "\n";
                    }
                    await SayInChannel(replyChannel, scheduleMessage);
                }
                break;

            case "mybets":
            case "bets":
                var myBets        = Bets.Where(bet => bet.DiscordID == discordId).ToList();
                var myPredictions = Predictions.Where(pred => pred.DiscordID == discordId).ToList();
                if (myBets.Count + myPredictions.Count == 0)
                {
                    await SayInChannel(replyChannel, "You do not have any active bets or predictions.");
                }
                else
                {
                    string betsString = "";
                    if (myBets.Count > 0)
                    {
                        betsString += "Your active bets:\n";
                        myBets.ForEach(bet => betsString += $"{bet.Wager:c} on {bet.Victor} beating {bet.Loser}\n");
                    }
                    if (myPredictions.Count > 0)
                    {
                        betsString += "Your predictions:\n";
                        myPredictions.ForEach(pred => betsString += $"{pred.Victor} will beat {pred.Loser}\n");
                    }
                    await SayInChannel(replyChannel, betsString);
                }
                break;

            case "allbets":
                if (Bets.Count + Predictions.Count == 0)
                {
                    await SayInChannel(replyChannel, "There are no active bets or predictions.");
                }
                else
                {
                    string betString = "";
                    if (Bets.Count > 0)
                    {
                        betString += "All active bets:\n";
                        Bets.ForEach(bet => betString += $"{ResolvePlayerName(bet.DiscordID)} bet {bet.Wager} on {bet.Victor} beating {bet.Loser}.\n");
                    }
                    if (Predictions.Count > 0)
                    {
                        betString += "Active predictions:\n";
                        Predictions.ForEach(pred => betString += $"{ResolvePlayerName(pred.DiscordID)} thinks {pred.Victor} will beat {pred.Loser}.\n");
                    }
                    await SayInChannel(replyChannel, betString);
                }
                break;

            // Parameterized commands
            case "nextgame":
            case "next":
                if (args.Count() == 0)
                {
                    await SayInChannel(replyChannel, "Enter a team to check the next game for (!next <Team>) or a pair of teams to find their next match (!next <Team1> <Team2>)");
                }
                else if (args.Count() > 2)
                {
                    await SayInChannel(replyChannel, "Usage: !next <Team> or !next <Team1> <Team2>");
                }
                else if (args.Count() == 1)
                {
                    LCSTeam team_check = LCSTeam.ParseTeam(args[0]);
                    if (team_check.ID == LCSTeam.TeamID.Unknown)
                    {
                        await SayInChannel(replyChannel, $"Unknown LCS team '{args[0]}'");
                    }
                    else
                    {
                        var nextMatchCheck = Schedule.GetNextMatchForTeam(team_check);
                        if (nextMatchCheck == null)
                        {
                            await SayInChannel(replyChannel, $"{team_check} has no matches left in the season.");
                        }
                        else
                        {
                            await SayInChannel(replyChannel, $"Next match for {team_check}: {nextMatchCheck}");
                        }
                    }
                }
                else     // Checking for match between two teams
                {
                    LCSTeam check1 = LCSTeam.ParseTeam(args[0]);
                    if (check1.ID == LCSTeam.TeamID.Unknown)
                    {
                        await SayInChannel(replyChannel, $"Unknown LCS team '{args[0]}'");

                        break;
                    }
                    LCSTeam check2 = LCSTeam.ParseTeam(args[1]);
                    if (check2.ID == LCSTeam.TeamID.Unknown)
                    {
                        await SayInChannel(replyChannel, $"Unknown LCS team '{args[1]}'");

                        break;
                    }
                    var nextMatchup = Schedule.GetNextMatchForMatchup(check1, check2);
                    if (nextMatchup == null)
                    {
                        await SayInChannel(replyChannel, $"{check1} and {check2} are not playing each other for the rest of the season.");
                    }
                    else
                    {
                        await SayInChannel(replyChannel, $"{check1} next plays {check2} at {nextMatchup.ScheduledTime:h:mm tt M/dd}");
                    }
                }
                break;

            case "predict":
                if (args.Count() == 0)
                {
                    await SayInChannel(replyChannel, "Enter a team to predict. (!predict <Team>)");
                }
                else if (args.Count() != 1)
                {
                    await SayInChannel(replyChannel, "Usage: !predict <Team>");
                }
                else
                {
                    LCSTeam team_pred = LCSTeam.ParseTeam(args[0]);
                    if (team_pred.ID == LCSTeam.TeamID.Unknown)
                    {
                        await SayInChannel(replyChannel, $"Unknown LCS team '{args[0]}'");
                    }
                    else
                    {
                        var nextScheduledMatch_pred = Schedule.GetNextMatchForTeam(team_pred);
                        if (nextScheduledMatch_pred == null)
                        {
                            await SayInChannel(replyChannel, $"Team {team_pred} has no games left in the season.");
                        }
                        else if (DateTime.Now > nextScheduledMatch_pred.ScheduledTime + TimeSpan.FromMinutes(BETBUFFER_MINS))     // If next game is within this hour
                        {
                            await SayInChannel(replyChannel, $"{team_pred} is currently playing! ({nextScheduledMatch_pred}) " +
                                               $"Wait until afer the current match to make a prediction.");
                        }
                        else
                        {
                            LCSTeam loser;
                            if (nextScheduledMatch_pred.Team1.Equals(team_pred))
                            {
                                loser = nextScheduledMatch_pred.Team2;
                            }
                            else
                            {
                                loser = nextScheduledMatch_pred.Team1;
                            }

                            // Update existing bets
                            bool updatedPred = false;
                            for (int i = 0; i < Predictions.Count; i++)
                            {
                                if (Predictions[i].DiscordID == discordId)                                             // Of my predictions...
                                {
                                    if (Predictions[i].Victor.Equals(team_pred) && Predictions[i].Loser.Equals(loser)) // Predicting same victory combo
                                    {
                                        await SayInChannel(replyChannel, $"You already predicted that {Predictions[i].Victor} will beat {Predictions[i].Loser} ({nextScheduledMatch_pred})");

                                        updatedPred = true;
                                        break;
                                    }
                                    else if (Predictions[i].Loser.Equals(team_pred) && Predictions[i].Victor.Equals(loser)) // Betting on opposite victory combo
                                    {
                                        Predictions[i] = new Prediction(team_pred, loser, discordId);                       // Create new Bet
                                        await SayInChannel(replyChannel, $"Prediction changed: {Predictions[i].Victor} will beat {Predictions[i].Loser} ({nextScheduledMatch_pred})");

                                        updatedPred = true;
                                        break;
                                    }
                                }
                            }

                            if (!updatedPred)     // Create new bet if no bets were modified
                            {
                                Prediction pred = new Prediction(team_pred, loser, discordId);
                                Predictions.Add(pred);
                                await SayInChannel(replyChannel, $"Prediction made: {pred.Victor} will beat {pred.Loser} ({nextScheduledMatch_pred.ScheduledTime:ddd M/d h:mm tt})");
                            }
                        }
                    }
                }
                break;

            case "placebet":
            case "makebet":
            case "bet":
                // Parse and verify arguments
                if (args.Count() != 2)
                {
                    await SayInChannel(replyChannel, "Usage: !bet <Team> <Wager>");

                    break;
                }
                decimal wager;
                int     argWagerIndex = 0;
                if (!decimal.TryParse(args[argWagerIndex].Trim('$'), out wager))
                {
                    argWagerIndex = 1;
                    if (!decimal.TryParse(args[argWagerIndex].Trim('$'), out wager))
                    {
                        await SayInChannel(replyChannel, "Please enter an amount of money. (!bet <Team> <Wager>)");

                        break;
                    }
                }
                if (wager <= 0)
                {
                    await SayInChannel(replyChannel, "Wager must be greater than $0.");

                    break;
                }
                int     argTeamIndex = argWagerIndex == 0 ? 1 : 0;
                LCSTeam team         = LCSTeam.ParseTeam(args[argTeamIndex]);
                if (team.ID == LCSTeam.TeamID.Unknown)
                {
                    await SayInChannel(replyChannel, $"Unknown LCS team '{args[argTeamIndex]}'");

                    break;
                }
                // Get match timing
                var nextScheduledMatch = Schedule.GetNextMatchForTeam(team);
                if (nextScheduledMatch == null)
                {
                    await SayInChannel(replyChannel, $"Team {team} has no games left in the season.");
                }
                else if (DateTime.Now > nextScheduledMatch.ScheduledTime + TimeSpan.FromMinutes(BETBUFFER_MINS))     // If next game is within this hour
                {
                    await SayInChannel(replyChannel, $"{team} is currently playing! ({nextScheduledMatch}) " +
                                       $"Wait until afer the current match to place a bet.");
                }
                else     // Timing looks good
                {
                    LCSTeam loser;
                    if (nextScheduledMatch.Team1.Equals(team))
                    {
                        loser = nextScheduledMatch.Team2;
                    }
                    else
                    {
                        loser = nextScheduledMatch.Team1;
                    }

                    // Update existing bets
                    bool updatedBet = false;
                    for (int i = 0; i < Bets.Count; i++)
                    {
                        if (Bets[i].DiscordID == discordId)                                 // Of my bets...
                        {
                            if (Bets[i].Victor.Equals(team) && Bets[i].Loser.Equals(loser)) // Betting on same victory combo
                            {
                                if (Bets[i].Wager != wager)                                 // Change wager for same victory combo
                                {
                                    if (Scoreboard[discordId].Money + Bets[i].Wager - wager < 0)
                                    {
                                        await SayInChannel(replyChannel, $"You can't change your bet by that much. You have {Scoreboard[discordId].Money:c}.");

                                        updatedBet = true;
                                        break;
                                    }
                                    else
                                    {
                                        Scoreboard[discordId].Money += Bets[i].Wager - wager;     // Adjust player money to match wager change
                                        Bets[i] = new Bet(team, loser, wager, discordId);
                                        await SayInChannel(replyChannel, $"Bet changed: {Bets[i].Wager:c} on {Bets[i].Victor} beating {Bets[i].Loser} ({nextScheduledMatch.ScheduledTime:ddd M/d h:mm tt})");

                                        updatedBet = true;
                                        Scoreboard.SaveToFile(SavePath);
                                        break;
                                    }
                                }
                                else     // Same wager and victory combo
                                {
                                    await SayInChannel(replyChannel, $"You already bet {Bets[i].Wager:c} on {Bets[i].Victor} (vs {Bets[i].Loser} {nextScheduledMatch.ScheduledTime:ddd M/d h:mm tt})");

                                    updatedBet = true;
                                    break;
                                }
                            }
                            else if (Bets[i].Loser.Equals(team) && Bets[i].Victor.Equals(loser))     // Betting on opposite victory combo
                            {
                                if (Scoreboard[discordId].Money + Bets[i].Wager - wager < 0)
                                {
                                    await SayInChannel(replyChannel, $"You can't change your bet to that. You have {Scoreboard[discordId].Money:c}.");

                                    updatedBet = true;
                                    break;
                                }
                                else
                                {
                                    Scoreboard[discordId].Money += Bets[i].Wager - wager; // Adjust player money to match wager change
                                    Bets[i] = new Bet(team, loser, wager, discordId);     // Create new Bet
                                    await SayInChannel(replyChannel, $"Bet changed: {Bets[i].Wager:c} on {Bets[i].Victor} beating {Bets[i].Loser} ({nextScheduledMatch.ScheduledTime:ddd M/d h:mm tt})");

                                    updatedBet = true;
                                    Scoreboard.SaveToFile(SavePath);
                                    break;
                                }
                            }
                        }
                    }

                    if (!updatedBet)     // Create new bet if no bets were modified
                    {
                        if (Scoreboard[discordId].Money - wager < 0)
                        {
                            await SayInChannel(replyChannel, $"You can't bet that much. You have {Scoreboard[discordId].Money:c}.");

                            updatedBet = true;
                            break;
                        }
                        else
                        {
                            Bet bet = new Bet(team, loser, wager, discordId);
                            Bets.Add(bet);
                            Scoreboard.TakeMoney(discordId, wager);
                            Scoreboard[discordId].BetCount += 1;
                            Scoreboard.SaveToFile(SavePath);
                            await SayInChannel(replyChannel, $"Bet placed: {wager:c} on {team} beating {loser} ({nextScheduledMatch.ScheduledTime:ddd M/d h:mm tt})");
                        }
                    }
                }
                break;
            }
        }