Ejemplo n.º 1
0
        public IHttpActionResult JoinGame(JoinGameBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest("You have to specify game id!"));
            }

            this._currentUserId = this._userIdProvider.GetUserId();
            var game = this._data.Games.Find(model.Id);

            if (this._currentUserId == game.FirstPlayerId)
            {
                return(this.BadRequest("You can't join your own game!"));
            }

            game.SecondPlayerId = _currentUserId;
            game.State          = GameState.FirstPlayerTurn;
            this._data.SaveChanges();

            JoinGameViewModel gameViewModel = new JoinGameViewModel()
            {
                Id               = game.Id,
                Name             = game.Name,
                FirstPlayerName  = game.FirstPlayer.UserName,
                SecondPlayerName = game.SecondPlayer.UserName,
                GameState        = GameState.FirstPlayerTurn.ToString(),
                Field            = game.Field
            };

            return(this.Ok(gameViewModel));
        }
Ejemplo n.º 2
0
        public PartialViewResult _joinGame(JoinGameViewModel JoinGameViewModel)
        {
            // find if there is exists a league in preparation with given league code
            League foundLeague = dbContext.Leagues.Where(l => l.Id == JoinGameViewModel.Id && l.State == LeagueState.Preparation)
                                 .FirstOrDefault();

            string response;

            if (foundLeague != null)
            {
                string userId = User.Identity.GetUserId();

                LeaguePlayer alreadyInLeague = dbContext.LeaguePlayer.Where(
                    lp => lp.League.Id == foundLeague.Id && lp.User.Id == userId).FirstOrDefault();

                if (alreadyInLeague != null)
                {
                    response = "You already belong to this league.";
                }
                else
                {
                    // league code is a match. Add the user to the league.
                    AddUserToLeague(foundLeague, User.Identity.GetUserId());

                    response = "Successfully added to league.";
                }
            }
            else
            {
                response = "League not found. Please try again.";
            }

            return(PartialView("_joinGameResponse", response));
        }
Ejemplo n.º 3
0
        public IHttpActionResult CreateGame(CreateGameBindingModel model)
        {
            this._currentUserId = this._userIdProvider.GetUserId();
            var theUser = this._data.Users.FirstOrDefault(u => u.Id == this._currentUserId);

            var newGame = new Game()
            {
                Name          = model.Name,
                FirstPlayerId = this._currentUserId,
                CreatedTime   = DateTime.Now,
                State         = GameState.WaitingForSecondPlayer
            };

            this._data.Games.Add(newGame);
            this._data.SaveChanges();

            JoinGameViewModel gameViewModel = new JoinGameViewModel()
            {
                Id              = newGame.Id,
                Name            = newGame.Name,
                FirstPlayerName = theUser.UserName,
                GameState       = GameState.WaitingForSecondPlayer.ToString(),
                Field           = newGame.Field
            };

            return(this.Ok(gameViewModel));
        }
        public async Task <IActionResult> JoinGame([Bind] JoinGameViewModel viewModel)
        {
            await _movementService.SpawnPlayer(viewModel.Username);

            var routeValues = new { nickname = viewModel.Username };

            return(RedirectToAction(nameof(GameController.Index), this.NameOf <GameController>(), routeValues));
        }
 public JoinGameView()
 {
     InitializeComponent();
     ViewModel = new JoinGameViewModel();
     //DataContext = ViewModel;
     SystemNavigationManager.GetForCurrentView().BackRequested += (s, e) => ViewModel?.LeaveGameView();
     SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
 }
