Esempio n. 1
0
        public void OnGet()
        {
            // Players have not been defined yet
            if (ActiveGame.Players == null || ActiveGame.Players.Count == 0)
            {
                IsInvalidAccess = true;
                return;
            }

            // There's no current player so set it
            if (ActiveGame.CurrentPlayer == null || ActiveGame.NextPlayer == null)
            {
                ActiveGame.InitPlayerPointers();

                // Just to get rid of IDE warnings
                if (ActiveGame.CurrentPlayer == null || ActiveGame.NextPlayer == null)
                {
                    throw new NullReferenceException();
                }
            }

            // Players haven't placed ships yet
            if (ActiveGame.CurrentPlayer.Ships.FirstOrDefault(ship => ship.IsPlaced) == null)
            {
                IsInvalidAccess = true;
                return;
            }

            if (Request.Query.ContainsKey("save"))
            {
                GameSaver.Save();
                IsStatus  = true;
                StatusMsg = "Game saved!";
                return;
            }

            // There's a winner
            if (ActiveGame.TrySetWinner())
            {
                IsWinner  = true;
                StatusMsg = $"The winner of the game is {ActiveGame.Winner.Name}!";
                return;
            }

            MainTitle       = $"{ActiveGame.CurrentPlayer.Name} is attacking {ActiveGame.NextPlayer.Name}";
            IsDisplayBoards = true;
        }
        public void OnPost()
        {
            IsStatus           = true;
            ActiveGame.Players = new List <Player>();

            // Reset last game state
            ActiveGame.Init();

            // Loop through each form param
            foreach (var key in Request.Form.Keys)
            {
                // Only check names
                if (!key.Contains("name-"))
                {
                    continue;
                }

                // Get name
                Request.Form.TryGetValue(key, out var names);
                var name = names.ToString();

                // Check input validity
                if (!InputValidator.CheckValidPlayerName(name))
                {
                    IsError   = true;
                    StatusMsg = "Invalid player name!";
                    return;
                }

                ActiveGame.Players.Add(new Player(name, ShipLogic.GenGameShipList()));
            }

            // Set pointers
            ActiveGame.InitPlayerPointers();

            StatusMsg = "Players created!";
        }