You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

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<Game> GameList = new List<Game>();
private readonly ILogger<ApiController> _logger;
public ApiController(ILogger<ApiController> 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<Witch> h_CreateWitches(int count)
{
List<Witch> witches = new List<Witch>();
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
}