dorfylegends/analyze/df/generate.go

90 lines
1.8 KiB
Go
Raw Normal View History

2022-04-14 18:39:18 +03:00
package df
import (
2022-04-15 16:27:51 +03:00
"sort"
2022-04-14 18:39:18 +03:00
2022-04-15 16:27:51 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
2022-04-14 18:39:18 +03:00
)
type Object struct {
Name string `json:"name"`
Id bool `json:"id,omitempty"`
Named bool `json:"named,omitempty"`
Typed bool `json:"typed,omitempty"`
2022-04-15 16:27:51 +03:00
SubTypes *[]Subtype `json:"subtypes,omitempty"`
2022-04-14 18:39:18 +03:00
SubTypeOf *string `json:"subtypeof,omitempty"`
2022-04-19 14:25:04 +03:00
SubType *string `json:"subtype,omitempty"`
2022-04-14 18:39:18 +03:00
Fields map[string]Field `json:"fields"`
}
2022-04-15 16:27:51 +03:00
type Subtype struct {
Name string `json:"name"`
BaseType string `json:"base"`
PlusType string `json:"plus"`
}
2022-04-14 18:39:18 +03:00
type Field struct {
Name string `json:"name"`
Type string `json:"type"`
Multiple bool `json:"multiple,omitempty"`
ElementType *string `json:"elements,omitempty"`
Legend string `json:"legend"`
2022-04-15 16:27:51 +03:00
Base bool
Plus bool
2022-04-18 14:39:28 +03:00
EnumValues *[]string
2022-04-14 18:39:18 +03:00
}
2022-04-15 16:27:51 +03:00
func (f Field) Active(plus bool) bool {
if plus && f.Plus {
return true
}
if !plus && f.Base {
return true
}
return false
}
type ActiveSubType struct {
Case string
Name string
Options []string
}
func (f Object) ActiveSubTypes(plus bool) []*ActiveSubType {
subs := make(map[string]*ActiveSubType)
for _, s := range *f.SubTypes {
if !plus && s.BaseType == "" {
continue
}
if plus && s.PlusType == "" {
continue
}
a := ActiveSubType{}
if plus {
a.Case = s.PlusType
} else {
a.Case = s.BaseType
}
a.Name = f.Name + s.Name
a.Options = append(a.Options, a.Name)
if sub, ok := subs[a.Case]; ok {
sub.Options = append(sub.Options, a.Name)
} else {
subs[a.Case] = &a
}
}
list := util.Values(subs)
2022-04-19 14:25:04 +03:00
for _, a := range list {
sort.Strings(a.Options)
}
2022-04-15 16:27:51 +03:00
sort.SliceStable(list, func(i, j int) bool {
return list[i].Case < list[j].Case
})
return list
}