Speeding up visible location call
Deploy Promiscuity Auth API / deploy (push) Successful in 46s
Deploy Promiscuity Character API / deploy (push) Successful in 57s
Deploy Promiscuity Inventory API / deploy (push) Successful in 59s
Deploy Promiscuity Locations API / deploy (push) Successful in 58s
k8s smoke test / test (push) Successful in 8s
Deploy Promiscuity Auth API / deploy (push) Successful in 46s
Deploy Promiscuity Character API / deploy (push) Successful in 57s
Deploy Promiscuity Inventory API / deploy (push) Successful in 59s
Deploy Promiscuity Locations API / deploy (push) Successful in 58s
k8s smoke test / test (push) Successful in 8s
This commit is contained in:
@@ -91,6 +91,7 @@ public class LocationsController : ControllerBase
|
||||
return BadRequest("radius must be non-negative");
|
||||
|
||||
var result = await _locations.GetOrCreateVisibleLocationsAsync(req.X, req.Y, req.Radius);
|
||||
await PopulateFloorInventoriesAsync(result);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
@@ -367,4 +368,54 @@ public class LocationsController : ControllerBase
|
||||
}
|
||||
|
||||
private static string NormalizeBiomeKey(string biomeKey) => biomeKey.Trim().ToLowerInvariant();
|
||||
|
||||
private async Task PopulateFloorInventoriesAsync(VisibleLocationWindowResponse result)
|
||||
{
|
||||
var locationIds = result.Locations
|
||||
.Where(location => !string.IsNullOrWhiteSpace(location.Id))
|
||||
.Select(location => location.Id!)
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToList();
|
||||
if (locationIds.Count == 0)
|
||||
return;
|
||||
|
||||
var inventoryBaseUrl = (_configuration["Services:InventoryApiBaseUrl"] ?? "http://localhost:5003").TrimEnd('/');
|
||||
var internalApiKey = (_configuration["InternalApi:Key"] ?? _configuration["Jwt:Key"] ?? string.Empty).Trim();
|
||||
var body = JsonSerializer.Serialize(new { ownerIds = locationIds });
|
||||
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, $"{inventoryBaseUrl}/api/inventory/internal/by-owner/location");
|
||||
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
|
||||
request.Headers.Add("X-Internal-Api-Key", internalApiKey);
|
||||
|
||||
using var response = await client.SendAsync(request);
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Failed to load batched floor inventories from InventoryApi. Status={StatusCode} Body={Body}",
|
||||
(int)response.StatusCode,
|
||||
responseBody);
|
||||
return;
|
||||
}
|
||||
|
||||
var parsed = JsonSerializer.Deserialize<List<OwnerInventorySummaryEnvelope>>(
|
||||
responseBody,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? [];
|
||||
var byOwnerId = parsed.ToDictionary(entry => entry.OwnerId, entry => entry.Items, StringComparer.Ordinal);
|
||||
|
||||
foreach (var location in result.Locations)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(location.Id))
|
||||
continue;
|
||||
location.FloorItems = byOwnerId.GetValueOrDefault(location.Id!, []);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class OwnerInventorySummaryEnvelope
|
||||
{
|
||||
public string OwnerId { get; set; } = string.Empty;
|
||||
|
||||
public List<FloorInventoryItemResponse> Items { get; set; } = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace LocationsApi.Models;
|
||||
|
||||
public class FloorInventoryItemResponse
|
||||
{
|
||||
public string ItemId { get; set; } = string.Empty;
|
||||
|
||||
public string ItemKey { get; set; } = string.Empty;
|
||||
|
||||
public int Quantity { get; set; }
|
||||
|
||||
public int? Slot { get; set; }
|
||||
}
|
||||
@@ -11,4 +11,6 @@ public class VisibleLocationResponse
|
||||
public string BiomeKey { get; set; } = "plains";
|
||||
|
||||
public VisibleLocationObjectResponse? LocationObject { get; set; }
|
||||
|
||||
public List<FloorInventoryItemResponse> FloorItems { get; set; } = [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user