public IHttpActionResult Status(GameParamModel gameModel) { var currentUserId = this.userInfoProvider.GetUserId(); var idAsGuid = new Guid(gameModel.GameId); var game = this.Data.Games.All() .Where(x => x.GameId == idAsGuid) .Select(x => new { x.FirstPlayerId, x.SecondPlayerId }) .FirstOrDefault(); if (game == null) { return(this.NotFound()); } if (game.FirstPlayerId != currentUserId && game.SecondPlayerId != currentUserId) { return(this.BadRequest("This is not your game!")); } var gameInfo = this.Data.Games.All() .Where(g => g.GameId == idAsGuid) .Select(g => new GameInfoDataModel() { Board = g.Board, Id = g.GameId, State = g.State, FirstPlayerName = g.FirstPlayer.UserName, SecondPlayerName = g.SecondPlayer.UserName, }) .FirstOrDefault(); return(this.Ok(gameInfo)); }
public IHttpActionResult Join(GameParamModel gameModel) { var currentUserId = this.userInfoProvider.GetUserId(); var game = this.Data.Games .All() .Where(g => g.GameId.ToString() == gameModel.GameId && g.State == GameState.WaitingForSecondPlayer && g.FirstPlayerId != currentUserId) .FirstOrDefault(); if (game == null) { return(this.NotFound()); } game.SecondPlayerId = currentUserId; game.State = this.GetRandomPlayerTurn(); this.Data.SaveChanges(); return(this.Ok()); }