Beispiel #1
0
        public async Task GameAsync(IMessageChannel lobbyChannel, int gameNumber, GuildModel.GameResult._Result result)
        {
            if (Context.Server.Lobbies.All(x => x.ChannelID != lobbyChannel.Id))
            {
                throw new Exception("Channel is not a lobby");
            }

            var game = Context.Server.Results.FirstOrDefault(x => x.LobbyID == lobbyChannel.Id && x.GameNumber == gameNumber);

            if (game.Result != GuildModel.GameResult._Result.Undecided)
            {
                await InlineReactionReplyAsync(
                    new ReactionCallbackData(
                        "",
                        new EmbedBuilder
                {
                    Description =
                        "This game's Result has already been set to:\n"
                        + $"{game.Result.ToString()}\n"
                        + "Please react with ☑ To Still modify the result and update scores\n"
                        + "Or react with 🇽 to cancel this command"
                }.Build()).WithCallback(new Emoji("☑"),
                                        (c, r) => GameManagement.GameResultAsync(Context, game, result))
                    .WithCallback(new Emoji("🇽"), (c, r) => SimpleEmbedAsync("Canceled Game Result")));
            }
            else
            {
                await GameManagement.GameResultAsync(Context, game, result);
            }
        }
        public async Task GameResultAsync(ulong lobbyChannel, int gameNumber, GuildModel.GameResult._Result result)
        {
            if (!Context.Server.Settings.GameSettings.AllowUserSubmissions)
            {
                throw new Exception("Users are not allowed to self submit game results in this server");
            }

            var selectedGame = Context.Server.Results.FirstOrDefault(x => x.LobbyID == lobbyChannel && x.GameNumber == gameNumber);

            if (selectedGame == null)
            {
                throw new Exception("Game Unavailable. Incorrect Data.");
            }

            if (selectedGame.Result != GuildModel.GameResult._Result.Undecided)
            {
                throw new Exception("Game must be undecided to submit player chosen result.");
            }

            if (result == GuildModel.GameResult._Result.Undecided)
            {
                throw new Exception("You cannot set the result to undecided");
            }

            if (selectedGame.Team1.Contains(Context.User.Id))
            {
                if (selectedGame.Proposal.P1 == 0)
                {
                    selectedGame.Proposal.P1 = Context.User.Id;
                    selectedGame.Proposal.R1 = result;
                }
                else
                {
                    throw new Exception("A player has already submitted a result from this team.");
                }
            }
            else if (selectedGame.Team2.Contains(Context.User.Id))
            {
                if (selectedGame.Proposal.P2 == 0)
                {
                    selectedGame.Proposal.P2 = Context.User.Id;
                    selectedGame.Proposal.R2 = result;
                }
                else
                {
                    throw new Exception("A player has already submitted a result from this team.");
                }
            }
            else
            {
                throw new Exception("You must be on either team to submit the game result.");
            }

            Context.Server.Save();
            await SimpleEmbedAsync("Result Proposal\n" +
                                   $"Team1 Submission: {selectedGame.Proposal.R1} Player: {Context.Guild.GetUser(selectedGame.Proposal.P1)?.Mention ?? "N/A"}\n" +
                                   $"Team2 Submission: {selectedGame.Proposal.R2} Player: {Context.Guild.GetUser(selectedGame.Proposal.P2)?.Mention ?? "N/A"}");

            if (selectedGame.Proposal.R1 == GuildModel.GameResult._Result.Undecided || selectedGame.Proposal.R2 == GuildModel.GameResult._Result.Undecided)
            {
                return;
            }

            if (selectedGame.Proposal.R1 == selectedGame.Proposal.R2)
            {
                await GameManagement.GameResultAsync(Context, selectedGame, result);
            }
            else
            {
                throw new Exception("Mismatched Game Result Proposals. Please allow an admin to manually submit a result");
            }
        }
 public Task GameResultAsync(ITextChannel channel, int gameNumber, GuildModel.GameResult._Result result)
 {
     return(GameResultAsync(channel.Id, gameNumber, result));
 }
 public Task GameResultAsync(int gameNumber, GuildModel.GameResult._Result result)
 {
     return(GameResultAsync(Context.Channel.Id, gameNumber, result));
 }
Beispiel #5
0
        public static async Task GameResultAsync(Context context, GuildModel.GameResult game, GuildModel.GameResult._Result result)
        {
            try
            {
                var gameObject = context.Server.Results.FirstOrDefault(x => x.LobbyID == game.LobbyID && x.GameNumber == game.GameNumber);
                if (result == GuildModel.GameResult._Result.Canceled)
                {
                    gameObject.Result = result;
                    context.Server.Save();
                    await context.Channel.SendMessageAsync("", false, new EmbedBuilder
                    {
                        Color       = Color.DarkOrange,
                        Description = "Success! Game has been canceled."
                    }.Build());

                    return;
                }

                var userList = new List <ulong>();
                userList.AddRange(game.Team1);
                userList.AddRange(game.Team2);
                var winEmbed = new EmbedBuilder
                {
                    Color = Color.Green
                };
                var loseEmbed = new EmbedBuilder
                {
                    Color = Color.Red
                };
                foreach (var userID in userList)
                {
                    var user = context.Server.Users.FirstOrDefault(x => x.UserID == userID);
                    if (user == null)
                    {
                        continue;
                    }

                    var maxRank = UserManagement.MaxRole(context, user);
                    if ((result == GuildModel.GameResult._Result.Team1 && game.Team1.Contains(user.UserID)) || (result == GuildModel.GameResult._Result.Team2 && game.Team2.Contains(user.UserID)))
                    {
                        user.Stats.Points += maxRank.WinModifier;
                        user.Stats.Wins++;
                        winEmbed.AddField($"{user.Username} (+{maxRank.WinModifier})", $"Points: {user.Stats.Points}\n" +
                                          $"Wins: {user.Stats.Wins}");
                    }
                    else
                    {
                        user.Stats.Points -= maxRank.LossModifier;
                        if (user.Stats.Points < 0)
                        {
                            if (!context.Server.Settings.GameSettings.AllowNegativeScore)
                            {
                                user.Stats.Points = 0;
                            }
                        }

                        user.Stats.Losses++;

                        loseEmbed.AddField($"{user.Username} (-{maxRank.LossModifier})", $"Points: {user.Stats.Points}\n" +
                                           $"Losses: {user.Stats.Losses}");
                    }

                    var rename = Task.Run(() => UserManagement.UserRenameAsync(context, user));
                    var role   = Task.Run(() => UserManagement.UpdateUserRanksAsync(context, user));
                    user.Stats.GamesPlayed++;
                }

                gameObject.Result = result;
                context.Server.Save();
                await context.Channel.SendMessageAsync("", false, winEmbed.Build());

                await context.Channel.SendMessageAsync("", false, loseEmbed.Build());
            }
            catch (Exception e)
            {
                LogHandler.LogMessage(context, e.ToString(), LogSeverity.Error);
            }
        }