Ejemplo n.º 6
0
        public async Task <ActionResult <GameUserModel> > Join(int id, [FromBody] JoinGameViewModel model, CancellationToken cancellationToken = default)
        {
            var user = await GetUserAsync(cancellationToken : cancellationToken);

            var result = await _gameService.AddUserToGameAsync(id, user.Id, model.DeckId, cancellationToken : cancellationToken);

            return(result);
        }
        public ActionResult Join()
        {
            var model = new JoinGameViewModel
            {
                Games = Helper.GetAllOpenGamesAsListItem()
                        //Colors = Helper.GetAvailableColors();
            };

            return(View("Join", model));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Index()
        {
            var vm          = new JoinGameViewModel();
            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            if (currentUser != null && currentUser.InGameName != null && !currentUser.InGameName.Contains("@") && !currentUser.InGameName.Equals(""))
            {
                vm.Username = currentUser.InGameName;
            }

            return(View(vm));
        }
Ejemplo n.º 9
0
        public IActionResult TestLobby()
        {
            if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") !=
                Environments.Development)
            {
                return(View("Join"));
            }

            var model = new JoinGameViewModel {
                Name = "Test Player"
            };

            return(Create(model));
        }
        public ActionResult Join(JoinGameViewModel model)
        {
            int gameId;

            if (int.TryParse(model.GameIdDD, out int gId))
            {
                gameId = gId;
            }
            else if (model.GameId != 0)
            {
                gameId = model.GameId;
            }
            else
            {
                return(RedirectToAction("Join"));
            }


            var game = GameHelper.AllGames[gameId];

            var name  = model.PlayerName;
            var email = model.PlayerEmail;
            var color = model.PlayerColor;

            var colorId = GameHelper.GetColorId(color);

            game.AddPlayer(name, email, colorId);

            CookieHelper.SetArrayCookieValue("Game", "Id", game.GameId.ToString());
            CookieHelper.SetArrayCookieValue("Game", "GId", game.GId.ToString());
            CookieHelper.SetArrayCookieValue("Game", "Players", game.NoPlayers.ToString());
            CookieHelper.SetArrayCookieValue("Player", "Id", colorId.ToString());
            CookieHelper.SetArrayCookieValue("Player", "Name", name);
            //model.Games = GameHelper.GetAllOpenGames();

            var gameModel = new GameModel
            {
                GameId = gameId,
                Game   = game
            };

            //UpdateBrickList(gameModel);

            //return RedirectToRoute("Game", new { id = game.GameId });
            return(RedirectToAction("Game", "Game", new { id = game.GameId }));

            //return View("Game",gameModel);
        }
Ejemplo n.º 11
0
        public IActionResult Create(JoinGameViewModel model)
        {
            if (!ModelState.IsValid || !model.IsValidCreate())
            {
                return(View("Join"));
            }

            var hostId  = Guid.NewGuid();
            var lobbyId = Guid.NewGuid();
            var lobby   = new Lobby(lobbyId, model.Name, hostId);

            Session.Guid(hostId);
            Session.LobbyController(lobby.Host);
            Lobbies[lobby.Id] = lobby;

            return(Lobby());
        }
Ejemplo n.º 12
0
        public IHttpActionResult GetGameByUsername(string username)
        {
            var searchedUser = this._data
                               .Users
                               .FirstOrDefault(u => u.Email == username);

            var game = new JoinGameViewModel()
            {
                Id               = searchedUser.Game.Id,
                Field            = searchedUser.Game.Field,
                FirstPlayerName  = searchedUser.Game.FirstPlayer.UserName,
                GameState        = searchedUser.Game.State.ToString(),
                Name             = searchedUser.Game.Name,
                SecondPlayerName = searchedUser.Game.SecondPlayer.UserName
            };

            return(this.Ok(game));
        }
Ejemplo n.º 13
0
        public IActionResult Join(JoinGameViewModel model)
        {
            if (!ModelState.IsValid ||
                !model.IsValidJoin() ||
                !Lobbies.TryGetValue(model.LobbyGuid(), out var lobby))
            {
                return(View("Join"));
            }

            var playerName = model.Name.Trim();
            var playerId   = Guid.NewGuid();
            var player     = lobby.Add(playerName, playerId);

            Session.Guid(playerId);
            Session.LobbyController(player);

            return(Lobby());
        }
Ejemplo n.º 14
0
        public ActionResult Join(JoinGameViewModel viewModel)
        {
            var game = _gameRepository.Get(viewModel.GameId);

            if (game == null)
            {
                return(HttpNotFound());
            }

            var player = _cookieManager.ExtractPlayerFromCookie(Request);

            if (!game.HasPlayer(player))
            {
                game.AddPlayer(player);
                game.StartGame();
                _gameRepository.Update(game);
            }

            return(RedirectToAction("Index", new { id = game.Id }));
        }
Ejemplo n.º 15
0
        public virtual async Task <ActionResult> JoinGame(JoinGameViewModel joinGameViewModel)
        {
            var gameInstance = _gameInstancesRepository
                               .GetAll()
                               .Include(_ => _.GameType)
                               .FirstOrDefault(x => x.Code == joinGameViewModel.GameCode);

            if (gameInstance == null)
            {
                return(Json(new AjaxResponse {
                    Error = new ErrorInfo(this.L("Game with this code does not exist!"))
                }));
            }

            return(RedirectToAction(gameInstance.GameType.GameName, "Game", new
            {
                gameCode = joinGameViewModel.GameCode,
                userName = joinGameViewModel.UserName
            }));
        }
