dorfylegends/backend/model/extensions.go

324 lines
8.3 KiB
Go
Raw Permalink Normal View History

2022-04-16 23:12:23 +03:00
package model
2022-04-21 17:25:35 +03:00
import (
2023-11-17 16:50:14 +02:00
"encoding/json"
2022-04-21 17:25:35 +03:00
"fmt"
2022-05-02 18:22:57 +03:00
"html/template"
2022-04-23 22:38:03 +03:00
"sort"
2022-04-21 23:01:18 +03:00
"strings"
2022-04-21 17:25:35 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
2022-05-07 23:19:30 +03:00
"golang.org/x/exp/maps"
2022-04-21 17:25:35 +03:00
)
2022-04-19 12:46:43 +03:00
2022-04-26 10:24:16 +03:00
func (w *DfWorld) AllEventTypes() []string {
types := make(map[string]bool)
for _, e := range w.HistoricalEvents {
types[e.Details.Type()] = true
}
var list = util.Keys(types)
sort.Strings(list)
return list
}
func (w *DfWorld) EventsOfType(t string) any {
var list []*HistoricalEvent
for _, e := range w.HistoricalEvents {
if e.Details.Type() == t {
list = append(list, e)
}
}
sort.Slice(list, func(i, j int) bool { return list[i].Id_ < list[j].Id_ })
return struct {
Type string
Events []*HistoricalEvent
}{
Type: t,
Events: list,
}
}
func (w *DfWorld) EventsMatching(f func(HistoricalEventDetails) bool) []*HistoricalEvent {
var list []*HistoricalEvent
for _, e := range w.HistoricalEvents {
2023-06-13 00:12:43 +03:00
if e.Details != nil && f(e.Details) {
2022-04-26 10:24:16 +03:00
list = append(list, e)
}
}
sort.Slice(list, func(a, b int) bool { return list[a].Id_ < list[b].Id_ })
return list
}
2022-04-29 16:28:08 +03:00
func (w *DfWorld) SiteHistory(siteId int) []*HistoricalEvent {
var list []*HistoricalEvent
for _, e := range w.HistoricalEvents {
if e.Details.RelatedToSite(siteId) {
switch e.Details.(type) {
case *HistoricalEventCreatedSite, *HistoricalEventDestroyedSite, *HistoricalEventSiteTakenOver, *HistoricalEventHfDestroyedSite, *HistoricalEventReclaimSite:
list = append(list, e)
}
}
}
sort.Slice(list, func(a, b int) bool { return list[a].Id_ < list[b].Id_ })
return list
}
2022-05-07 23:19:30 +03:00
func (w *DfWorld) Races() []string {
races := make(map[string]bool)
for _, hf := range w.HistoricalFigures {
races[hf.Race] = true
}
list := maps.Keys(races)
sort.Strings(list)
return list
}
2022-05-03 15:59:47 +03:00
func (c *HistoricalEventCollection) Type() string {
if c.Details == nil {
return "unk"
}
return c.Details.Type()
}
func (e *HistoricalEventCollection) Html(c *Context) string {
if e.Details == nil {
return "unk"
}
return e.Details.Html(e, c)
}
2022-04-28 22:24:55 +03:00
func (e *Artifact) Type() string {
2022-04-29 12:31:05 +03:00
switch e.ItemSubtype {
case "scroll":
return "scroll"
}
switch e.ItemType {
case "weapon", "tool", "book", "slab":
return e.ItemType
case "armor", "shoe", "gloves", "helm", "pants":
return "armor"
default:
return "item"
}
2022-04-28 22:24:55 +03:00
}
2022-04-27 22:44:39 +03:00
func (e *Entity) Type() string {
return e.Type_.String()
}
2022-05-07 18:18:08 +03:00
func (x *Entity) Color() string {
c := ""
switch x.Race {
case "dwarf":
c = `#FFCC33`
case "elf":
c = `#99FF00`
case "human":
c = `#0000CC`
case "kobold":
c = `#333`
case "goblin":
c = `#CC0000`
}
if x.Necromancer {
c = `#A0A`
}
return c
}
2022-05-06 14:11:12 +03:00
func (e *Entity) Weapons() []string {
return util.Map(e.Weapon, func(w EntityWeapon) string { return w.String() })
}
2022-04-16 23:12:23 +03:00
func (e *Entity) Position(id int) *EntityPosition {
for _, p := range e.EntityPosition {
if p.Id_ == id {
return p
}
}
2022-04-20 00:54:29 +03:00
return &EntityPosition{Name_: "UNKNOWN POSITION"}
2022-04-16 23:12:23 +03:00
}
2022-05-04 12:42:02 +03:00
func (e *Entity) PositionByIndex(index int) *EntityPosition {
return e.EntityPosition[len(e.EntityPosition)-1-index]
}
2022-04-19 18:46:11 +03:00
func (p *EntityPosition) GenderName(hf *HistoricalFigure) string {
if hf.Female() && p.NameFemale != "" {
return p.NameFemale
} else if hf.Male() && p.NameMale != "" {
return p.NameMale
} else {
return p.Name_
}
}
func (hf *HistoricalFigure) Female() bool {
return hf.Sex == 0 || hf.Caste == "FEMALE"
}
func (hf *HistoricalFigure) Male() bool {
return hf.Sex == 1 || hf.Caste == "MALE"
}
2022-04-23 14:31:23 +03:00
func (hf *HistoricalFigure) Pronoun() string {
if hf.Female() {
return "she"
}
return "he"
}
func (hf *HistoricalFigure) PossesivePronoun() string {
if hf.Female() {
return "her"
}
return "his"
}
2022-04-23 00:57:10 +03:00
func (hf *HistoricalFigure) FirstName() string {
return strings.Split(hf.Name_, " ")[0]
}
2022-05-06 17:17:50 +03:00
func (hf *HistoricalFigure) Goals() string {
return andList(util.Map(hf.Goal, func(g HistoricalFigureGoal) string { return g.String() }))
}
func (hf *HistoricalFigure) Pets() string {
return andList(util.Map(hf.JourneyPet, func(g string) string { return articled(strings.ToLower(g)) }))
}
2022-04-26 10:24:16 +03:00
func (x *Honor) Requirement() string {
var list []string
if x.RequiresAnyMeleeOrRangedSkill {
list = append(list, "attaining sufficent skill with a weapon or technique")
2022-04-23 22:38:03 +03:00
}
2022-04-26 10:24:16 +03:00
if x.RequiredSkill != HonorRequiredSkill_Unknown {
list = append(list, "attaining enough skill with the "+x.RequiredSkill.String())
2022-04-23 22:38:03 +03:00
}
2022-04-26 10:24:16 +03:00
if x.RequiredBattles == 1 {
list = append(list, "serving in combat")
2022-04-23 22:38:03 +03:00
}
2022-04-26 10:24:16 +03:00
if x.RequiredBattles > 1 {
list = append(list, fmt.Sprintf("participating in %d battles", x.RequiredBattles))
2022-04-23 00:57:10 +03:00
}
2022-04-26 10:24:16 +03:00
if x.RequiredYears >= 1 {
list = append(list, fmt.Sprintf("%d years of membership", x.RequiredYears))
2022-04-22 13:34:42 +03:00
}
2022-04-26 10:24:16 +03:00
if x.RequiredKills >= 1 {
list = append(list, fmt.Sprintf("slaying %d enemies", x.RequiredKills))
2022-04-22 18:45:10 +03:00
}
2022-04-26 10:24:16 +03:00
return " after " + andList(list)
2022-04-23 00:57:10 +03:00
}
2022-04-28 22:24:55 +03:00
func (r *Region) Type() string {
return r.Type_.String()
}
func (s *Site) Type() string {
return s.Type_.String()
}
2022-04-29 15:21:27 +03:00
func (s *Structure) Type() string {
return s.Type_.String()
}
2022-04-28 22:24:55 +03:00
func (w *WorldConstruction) Type() string {
return w.Type_.String()
}
2022-04-26 10:24:16 +03:00
func (w *WrittenContent) Name() string {
return w.Title
2022-04-23 12:18:37 +03:00
}
2022-04-28 22:24:55 +03:00
func (w *WrittenContent) Type() string {
2022-05-06 16:40:45 +03:00
return w.Form.String()
2022-04-28 22:24:55 +03:00
}
func (w *DanceForm) Type() string {
return "dance form"
}
func (w *MusicalForm) Type() string {
return "musical form"
}
func (w *PoeticForm) Type() string {
return "poetic form"
}
2022-05-02 18:22:57 +03:00
func (r *Reference) Html(c *Context) template.HTML {
switch r.Type_ {
case ReferenceType_ABSTRACTBUILDING:
return template.HTML("a building")
case ReferenceType_ARTIFACT:
return template.HTML(c.artifact(r.Id_))
case ReferenceType_DANCEFORM:
return template.HTML(c.danceForm(r.Id_))
case ReferenceType_ENTITY:
return template.HTML(c.entity(r.Id_))
case ReferenceType_HISTORICALEVENT:
if e, ok := c.World.HistoricalEvents[r.Id_]; ok {
return template.HTML("how in " + Time(e.Year, e.Seconds72) + " " + e.Details.Html(c))
}
case ReferenceType_HISTORICALFIGURE:
return template.HTML(c.hf(r.Id_))
case ReferenceType_INTERACTION:
return template.HTML("an interaction")
case ReferenceType_KNOWLEDGESCHOLARFLAG:
return template.HTML("specific knowledge")
case ReferenceType_LANGUAGE:
return template.HTML("a language")
case ReferenceType_MUSICALFORM:
return template.HTML(c.musicalForm(r.Id_))
case ReferenceType_POETICFORM:
return template.HTML(c.poeticForm(r.Id_))
case ReferenceType_SITE:
return template.HTML(c.site(r.Id_, ""))
case ReferenceType_SUBREGION:
return template.HTML(c.region(r.Id_))
case ReferenceType_VALUELEVEL:
return template.HTML("a value")
case ReferenceType_WRITTENCONTENT:
return template.HTML(c.writtenContent(r.Id_))
}
return template.HTML(r.Type_.String())
}
2022-05-07 22:17:05 +03:00
func (r *River) Id() int {
return r.Id_
}
2023-11-17 16:50:14 +02:00
type HistoricalEventUnknown struct {
EventType string
}
func NewHistoricalEventUnknown(eventType string) *HistoricalEventUnknown {
return &HistoricalEventUnknown{
EventType: eventType,
}
}
func (x *HistoricalEventUnknown) Type() string { return x.EventType }
func (x *HistoricalEventUnknown) RelatedToEntity(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToHf(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToArtifact(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToSite(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToStructure(siteId, id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToRegion(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToWorldConstruction(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToWrittenContent(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToDanceForm(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToMusicalForm(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToPoeticForm(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToMountain(id int) bool { return false }
func (x *HistoricalEventUnknown) RelatedToIdentity(id int) bool { return false }
func (x *HistoricalEventUnknown) CheckFields() {}
func (x *HistoricalEventUnknown) Html(c *Context) string { return x.EventType }
func (x *HistoricalEventUnknown) MarshalJSON() ([]byte, error) {
return json.Marshal(make(map[string]any))
}