Esempio n. 1
0
        public async Task <IActionResult> JoinRoom([FromBody] RoomJoin roomJoin)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var gameRoom = _gameRooms.GameRoomsList.Find(r => r.RoomID == roomJoin.RoomId);

            if (gameRoom == null)
            {
                return(NotFound("Game room could not be found"));
            }
            if (gameRoom.IsRoomFull())
            {
                return(NotFound("Game room is full"));
            }

            Battleships_MBernackiUser user = await GetCurrentUserAsync();

            int playerRoomKey = gameRoom.AddPlayer(user.UserName, user.Id);

            return(Ok(new JoinedRoomInfo()
            {
                PlayerRoomKey = playerRoomKey,
                RoomID = gameRoom.RoomID,
                OponentName = gameRoom.PlayersNames[0],
                RoomName = gameRoom.RoomName,
                ShipsList = gameRoom.ShipList,
                MapSize = gameRoom.MapSize
            }));
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateRoom([FromBody] RoomCreation roomCreation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            short mapSize = 6;

            short[] shipList = new short[] { 0, 2, 0, 0 };


            if (roomCreation.MapSize != null && roomCreation.MapSize >= 3 && roomCreation.MapSize <= 8)
            {
                mapSize = roomCreation.MapSize;
            }

            bool flag = true;

            if (roomCreation.ShipsList.Length != 4)
            {
                flag = false;
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {
                    if (roomCreation.ShipsList[i] < 0 || roomCreation.ShipsList[i] >= (6 - i))
                    {
                        flag = false;
                    }
                }
            }

            if (flag)
            {
                shipList = roomCreation.ShipsList;
            }


            GameRoom newGameRoom = new GameRoom(_gameRooms.GenerateRoomId(), roomCreation.RoomName, mapSize, shipList);

            Battleships_MBernackiUser user = await GetCurrentUserAsync();

            int playerRoomKey = newGameRoom.AddPlayer(user.UserName, user.Id);

            _gameRooms.GameRoomsList.Add(newGameRoom);


            return(Ok(new JoinedRoomInfo()
            {
                PlayerRoomKey = playerRoomKey,
                RoomID = newGameRoom.RoomID,
                OponentName = "",
                RoomName = newGameRoom.RoomName,
                ShipsList = shipList,
                MapSize = mapSize
            }));
        }
Esempio n. 3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new Battleships_MBernackiUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }