public async Task RollInitiative()
        {
            try
            {
                var caller = Context.Guild.GetUser(Context.Message.Author.Id);

                List<ulong> players = caller.VoiceChannel.Users.Where(u => !u.IsBot).Select(u => u.Id).ToList();

                players.Remove(caller.Id);

                SortedDictionary<ulong, int> playerScores = new SortedDictionary<ulong, int>();

                List<string> failedInitiative = new List<string>();

                foreach (var player in players)
                {
                    var attributeResponse = CharacterService.GetAttribute(player, "Init");

                    if (!attributeResponse.Success)
                    {
                        await ReplyAsync($"`could not retrieve {Context.Guild.GetUser(player).Username} initiative. Error: {attributeResponse.Error}");
                        continue;
                    }

                    if (!int.TryParse(attributeResponse.Value, out int init))
                    {
                        failedInitiative.Add(Context.Guild.GetUser(player).Mention);
                    }
                    playerScores.Add(player, DiceService.RollExactDice("1d20").Sum() + init);
                }

                if (failedInitiative.Any())
                {
                    await ReplyAsync($"Players not registered: {string.Join(',', failedInitiative)}");
                }

                string response = string.Empty;
                foreach (var player in playerScores.OrderByDescending(x => x.Value))
                {
                    response += $"`[{Context.Guild.GetUser(player.Key).Username}]` : {player.Value}\r\n";
                }


                await ReplyAsync(response);
            }
            catch(Exception e)
            {
                string s = e.Message;
            }

            await Task.CompletedTask;
        }
Example #2
0
        private async Task RollDice(string diceString)
        {
            if (Context.Message.Author.Id != lastMessageSender)
            {
                await ReplyAsync("_ _");
            }

            lastMessageSender = Context.Message.Author.Id;

            var character      = CharacterService.GetCharacter(Context.Message.Author.Id);
            var editionService = EditionService[character != null ? character.Edition : "Default"];

            diceString = diceString.ToLower().Replace(" ", "");

            var displayEquationString = diceString;
            var equationString        = diceString;

            Match match = WordRegex.Match(equationString);

            while (match.Success)
            {
                var attributeResponse = CharacterService.GetAttribute(Context.Message.Author.Id, match.Value);

                if (!attributeResponse.Success)
                {
                    await ReplyAsync($"Error: {match.Value} - {attributeResponse.Error}");
                }

                var regex = new Regex(Regex.Escape(match.Value));
                displayEquationString = regex.Replace(displayEquationString, $"[{match.Value} = {attributeResponse.Value}]", 1);
                equationString        = regex.Replace(equationString, attributeResponse.Value, 1);

                match = WordRegex.Match(equationString);
            }

            // if no diceroll, add 1d20
            if (!DiceRegex.Match(equationString).Success)
            {
                equationString        = editionService.DefaultRoll(equationString);
                displayEquationString = editionService.DefaultRoll(displayEquationString);
            }

            match = DiceRegex.Match(equationString);
            while (match.Success)
            {
                var rolls = DiceService.RollExactDice(match.Value);

                var regex = new Regex(Regex.Escape(match.Value));
                displayEquationString = regex.Replace(displayEquationString, $"[{match.Value} = {string.Join(", ", rolls.Select(n => n.ToString()).ToArray())}]", 1);

                equationString = regex.Replace(equationString, editionService.ParseRoll(rolls, match.Value), 1);

                match = DiceRegex.Match(equationString);
            }

            var username = Context.Message.Author.Mention;

            var result = editionService.CompleteRoll(equationString);

            await ReplyAsync($"{username} **{CleanUp(diceString)}** roll: \r\n`{displayEquationString}` results in... **{result}**");
        }