Пример #1
0
        public void OnPost()
        {
            // Overwrite current rules
            foreach (var key in Request.Form.Keys)
            {
                // Get value for current key
                Request.Form.TryGetValue(key, out var strVal);

                // Skip empty fields
                if (string.IsNullOrWhiteSpace(strVal) || string.IsNullOrEmpty(strVal))
                {
                    continue;
                }

                // Skip non-integer parameters
                if (!int.TryParse(key, out var intKey))
                {
                    continue;
                }

                if (!int.TryParse(strVal, out var intVal))
                {
                    StatusMsg = "Non-integer rules!";
                    IsError   = true;
                    return;
                }

                // Update static rule context
                ActiveGame.TryChangeRule((RuleType)intKey, intVal);
            }

            IsStatus          = true;
            StatusMsg         = "Rules saved!";
            IsDisplayContinue = true;
        }
Пример #2
0
        public static void ChangeRuleValue(RuleType ruleType)
        {
            var rule = ActiveGame.GetRule(ruleType);

            var menu = new Menu {
                Title     = $"Change rule {rule.RuleType}",
                MenuTypes = new List <MenuType> {
                    MenuType.Input, MenuType.RuleIntInput
                },
                MenuItems = new List <MenuItem> {
                    new MenuItem {
                        Description = $"Current value: {rule.Value}"
                    },
                    new MenuItem {
                        Description = $"Enter a value between {rule.MinVal} and {rule.MaxVal}"
                    }
                }
            };

            while (true)
            {
                // Run menu, ask user for integer input
                var input = menu.RunMenu();

                // User entered exit shortcut
                if (input.ToUpper() == MenuInitializers.GoBackItem.Shortcut)
                {
                    return;
                }

                // Attempt to parse input as int
                if (string.IsNullOrEmpty(input) || !int.TryParse(input, out var value))
                {
                    Console.WriteLine("Value not an integer!");
                    Console.ReadKey(true);
                    continue;
                }

                if (!ActiveGame.TryChangeRule(ruleType, value))
                {
                    Console.WriteLine("Value not in range!");
                    Console.ReadKey(true);
                    continue;
                }

                Console.WriteLine("Rule value changed!");
                Console.ReadKey(true);
                break;
            }
        }
Пример #3
0
        public static void Load(int gameId)
        {
            using (var ctx = new AppDbContext()) {
                // Reset current game to initial state
                ActiveGame.Init();

                // Find the DAL game
                var dalGame = ctx.Games.Find(gameId);

                // Load game data into static context
                ActiveGame.Players      = DomainConverter.GetAndConvertPlayers(ctx, gameId);
                ActiveGame.Moves        = DomainConverter.GetAndConvertMoves(ctx, gameId, ActiveGame.Players);
                ActiveGame.Winner       = ActiveGame.Players.FirstOrDefault(player => player.Name.Equals(dalGame.Winner));
                ActiveGame.RoundCounter = dalGame.RoundCounter;
                ActiveGame.GameId       = dalGame.Id;

                // Load rules into static context
                var dalRules = ctx.Rules.Where(rule => rule.Game.Id == gameId);
                foreach (var dalRule in dalRules)
                {
                    ActiveGame.TryChangeRule((RuleType)dalRule.RuleType, dalRule.Value);
                }
            }
        }