using Microsoft.AspNetCore.Mvc; namespace ooya.ga_api.Controllers; [ApiController] [Microsoft.AspNetCore.Mvc.Route("/api/v1")] public class ApiController : ControllerBase { private static readonly Random Random = new Random(); private static readonly List GameList = new List(); private readonly ILogger _logger; public ApiController(ILogger logger) { _logger = logger; } [HttpGet("version", Name = "version", Order = 1)] public string Version() { return "1.0.0"; } [HttpGet("game", Name = "game", Order = 2)] public GameDto NewGame() { Game pGame = h_NewGame(0); GameList.Add(pGame); return Mapper.ToGameDto(pGame); } [HttpGet("turn", Name = "turn", Order = 3)] public GameDto? Turn( string gameGuid, string witchGuid ) { Game pGame = GameList.FirstOrDefault(p => p.Guid == gameGuid); if (pGame == null) return null; Witch pWitch = pGame.Witches.FirstOrDefault(p => p.Guid == witchGuid); if (pWitch == null) return null; if (pWitch.FlagReal) { Game pNewGame = h_NewGame(pGame.Score + pGame.Witches.Count()); GameList.Add(pNewGame); return Mapper.ToGameDto(pNewGame); } else { pGame.Score--; return Mapper.ToGameDto(pGame); } } #region private methods private Game h_NewGame(int iScore) { Game pGame = new Game(iScore) { Witches = h_CreateWitches(Random.Next(2, 5)) }; return pGame; } private IEnumerable h_CreateWitches(int count) { List witches = new List(); for (int ii = 0; ii < count; ii++) { witches.Add(new Witch(ii)); } int iIndex = Random.Next(0, count); witches[iIndex].FlagReal = true; return witches; } #endregion }