Example #1
0
        public EmbedBuilder GetGameEmbed(ManualGameResult game)
        {
            var embed = new EmbedBuilder();

            if (game.GameState == ManualGameState.Win)
            {
                embed.Color = Color.Green;
                embed.Title = "Win";
            }
            else if (game.GameState == ManualGameState.Lose)
            {
                embed.Color = Color.Red;
                embed.Title = "Lose";
            }
            else if (game.GameState == ManualGameState.Draw)
            {
                embed.Color = Color.Gold;
                embed.Title = "Draw";
            }
            else
            {
                embed.Color = Color.Blue;
                embed.Title = "Legacy";
            }

            using (var db = new Database())
            {
                var scoreUpdates = db.ManualGameScoreUpdates.Where(x => x.GuildId == game.GuildId && x.ManualGameId == game.GameId);
                embed.Description = $"**GameId:** {game.GameId}\n" +
                                    $"**Creation Time:** {game.CreationTime.ToString("dd MMM yyyy")} {game.CreationTime.ToShortTimeString()}\n" +
                                    $"**Comment:** {game.Comment ?? "N/A"}\n" +
                                    $"**Submitted By:** {MentionUtils.MentionUser(game.Submitter)}\n" +
                                    string.Join("\n", scoreUpdates.Select(x => $"{MentionUtils.MentionUser(x.UserId)} {(x.ModifyAmount >= 0 ? $"`+{x.ModifyAmount}`" : $"`{x.ModifyAmount}`")}"));
            }
Example #2
0
        public EmbedBuilder GetGameEmbed(ManualGameResult game)
        {
            var embed = new EmbedBuilder();

            if (game.GameState == ManualGameResult.ManualGameState.Win)
            {
                embed.Color = Color.Green;
                embed.Title = "Win";
            }
            else if (game.GameState == ManualGameResult.ManualGameState.Lose)
            {
                embed.Color = Color.Red;
                embed.Title = "Lose";
            }
            else if (game.GameState == ManualGameResult.ManualGameState.Draw)
            {
                embed.Color = Color.Gold;
                embed.Title = "Draw";
            }
            else
            {
                embed.Color = Color.Blue;
                embed.Title = "Legacy";
            }


            embed.Description = $"**GameId:** {game.GameId}\n" +
                                $"**Creation Time:** {game.CreationTime.ToString("dd MMM yyyy")} {game.CreationTime.ToShortTimeString()}\n" +
                                $"**Comment:** {game.Comment ?? "N/A"}\n" +
                                $"**Submitted By:** {MentionUtils.MentionUser(game.Submitter)}\n" +
                                string.Join("\n", game.ScoreUpdates.Select(x => $"{MentionUtils.MentionUser(x.Key)} {(x.Value >= 0 ? $"`+{x.Value}`" : $"`{x.Value}`")}")).FixLength(1024);
Example #3
0
 public void SaveManualGame(ManualGameResult game)
 {
     Database.Store <ManualGameResult>(game, ManualGameResult.DocumentName(game.GameId, game.GuildId));
 }
Example #4
0
 public void RemoveManualGame(ManualGameResult game)
 {
     Database.Remove <ManualGameResult>(ManualGameResult.DocumentName(game.GameId, game.GuildId));
 }
Example #5
0
 public ManualGameResult GetManualGame(ulong guildId, int gameId)
 {
     return(Database.Load <ManualGameResult>(ManualGameResult.DocumentName(gameId, guildId)));
 }
Example #6
0
        public virtual async Task UpdateTeamScoresAsync(bool win, HashSet <ulong> userIds)
        {
            using (var db = new Database())
            {
                var competition = db.GetOrCreateCompetition(Context.Guild.Id);
                var updates     = new List <(Player, int, Rank, RankChangeState, Rank)>();
                var ranks       = db.Ranks.Where(x => x.GuildId == Context.Guild.Id).ToArray();
                var embed       = new EmbedBuilder
                {
                    Title = (win ? "Win" : "Lose") + $" Manual Game: #{competition.ManualGameCounter + 1}",
                    Color = win ? Color.Green : Color.Red,
                };

                var sb = new StringBuilder();
                foreach (var userId in userIds)
                {
                    var player = db.Players.Find(Context.Guild.Id, userId);
                    if (player == null)
                    {
                        continue;
                    }

                    //This represents the current user's rank
                    var maxRank = ranks.Where(x => x.Points <= player.Points).OrderByDescending(x => x.Points).FirstOrDefault();

                    int             updateVal;
                    RankChangeState state   = RankChangeState.None;
                    Rank            newRank = null;

                    if (win)
                    {
                        updateVal      = maxRank?.WinModifier ?? competition.DefaultWinModifier;
                        player.Points += updateVal;
                        player.Wins++;
                        newRank = ranks.Where(x => x.Points <= player.Points).OrderByDescending(x => x.Points).FirstOrDefault();
                        if (newRank != null)
                        {
                            if (maxRank == null)
                            {
                                state = RankChangeState.RankUp;
                            }
                            else if (newRank.RoleId != maxRank.RoleId)
                            {
                                state = RankChangeState.RankUp;
                            }
                        }
                    }
                    else
                    {
                        //Loss modifier is always positive so subtract it
                        updateVal = maxRank?.LossModifier ?? competition.DefaultLossModifier;

                        player.Points -= updateVal;
                        if (!competition.AllowNegativeScore && player.Points < 0)
                        {
                            player.Points = 0;
                        }
                        player.Losses++;
                        //Set the update value to a negative value for returning purposes.
                        updateVal = -Math.Abs(updateVal);

                        if (maxRank != null)
                        {
                            if (player.Points < maxRank.Points)
                            {
                                state   = RankChangeState.DeRank;
                                newRank = ranks.Where(x => x.Points <= player.Points).OrderByDescending(x => x.Points).FirstOrDefault();
                            }
                        }
                    }

                    updates.Add((player, updateVal, maxRank, state, newRank));
                    db.Update(player);

                    //Ignore user updates if they aren't found in the server.
                    var gUser = Context.Guild.GetUser(userId);
                    if (gUser == null)
                    {
                        continue;
                    }

                    var _ = Task.Run(async() => await UserService.UpdateUserAsync(competition, player, ranks, gUser));

                    var rankUpdate = "";
                    if (maxRank != null || newRank != null)
                    {
                        var oldRoleMention = maxRank == null ? "N/A" : MentionUtils.MentionRole(maxRank.RoleId);
                        var newRoleMention = newRank == null ? "N/A" : MentionUtils.MentionRole(newRank.RoleId);
                        rankUpdate = $" Rank: {oldRoleMention} => {newRoleMention}";
                    }

                    sb.AppendLine($"{gUser.Mention} Points: {player.Points} {(win ? "Added:" : "Removed:")} {updateVal}{rankUpdate}");
                }

                //Update counter and save new competition config


                //Create new game info
                var vals  = ((IQueryable <ManualGameResult>)db.ManualGameResults).Where(x => x.GuildId == Context.Guild.Id).ToArray();
                var count = vals.Length == 0 ? 0 : vals.Max(x => x.GameId);
                var game  = new ManualGameResult
                {
                    GuildId = Context.Guild.Id,
                    GameId  = count + 1
                };

                competition.ManualGameCounter = game.GameId;
                db.Update(competition);
                game.Submitter = Context.User.Id;
                game.GameState = win ? ManualGameState.Win : ManualGameState.Lose;
                db.ManualGameResults.Add(game);
                db.SaveChanges();
                game = db.ManualGameResults.Where(x => x.GuildId == Context.Guild.Id).OrderByDescending(x => x.GameId).First();

                foreach (var upd in updates)
                {
                    db.ManualGameScoreUpdates.Add(new ManualGameScoreUpdate
                    {
                        GuildId      = Context.Guild.Id,
                        ManualGameId = game.GameId,
                        ModifyAmount = upd.Item2,
                        UserId       = upd.Item1.UserId
                    });
                }

                embed.Description = sb.ToString();
                db.SaveChanges();
                //save scores
                await ReplyAsync(embed);
            }
        }
Example #7
0
        public async Task UpdateTeamScoresAsync(bool win, HashSet <ulong> userIds)
        {
            var competition = Service.GetOrCreateCompetition(Context.Guild.Id);
            var updates     = new List <(Player, int, Rank, RankChangeState, Rank)>();

            var embed = new EmbedBuilder
            {
                Title = (win ? "Win" : "Lose") + $" Manual Game: #{competition.ManualGameCounter + 1}",
                Color = win ? Color.Green : Color.Red,
            };
            var sb = new StringBuilder();

            foreach (var userId in userIds)
            {
                var player = Service.GetPlayer(Context.Guild.Id, userId);
                if (player == null)
                {
                    continue;
                }

                //This represents the current user's rank
                var maxRank = competition.MaxRank(player.Points);

                int             updateVal;
                RankChangeState state   = RankChangeState.None;
                Rank            newRank = null;

                if (win)
                {
                    updateVal = maxRank?.WinModifier ?? competition.DefaultWinModifier;
                    player.SetPoints(competition, player.Points + updateVal);
                    player.Wins++;
                    newRank = competition.MaxRank(player.Points);
                    if (newRank != null)
                    {
                        if (maxRank == null)
                        {
                            state = RankChangeState.RankUp;
                        }
                        else if (newRank.RoleId != maxRank.RoleId)
                        {
                            state = RankChangeState.RankUp;
                        }
                    }
                }
                else
                {
                    //Loss modifier is always positive so subtract it
                    updateVal = maxRank?.LossModifier ?? competition.DefaultLossModifier;
                    player.SetPoints(competition, player.Points - updateVal);
                    player.Losses++;
                    //Set the update value to a negative value for returning purposes.
                    updateVal = -updateVal;

                    if (maxRank != null)
                    {
                        if (player.Points < maxRank.Points)
                        {
                            state   = RankChangeState.DeRank;
                            newRank = competition.MaxRank(player.Points);
                        }
                    }
                }

                updates.Add((player, updateVal, maxRank, state, newRank));

                //TODO: Rank checking?
                //I forget what this means honestly
                Service.SavePlayer(player);

                //Ignore user updates if they aren't found in the server.
                var gUser = Context.Guild.GetUser(userId);
                if (gUser == null)
                {
                    continue;
                }

                await Service.UpdateUserAsync(competition, player, gUser);

                var rankUpdate = "";
                if (maxRank != null || newRank != null)
                {
                    var oldRoleMention = maxRank == null ? "N/A" : MentionUtils.MentionRole(maxRank.RoleId);
                    var newRoleMention = newRank == null ? "N/A" : MentionUtils.MentionRole(newRank.RoleId);
                    rankUpdate = $" Rank: {oldRoleMention} => {newRoleMention}";
                }

                sb.AppendLine($"{gUser.Mention} Points: {player.Points} {(win ? "Added:" : "Removed:")} {updateVal}{rankUpdate}");
            }

            //Update counter and save new competition config
            competition.ManualGameCounter++;
            Service.SaveCompetition(competition);

            //Create new game info
            var game = new ManualGameResult(competition.ManualGameCounter, Context.Guild.Id);

            game.Submitter    = Context.User.Id;
            game.GameState    = win ? ManualGameResult.ManualGameState.Win : ManualGameResult.ManualGameState.Lose;
            game.ScoreUpdates = updates.ToDictionary(x => x.Item1.UserId, x => x.Item2);
            embed.Description = sb.ToString();
            Service.SaveManualGame(game);
            await ReplyAsync(embed);
        }
Example #8
0
        public virtual async Task DisplayGameAsync(ManualGameResult game)
        {
            var embed = GameService.GetGameEmbed(game);

            await ReplyAsync(embed);
        }