public IHttpActionResult Create(GameModel model) { var userId = this.User.Identity.GetUserId(); var redPlayer = this.data.Users.Find(userId); var newGame = new Game { Name = model.Name, DateCreated = DateTime.Now, RedPlayerId = userId, RedPlayer = redPlayer, //BluePlayerId = null, RedNumber = model.Number, //BlueNumber = null, State = GameState.WaitingForOpponent }; this.data.Games.Add(newGame); this.data.SaveChanges(); // Make new Guess on creating the game this.Guess(newGame.Id, new GuessModel { Number = newGame.RedNumber }); var gameModel = new GameModel(newGame); gameModel.Red = redPlayer.UserName; // Send notification to the creator of the game string notificationMessage = "You have created new game with Name: " + newGame.Name; SendNotification(newGame.Id, userId, "GameCreated", notificationMessage); return Ok(gameModel); }
public IHttpActionResult Join(int id, GameModel model) { // MAKE SURE YOU ARE NOT THE CREATOR OF THE GAME! var currentUserId = this.User.Identity.GetUserId(); var game = this.data.Games .All() .Where(g => g.State == GameState.WaitingForOpponent && g.RedPlayerId != currentUserId) .FirstOrDefault(); if (game == null) { return NotFound(); } var bluePlayer = this.data.Users.Find(currentUserId); game.BluePlayerId = currentUserId; game.BluePlayer = bluePlayer; game.State = GameState.TurnBlue; this.data.SaveChanges(); this.Guess(game.Id, new GuessModel { Number = model.Number }); var joinGameResultModel = new JoinGameModel(game.Name); // Send notification to the creator of the game about new player Joining his game string notificationMessage = bluePlayer.UserName + " joined your game " + game.Name; SendNotification(game.Id, currentUserId, "GameJoined", notificationMessage); return Ok(joinGameResultModel); }