Refactor storages and extractors to actual functions

This commit is contained in:
2025-10-10 23:15:00 +02:00
parent 42ba6160bf
commit 6dbd136cd4
2 changed files with 220 additions and 114 deletions

View File

@@ -222,18 +222,6 @@ func (rh *RouteHandler) handlePlanetsFull(ctx *fasthttp.RequestCtx) {
}
// ExtractorInfo represents extractor information
type ExtractorInfo struct {
PlanetName string `json:"planet_name"`
ExtractorNumber int `json:"extractor_number"`
ExpiryDate string `json:"expiry_date"`
}
// StorageInfo represents storage information
type StorageInfo struct {
PlanetName string `json:"planet_name"`
StorageType string `json:"storage_type"`
Utilization float64 `json:"utilization"`
}
// handleGetExtractors returns extractor information for a character
func (rh *RouteHandler) handleGetExtractors(ctx *fasthttp.RequestCtx) {
@@ -249,55 +237,15 @@ func (rh *RouteHandler) handleGetExtractors(ctx *fasthttp.RequestCtx) {
return
}
// Get planets
planets, err := rh.ESI.GetCharacterPlanets(context.Background(), int(char.ID), char.AccessToken)
// Get extractors using the standalone function
extractors, err := GetExtractorsForCharacter(rh.ESI, int(char.ID), char.AccessToken)
if err != nil {
logger.Error("Failed to get planets for character %s: %v", characterName, err)
logger.Error("Failed to get extractors for character %s: %v", characterName, err)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString("Failed to get planets")
ctx.SetBodyString("Failed to get extractors")
return
}
var extractors []ExtractorInfo
// Get details for each planet and extract extractor info
for _, planet := range planets {
details, err := rh.ESI.GetPlanetDetails(context.Background(), int(char.ID), planet.PlanetID, char.AccessToken)
if err != nil {
logger.Warning("Failed to fetch details for planet %d: %v", planet.PlanetID, err)
continue
}
if details != nil {
// Get planet name from universe endpoint
planetNameData, err := rh.ESI.GetPlanetName(context.Background(), planet.PlanetID)
if err != nil {
logger.Error("Failed to get planet name for planet ID %d: %v", planet.PlanetID, err)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString("Failed to get planet name")
return
}
// Count extractors and get expiry dates
extractorCount := 0
for _, pin := range details.Pins {
if pin.ExtractorDetails != nil {
extractorCount++
expiryDate := "N/A"
if pin.ExpiryTime != nil {
expiryDate = *pin.ExpiryTime
}
extractors = append(extractors, ExtractorInfo{
PlanetName: planetNameData.Name,
ExtractorNumber: extractorCount,
ExpiryDate: expiryDate,
})
}
}
}
}
// Return extractors as JSON
ctx.SetContentType("application/json")
ctx.SetStatusCode(fasthttp.StatusOK)
@@ -318,69 +266,15 @@ func (rh *RouteHandler) handleGetStorage(ctx *fasthttp.RequestCtx) {
return
}
// Get planets
planets, err := rh.ESI.GetCharacterPlanets(context.Background(), int(char.ID), char.AccessToken)
// Get storage using the standalone function
storage, err := GetStorageForCharacter(rh.ESI, int(char.ID), char.AccessToken)
if err != nil {
logger.Error("Failed to get planets for character %s: %v", characterName, err)
logger.Error("Failed to get storage for character %s: %v", characterName, err)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString("Failed to get planets")
ctx.SetBodyString("Failed to get storage")
return
}
var storage []StorageInfo
// Get details for each planet and extract storage info
for _, planet := range planets {
details, err := rh.ESI.GetPlanetDetails(context.Background(), int(char.ID), planet.PlanetID, char.AccessToken)
if err != nil {
logger.Warning("Failed to fetch details for planet %d: %v", planet.PlanetID, err)
continue
}
if details != nil {
// Get planet name from universe endpoint
planetNameData, err := rh.ESI.GetPlanetName(context.Background(), planet.PlanetID)
if err != nil {
logger.Error("Failed to get planet name for planet ID %d: %v", planet.PlanetID, err)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString("Failed to get planet name")
return
}
// Analyze storage utilization
for _, pin := range details.Pins {
if len(pin.Contents) > 0 {
// Calculate utilization based on contents
totalAmount := 0
for _, content := range pin.Contents {
totalAmount += content.Amount
}
// Determine storage type based on pin type
storageType := "Unknown"
switch pin.TypeID {
case 2254, 2255, 2256: // Launch pads
storageType = "Launch Pad"
case 2524, 2525, 2526: // Storage facilities
storageType = "Storage"
}
// Calculate utilization percentage (assuming max capacity of 10000)
utilization := float64(totalAmount) / 10000.0 * 100.0
if utilization > 100.0 {
utilization = 100.0
}
storage = append(storage, StorageInfo{
PlanetName: planetNameData.Name,
StorageType: storageType,
Utilization: utilization,
})
}
}
}
}
// Return storage as JSON
ctx.SetContentType("application/json")
ctx.SetStatusCode(fasthttp.StatusOK)