Allowing player to perform action

This commit is contained in:
hz
2025-06-09 09:28:24 -05:00
parent d16ecac876
commit e1b489dbc8
4 changed files with 103 additions and 0 deletions
+47
View File
@@ -275,6 +275,53 @@ public class PlayerController : ControllerBase
return Ok($"Player '{player.Name}' moved {direction} to '{targetLocation.X}, {targetLocation.Y}'.");
}
[HttpPost("action")]
[Authorize(Roles = "USER,SUPER")]
public async Task<IActionResult> PerformAction([FromBody] PerformActionRequest req)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var player = await _playerService.GetByUserIdAsync(userId);
if (player == null)
return NotFound("Player not found.");
if (string.IsNullOrWhiteSpace(player.LocationId))
return BadRequest("Player is not currently in a location.");
if (string.IsNullOrWhiteSpace(req.Action))
return BadRequest("Action is required.");
var location = await _locationService.GetByIdAsync(player.LocationId);
if (location == null)
return BadRequest("Current location not found.");
foreach (var itemId in location.Content)
{
var item = await _itemService.GetByIdAsync(itemId);
if (item == null)
continue;
var spawnable = await _spawnableService.GetByNameAsync(item.Name.ToLower());
if (spawnable == null)
continue;
if (!string.Equals(spawnable.CollectAction, req.Action, StringComparison.OrdinalIgnoreCase))
continue;
var roll = Random.Shared.Next(0, 100);
var cumulative = 0;
foreach (var outcome in spawnable.CollectResults)
{
cumulative += outcome.SuccessRate;
if (roll < cumulative)
return Ok(outcome.Result);
}
return Ok("Nothing happens.");
}
return Ok("Nothing happens.");
}
private static readonly Dictionary<string, (int dx, int dy, string reverse)> DirectionData = new()
{
{ "N", (0, 1, "S") }, { "NE", (1, 1, "SW") }, { "E", (1, 0, "W") }, { "SE", (1, -1, "NW") },