73 lines
1.7 KiB
Go

package components
import (
"dk/internal/auth"
"dk/internal/helpers"
"dk/internal/models/spells"
"dk/internal/models/towns"
"dk/internal/models/users"
"dk/internal/router"
)
// LeftAside generates the data map for the left sidebar.
// Returns an empty map when not auth'd.
func LeftAside(ctx router.Ctx) map[string]any {
data := map[string]any{}
if !auth.IsAuthenticated(ctx) {
return data
}
user := ctx.UserValue("user").(*users.User)
// Build owned town maps list
if user.Towns != "" {
townMap := helpers.NewOrderedMap[int, *towns.Town]()
for _, id := range user.GetTownIDs() {
if town, err := towns.Find(id); err == nil {
townMap.Set(id, town)
}
}
data["_towns"] = townMap.ToSlice()
}
return data
}
// RightAside generates the data map for the right sidebar.
// Returns an empty map when not auth'd.
func RightAside(ctx router.Ctx) map[string]any {
data := map[string]any{}
if !auth.IsAuthenticated(ctx) {
return data
}
user := ctx.UserValue("user").(*users.User)
hpPct := helpers.ClampPct(float64(user.HP), float64(user.MaxHP), 0, 100)
data["hppct"] = hpPct
data["mppct"] = helpers.ClampPct(float64(user.MP), float64(user.MaxMP), 0, 100)
data["tppct"] = helpers.ClampPct(float64(user.TP), float64(user.MaxTP), 0, 100)
data["hpcolor"] = ""
if hpPct < 35 {
data["hpcolor"] = "danger"
} else if hpPct < 75 {
data["hpcolor"] = "warning"
}
// Build known healing spells list
if user.Spells != "" {
spellMap := helpers.NewOrderedMap[int, string]()
for _, id := range user.GetSpellIDs() {
if spell, err := spells.Find(id); err == nil {
spellMap.Set(id, spell.Name)
}
}
data["_spells"] = spellMap.ToSlice()
}
return data
}