public GameSpinDto Spin(int gameId, int bet, int numberOfScratchCards) { GameSpinDto result = new GameSpinDto(); GameConfigurationDto gameConfiguration = this.GetGameConfiguration(gameId); if (gameConfiguration.MaxNumberOfScratchCards < numberOfScratchCards) { throw new ArgumentException("Max number of scratch cards exceeded."); } if (gameConfiguration == null) { throw new ArgumentException("Game not found."); } this.SelectWheelSigns(ref result, gameConfiguration); this.SelectScratchCardsSigns(ref result, gameConfiguration, numberOfScratchCards); foreach (ScratchCardDto scratchCard in result.ScratchCards) { scratchCard.Factor = this.CalculatePrizeFactor(bet, result.WheelSignIds, scratchCard.SignIds, gameConfiguration); scratchCard.Prize = scratchCard.Factor * bet; } result.Prize = result.ScratchCards.Sum(sc => sc.Prize); return(result); }
private void SelectScratchCardsSigns(ref GameSpinDto gameSpin, GameConfigurationDto gameConfiguration, int numberOfScratchCards) { Random randomizer = new Random(); gameSpin.ScratchCards = new ScratchCardDto[numberOfScratchCards]; for (int i = 0; i < numberOfScratchCards; i++) { List <SignDto> signs = gameConfiguration.Signs.ToList(); ScratchCardDto scratchCard = new ScratchCardDto { SignIds = new int[gameConfiguration.SignsNumberOnScratchCard] }; for (int j = 0; j < gameConfiguration.SignsNumberOnScratchCard; j++) { int randomIndex = randomizer.Next(0, signs.Count); scratchCard.SignIds[j] = signs[randomIndex].Id; if (!gameConfiguration.ScratchCardSignsCanRepeat) { signs.RemoveAt(randomIndex); } } gameSpin.ScratchCards[i] = scratchCard; } }
public IActionResult Spin(int gameId, [FromForm] GameSpinRequest request) { if (request == null) { return(this.BadRequest("Request data should be provided.")); } IActionResult result = null; try { GameSpinDto res = this.gameManager.Spin(gameId, request.Bet, request.NumberOfScratchCards); GameSpinResponse response = new GameSpinResponse { Prize = res.Prize, WheelSignIds = res.WheelSignIds, ScratchCards = res.ScratchCards.Select(sc => new GameSpinResponse.ScratchCard { Prize = sc.Prize, SignIds = sc.SignIds, Factor = sc.Factor }).ToArray() }; result = this.Ok(response); } catch (ArgumentException ex) { result = this.BadRequest(ex.Message); } return(result); }
private void SelectWheelSigns(ref GameSpinDto gameSpin, GameConfigurationDto gameConfiguration) { Random randomizer = new Random(); gameSpin.WheelSignIds = new int[gameConfiguration.SignsNumberOnWheel]; List <SignDto> signs = gameConfiguration.Signs.Where(s => !s.Special).ToList(); for (int i = 0; i < gameConfiguration.SignsNumberOnWheel; i++) { int randomIndex = randomizer.Next(0, signs.Count); gameSpin.WheelSignIds[i] = signs[randomIndex].Id; signs.RemoveAt(randomIndex); } }