97 lines
3.4 KiB
Go
97 lines
3.4 KiB
Go
package content
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// Sets are immutable authoring building blocks. Consumers receive expanded
|
|
// values at publication; earned awards and acquired items never hold live refs.
|
|
type RequirementSet struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Requirements []TitleRequirement `json:"requirements"`
|
|
}
|
|
type EffectSet struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Modifiers []StatModifier `json:"modifiers"`
|
|
}
|
|
|
|
func ValidRequirement(r TitleRequirement) bool {
|
|
return (KnownStat(r.Metric) || slices.Contains([]string{"level", "kills", "worldKills", "bossKills", "worldsSurvived", "gathering", "crafting", "combat", "deaths"}, r.Metric)) && r.AtLeast >= 1 && r.AtLeast <= 1000000000
|
|
}
|
|
|
|
func validSetIdentity(id, name, description string) bool {
|
|
return titleID.MatchString(id) && len(strings.TrimSpace(name)) > 0 && len(name) <= 80 && len(description) <= 500
|
|
}
|
|
|
|
func (c Catalog) validateLibrary() error {
|
|
if len(c.RequirementSets) > 256 || len(c.EffectSets) > 256 {
|
|
return fmt.Errorf("too many reusable sets")
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, set := range c.RequirementSets {
|
|
if !validSetIdentity(set.ID, set.Name, set.Description) || seen[set.ID] || len(set.Requirements) < 1 || len(set.Requirements) > 8 {
|
|
return fmt.Errorf("invalid or duplicate requirement set %q", set.ID)
|
|
}
|
|
seen[set.ID] = true
|
|
metrics := map[string]bool{}
|
|
for _, r := range set.Requirements {
|
|
if !ValidRequirement(r) || metrics[r.Metric] {
|
|
return fmt.Errorf("invalid or repeated metric in requirement set %q", set.ID)
|
|
}
|
|
metrics[r.Metric] = true
|
|
}
|
|
}
|
|
seen = map[string]bool{}
|
|
for _, set := range c.EffectSets {
|
|
if !validSetIdentity(set.ID, set.Name, set.Description) || seen[set.ID] || len(set.Modifiers) < 1 {
|
|
return fmt.Errorf("invalid or duplicate effect set %q", set.ID)
|
|
}
|
|
seen[set.ID] = true
|
|
if err := ValidateModifiers(set.Modifiers); err != nil {
|
|
return fmt.Errorf("effect set %q: %w", set.ID, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ResolveSets is also used by publication previews. References cannot recurse:
|
|
// sets contain only concrete predicates/modifiers, never other set references.
|
|
func (c Catalog) ResolveSets(requirements, effects []string) ([]TitleRequirement, []StatModifier, error) {
|
|
if len(requirements) > 8 || len(effects) > 8 {
|
|
return nil, nil, fmt.Errorf("choose at most eight sets of each kind")
|
|
}
|
|
var req []TitleRequirement
|
|
var mods []StatModifier
|
|
seen := map[string]bool{}
|
|
for _, id := range requirements {
|
|
if seen[id] {
|
|
return nil, nil, fmt.Errorf("repeated requirement set %q", id)
|
|
}
|
|
seen[id] = true
|
|
index := slices.IndexFunc(c.RequirementSets, func(s RequirementSet) bool { return s.ID == id })
|
|
if index < 0 {
|
|
return nil, nil, fmt.Errorf("unknown requirement set %q", id)
|
|
}
|
|
req = append(req, c.RequirementSets[index].Requirements...)
|
|
}
|
|
seen = map[string]bool{}
|
|
for _, id := range effects {
|
|
if seen[id] {
|
|
return nil, nil, fmt.Errorf("repeated effect set %q", id)
|
|
}
|
|
seen[id] = true
|
|
index := slices.IndexFunc(c.EffectSets, func(s EffectSet) bool { return s.ID == id })
|
|
if index < 0 {
|
|
return nil, nil, fmt.Errorf("unknown effect set %q", id)
|
|
}
|
|
mods = append(mods, c.EffectSets[index].Modifiers...)
|
|
}
|
|
return req, mods, nil
|
|
}
|