Files
GaCy-GO/internal/sdkserver/api_command_completions.go

98 lines
3.0 KiB
Go

package sdkserver
import (
"encoding/json"
"fmt"
"gac-hkrpg-go/internal/common"
"net/http"
"sort"
)
type commandCompletion struct {
Value string `json:"value"`
Label string `json:"label"`
Plane uint32 `json:"plane,omitempty"`
Star uint32 `json:"star,omitempty"`
Avatar uint32 `json:"avatar,omitempty"`
}
// Read-only, resource-backed argument choices. No save or live run is changed.
func apiCommandCompletions(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
out := map[string][]commandCompletion{}
add := func(kind string, id uint32, label string) {
out[kind] = append(out[kind], commandCompletion{Value: fmt.Sprint(id), Label: label})
}
var issues []string
if raw, err := common.ReadResourceFile("resources/InventoryItemConfig.json"); err == nil {
var items struct {
IDs []uint32 `json:"item_ids"`
}
if err = json.Unmarshal(raw, &items); err == nil {
for _, id := range items.IDs {
add("items", id, fmt.Sprintf("Item %d", id))
}
} else {
issues = append(issues, err.Error())
}
} else {
issues = append(issues, err.Error())
}
if res, err := common.GameRes(); err == nil {
planes := map[uint32]bool{}
floors := map[[2]uint32]bool{}
for _, entry := range res.MapEntrances {
if !planes[entry.PlaneID] {
add("planes", entry.PlaneID, fmt.Sprintf("Plane %d", entry.PlaneID))
planes[entry.PlaneID] = true
}
key := [2]uint32{entry.PlaneID, entry.FloorID}
if !floors[key] {
out["floors"] = append(out["floors"], commandCompletion{Value: fmt.Sprint(entry.FloorID), Label: fmt.Sprintf("Floor %d", entry.FloorID), Plane: entry.PlaneID})
floors[key] = true
}
}
} else {
issues = append(issues, err.Error())
}
if catalog, err := common.LoadCurrencyWarsCatalog(); err == nil {
for _, v := range catalog.CapturedVariants {
out["cw_roles"] = append(out["cw_roles"], commandCompletion{Value: fmt.Sprint(v.Config.ID), Label: fmt.Sprintf("Role %d · %d★", v.Config.ID, v.Star), Star: v.Star, Avatar: v.Config.AvatarID})
}
if equipment, e := common.LoadCurrencyWarsEquipment(catalog); e == nil {
for id, row := range equipment {
add("cw_equip", id, row.AbilityName)
}
} else {
issues = append(issues, e.Error())
}
if rewards, e := common.LoadCurrencyWarsRewards(catalog); e == nil {
for id := range rewards.Orbs {
add("cw_orb", id, fmt.Sprintf("Orb %d", id))
}
for id := range rewards.Consumables {
add("cw_consumable", id, fmt.Sprintf("Consumable %d", id))
}
for id := range rewards.Forges {
add("cw_forge", id, fmt.Sprintf("Forge %d", id))
}
} else {
issues = append(issues, e.Error())
}
} else {
issues = append(issues, err.Error())
}
for key := range out {
sort.Slice(out[key], func(i, j int) bool {
if out[key][i].Value == out[key][j].Value {
return out[key][i].Star < out[key][j].Star
}
return out[key][i].Value < out[key][j].Value
})
}
writeJSON(w, map[string]any{"choices": out, "issues": issues})
}