public IHttpActionResult MakeGuess(int id, GuessModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (model.Number.ToString().Length != 4) { return this.BadRequest("Please enter number with 4 digits"); } if (ContainsDuplicates(model.Number)) { return this.BadRequest("Your number should consist of 4 different digits"); } var currentGame = this.ApplicationData.Games.Find(id); if (currentGame.isFinished) { return this.BadRequest("The game has already finished"); } var currentUserId = this.User.Identity.GetUserId(); var currentUserColor = currentGame.BluePlayer.Id == currentUserId ? "blue" : "red"; if (currentGame.BluePlayer.Id != currentUserId && currentGame.RedPlayer.Id != currentUserId) { return this.BadRequest("You must be either blue or red player in this game to see details"); } if (currentGame.GameState == GameState.BlueInTurn) { if (currentUserColor == "red") { return this.BadRequest("It is not your turn"); } } if (currentGame.GameState == GameState.RedInTurn) { if (currentUserColor == "blue") { return this.BadRequest("It is not your turn"); } } var guessedNumber = model.Number; int? enemyPlayerNumber = currentUserColor == "blue" ? currentGame.RedPlayerNumber : currentGame.BluePlayerNumber; var cows = this.CheckForCows(guessedNumber, enemyPlayerNumber); var bulls = this.CheckForBulls(guessedNumber, enemyPlayerNumber); var guess = new Guess { UserId = currentUserId, GameId = currentGame.Id, Number = model.Number, DateMade = DateTime.Now, CowsCount = cows, BullsCount = bulls }; if (currentUserColor == "blue") { currentGame.BluePlayerGuesses.Add(guess); if (bulls == 4) { currentGame.isFinished = true; currentGame.BluePlayer.WinsCount++; currentGame.RedPlayer.LossesCount--; currentGame.BluePlayer.Rank += 100; currentGame.RedPlayer.Rank += 15; currentGame.GameState = GameState.Finished; } else { currentGame.GameState = GameState.RedInTurn; } } else { currentGame.RedPlayerGuesses.Add(guess); if (bulls == 4) { currentGame.isFinished = true; currentGame.RedPlayer.WinsCount++; currentGame.BluePlayer.LossesCount--; currentGame.RedPlayer.Rank += 100; currentGame.BluePlayer.Rank += 15; currentGame.GameState = GameState.Finished; } else { currentGame.GameState = GameState.BlueInTurn; } } var addedGuess = this.ApplicationData.Guesses.Add(guess); this.ApplicationData.SaveChanges(); model.Id = addedGuess.Id; model.UserId = addedGuess.UserId; model.Username = this.User.Identity.Name; model.GameId = addedGuess.GameId; model.DateMade = addedGuess.DateMade; model.CowsCount = addedGuess.CowsCount; model.BullsCount = addedGuess.BullsCount; return Ok(model); }
public IHttpActionResult MakeGuess(int id, MakeGuessModel guessMade) { // Check if the number is valid...no need to continue if it's not; if (!IsValidNumber(guessMade.Number)) { return this.BadRequest("Invalid Number"); } // Get the game var currentGame = this.data.Games.Find(id); if (currentGame == null) { return this.BadRequest(string.Format(INVALID_ITEM_FORMAT, "game")); } // Check if game can be played if (currentGame.GameState != GameState.InProgress) { return this.BadRequest("This game is not in progress! You can't play in this game!"); } // Get the user var userId = this.userIdProvider.GetUserId(); var currentUser = this.data.Users.Find(userId); if (currentUser == null) { return this.BadRequest(string.Format(INVALID_ITEM_FORMAT, "user")); } // Check if the user is in this game if (currentUser.UserName != currentGame.Red.UserName && currentUser.UserName != currentGame.Blue.UserName) { return this.BadRequest("This is not your game!"); } // Process turn if (currentGame.PlayerInTurn == PlayerInTurn.Red) { if (currentUser.UserName != currentGame.Red.UserName) { return this.BadRequest("It's not your turn!"); } var result = CalculateGuessResult(guessMade.Number, currentGame.BluePlayerNumber); if (result.BullsCount == 4) { // Finish the game currentGame.GameState = GameState.Finished; // Notifcation for both players // Blue var notificationForBlue = new Notification() { Message = string.Format("{0} beat you in game \"{1}\"", currentUser.UserName, currentGame.Name), Game = currentGame, Type = NotificationType.GameLost, User = currentGame.Blue, DateCreated = DateTime.Now, State = NotificationState.Unread }; // Calculate Rank currentGame.Blue.Losses++; currentGame.Blue.Rank = (currentGame.Blue.Wins * 100) + (currentGame.Blue.Losses * 15); // Red var notificationForRed = new Notification() { Message = string.Format("You beat {0} in game \"{1}\"", currentGame.Blue.UserName, currentGame.Name), Game = currentGame, Type = NotificationType.GameWon, User = currentGame.Red, DateCreated = DateTime.Now, State = NotificationState.Unread }; // Calculate Rank currentGame.Red.Wins++; currentGame.Red.Rank = (currentGame.Red.Wins * 100) + (currentGame.Red.Losses * 15); this.data.Notifications.Add(notificationForBlue); this.data.Notifications.Add(notificationForRed); this.data.SaveChanges(); } else { // Change Turn Notification var notification = new Notification() { Message = string.Format("It is your turn in game \"{0}!\"", currentGame.Name), Game = currentGame, Type = NotificationType.YourTurn, User = currentGame.Blue, DateCreated = DateTime.Now, State = NotificationState.Unread }; this.data.Notifications.Add(notification); // Change turn currentGame.PlayerInTurn = PlayerInTurn.Blue; this.data.SaveChanges(); } var guessToSave = new Guess() { GuessNumber = guessMade.Number, User = currentUser, ForGame = currentGame, BullsCount = result.BullsCount, CowsCount = result.CowsCount, DateMade = DateTime.Now, }; this.data.Guesses.Add(guessToSave); this.data.SaveChanges(); var guessToReturn = new GuessModel() { UserName = currentUser.UserName, BullsCount = result.BullsCount, CowsCount = result.CowsCount, UserId = currentUser.Id, GameId = currentGame.Id, DateMade = DateTime.Now, Number = guessMade.Number }; return this.Ok(guessToReturn); } else { if (currentUser.UserName != currentGame.Blue.UserName) { return this.BadRequest("It's not your turn!"); } var result = CalculateGuessResult(guessMade.Number, currentGame.RedPlayerNumber); if (result.BullsCount == 4) { // Finish the game currentGame.GameState = GameState.Finished; // Notifcation for both players // Blue var notificationForBlue = new Notification() { Message = string.Format("{0} beat you in game \"{1}\"", currentUser.UserName, currentGame.Name), Game = currentGame, Type = NotificationType.GameLost, User = currentGame.Red, DateCreated = DateTime.Now, State = NotificationState.Unread }; // Calculate Rank currentGame.Red.Losses++; currentGame.Red.Rank = (currentGame.Red.Wins * 100) + (currentGame.Red.Losses * 15); // Red var notificationForRed = new Notification() { Message = string.Format("You beat {0} in game \"{1}\"", currentGame.Red.UserName, currentGame.Name), Game = currentGame, Type = NotificationType.GameWon, User = currentGame.Blue, DateCreated = DateTime.Now, State = NotificationState.Unread }; // Calculate Rank currentGame.Blue.Wins++; currentGame.Blue.Rank = (currentGame.Blue.Wins * 100) + (currentGame.Blue.Losses * 15); this.data.Notifications.Add(notificationForBlue); this.data.Notifications.Add(notificationForRed); this.data.SaveChanges(); } else { // Change Turn Notification var notification = new Notification() { Message = string.Format("It is your turn in game \"{0}!\"", currentGame.Name), Game = currentGame, Type = NotificationType.YourTurn, User = currentGame.Red, DateCreated = DateTime.Now, State = NotificationState.Unread }; this.data.Notifications.Add(notification); // Change turn currentGame.PlayerInTurn = PlayerInTurn.Red; this.data.SaveChanges(); } var guessToSave = new Guess() { GuessNumber = guessMade.Number, User = currentUser, ForGame = currentGame, BullsCount = result.BullsCount, CowsCount = result.CowsCount, DateMade = DateTime.Now, }; this.data.Guesses.Add(guessToSave); this.data.SaveChanges(); var guessToReturn = new GuessModel() { UserName = currentUser.UserName, BullsCount = result.BullsCount, CowsCount = result.CowsCount, UserId = currentUser.Id, GameId = currentGame.Id, DateMade = DateTime.Now, Number = guessMade.Number }; return this.Ok(guessToReturn); } }