Updating location schema to tracker user territory and lock visibility

This commit is contained in:
hz
2026-05-18 13:59:33 -05:00
parent dd151c5af1
commit afeb9cf625
23 changed files with 590 additions and 38 deletions
@@ -17,6 +17,7 @@ public class LocationsController : ControllerBase
_locations = locations;
}
[Authorize(Roles = "USER,SUPER")]
[HttpGet]
public async Task<ActionResult<List<LocationResponse>>> GetAll(
[FromQuery] int? minQ,
@@ -24,6 +25,25 @@ public class LocationsController : ControllerBase
[FromQuery] int? minR,
[FromQuery] int? maxR)
{
if (!User.IsInRole("SUPER"))
{
var username = User.FindFirstValue(ClaimTypes.Name);
if (string.IsNullOrWhiteSpace(username))
{
return Unauthorized();
}
var visible = await _locations.GetVisibleAsync(username);
if (minQ.HasValue && maxQ.HasValue && minR.HasValue && maxR.HasValue)
{
visible = visible
.Where(x => x.Q >= minQ.Value && x.Q <= maxQ.Value && x.R >= minR.Value && x.R <= maxR.Value)
.ToList();
}
return Ok(visible);
}
var locations = minQ.HasValue && maxQ.HasValue && minR.HasValue && maxR.HasValue
? await _locations.GetRangeAsync(minQ.Value, maxQ.Value, minR.Value, maxR.Value)
: await _locations.GetAllAsync();
@@ -31,9 +51,22 @@ public class LocationsController : ControllerBase
return Ok(locations.Select(LocationResponse.FromLocation).ToList());
}
[Authorize(Roles = "USER,SUPER")]
[HttpGet("{q:int}/{r:int}")]
public async Task<ActionResult<LocationResponse>> Get(int q, int r)
{
if (!User.IsInRole("SUPER"))
{
var username = User.FindFirstValue(ClaimTypes.Name);
if (string.IsNullOrWhiteSpace(username))
{
return Unauthorized();
}
var visibleLocation = await _locations.GetVisibleLocationAsync(username, q, r);
return visibleLocation is null ? NotFound() : Ok(visibleLocation);
}
var location = await _locations.GetAsync(q, r);
return location is null ? NotFound() : Ok(LocationResponse.FromLocation(location));
}
@@ -48,10 +81,61 @@ public class LocationsController : ControllerBase
return Unauthorized();
}
if (!User.IsInRole("SUPER") && !await _locations.CanSeeAsync(username, request.Q, request.R))
{
return NotFound();
}
var location = await _locations.ClaimAsync(request.Q, request.R, username);
if (location is null)
{
return Conflict("Location is already controlled by another player.");
}
return Ok(LocationResponse.FromLocation(location));
}
[Authorize(Roles = "USER,SUPER")]
[HttpGet("home")]
public async Task<ActionResult<LocationResponse>> GetHome()
{
var username = User.FindFirstValue(ClaimTypes.Name);
if (string.IsNullOrWhiteSpace(username))
{
return Unauthorized();
}
var home = await _locations.GetHomeAsync(username);
return home is null ? NotFound() : Ok(LocationResponse.FromLocation(home));
}
[Authorize(Roles = "USER,SUPER")]
[HttpPost("home")]
public async Task<ActionResult<LocationResponse>> AssignHome()
{
var username = User.FindFirstValue(ClaimTypes.Name);
if (string.IsNullOrWhiteSpace(username))
{
return Unauthorized();
}
var home = await _locations.AssignHomeAsync(username);
return Ok(LocationResponse.FromLocation(home));
}
[Authorize(Roles = "USER,SUPER")]
[HttpGet("visible")]
public async Task<ActionResult<List<LocationResponse>>> GetVisible([FromQuery] int? depth)
{
var username = User.FindFirstValue(ClaimTypes.Name);
if (string.IsNullOrWhiteSpace(username))
{
return Unauthorized();
}
return Ok(await _locations.GetVisibleAsync(username, depth));
}
[Authorize(Roles = "SUPER")]
[HttpPut]
public async Task<ActionResult<LocationResponse>> Upsert([FromBody] UpsertLocationRequest request)