Пример #1
0
        private static DiceModel ParseDice(string msg)
        {
            var model = new DiceModel();

            if (msg.Contains("+"))
            {
                msg = CheckModify(msg, model);
            }

            return(!msg.Contains("d") ? null : CheckD(msg, model));
        }
Пример #2
0
        private static DiceResultModel ConsoleDice(DiceModel model)
        {
            var result = new DiceResultModel
            {
                Modify = model.Modify,
                Result = new List <int>()
            };

            for (var i = 0; i < model.Count; i++)
            {
                var value = Rander.RandInt(model.Size) + 1;
                result.Result.Add(value);
            }

            return(result);
        }
Пример #3
0
        private static string CheckModify(string msg, DiceModel model)
        {
            if (msg.Count(p => p == '+') > 1)
            {
                return(msg);
            }

            var strs1 = msg.Split(new[] { '+' }, StringSplitOptions.RemoveEmptyEntries);

            if (strs1.Length != 2)
            {
                return(msg);
            }

            if (!int.TryParse(strs1[1], out var modify) || modify <= 0)
            {
                return(msg);
            }
            model.Modify = modify;
            return(strs1[0]);
        }
Пример #4
0
        private static DiceModel CheckD(string msg, DiceModel model)
        {
            if (msg.Count(p => p == 'd') > 1)
            {
                return(null);
            }

            var strs = msg.Split(new[] { 'd' }, StringSplitOptions.RemoveEmptyEntries);

            if (strs.Length != 1 && strs.Length != 2)
            {
                return(null);
            }

            if (strs.Length == 1)
            {
                if (!int.TryParse(strs[0], out var size) || size <= 0)
                {
                    return(null);
                }
                model.Size  = size;
                model.Count = 1;

                return(model);
            }
            else
            {
                if (!int.TryParse(strs[0], out var count) ||
                    !int.TryParse(strs[1], out var size) ||
                    count <= 0 ||
                    size <= 0)
                {
                    return(null);
                }
                model.Size  = size;
                model.Count = count;

                return(model);
            }
        }