Ejemplo n.º 16
0
        public ActionResult ConfirmJoin(string id)
        {
            var game = _gameRepository.Get(id);

            if (game == null)
            {
                return(HttpNotFound());
            }

            var player = _cookieManager.ExtractPlayerFromCookie(Request);

            if (game.HasPlayer(player))
            {
                return(RedirectToAction("Index", new { id = game.Id }));
            }

            var viewModel = new JoinGameViewModel {
                GameId = game.Id, Name = game.Name
            };

            return(View(viewModel));
        }
Ejemplo n.º 17
0
        public async Task <IActionResult> Join(JoinGameViewModel joinGameVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var game = await _context.Games.FirstOrDefaultAsync(x => x.GameId.Equals(joinGameVM.GameId));

                    if (game == null)
                    {
                        return(NotFound());
                    }

                    var user = new User()
                    {
                        Username = joinGameVM.Username,
                        GameId   = game.GameId
                    };

                    _context.Users.Add(user);
                    await _context.SaveChangesAsync();

                    var cookieOptions = new CookieOptions
                    {
                        Expires = DateTime.UtcNow.AddHours(1)
                    };

                    Response.Cookies.Append("user", user.ToJsonBase64String(), cookieOptions);
                    return(Ok(Url.Action("Start", "Game", new { id = game.GameId })));
                }

                return(PartialView("_JoinGamePartial", joinGameVM));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"An exception occurred in {MethodBase.GetCurrentMethod().Name}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Ejemplo n.º 18
0
        public async Task <IActionResult> Index(JoinGameViewModel vm)
        {
            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            if (vm.Username == null || vm.GameName == null || vm.Password == null || "".Equals(vm.Username) || "".Equals(vm.GameName) || "".Equals(vm.Password))
            {
                return(View(vm));
            }

            List <Review>    reviews = null;
            List <GameModel> games   = null;

            using (var db = new GameDbContext(((Server)_server).ConnectionString))
            {
                games   = db.Games.Where(g => !g.Private).ToList();
                reviews = db.Reviews.Include(r => r.Game).Where(r => !r.Game.Private).ToList();

                if (currentUser != null)
                {
                    games.AddRange(db.Games.Where(g => (g.Private && g.UserId.ToString().Equals(currentUser.Id))).ToList());
                    reviews.AddRange(db.Reviews.Include(r => r.Game).Where(r => (r.Game.Private && r.Game.UserId.ToString().Equals(currentUser.Id))).ToList());
                }
            }
            while (reviews == null || games == null)
            {
                ;
            }

            var revGames = reviews.Select(r => r.Game.Name).Distinct().ToList();
            var gamGames = games.Select(g => g.Name).Distinct().ToList();

            if (revGames.Count != gamGames.Count)
            {
                var diff = gamGames.Except(revGames).ToList();
                if (diff.Count > 0)
                {
                    foreach (var gn in diff)
                    {
                        reviews.Add(new Review
                        {
                            Game = new GameModel
                            {
                                Name        = gn,
                                Description = games.First(g => g.Name.Equals(gn)).Description
                            },
                            Comment = null
                        });
                    }
                }
            }

            vm.Username = RegExHelperExtensions.ReplaceNotMatching(vm.Username, "[a-z0-9]*", "", RegexOptions.IgnoreCase);
            vm.GameName = RegExHelperExtensions.ReplaceNotMatching(vm.GameName, "[a-z0-9]*", "", RegexOptions.IgnoreCase);
            vm.Password = RegExHelperExtensions.ReplaceNotMatching(vm.Password, "[a-z0-9]*", "", RegexOptions.IgnoreCase);
            vm.reviews  = reviews;

            if (currentUser == null)
            {
                return(View(vm));
            }

            currentUser.InGameName = vm.Username;
            await _userManager.UpdateAsync(currentUser);

            return(View(vm));
        }
Ejemplo n.º 19
0
 public JoinGamePage()
 {
     InitializeComponent();
     _vm            = App.IOC.JoinGame;
     BindingContext = _vm;
 }
Ejemplo n.º 20
0
        public IActionResult Join()
        {
            var joinGameVM = new JoinGameViewModel();

            return(PartialView("_JoinGamePartial", joinGameVM));
        }