dorfylegends/backend/model/context.go

369 lines
11 KiB
Go
Raw Normal View History

2022-04-26 10:24:16 +03:00
package model
import (
"fmt"
"github.com/iancoleman/strcase"
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
)
2022-04-26 18:39:24 +03:00
type Context struct {
World *DfWorld
HfId int
Story bool
2022-05-05 17:55:57 +03:00
Event *HistoricalEvent
2022-04-26 10:24:16 +03:00
}
2022-05-02 18:22:57 +03:00
func NewContext(w *DfWorld, ref any) *Context {
c := &Context{World: w}
switch r := ref.(type) {
case *WrittenContent:
c.HfId = r.AuthorHfid
default:
fmt.Printf("unknown type for context %T\n", ref)
}
return c
}
2022-05-05 17:55:57 +03:00
func (c *Context) WithEvent(e *HistoricalEvent) *Context {
c2 := *c
c2.Event = e
return &c2
}
2022-04-26 18:39:24 +03:00
func (c *Context) hf(id int) string {
if c.HfId != -1 {
if c.HfId == id {
2022-04-26 10:24:16 +03:00
return c.hfShort(id)
} else {
2022-04-26 18:39:24 +03:00
return c.hfRelated(id, c.HfId)
2022-04-26 10:24:16 +03:00
}
}
2022-04-26 18:39:24 +03:00
if x, ok := c.World.HistoricalFigures[id]; ok {
2022-05-05 17:55:57 +03:00
return hf(x)
}
return "UNKNOWN HISTORICAL FIGURE"
}
func (c *Context) hfUnrelated(id int) string {
if c.HfId != -1 && c.HfId == id {
return c.hfShort(id)
}
if x, ok := c.World.HistoricalFigures[id]; ok {
return hf(x)
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN HISTORICAL FIGURE"
}
2022-04-26 18:39:24 +03:00
func (c *Context) hfShort(id int) string {
if x, ok := c.World.HistoricalFigures[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="hf" href="./hf/%d">%s%s</a>`, x.Id(), hfIcon(x), util.Title(x.FirstName()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN HISTORICAL FIGURE"
}
2022-04-26 18:39:24 +03:00
func (c *Context) hfRelated(id, to int) string {
2022-04-26 21:06:21 +03:00
if c.HfId != -1 && to != c.HfId {
2022-04-26 18:39:24 +03:00
if c.HfId == id {
2022-04-26 10:24:16 +03:00
return c.hfShort(id)
} else {
2022-04-26 18:39:24 +03:00
return c.hfRelated(id, c.HfId)
2022-04-26 10:24:16 +03:00
}
}
2022-04-26 18:39:24 +03:00
if x, ok := c.World.HistoricalFigures[id]; ok {
if t, ok := c.World.HistoricalFigures[to]; ok {
2022-04-26 10:24:16 +03:00
if y, ok := util.Find(t.HfLink, func(l *HfLink) bool { return l.Hfid == id }); ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`%s %s <a class="hf" href="./hf/%d">%s%s</a>`, t.PossesivePronoun(), y.LinkType, x.Id(), hfIcon(x), util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
}
2022-05-05 17:55:57 +03:00
return hf(x)
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN HISTORICAL FIGURE"
}
2022-05-05 23:18:31 +03:00
func hfIcon(x *HistoricalFigure) string {
switch {
case x.Leader:
return `<i class="fa-solid fa-crown fa-xs"></i> `
2022-05-06 14:11:12 +03:00
case x.Deity:
return `<i class="fa-solid fa-hands-holding fa-xs"></i> `
case x.Force:
return `<i class="fa-solid fa-hands-holding-circle fa-xs"></i> `
case x.Adventurer:
return `<i class="fa-solid fa-person-hiking fa-xs"></i> `
case x.Necromancer:
return `<i class="fa-solid fa-book-skull fa-xs"></i> `
case x.Werebeast:
2022-05-06 16:16:16 +03:00
return `<i class="fa-solid fa-cloud-moon fa-xs"></i> `
2022-05-06 14:11:12 +03:00
case x.Vampire:
return `<i class="fa-solid fa-droplet fa-xs"></i> `
2022-05-05 23:18:31 +03:00
}
2022-05-06 16:16:16 +03:00
if len(x.SiteLink) > 0 && x.SiteLink[0].LinkType == SiteLinkLinkType_Lair {
return `<i class="fa-solid fa-paw fa-xs"></i> `
}
2022-05-05 23:18:31 +03:00
return ""
}
2022-05-05 17:55:57 +03:00
func hf(x *HistoricalFigure) string {
r := x.Race
if x.Deity {
r += " deity"
}
if x.Force {
r += " deity"
}
if x.Necromancer {
r += " necromancer"
}
if x.Werebeast {
r += " werebeast"
}
if x.Vampire {
r += " vampire"
}
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`the %s <a class="hf" href="./hf/%d">%s%s</a>`, r, x.Id(), hfIcon(x), util.Title(x.Name()))
2022-05-05 17:55:57 +03:00
}
2022-04-26 18:39:24 +03:00
func (c *Context) hfList(ids []int) string {
2022-04-26 10:24:16 +03:00
return andList(util.Map(ids, func(id int) string { return c.hf(id) }))
}
2022-04-26 18:39:24 +03:00
func (c *Context) hfListRelated(ids []int, to int) string {
2022-04-26 10:24:16 +03:00
return andList(util.Map(ids, func(id int) string { return c.hfRelated(id, to) }))
}
2022-04-26 18:39:24 +03:00
func (c *Context) artifact(id int) string {
if x, ok := c.World.Artifacts[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="artifact" href="./artifact/%d"><i class="%s fa-xs"></i> %s</a>`, x.Id(), x.Icon(), util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN ARTIFACT"
}
2022-04-26 18:39:24 +03:00
func (c *Context) entity(id int) string {
if x, ok := c.World.Entities[id]; ok {
2022-05-07 18:18:08 +03:00
c := x.Color()
if c != "" {
c = fmt.Sprintf(` style="color:%s"`, c)
2022-05-06 16:16:16 +03:00
}
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="entity" href="./entity/%d"><i class="%s fa-xs" %s></i> %s</a>`, x.Id(), x.Icon(), c, util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN ENTITY"
}
2022-04-26 18:39:24 +03:00
func (c *Context) entityList(ids []int) string {
2022-04-26 10:24:16 +03:00
return andList(util.Map(ids, func(id int) string { return c.entity(id) }))
}
2022-04-26 18:39:24 +03:00
func (c *Context) position(entityId, positionId, hfId int) string {
if e, ok := c.World.Entities[entityId]; ok {
if h, ok := c.World.HistoricalFigures[hfId]; ok {
2022-04-26 10:24:16 +03:00
return e.Position(positionId).GenderName(h)
}
}
return "UNKNOWN POSITION"
}
2022-04-26 18:39:24 +03:00
func (c *Context) siteCiv(siteCivId, civId int) string {
2022-04-26 10:24:16 +03:00
if siteCivId == civId {
return c.entity(civId)
}
return util.If(siteCivId != -1, c.entity(siteCivId), "") + util.If(civId != -1 && siteCivId != -1, " of ", "") + util.If(civId != -1, c.entity(civId), "")
}
2022-04-26 18:39:24 +03:00
func (c *Context) siteStructure(siteId, structureId int, prefix string) string {
2022-04-26 10:24:16 +03:00
if siteId == -1 {
return ""
}
return " " + prefix + " " + util.If(structureId != -1, c.structure(siteId, structureId)+" in ", "") + c.site(siteId, "")
}
2022-04-26 18:39:24 +03:00
func (c *Context) site(id int, prefix string) string {
if x, ok := c.World.Sites[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`%s <a class="site" href="./site/%d"><i class="%s fa-xs%s"></i> %s</a>`, prefix, x.Id(), x.Icon(), util.If(x.Ruin, " ruin", ""), util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN SITE"
}
2022-04-26 18:39:24 +03:00
func (c *Context) structure(siteId, structureId int) string {
if x, ok := c.World.Sites[siteId]; ok {
2022-04-26 10:24:16 +03:00
if y, ok := x.Structures[structureId]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="structure" href="./site/%d/structure/%d"><i class="%s fa-xs%s"></i> %s</a>`, siteId, structureId, y.Icon(), util.If(y.Ruin, " ruin", ""), util.Title(y.Name()))
2022-04-26 10:24:16 +03:00
}
}
return "UNKNOWN STRUCTURE"
}
2022-04-26 18:39:24 +03:00
func (c *Context) property(siteId, propertyId int) string {
if x, ok := c.World.Sites[siteId]; ok {
2022-04-26 10:24:16 +03:00
if y, ok := x.SiteProperties[propertyId]; ok {
if y.StructureId != -1 {
return c.structure(siteId, y.StructureId)
}
2022-04-27 22:44:39 +03:00
return articled(y.Type_.String())
2022-04-26 10:24:16 +03:00
}
}
return "UNKNOWN PROPERTY"
}
2022-04-26 18:39:24 +03:00
func (c *Context) region(id int) string {
if x, ok := c.World.Regions[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="region" href="./region/%d">%s</a>`, x.Id(), util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN REGION"
}
2022-04-26 18:39:24 +03:00
func (c *Context) location(siteId int, sitePrefix string, regionId int, regionPrefix string) string {
2022-04-26 10:24:16 +03:00
if siteId != -1 {
return c.site(siteId, sitePrefix)
}
if regionId != -1 {
return regionPrefix + " " + c.region(regionId)
}
return ""
}
2022-04-26 18:39:24 +03:00
func (c *Context) place(structureId, siteId int, sitePrefix string, regionId int, regionPrefix string) string {
2022-04-26 10:24:16 +03:00
if siteId != -1 {
return c.siteStructure(siteId, structureId, sitePrefix)
}
if regionId != -1 {
return regionPrefix + " " + c.region(regionId)
}
return ""
}
2022-04-26 18:39:24 +03:00
func (c *Context) mountain(id int) string {
if x, ok := c.World.MountainPeaks[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="mountain" href="./mountain/%d"><i class="fa-solid %s"></i> %s</a>`,
2022-05-04 12:42:02 +03:00
x.Id(), util.If(x.IsVolcano, "fa-volcano", "fa-mountain"), util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN MOUNTAIN"
}
2022-05-04 12:42:02 +03:00
func (c *Context) landmass(id int) string {
if x, ok := c.World.Landmasses[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="landmass" href="./landmass/%d">%s</a>`, x.Id(), util.Title(x.Name()))
2022-05-04 12:42:02 +03:00
}
return "UNKNOWN LANDMASS"
}
func (c *Context) river(id int) string {
x := c.World.Rivers[id]
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="river" href="./river/%d">%s</a>`, id, util.Title(x.Name()))
2022-05-04 12:42:02 +03:00
}
2022-04-26 18:39:24 +03:00
func (c *Context) identity(id int) string {
if x, ok := c.World.Identities[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="identity" href="./identity/%d">%s</a>`, x.Id(), util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN IDENTITY"
}
2022-04-26 18:39:24 +03:00
func (c *Context) fullIdentity(id int) string {
if x, ok := c.World.Identities[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`&quot;the %s <a class="identity" href="./identity/%d">%s</a> of %s&quot;`, x.Profession.String(), x.Id(), util.Title(x.Name()), c.entity(x.EntityId))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN IDENTITY"
}
2022-04-26 18:39:24 +03:00
func (c *Context) danceForm(id int) string {
if x, ok := c.World.DanceForms[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="artform" href="./danceform/%d"><i class="fa-solid fa-shoe-prints fa-xs"></i> %s</a>`, id, util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN DANCE FORM"
}
2022-04-26 18:39:24 +03:00
func (c *Context) musicalForm(id int) string {
if x, ok := c.World.MusicalForms[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="artform" href="./musicalform/%d"><i class="fa-solid fa-music fa-xs"></i> %s</a>`, id, util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN MUSICAL FORM"
}
2022-04-26 18:39:24 +03:00
func (c *Context) poeticForm(id int) string {
if x, ok := c.World.PoeticForms[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="artform" href="./poeticform/%d"><i class="fa-solid fa-comment-dots fa-xs"></i> %s</a>`, id, util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN POETIC FORM"
}
2022-04-26 18:39:24 +03:00
func (c *Context) worldConstruction(id int) string {
if x, ok := c.World.WorldConstructions[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="worldconstruction" href="./worldconstruction/%d"><i class="%s fa-xs"></i> %s</a>`, id, x.Icon(), util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN WORLD CONSTRUCTION"
}
2022-04-26 18:39:24 +03:00
func (c *Context) writtenContent(id int) string {
if x, ok := c.World.WrittenContents[id]; ok {
2022-05-09 18:20:31 +03:00
return fmt.Sprintf(`<a class="writtencontent" href="./writtencontent/%d">%s</a>`, id, util.Title(x.Name()))
2022-04-26 10:24:16 +03:00
}
return "UNKNOWN WORLD CONSTRUCTION"
}
2022-05-03 15:59:47 +03:00
func (c *Context) collection(id int) string {
if x, ok := c.World.HistoricalEventCollections[id]; ok {
return x.Details.Html(x, c)
}
return "UNKNOWN EVENT COLLECTION"
}
2022-04-26 18:39:24 +03:00
func (c *Context) feature(x *Feature) string {
2022-04-27 22:44:39 +03:00
switch x.Type_ {
2022-04-26 10:24:16 +03:00
case FeatureType_DancePerformance:
return "a perfomance of " + c.danceForm(x.Reference)
case FeatureType_Images:
if x.Reference != -1 {
return "images of " + c.hf(x.Reference)
}
return "images"
case FeatureType_MusicalPerformance:
return "a perfomance of " + c.musicalForm(x.Reference)
case FeatureType_PoetryRecital:
return "a recital of " + c.poeticForm(x.Reference)
case FeatureType_Storytelling:
if x.Reference != -1 {
2022-04-26 18:39:24 +03:00
if e, ok := c.World.HistoricalEvents[x.Reference]; ok {
2022-04-28 22:24:55 +03:00
return "a telling of the story of " + e.Details.Html(&Context{World: c.World, Story: true}) + " in " + Time(e.Year, e.Seconds72)
2022-04-26 10:24:16 +03:00
}
}
return "a story recital"
default:
2022-04-27 22:44:39 +03:00
return strcase.ToDelimited(x.Type_.String(), ' ')
2022-04-26 10:24:16 +03:00
}
}
2022-04-26 18:39:24 +03:00
func (c *Context) schedule(x *Schedule) string {
2022-04-27 22:44:39 +03:00
switch x.Type_ {
2022-04-26 10:24:16 +03:00
case ScheduleType_DancePerformance:
return "a perfomance of " + c.danceForm(x.Reference)
case ScheduleType_MusicalPerformance:
return "a perfomance of " + c.musicalForm(x.Reference)
case ScheduleType_PoetryRecital:
return "a recital of " + c.poeticForm(x.Reference)
case ScheduleType_Storytelling:
if x.Reference != -1 {
2022-04-26 18:39:24 +03:00
if e, ok := c.World.HistoricalEvents[x.Reference]; ok {
2022-04-28 22:24:55 +03:00
return "the story of " + e.Details.Html(&Context{World: c.World, Story: true}) + " in " + Time(e.Year, e.Seconds72)
2022-04-26 10:24:16 +03:00
}
}
return "a story recital"
default:
2022-04-27 22:44:39 +03:00
return strcase.ToDelimited(x.Type_.String(), ' ')
2022-04-26 10:24:16 +03:00
}
}
2022-04-26 18:39:24 +03:00
func (c *Context) pronoun(id int) string {
if x, ok := c.World.HistoricalFigures[id]; ok {
2022-04-26 10:24:16 +03:00
return x.Pronoun()
}
return "he"
}
2022-04-26 18:39:24 +03:00
func (c *Context) posessivePronoun(id int) string {
if x, ok := c.World.HistoricalFigures[id]; ok {
2022-04-26 10:24:16 +03:00
return x.PossesivePronoun()
}
return "his"
}