public IHttpActionResult Join(string id, PlayerRequestDataModel model) { var currentUserId = this.userIdProvider.GetUserId(); var bluePlayer = this.data.Users.Find(currentUserId); var game = this.data.Games .All() .Where(g => g.GameState == GameState.WaitingForOpponent && g.RedPlayerId != currentUserId) .FirstOrDefault(); if (game == null) { return(NotFound()); } var number = model.Number; if (!IsValudNumber(number)) { return(BadRequest("Invalid number")); } game.BluePlayerId = currentUserId; game.BluePlayer = bluePlayer; game.BluePlayerNumber = number; game.GameState = (GameState)random.Next(1, 3); var notification = new Notification { Message = game.BluePlayer.UserName + " joined your game \"" + game.Name + "\"", DateCreated = DateTime.Now, Type = NotificationType.GameJoined, State = NotificationState.Unread, GameId = game.Id, ApplicationUserId = game.RedPlayer.Id, }; game.RedPlayer.Notifications.Add(notification); this.data.SaveChanges(); var result = new JoinResponseDataModel(game.Name); return(Ok(result)); }
public IHttpActionResult Guess(int id, PlayerRequestDataModel request) { var currentUserId = this.userIdProvider.GetUserId(); var player = this.data.Users.Find(currentUserId); if (request == null || !ModelState.IsValid) { return(this.BadRequest(ModelState)); } var game = this.data.Games.Find(id); if (game == null) { return(this.BadRequest("Invalid game id")); } if (game.RedPlayerId != currentUserId && game.BluePlayerId != currentUserId) { return(this.BadRequest("This is not your game!")); } if (game.GameState != GameState.BlueInTurn && game.GameState != GameState.RedInTurn) { return(this.BadRequest("Invalid game state!")); } if ((game.GameState == GameState.RedInTurn && game.RedPlayerId != currentUserId) || (game.GameState == GameState.BlueInTurn && game.BluePlayerId != currentUserId)) { return(this.BadRequest("It's not your turn!")); } var number = request.Number; if (!IsValudNumber(number)) { return(BadRequest("Invalid number")); } var opponentNumber = game.RedPlayerId == currentUserId ? game.BluePlayerNumber : game.RedPlayerNumber; var guessResult = this.resultValidator.GetResult(request.Number, opponentNumber); if (game.RedPlayerId == currentUserId) { if (guessResult.BullsCount == 4) { game.GameState = GameState.WonByRed; game.BluePlayer.UserRank += 15; game.RedPlayer.UserRank += 100; } else { game.GameState = GameState.BlueInTurn; } } else if (game.BluePlayerId == currentUserId) { if (guessResult.BullsCount == 4) { game.GameState = GameState.WonByBlue; game.BluePlayer.UserRank += 100; game.RedPlayer.UserRank += 15; } else { game.GameState = GameState.RedInTurn; } } var guess = new Guess { BullsCount = guessResult.BullsCount, CowsCount = guessResult.CowsCount, DateMade = DateTime.Now, GameId = game.Id, Game = game, Number = number, UserId = currentUserId, UserName = player.UserName, }; game.Guesses.Add(guess); if (game.GameState == GameState.WonByBlue) { var notificationBlue = new Notification { Message = "You beat " + game.RedPlayer.UserName + "in game \"" + game.Name + "\"", Type = NotificationType.GameWon, DateCreated = DateTime.Now, ApplicationUserId = game.BluePlayer.Id, State = NotificationState.Unread, GameId = game.Id }; var notificationRed = new Notification { Message = game.BluePlayer.UserName + " beat you in game \"" + game.Name + "\"", Type = NotificationType.GameLost, DateCreated = DateTime.Now, ApplicationUserId = game.RedPlayer.Id, State = NotificationState.Unread, GameId = game.Id }; game.RedPlayer.Notifications.Add(notificationRed); game.BluePlayer.Notifications.Add(notificationBlue); } else if (game.GameState == GameState.WonByRed) { var notificationBlue = new Notification { Message = game.BluePlayer.UserName + " beat you in game \"" + game.Name + "\"", Type = NotificationType.GameLost, DateCreated = DateTime.Now, ApplicationUserId = game.BluePlayer.Id, State = NotificationState.Unread, GameId = game.Id }; var notificationRed = new Notification { Message = "You beat " + game.BluePlayer.UserName + "in game \"" + game.Name + "\"", Type = NotificationType.GameWon, DateCreated = DateTime.Now, ApplicationUserId = game.RedPlayer.Id, State = NotificationState.Unread, GameId = game.Id }; game.RedPlayer.Notifications.Add(notificationRed); game.BluePlayer.Notifications.Add(notificationBlue); } else if (game.GameState == GameState.RedInTurn) { var notification = new Notification { Message = "Is your turn in game \"" + game.Name + "\"", Type = NotificationType.YourTurn, DateCreated = DateTime.Now, ApplicationUserId = game.RedPlayer.Id, State = NotificationState.Unread, GameId = game.Id }; game.RedPlayer.Notifications.Add(notification); } else if (game.GameState == GameState.BlueInTurn) { var notification = new Notification { Message = "Is your turn in game \"" + game.Name + "\"", Type = NotificationType.YourTurn, DateCreated = DateTime.Now, ApplicationUserId = game.BluePlayer.Id, State = NotificationState.Unread, GameId = game.Id }; game.BluePlayer.Notifications.Add(notification); } this.data.SaveChanges(); var result = new PlayerGuessResponseDataModel(guess); return(Ok(result)); }