Example #1
0
        public IHttpActionResult Join(int id, GameCreateModel model)
        {
            var game = data.Games.All().FirstOrDefault(g => g.Id == id);
            if (game == null)
            {
                return BadRequest("The game doesn't exist!");
            }

            var userID = this.User.Identity.GetUserId();
            if (game.RedPlayerId == userID || game.BluePlayerId == userID)
            {
                return BadRequest("You have already joined this game!");
            }

            game.BlueUserNumber = model.Number;
            game.BluePlayerId = userID;
            game.GameState = random.Next(1, 3) == 1 ? GameStateEnum.RedInTurn : GameStateEnum.BlueInTurn;

            this.data.Games.Update(game);
            this.data.Games.SaveChanges();
            var result = new ResultModel
            {
                Result = string.Format("You joined game \"{0}\"", game.Name)
            };

            return Ok(result);
        }
Example #2
0
        public IHttpActionResult Create(GameCreateModel model)
        {
            if (!IsValidUserNumber(model.Number))
            {
                return BadRequest("The number must be 4 didgits long and all didgits must be unique!");
            }
            GameDataModel viewModel = new GameDataModel();
            var userID = this.User.Identity.GetUserId();
            var userName = this.User.Identity.GetUserName();

            var game = new Game
            {
                RedUserNumber = model.Number,
                Name = model.Name,
                GameState = GameStateEnum.WaitingForOpponent,
                RedPlayerId = userID,
                DateCreated = DateTime.Now,
                //RedUserNumber=model.RedUserNumber
            };
            this.data.Games.Add(game);
            this.data.Games.SaveChanges();

            // TODO moove to method
            viewModel.Name = model.Name;
            viewModel.Id = game.Id;
            viewModel.GameState = game.GameState.ToString();
            viewModel.Red = userName;
            viewModel.Blue = "No blue player yet";
            viewModel.DateCreated = game.DateCreated;

            return Ok(viewModel);
        }