public Guess MakeGuess(string number, int gameId) { var game = this.games.GetById(gameId); var playersNumber = game.GameState == GameState.RedTurn ? game.RedPlayerNumber : game.BluePlayerNumber; var player = game.GameState == GameState.RedTurn ? game.RedPlayerId : game.BluePlayerId; var guessToMake = new Guess { MadeOn = DateTime.UtcNow, UserId = player, Number = number, Bulls = CalculatBulls(number, playersNumber), Cows = CalculateCows(number, playersNumber) }; this.guesses.Add(guessToMake); //((guessToMake.Bulls == 4 // ? (Action)(() => game.State = GameState.Finished) // : () => { game.State = game.State == GameState.RedTurn ? GameState.BlueTurn : GameState.RedTurn; }))(); if(guessToMake.Bulls == 4) { game.GameState = GameState.Finished; game.Result = game.GameState == GameState.RedTurn ? GameResult.WonByRed : GameResult.WonByBlue; } else { game.GameState = game.GameState == GameState.RedTurn ? GameState.BlueTurn : GameState.RedTurn; } game.Guesses.Add(guessToMake); this.games.Update(game); this.games.SaveChanges(); this.guesses.SaveChanges(); // TODO: Add player.AddGuess(); && game.AddGuess(); return guessToMake; }
public Guess MakeGuess(int gameId, string number, string userId) { var game = this.games .GetGameDetails(gameId) .Select(g => new { g.Id, g.GameState, g.BlueUserNumber, g.RedUserNumber }) .FirstOrDefault(); string correctNumber = null; if (game.GameState == GameState.BlueInTurn) { correctNumber = game.RedUserNumber; } else if(game.GameState == GameState.RedInTurn) { correctNumber = game.BlueUserNumber; } var cows = this.GetCows(number, correctNumber); var bulls = this.GetBulls(number, correctNumber); var newGuess = new Guess { GameId = gameId, Number = number, UserId = userId, DateMade = DateTime.UtcNow, BullsCount = bulls, CowsCount = cows }; this.guesses.Add(newGuess); this.guesses.SaveChanges(); this.games.ChangeGameState(gameId, bulls == GameConstants.GuessNumberLength); return newGuess; }