diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index c565ff5..fe32108 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -26,7 +26,8 @@ }, // Add the IDs of extensions you want installed when the container is created. "extensions": [ - "golang.Go" + "golang.Go", + "jinliming2.vscode-go-template" ], // Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [], diff --git a/backend/main.go b/backend/main.go index 92d1a8e..041099a 100644 --- a/backend/main.go +++ b/backend/main.go @@ -12,6 +12,7 @@ import ( "github.com/pkg/profile" "github.com/robertjanetzko/LegendsBrowser2/backend/model" "github.com/robertjanetzko/LegendsBrowser2/backend/server" + "github.com/robertjanetzko/LegendsBrowser2/backend/templates" ) //go:embed static @@ -20,8 +21,11 @@ var static embed.FS func main() { f := flag.String("f", "", "open a file") p := flag.Bool("p", false, "start profiling") + d := flag.Bool("d", false, "debug templates") flag.Parse() + templates.DebugTemplates = *d + if len(*f) > 0 { if *p { defer profile.Start(profile.ProfilePath(".")).Stop() @@ -37,8 +41,9 @@ func main() { } runtime.GC() - server.StartServer(w, static) - } + } else { + server.StartServer(nil, static) + } } diff --git a/backend/model/context.go b/backend/model/context.go index ba97b73..eae8af1 100644 --- a/backend/model/context.go +++ b/backend/model/context.go @@ -7,43 +7,43 @@ import ( "github.com/robertjanetzko/LegendsBrowser2/backend/util" ) -type context struct { - world *DfWorld - hfId int - story bool +type Context struct { + World *DfWorld + HfId int + Story bool } -func (c *context) hf(id int) string { - if c.hfId != -1 { - if c.hfId == id { +func (c *Context) hf(id int) string { + if c.HfId != -1 { + if c.HfId == id { return c.hfShort(id) } else { - return c.hfRelated(id, c.hfId) + return c.hfRelated(id, c.HfId) } } - if x, ok := c.world.HistoricalFigures[id]; ok { + if x, ok := c.World.HistoricalFigures[id]; ok { return fmt.Sprintf(`the %s %s`, x.Race+util.If(x.Deity, " deity", "")+util.If(x.Force, " force", ""), x.Id(), util.Title(x.Name())) } return "UNKNOWN HISTORICAL FIGURE" } -func (c *context) hfShort(id int) string { - if x, ok := c.world.HistoricalFigures[id]; ok { +func (c *Context) hfShort(id int) string { + if x, ok := c.World.HistoricalFigures[id]; ok { return fmt.Sprintf(`%s`, x.Id(), util.Title(x.FirstName())) } return "UNKNOWN HISTORICAL FIGURE" } -func (c *context) hfRelated(id, to int) string { - if c.hfId != -1 { - if c.hfId == id { +func (c *Context) hfRelated(id, to int) string { + if c.HfId != -1 { + if c.HfId == id { return c.hfShort(id) } else { - return c.hfRelated(id, c.hfId) + return c.hfRelated(id, c.HfId) } } - if x, ok := c.world.HistoricalFigures[id]; ok { - if t, ok := c.world.HistoricalFigures[to]; ok { + if x, ok := c.World.HistoricalFigures[id]; ok { + if t, ok := c.World.HistoricalFigures[to]; ok { if y, ok := util.Find(t.HfLink, func(l *HfLink) bool { return l.Hfid == id }); ok { return fmt.Sprintf(`%s %s %s`, t.PossesivePronoun(), y.LinkType, x.Id(), util.Title(x.Name())) } @@ -53,64 +53,64 @@ func (c *context) hfRelated(id, to int) string { return "UNKNOWN HISTORICAL FIGURE" } -func (c *context) hfList(ids []int) string { +func (c *Context) hfList(ids []int) string { return andList(util.Map(ids, func(id int) string { return c.hf(id) })) } -func (c *context) hfListRelated(ids []int, to int) string { +func (c *Context) hfListRelated(ids []int, to int) string { return andList(util.Map(ids, func(id int) string { return c.hfRelated(id, to) })) } -func (c *context) artifact(id int) string { - if x, ok := c.world.Artifacts[id]; ok { +func (c *Context) artifact(id int) string { + if x, ok := c.World.Artifacts[id]; ok { return fmt.Sprintf(`%s`, x.Id(), util.Title(x.Name())) } return "UNKNOWN ARTIFACT" } -func (c *context) entity(id int) string { - if x, ok := c.world.Entities[id]; ok { +func (c *Context) entity(id int) string { + if x, ok := c.World.Entities[id]; ok { return fmt.Sprintf(`%s`, x.Id(), util.Title(x.Name())) } return "UNKNOWN ENTITY" } -func (c *context) entityList(ids []int) string { +func (c *Context) entityList(ids []int) string { return andList(util.Map(ids, func(id int) string { return c.entity(id) })) } -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 { +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 { return e.Position(positionId).GenderName(h) } } return "UNKNOWN POSITION" } -func (c *context) siteCiv(siteCivId, civId int) string { +func (c *Context) siteCiv(siteCivId, civId int) string { 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), "") } -func (c *context) siteStructure(siteId, structureId int, prefix string) string { +func (c *Context) siteStructure(siteId, structureId int, prefix string) string { if siteId == -1 { return "" } return " " + prefix + " " + util.If(structureId != -1, c.structure(siteId, structureId)+" in ", "") + c.site(siteId, "") } -func (c *context) site(id int, prefix string) string { - if x, ok := c.world.Sites[id]; ok { +func (c *Context) site(id int, prefix string) string { + if x, ok := c.World.Sites[id]; ok { return fmt.Sprintf(`%s %s`, prefix, x.Id(), util.Title(x.Name())) } return "UNKNOWN SITE" } -func (c *context) structure(siteId, structureId int) string { - if x, ok := c.world.Sites[siteId]; ok { +func (c *Context) structure(siteId, structureId int) string { + if x, ok := c.World.Sites[siteId]; ok { if y, ok := x.Structures[structureId]; ok { return fmt.Sprintf(`%s`, siteId, structureId, util.Title(y.Name())) } @@ -118,8 +118,8 @@ func (c *context) structure(siteId, structureId int) string { return "UNKNOWN STRUCTURE" } -func (c *context) property(siteId, propertyId int) string { - if x, ok := c.world.Sites[siteId]; ok { +func (c *Context) property(siteId, propertyId int) string { + if x, ok := c.World.Sites[siteId]; ok { if y, ok := x.SiteProperties[propertyId]; ok { if y.StructureId != -1 { return c.structure(siteId, y.StructureId) @@ -130,14 +130,14 @@ func (c *context) property(siteId, propertyId int) string { return "UNKNOWN PROPERTY" } -func (c *context) region(id int) string { - if x, ok := c.world.Regions[id]; ok { +func (c *Context) region(id int) string { + if x, ok := c.World.Regions[id]; ok { return fmt.Sprintf(`%s`, x.Id(), util.Title(x.Name())) } return "UNKNOWN REGION" } -func (c *context) location(siteId int, sitePrefix string, regionId int, regionPrefix string) string { +func (c *Context) location(siteId int, sitePrefix string, regionId int, regionPrefix string) string { if siteId != -1 { return c.site(siteId, sitePrefix) } @@ -147,7 +147,7 @@ func (c *context) location(siteId int, sitePrefix string, regionId int, regionPr return "" } -func (c *context) place(structureId, siteId int, sitePrefix string, regionId int, regionPrefix string) string { +func (c *Context) place(structureId, siteId int, sitePrefix string, regionId int, regionPrefix string) string { if siteId != -1 { return c.siteStructure(siteId, structureId, sitePrefix) } @@ -157,63 +157,63 @@ func (c *context) place(structureId, siteId int, sitePrefix string, regionId int return "" } -func (c *context) mountain(id int) string { - if x, ok := c.world.MountainPeaks[id]; ok { +func (c *Context) mountain(id int) string { + if x, ok := c.World.MountainPeaks[id]; ok { return fmt.Sprintf(`%s`, x.Id(), util.Title(x.Name())) } return "UNKNOWN MOUNTAIN" } -func (c *context) identity(id int) string { - if x, ok := c.world.Identities[id]; ok { +func (c *Context) identity(id int) string { + if x, ok := c.World.Identities[id]; ok { return fmt.Sprintf(`%s`, x.Id(), util.Title(x.Name())) } return "UNKNOWN IDENTITY" } -func (c *context) fullIdentity(id int) string { - if x, ok := c.world.Identities[id]; ok { +func (c *Context) fullIdentity(id int) string { + if x, ok := c.World.Identities[id]; ok { return fmt.Sprintf(`"the %s %s of %s"`, x.Profession.String(), x.Id(), util.Title(x.Name()), c.entity(x.EntityId)) } return "UNKNOWN IDENTITY" } -func (c *context) danceForm(id int) string { - if x, ok := c.world.DanceForms[id]; ok { +func (c *Context) danceForm(id int) string { + if x, ok := c.World.DanceForms[id]; ok { return fmt.Sprintf(`%s`, id, util.Title(x.Name())) } return "UNKNOWN DANCE FORM" } -func (c *context) musicalForm(id int) string { - if x, ok := c.world.MusicalForms[id]; ok { +func (c *Context) musicalForm(id int) string { + if x, ok := c.World.MusicalForms[id]; ok { return fmt.Sprintf(`%s`, id, util.Title(x.Name())) } return "UNKNOWN MUSICAL FORM" } -func (c *context) poeticForm(id int) string { - if x, ok := c.world.PoeticForms[id]; ok { +func (c *Context) poeticForm(id int) string { + if x, ok := c.World.PoeticForms[id]; ok { return fmt.Sprintf(`%s`, id, util.Title(x.Name())) } return "UNKNOWN POETIC FORM" } -func (c *context) worldConstruction(id int) string { - if x, ok := c.world.WorldConstructions[id]; ok { +func (c *Context) worldConstruction(id int) string { + if x, ok := c.World.WorldConstructions[id]; ok { return fmt.Sprintf(`%s`, id, util.Title(x.Name())) } return "UNKNOWN WORLD CONSTRUCTION" } -func (c *context) writtenContent(id int) string { - if x, ok := c.world.WrittenContents[id]; ok { +func (c *Context) writtenContent(id int) string { + if x, ok := c.World.WrittenContents[id]; ok { return fmt.Sprintf(`%s`, id, util.Title(x.Name())) } return "UNKNOWN WORLD CONSTRUCTION" } -func (c *context) feature(x *Feature) string { +func (c *Context) feature(x *Feature) string { switch x.Type { case FeatureType_DancePerformance: return "a perfomance of " + c.danceForm(x.Reference) @@ -228,8 +228,8 @@ func (c *context) feature(x *Feature) string { return "a recital of " + c.poeticForm(x.Reference) case FeatureType_Storytelling: if x.Reference != -1 { - if e, ok := c.world.HistoricalEvents[x.Reference]; ok { - return "a telling of the story of " + e.Details.Html(&context{story: true}) + " in " + Time(e.Year, e.Seconds72) + if e, ok := c.World.HistoricalEvents[x.Reference]; ok { + return "a telling of the story of " + e.Details.Html(&Context{Story: true}) + " in " + Time(e.Year, e.Seconds72) } } return "a story recital" @@ -238,7 +238,7 @@ func (c *context) feature(x *Feature) string { } } -func (c *context) schedule(x *Schedule) string { +func (c *Context) schedule(x *Schedule) string { switch x.Type { case ScheduleType_DancePerformance: return "a perfomance of " + c.danceForm(x.Reference) @@ -248,8 +248,8 @@ func (c *context) schedule(x *Schedule) string { return "a recital of " + c.poeticForm(x.Reference) case ScheduleType_Storytelling: if x.Reference != -1 { - if e, ok := c.world.HistoricalEvents[x.Reference]; ok { - return "the story of " + e.Details.Html(&context{story: true}) + " in " + Time(e.Year, e.Seconds72) + if e, ok := c.World.HistoricalEvents[x.Reference]; ok { + return "the story of " + e.Details.Html(&Context{Story: true}) + " in " + Time(e.Year, e.Seconds72) } } return "a story recital" @@ -258,15 +258,15 @@ func (c *context) schedule(x *Schedule) string { } } -func (c *context) pronoun(id int) string { - if x, ok := c.world.HistoricalFigures[id]; ok { +func (c *Context) pronoun(id int) string { + if x, ok := c.World.HistoricalFigures[id]; ok { return x.Pronoun() } return "he" } -func (c *context) posessivePronoun(id int) string { - if x, ok := c.world.HistoricalFigures[id]; ok { +func (c *Context) posessivePronoun(id int) string { + if x, ok := c.World.HistoricalFigures[id]; ok { return x.PossesivePronoun() } return "his" diff --git a/backend/model/eventList.go b/backend/model/eventList.go index c153ada..0ba0d0f 100644 --- a/backend/model/eventList.go +++ b/backend/model/eventList.go @@ -10,7 +10,7 @@ type HistoricalEventDetails interface { RelatedToArtifact(int) bool RelatedToSite(int) bool RelatedToRegion(int) bool - Html(*context) string + Html(*Context) string Type() string } @@ -19,19 +19,19 @@ type HistoricalEventCollectionDetails interface { type EventList struct { Events []*HistoricalEvent - Context *context + Context *Context } func NewEventList(world *DfWorld, obj any) *EventList { el := EventList{ - Context: &context{hfId: -1}, + Context: &Context{HfId: -1}, } switch x := obj.(type) { case *Entity: el.Events = world.EventsMatching(func(d HistoricalEventDetails) bool { return d.RelatedToEntity(x.Id()) }) case *HistoricalFigure: - el.Context.hfId = x.Id() + el.Context.HfId = x.Id() el.Events = world.EventsMatching(func(d HistoricalEventDetails) bool { return d.RelatedToHf(x.Id()) }) case *Artifact: el.Events = world.EventsMatching(func(d HistoricalEventDetails) bool { return d.RelatedToArtifact(x.Id()) }) diff --git a/backend/model/events.go b/backend/model/events.go index c6970aa..184801f 100644 --- a/backend/model/events.go +++ b/backend/model/events.go @@ -9,17 +9,17 @@ import ( "github.com/robertjanetzko/LegendsBrowser2/backend/util" ) -func (x *HistoricalEventAddHfEntityHonor) Html(c *context) string { - e := c.world.Entities[x.EntityId] +func (x *HistoricalEventAddHfEntityHonor) Html(c *Context) string { + e := c.World.Entities[x.EntityId] h := e.Honor[x.HonorId] return fmt.Sprintf("%s received the title %s of %s%s", c.hf(x.Hfid), h.Name(), c.entity(x.EntityId), h.Requirement()) } -func (x *HistoricalEventAddHfEntityLink) Html(c *context) string { +func (x *HistoricalEventAddHfEntityLink) Html(c *Context) string { h := c.hf(x.Hfid) e := c.entity(x.CivId) - if c.story { + if c.Story { return "the ascension of " + h + " to " + c.position(x.CivId, x.PositionId, x.Hfid) + " of " + e } @@ -43,7 +43,7 @@ func (x *HistoricalEventAddHfEntityLink) Html(c *context) string { return h + " became SOMETHING of " + e } -func (x *HistoricalEventAddHfHfLink) Html(c *context) string { +func (x *HistoricalEventAddHfHfLink) Html(c *Context) string { h := c.hf(x.Hfid) t := c.hf(x.HfidTarget) switch x.LinkType { @@ -68,7 +68,7 @@ func (x *HistoricalEventAddHfHfLink) Html(c *context) string { } } -func (x *HistoricalEventAddHfSiteLink) Html(c *context) string { +func (x *HistoricalEventAddHfSiteLink) Html(c *Context) string { h := c.hf(x.Histfig) e := "" if x.Civ != -1 { @@ -95,7 +95,7 @@ func (x *HistoricalEventAddHfSiteLink) Html(c *context) string { } } -func (x *HistoricalEventAgreementConcluded) Html(c *context) string { // TODO wording +func (x *HistoricalEventAgreementConcluded) Html(c *Context) string { // TODO wording r := "" switch x.Topic { case HistoricalEventAgreementConcludedTopic_Treequota: @@ -104,11 +104,11 @@ func (x *HistoricalEventAgreementConcluded) Html(c *context) string { // TODO wo return r + " proposed by " + c.entity(x.Source) + " was concluded by " + c.entity(x.Destination) + c.site(x.Site, " at") } -func (x *HistoricalEventAgreementFormed) Html(c *context) string { // TODO no info +func (x *HistoricalEventAgreementFormed) Html(c *Context) string { // TODO no info return "UNKNWON HistoricalEventAgreementFormed" } -func (x *HistoricalEventAgreementMade) Html(c *context) string { +func (x *HistoricalEventAgreementMade) Html(c *Context) string { r := "" switch x.Topic { case HistoricalEventAgreementMadeTopic_Becomelandholder: @@ -121,7 +121,7 @@ func (x *HistoricalEventAgreementMade) Html(c *context) string { return r + " proposed by " + c.entity(x.Source) + " was accepted by " + c.entity(x.Destination) + c.site(x.SiteId, " at") } -func (x *HistoricalEventAgreementRejected) Html(c *context) string { +func (x *HistoricalEventAgreementRejected) Html(c *Context) string { r := "" switch x.Topic { case HistoricalEventAgreementRejectedTopic_Becomelandholder: @@ -136,13 +136,13 @@ func (x *HistoricalEventAgreementRejected) Html(c *context) string { return r + " proposed by " + c.entity(x.Source) + " was rejected by " + c.entity(x.Destination) + c.site(x.SiteId, " at") } -func (x *HistoricalEventArtifactClaimFormed) Html(c *context) string { +func (x *HistoricalEventArtifactClaimFormed) Html(c *Context) string { a := c.artifact(x.ArtifactId) switch x.Claim { case HistoricalEventArtifactClaimFormedClaim_Heirloom: return a + " was made a family heirloom by " + c.hf(x.HistFigureId) case HistoricalEventArtifactClaimFormedClaim_Symbol: - p := c.world.Entities[x.EntityId].Position(x.PositionProfileId).Name_ + p := c.World.Entities[x.EntityId].Position(x.PositionProfileId).Name_ e := c.entity(x.EntityId) return a + " was made a symbol of the " + p + " by " + e case HistoricalEventArtifactClaimFormedClaim_Treasure: @@ -159,14 +159,14 @@ func (x *HistoricalEventArtifactClaimFormed) Html(c *context) string { return a + " was claimed" } -func (x *HistoricalEventArtifactCopied) Html(c *context) string { +func (x *HistoricalEventArtifactCopied) Html(c *Context) string { s := util.If(x.FromOriginal, "made a copy of the original", "aquired a copy of") return fmt.Sprintf("%s %s %s %s of %s, keeping it%s", c.entity(x.DestEntityId), s, c.artifact(x.ArtifactId), c.siteStructure(x.SourceSiteId, x.SourceStructureId, "from"), c.entity(x.SourceEntityId), c.siteStructure(x.DestSiteId, x.DestStructureId, "within")) } -func (x *HistoricalEventArtifactCreated) Html(c *context) string { +func (x *HistoricalEventArtifactCreated) Html(c *Context) string { a := c.artifact(x.ArtifactId) h := c.hf(x.HistFigureId) s := "" @@ -195,11 +195,11 @@ func (x *HistoricalEventArtifactCreated) Html(c *context) string { } } -func (x *HistoricalEventArtifactDestroyed) Html(c *context) string { +func (x *HistoricalEventArtifactDestroyed) Html(c *Context) string { return c.artifact(x.ArtifactId) + " was destroyed" + util.If(x.DestroyerEnid != -1, " by "+c.entity(x.DestroyerEnid), "") + c.site(x.SiteId, " in") } -func (x *HistoricalEventArtifactFound) Html(c *context) string { +func (x *HistoricalEventArtifactFound) Html(c *Context) string { w := "" if x.SiteId != -1 { w = c.site(x.SiteId, "") @@ -210,7 +210,7 @@ func (x *HistoricalEventArtifactFound) Html(c *context) string { return fmt.Sprintf("%s was found in %s by %s", c.artifact(x.ArtifactId), w, util.If(x.HistFigureId != -1, c.hf(x.HistFigureId), "an unknown creature")) } -func (x *HistoricalEventArtifactGiven) Html(c *context) string { +func (x *HistoricalEventArtifactGiven) Html(c *Context) string { r := "" if x.ReceiverHistFigureId != -1 { r = c.hf(x.ReceiverHistFigureId) @@ -236,7 +236,7 @@ func (x *HistoricalEventArtifactGiven) Html(c *context) string { } return fmt.Sprintf("%s was offered to %s by %s%s", c.artifact(x.ArtifactId), r, g, reason) } -func (x *HistoricalEventArtifactLost) Html(c *context) string { +func (x *HistoricalEventArtifactLost) Html(c *Context) string { w := "" if x.SubregionId != -1 { w = c.region(x.SubregionId) @@ -250,7 +250,7 @@ func (x *HistoricalEventArtifactLost) Html(c *context) string { return fmt.Sprintf("%s was lost in %s", c.artifact(x.ArtifactId), w) } -func (x *HistoricalEventArtifactPossessed) Html(c *context) string { +func (x *HistoricalEventArtifactPossessed) Html(c *Context) string { a := c.artifact(x.ArtifactId) h := c.hf(x.HistFigureId) w := "" @@ -275,7 +275,7 @@ func (x *HistoricalEventArtifactPossessed) Html(c *context) string { return fmt.Sprintf("%s was claimed in %s by %s%s", a, w, h, circumstance) // TODO wording } -func (x *HistoricalEventArtifactRecovered) Html(c *context) string { +func (x *HistoricalEventArtifactRecovered) Html(c *Context) string { a := c.artifact(x.ArtifactId) h := c.hf(x.HistFigureId) w := "" @@ -291,7 +291,7 @@ func (x *HistoricalEventArtifactRecovered) Html(c *context) string { return fmt.Sprintf("%s was recovered %s by %s", a, w, h) } -func (x *HistoricalEventArtifactStored) Html(c *context) string { +func (x *HistoricalEventArtifactStored) Html(c *Context) string { if x.HistFigureId != -1 { return fmt.Sprintf("%s stored %s in %s", c.hf(x.HistFigureId), c.artifact(x.ArtifactId), c.site(x.SiteId, "")) } else { @@ -299,11 +299,11 @@ func (x *HistoricalEventArtifactStored) Html(c *context) string { } } -func (x *HistoricalEventArtifactTransformed) Html(c *context) string { +func (x *HistoricalEventArtifactTransformed) Html(c *Context) string { return fmt.Sprintf("%s was made from %s by %s in %s", c.artifact(x.NewArtifactId), c.artifact(x.OldArtifactId), c.hf(x.HistFigureId), c.site(x.SiteId, "")) // TODO wording } -func (x *HistoricalEventAssumeIdentity) Html(c *context) string { +func (x *HistoricalEventAssumeIdentity) Html(c *Context) string { h := c.hf(x.TricksterHfid) i := c.identity(x.IdentityId) if x.TargetEnid == -1 { @@ -313,7 +313,7 @@ func (x *HistoricalEventAssumeIdentity) Html(c *context) string { } } -func (x *HistoricalEventAttackedSite) Html(c *context) string { +func (x *HistoricalEventAttackedSite) Html(c *Context) string { atk := c.entity(x.AttackerCivId) def := c.siteCiv(x.SiteCivId, x.DefenderCivId) generals := "" @@ -339,7 +339,7 @@ func (x *HistoricalEventAttackedSite) Html(c *context) string { return fmt.Sprintf("%s attacked %s at %s%s%s", atk, def, c.site(x.SiteId, ""), generals, mercs) } -func (x *HistoricalEventBodyAbused) Html(c *context) string { +func (x *HistoricalEventBodyAbused) Html(c *Context) string { s := "the " + util.If(len(x.Bodies) > 1, "bodies", "body") + " of " + c.hfList(x.Bodies) + " " + util.If(len(x.Bodies) > 1, "were", "was") switch x.AbuseType { @@ -369,16 +369,16 @@ func (x *HistoricalEventBodyAbused) Html(c *context) string { return s } -func (x *HistoricalEventBuildingProfileAcquired) Html(c *context) string { +func (x *HistoricalEventBuildingProfileAcquired) Html(c *Context) string { return util.If(x.AcquirerEnid != -1, c.entity(x.AcquirerEnid), c.hf(x.AcquirerHfid)) + util.If(x.PurchasedUnowned, " purchased ", " inherited ") + c.property(x.SiteId, x.BuildingProfileId) + c.site(x.SiteId, " in") + util.If(x.LastOwnerHfid != -1, " formerly owned by "+c.hfRelated(x.LastOwnerHfid, x.AcquirerHfid), "") } -func (x *HistoricalEventCeremony) Html(c *context) string { +func (x *HistoricalEventCeremony) Html(c *Context) string { r := c.entity(x.CivId) + " held a ceremony in " + c.site(x.SiteId, "") - if e, ok := c.world.Entities[x.CivId]; ok { + if e, ok := c.World.Entities[x.CivId]; ok { o := e.Occasion[x.OccasionId] r += " as part of " + o.Name() s := o.Schedule[x.ScheduleId] @@ -389,7 +389,7 @@ func (x *HistoricalEventCeremony) Html(c *context) string { return r } -func (x *HistoricalEventChangeHfBodyState) Html(c *context) string { +func (x *HistoricalEventChangeHfBodyState) Html(c *Context) string { r := c.hf(x.Hfid) switch x.BodyState { case HistoricalEventChangeHfBodyStateBodyState_EntombedAtSite: @@ -402,7 +402,7 @@ func (x *HistoricalEventChangeHfBodyState) Html(c *context) string { return r } -func (x *HistoricalEventChangeHfJob) Html(c *context) string { +func (x *HistoricalEventChangeHfJob) Html(c *Context) string { w := "" if x.SubregionId != -1 { w = " in " + c.region(x.SubregionId) @@ -421,7 +421,7 @@ func (x *HistoricalEventChangeHfJob) Html(c *context) string { } } -func (x *HistoricalEventChangeHfState) Html(c *context) string { +func (x *HistoricalEventChangeHfState) Html(c *Context) string { r := "" switch x.Reason { case HistoricalEventChangeHfStateReason_BeWithMaster: @@ -492,12 +492,12 @@ func (x *HistoricalEventChangeHfState) Html(c *context) string { return "UNKNWON HistoricalEventChangeHfState" } -func (x *HistoricalEventChangedCreatureType) Html(c *context) string { +func (x *HistoricalEventChangedCreatureType) Html(c *Context) string { return c.hf(x.ChangerHfid) + " changed " + c.hfRelated(x.ChangeeHfid, x.ChangerHfid) + " from " + articled(x.OldRace) + " to " + articled(x.NewRace) } -func (x *HistoricalEventCompetition) Html(c *context) string { - e := c.world.Entities[x.CivId] +func (x *HistoricalEventCompetition) Html(c *Context) string { + e := c.World.Entities[x.CivId] o := e.Occasion[x.OccasionId] s := o.Schedule[x.ScheduleId] return c.entity(x.CivId) + " held a " + strcase.ToDelimited(s.Type.String(), ' ') + c.site(x.SiteId, " in") + " as part of the " + o.Name() + @@ -505,7 +505,7 @@ func (x *HistoricalEventCompetition) Html(c *context) string { util.Capitalize(c.hf(x.WinnerHfid)) + " was the victor" } -func (x *HistoricalEventCreateEntityPosition) Html(c *context) string { +func (x *HistoricalEventCreateEntityPosition) Html(c *Context) string { e := c.entity(x.Civ) if x.SiteCiv != x.Civ { e = c.entity(x.SiteCiv) + " of " + e @@ -530,7 +530,7 @@ func (x *HistoricalEventCreateEntityPosition) Html(c *context) string { return e + " created the position of " + x.Position } -func (x *HistoricalEventCreatedSite) Html(c *context) string { +func (x *HistoricalEventCreatedSite) Html(c *Context) string { f := util.If(x.ResidentCivId != -1, " for "+c.entity(x.ResidentCivId), "") if x.BuilderHfid != -1 { return c.hf(x.BuilderHfid) + " created " + c.site(x.SiteId, "") + f @@ -539,7 +539,7 @@ func (x *HistoricalEventCreatedSite) Html(c *context) string { } -func (x *HistoricalEventCreatedStructure) Html(c *context) string { // TODO rebuild/rebuilt +func (x *HistoricalEventCreatedStructure) Html(c *Context) string { // TODO rebuild/rebuilt if x.BuilderHfid != -1 { return c.hf(x.BuilderHfid) + " thrust a spire of slade up from the underworld, naming it " + c.structure(x.SiteId, x.StructureId) + ", and established a gateway between worlds in " + c.site(x.SiteId, "") @@ -547,19 +547,19 @@ func (x *HistoricalEventCreatedStructure) Html(c *context) string { // TODO rebu return c.siteCiv(x.SiteCivId, x.CivId) + util.If(x.Rebuilt, " rebuild ", " constructed ") + c.siteStructure(x.SiteId, x.StructureId, "") } -func (x *HistoricalEventCreatedWorldConstruction) Html(c *context) string { +func (x *HistoricalEventCreatedWorldConstruction) Html(c *Context) string { return c.siteCiv(x.SiteCivId, x.CivId) + " finished the contruction of " + c.worldConstruction(x.Wcid) + " connecting " + c.site(x.SiteId1, "") + " with " + c.site(x.SiteId2, "") + util.If(x.MasterWcid != -1, " as part of "+c.worldConstruction(x.MasterWcid), "") } -func (x *HistoricalEventCreatureDevoured) Html(c *context) string { +func (x *HistoricalEventCreatureDevoured) Html(c *Context) string { return c.hf(x.Eater) + " devoured " + util.If(x.Victim != -1, c.hfRelated(x.Victim, x.Eater), articled(x.Race)) + util.If(x.Entity != -1, " of "+c.entity(x.Entity), "") + c.location(x.SiteId, " in", x.SubregionId, " in") } -func (x *HistoricalEventDanceFormCreated) Html(c *context) string { +func (x *HistoricalEventDanceFormCreated) Html(c *Context) string { reason := "" switch x.Reason { case HistoricalEventDanceFormCreatedReason_GlorifyHf: @@ -579,23 +579,23 @@ func (x *HistoricalEventDanceFormCreated) Html(c *context) string { return c.danceForm(x.FormId) + " was created by " + c.hf(x.HistFigureId) + c.location(x.SiteId, " in", x.SubregionId, " in") + reason + circumstance } -func (x *HistoricalEventDestroyedSite) Html(c *context) string { +func (x *HistoricalEventDestroyedSite) Html(c *Context) string { return c.entity(x.AttackerCivId) + " defeated " + c.siteCiv(x.SiteCivId, x.DefenderCivId) + " and destroyed " + c.site(x.SiteId, "") } -func (x *HistoricalEventDiplomatLost) Html(c *context) string { +func (x *HistoricalEventDiplomatLost) Html(c *Context) string { return c.entity(x.Entity) + " lost a diplomant in " + c.site(x.SiteId, "") + ". They suspected the involvement of " + c.entity(x.Involved) } -func (x *HistoricalEventEntityAllianceFormed) Html(c *context) string { +func (x *HistoricalEventEntityAllianceFormed) Html(c *Context) string { return c.entityList(x.JoiningEnid) + " swore to support " + c.entity(x.InitiatingEnid) + " in war if the latter did likewise" } -func (x *HistoricalEventEntityBreachFeatureLayer) Html(c *context) string { +func (x *HistoricalEventEntityBreachFeatureLayer) Html(c *Context) string { return c.siteCiv(x.SiteEntityId, x.CivEntityId) + " breached the Underworld at " + c.site(x.SiteId, "") } -func (x *HistoricalEventEntityCreated) Html(c *context) string { +func (x *HistoricalEventEntityCreated) Html(c *Context) string { if x.CreatorHfid != -1 { return c.hf(x.CreatorHfid) + " formed " + c.entity(x.EntityId) + c.siteStructure(x.SiteId, x.StructureId, "in") } else { @@ -603,28 +603,28 @@ func (x *HistoricalEventEntityCreated) Html(c *context) string { } } -func (x *HistoricalEventEntityDissolved) Html(c *context) string { +func (x *HistoricalEventEntityDissolved) Html(c *Context) string { return c.entity(x.EntityId) + " dissolved after " + x.Reason.String() } -func (x *HistoricalEventEntityEquipmentPurchase) Html(c *context) string { // todo check hfid +func (x *HistoricalEventEntityEquipmentPurchase) Html(c *Context) string { // todo check hfid return c.entity(x.EntityId) + " purchased " + equipmentLevel(x.NewEquipmentLevel) + " equipment" } -func (x *HistoricalEventEntityExpelsHf) Html(c *context) string { +func (x *HistoricalEventEntityExpelsHf) Html(c *Context) string { return c.entity(x.EntityId) + " expelled " + c.hf(x.Hfid) + c.site(x.SiteId, " from") } -func (x *HistoricalEventEntityFledSite) Html(c *context) string { +func (x *HistoricalEventEntityFledSite) Html(c *Context) string { return c.entity(x.FledCivId) + " fled " + c.site(x.SiteId, "") } -func (x *HistoricalEventEntityIncorporated) Html(c *context) string { // TODO site +func (x *HistoricalEventEntityIncorporated) Html(c *Context) string { // TODO site return c.entity(x.JoinerEntityId) + util.If(x.PartialIncorporation, " began operating at the direction of ", " fully incorporated into ") + c.entity(x.JoinedEntityId) + " under the leadership of " + c.hf(x.LeaderHfid) } -func (x *HistoricalEventEntityLaw) Html(c *context) string { +func (x *HistoricalEventEntityLaw) Html(c *Context) string { switch x.LawAdd { case HistoricalEventEntityLawLawAdd_Harsh: return c.hf(x.HistFigureId) + " laid a series of oppressive edicts upon " + c.entity(x.EntityId) @@ -636,13 +636,13 @@ func (x *HistoricalEventEntityLaw) Html(c *context) string { return c.hf(x.HistFigureId) + " UNKNOWN LAW upon " + c.entity(x.EntityId) } -func (x *HistoricalEventEntityOverthrown) Html(c *context) string { +func (x *HistoricalEventEntityOverthrown) Html(c *Context) string { return c.hf(x.InstigatorHfid) + " toppled the government of " + util.If(x.OverthrownHfid != -1, c.hfRelated(x.OverthrownHfid, x.InstigatorHfid)+" of ", "") + c.entity(x.EntityId) + " and " + util.If(x.PosTakerHfid == x.InstigatorHfid, "assumed control", "placed "+c.hfRelated(x.PosTakerHfid, x.InstigatorHfid)+" in power") + c.site(x.SiteId, " in") + util.If(len(x.ConspiratorHfid) > 0, ". The support of "+c.hfListRelated(x.ConspiratorHfid, x.InstigatorHfid)+" was crucial to the coup", "") } -func (x *HistoricalEventEntityPersecuted) Html(c *context) string { +func (x *HistoricalEventEntityPersecuted) Html(c *Context) string { var l []string if len(x.ExpelledHfid) > 0 { l = append(l, c.hfListRelated(x.ExpelledHfid, x.PersecutorHfid)+util.If(len(x.ExpelledHfid) > 1, " were", " was")+" expelled") @@ -659,31 +659,31 @@ func (x *HistoricalEventEntityPersecuted) Html(c *context) string { util.If(len(l) > 0, ". "+util.Capitalize(andList(l)), "") } -func (x *HistoricalEventEntityPrimaryCriminals) Html(c *context) string { // TODO structure +func (x *HistoricalEventEntityPrimaryCriminals) Html(c *Context) string { // TODO structure return c.entity(x.EntityId) + " became the primary criminal organization in " + c.site(x.SiteId, "") } -func (x *HistoricalEventEntityRampagedInSite) Html(c *context) string { +func (x *HistoricalEventEntityRampagedInSite) Html(c *Context) string { return "the forces of " + c.entity(x.RampageCivId) + " rampaged throughout " + c.site(x.SiteId, "") } -func (x *HistoricalEventEntityRelocate) Html(c *context) string { +func (x *HistoricalEventEntityRelocate) Html(c *Context) string { return c.entity(x.EntityId) + " moved" + c.siteStructure(x.SiteId, x.StructureId, "to") } -func (x *HistoricalEventEntitySearchedSite) Html(c *context) string { +func (x *HistoricalEventEntitySearchedSite) Html(c *Context) string { return c.entity(x.SearcherCivId) + " searched " + c.site(x.SiteId, "") + util.If(x.Result == HistoricalEventEntitySearchedSiteResult_FoundNothing, " and found nothing", "") } -func (x *HistoricalEventFailedFrameAttempt) Html(c *context) string { +func (x *HistoricalEventFailedFrameAttempt) Html(c *Context) string { return c.hf(x.FramerHfid) + " attempted to frame " + c.hfRelated(x.TargetHfid, x.FramerHfid) + " for " + x.Crime.String() + util.If(x.PlotterHfid != -1, " at the behest of "+c.hfRelated(x.PlotterHfid, x.FramerHfid), "") + " by fooling " + c.hfRelated(x.FooledHfid, x.FramerHfid) + " and " + c.entity(x.ConvicterEnid) + " with fabricated evidence, but nothing came of it" } -func (x *HistoricalEventFailedIntrigueCorruption) Html(c *context) string { +func (x *HistoricalEventFailedIntrigueCorruption) Html(c *Context) string { action := "" switch x.Action { case HistoricalEventFailedIntrigueCorruptionAction_BribeOfficial: @@ -744,7 +744,7 @@ func (x *HistoricalEventFailedIntrigueCorruption) Html(c *context) string { util.If(x.FailedJudgmentTest, ", while completely misreading the situation,", "") + " " + method + ". " + fail } -func (x *HistoricalEventFieldBattle) Html(c *context) string { +func (x *HistoricalEventFieldBattle) Html(c *Context) string { atk := c.entity(x.AttackerCivId) def := c.entity(x.DefenderCivId) generals := "" @@ -770,11 +770,11 @@ func (x *HistoricalEventFieldBattle) Html(c *context) string { return fmt.Sprintf("%s attacked %s at %s%s%s", atk, def, c.region(x.SubregionId), generals, mercs) } -func (x *HistoricalEventFirstContact) Html(c *context) string { +func (x *HistoricalEventFirstContact) Html(c *Context) string { return c.entity(x.ContactorEnid) + " made contact with " + c.entity(x.ContactedEnid) + c.site(x.SiteId, " at") } -func (x *HistoricalEventGamble) Html(c *context) string { +func (x *HistoricalEventGamble) Html(c *Context) string { outcome := "" switch d := x.NewAccount - x.OldAccount; { case d <= -5000: @@ -790,15 +790,15 @@ func (x *HistoricalEventGamble) Html(c *context) string { util.If(x.OldAccount >= 0 && x.NewAccount < 0, " and went into debt", "") } -func (x *HistoricalEventHfAbducted) Html(c *context) string { +func (x *HistoricalEventHfAbducted) Html(c *Context) string { return c.hf(x.TargetHfid) + " was abducted " + c.location(x.SiteId, "from", x.SubregionId, "from") + " by " + c.hfRelated(x.SnatcherHfid, x.TargetHfid) } -func (x *HistoricalEventHfAttackedSite) Html(c *context) string { +func (x *HistoricalEventHfAttackedSite) Html(c *Context) string { return c.hf(x.AttackerHfid) + " attacked " + c.siteCiv(x.SiteCivId, x.DefenderCivId) + c.site(x.SiteId, " in") } -func (x *HistoricalEventHfConfronted) Html(c *context) string { +func (x *HistoricalEventHfConfronted) Html(c *Context) string { return c.hf(x.Hfid) + " aroused " + x.Situation.String() + c.location(x.SiteId, " in", x.SubregionId, " in") + " after " + andList(util.Map(x.Reason, func(r HistoricalEventHfConfrontedReason) string { switch r { @@ -811,7 +811,7 @@ func (x *HistoricalEventHfConfronted) Html(c *context) string { })) } -func (x *HistoricalEventHfConvicted) Html(c *context) string { // TODO no_prison_available, interrogator_hfid +func (x *HistoricalEventHfConvicted) Html(c *Context) string { // TODO no_prison_available, interrogator_hfid r := util.If(x.ConfessedAfterApbArrestEnid != -1, "after being recognized and arrested, ", "") switch { case x.SurveiledCoconspirator: @@ -869,11 +869,11 @@ func (x *HistoricalEventHfConvicted) Html(c *context) string { // TODO no_prison return r } -func (x *HistoricalEventHfDestroyedSite) Html(c *context) string { +func (x *HistoricalEventHfDestroyedSite) Html(c *Context) string { return c.hf(x.AttackerHfid) + " routed " + c.siteCiv(x.SiteCivId, x.DefenderCivId) + " and destroyed " + c.site(x.SiteId, "") } -func (x *HistoricalEventHfDied) Html(c *context) string { +func (x *HistoricalEventHfDied) Html(c *Context) string { hf := c.hf(x.Hfid) loc := c.location(x.SiteId, " in", x.SubregionId, " in") slayer := "" @@ -948,7 +948,7 @@ func (x *HistoricalEventHfDied) Html(c *context) string { case HistoricalEventHfDiedCause_Suffocate, HistoricalEventHfDiedCause_Air: return hf + " suffocated, slain by " + slayer + loc case HistoricalEventHfDiedCause_SuicideDrowned, HistoricalEventHfDiedCause_DrownAltTwo: - return hf + " drowned " + util.If(c.world.HistoricalFigures[x.Hfid].Female(), "herself ", "himself ") + loc + return hf + " drowned " + util.If(c.World.HistoricalFigures[x.Hfid].Female(), "herself ", "himself ") + loc case HistoricalEventHfDiedCause_SuicideLeaping, HistoricalEventHfDiedCause_LeaptFromHeight: return hf + " leapt from a great height" + loc case HistoricalEventHfDiedCause_Thirst: @@ -960,11 +960,11 @@ func (x *HistoricalEventHfDied) Html(c *context) string { return hf + " died: " + x.Cause.String() + slayer + loc } -func (x *HistoricalEventHfDisturbedStructure) Html(c *context) string { +func (x *HistoricalEventHfDisturbedStructure) Html(c *Context) string { return c.hf(x.HistFigId) + " disturbed " + c.siteStructure(x.SiteId, x.StructureId, "") } -func (x *HistoricalEventHfDoesInteraction) Html(c *context) string { // TODO ignore source +func (x *HistoricalEventHfDoesInteraction) Html(c *Context) string { // TODO ignore source i := strings.Index(x.InteractionAction, " ") if i > 0 { return c.hf(x.DoerHfid) + " " + x.InteractionAction[:i+1] + c.hfRelated(x.TargetHfid, x.DoerHfid) + x.InteractionAction[i:] + util.If(x.Site != -1, c.site(x.Site, " in"), "") @@ -973,20 +973,20 @@ func (x *HistoricalEventHfDoesInteraction) Html(c *context) string { // TODO ign } } -func (x *HistoricalEventHfEnslaved) Html(c *context) string { +func (x *HistoricalEventHfEnslaved) Html(c *Context) string { return c.hf(x.SellerHfid) + " sold " + c.hfRelated(x.EnslavedHfid, x.SellerHfid) + " to " + c.entity(x.PayerEntityId) + c.site(x.MovedToSiteId, " in") } -func (x *HistoricalEventHfEquipmentPurchase) Html(c *context) string { // TODO site, structure, region +func (x *HistoricalEventHfEquipmentPurchase) Html(c *Context) string { // TODO site, structure, region return c.hf(x.GroupHfid) + " purchased " + equipmentLevel(x.Quality) + " equipment" } -func (x *HistoricalEventHfFreed) Html(c *context) string { +func (x *HistoricalEventHfFreed) Html(c *Context) string { return util.If(x.FreeingHfid != -1, c.hf(x.FreeingHfid), "the forces") + " of " + c.entity(x.FreeingCivId) + " freed " + c.hfList(x.RescuedHfid) + c.site(x.SiteId, " from") + " and " + c.siteCiv(x.SiteCivId, x.HoldingCivId) } -func (x *HistoricalEventHfGainsSecretGoal) Html(c *context) string { +func (x *HistoricalEventHfGainsSecretGoal) Html(c *Context) string { switch x.SecretGoal { case HistoricalEventHfGainsSecretGoalSecretGoal_Immortality: return c.hf(x.Hfid) + " became obsessed with " + c.posessivePronoun(x.Hfid) + " own mortality and sought to extend " + c.posessivePronoun(x.Hfid) + " life by any means" @@ -994,12 +994,12 @@ func (x *HistoricalEventHfGainsSecretGoal) Html(c *context) string { return c.hf(x.Hfid) + " UNKNOWN SECRET GOAL" } -func (x *HistoricalEventHfInterrogated) Html(c *context) string { // TODO wanted_and_recognized, held_firm_in_interrogation, implicated_hfid +func (x *HistoricalEventHfInterrogated) Html(c *Context) string { // TODO wanted_and_recognized, held_firm_in_interrogation, implicated_hfid return c.hf(x.TargetHfid) + " was recognized and arrested by " + c.entity(x.ArrestingEnid) + ". Despite the interrogation by " + c.hfRelated(x.InterrogatorHfid, x.TargetHfid) + ", " + c.hfShort(x.TargetHfid) + " refused to reveal anything and was released" } -func (x *HistoricalEventHfLearnsSecret) Html(c *context) string { +func (x *HistoricalEventHfLearnsSecret) Html(c *Context) string { if x.ArtifactId != -1 { return c.hf(x.StudentHfid) + " learned " + x.SecretText.String() + " from " + c.artifact(x.ArtifactId) } else { @@ -1007,17 +1007,17 @@ func (x *HistoricalEventHfLearnsSecret) Html(c *context) string { } } -func (x *HistoricalEventHfNewPet) Html(c *context) string { +func (x *HistoricalEventHfNewPet) Html(c *Context) string { return c.hf(x.GroupHfid) + " tamed " + articled(x.Pets) + c.location(x.SiteId, " of", x.SubregionId, " of") } -func (x *HistoricalEventHfPerformedHorribleExperiments) Html(c *context) string { +func (x *HistoricalEventHfPerformedHorribleExperiments) Html(c *Context) string { return c.hf(x.GroupHfid) + " performed horrible experiments " + c.place(x.StructureId, x.SiteId, " in", x.SubregionId, " in") } -func (x *HistoricalEventHfPrayedInsideStructure) Html(c *context) string { +func (x *HistoricalEventHfPrayedInsideStructure) Html(c *Context) string { return c.hf(x.HistFigId) + " prayed " + c.siteStructure(x.SiteId, x.StructureId, "inside") } -func (x *HistoricalEventHfPreach) Html(c *context) string { // relevant site +func (x *HistoricalEventHfPreach) Html(c *Context) string { // relevant site topic := "" switch x.Topic { case HistoricalEventHfPreachTopic_Entity1ShouldLoveEntityTwo: @@ -1028,25 +1028,25 @@ func (x *HistoricalEventHfPreach) Html(c *context) string { // relevant site return c.hf(x.SpeakerHfid) + " preached to " + c.entity(x.Entity1) + topic + c.entity(x.Entity2) + c.site(x.SiteHfid, " in") } -func (x *HistoricalEventHfProfanedStructure) Html(c *context) string { +func (x *HistoricalEventHfProfanedStructure) Html(c *Context) string { return c.hf(x.HistFigId) + " profaned " + c.siteStructure(x.SiteId, x.StructureId, "") } -func (x *HistoricalEventHfRansomed) Html(c *context) string { +func (x *HistoricalEventHfRansomed) Html(c *Context) string { return c.hf(x.RansomerHfid) + " ransomed " + c.hfRelated(x.RansomedHfid, x.RansomerHfid) + " to " + util.If(x.PayerHfid != -1, c.hfRelated(x.PayerHfid, x.RansomerHfid), c.entity(x.PayerEntityId)) + ". " + c.hfShort(x.RansomedHfid) + " was sent " + c.site(x.MovedToSiteId, "to") } -func (x *HistoricalEventHfReachSummit) Html(c *context) string { - id, _, _ := util.FindInMap(c.world.MountainPeaks, func(m *MountainPeak) bool { return m.Coords == x.Coords }) +func (x *HistoricalEventHfReachSummit) Html(c *Context) string { + id, _, _ := util.FindInMap(c.World.MountainPeaks, func(m *MountainPeak) bool { return m.Coords == x.Coords }) return c.hfList(x.GroupHfid) + util.If(len(x.GroupHfid) > 1, " were", " was") + " the first to reach the summit of " + c.mountain(id) + " which rises above " + c.region(x.SubregionId) } -func (x *HistoricalEventHfRecruitedUnitTypeForEntity) Html(c *context) string { +func (x *HistoricalEventHfRecruitedUnitTypeForEntity) Html(c *Context) string { return c.hf(x.Hfid) + " recruited " + x.UnitType.String() + "s into " + c.entity(x.EntityId) + c.location(x.SiteId, " in", x.SubregionId, " in") } -func (x *HistoricalEventHfRelationshipDenied) Html(c *context) string { +func (x *HistoricalEventHfRelationshipDenied) Html(c *Context) string { r := c.hf(x.SeekerHfid) switch x.Relationship { case HistoricalEventHfRelationshipDeniedRelationship_Apprentice: @@ -1064,11 +1064,11 @@ func (x *HistoricalEventHfRelationshipDenied) Html(c *context) string { return r } -func (x *HistoricalEventHfReunion) Html(c *context) string { +func (x *HistoricalEventHfReunion) Html(c *Context) string { return c.hf(x.Group1Hfid) + " was reunited with " + c.hfListRelated(x.Group2Hfid, x.Group1Hfid) + c.location(x.SiteId, " in", x.SubregionId, " in") } -func (x *HistoricalEventHfRevived) Html(c *context) string { +func (x *HistoricalEventHfRevived) Html(c *Context) string { r := c.hf(x.Hfid) if x.ActorHfid != -1 { if x.Disturbance { @@ -1083,7 +1083,7 @@ func (x *HistoricalEventHfRevived) Html(c *context) string { c.location(x.SiteId, " in", x.SubregionId, " in") } -func (x *HistoricalEventHfSimpleBattleEvent) Html(c *context) string { +func (x *HistoricalEventHfSimpleBattleEvent) Html(c *Context) string { group1 := c.hf(x.Group1Hfid) group2 := c.hfRelated(x.Group2Hfid, x.Group1Hfid) loc := c.location(x.SiteId, " in", x.SubregionId, " in") @@ -1116,15 +1116,15 @@ func (x *HistoricalEventHfSimpleBattleEvent) Html(c *context) string { return group1 + " attacked " + group2 + loc } -func (x *HistoricalEventHfTravel) Html(c *context) string { +func (x *HistoricalEventHfTravel) Html(c *Context) string { return c.hfList(x.GroupHfid) + util.If(x.Return, " returned", " made a journey") + c.location(x.SiteId, " to", x.SubregionId, " to") } -func (x *HistoricalEventHfViewedArtifact) Html(c *context) string { +func (x *HistoricalEventHfViewedArtifact) Html(c *Context) string { return c.hf(x.HistFigId) + " viewed " + c.artifact(x.ArtifactId) + c.siteStructure(x.SiteId, x.StructureId, " in") } -func (x *HistoricalEventHfWounded) Html(c *context) string { +func (x *HistoricalEventHfWounded) Html(c *Context) string { r := c.hf(x.WoundeeHfid) bp := "UNKNOWN BODYPART" // TODO bodyparts switch x.InjuryType { @@ -1143,7 +1143,7 @@ func (x *HistoricalEventHfWounded) Html(c *context) string { return r + c.hfRelated(x.WounderHfid, x.WoundeeHfid) + c.location(x.SiteId, " in", x.SubregionId, " in") + util.If(x.WasTorture, " as a means of torture", "") } -func (x *HistoricalEventHfsFormedIntrigueRelationship) Html(c *context) string { +func (x *HistoricalEventHfsFormedIntrigueRelationship) Html(c *Context) string { if x.Circumstance == HistoricalEventHfsFormedIntrigueRelationshipCircumstance_IsEntitySubordinate { return c.hf(x.CorruptorHfid) + " subordinated " + c.hfRelated(x.TargetHfid, x.CorruptorHfid) + " as a member of " + c.entity(x.CircumstanceId) + " toward the fullfillment of plots and schemes" + c.location(x.SiteId, " in", x.SubregionId, " in") @@ -1205,7 +1205,7 @@ func (x *HistoricalEventHfsFormedIntrigueRelationship) Html(c *context) string { " and " + method + ". " + success } -func (x *HistoricalEventHfsFormedReputationRelationship) Html(c *context) string { +func (x *HistoricalEventHfsFormedReputationRelationship) Html(c *Context) string { hf1 := c.hf(x.Hfid1) + util.If(x.IdentityId1 != -1, " as "+c.fullIdentity(x.IdentityId1), "") hf2 := c.hfRelated(x.Hfid2, x.Hfid1) + util.If(x.IdentityId2 != -1, " as "+c.fullIdentity(x.IdentityId2), "") loc := c.location(x.SiteId, " in", x.SubregionId, " in") @@ -1218,10 +1218,10 @@ func (x *HistoricalEventHfsFormedReputationRelationship) Html(c *context) string return hf1 + " and " + hf2 + ", formed an UNKNOWN RELATIONSHIP" + loc } -func (x *HistoricalEventHolyCityDeclaration) Html(c *context) string { +func (x *HistoricalEventHolyCityDeclaration) Html(c *Context) string { return c.entity(x.ReligionId) + " declared " + c.site(x.SiteId, "") + " to be a holy site" } -func (x *HistoricalEventInsurrectionStarted) Html(c *context) string { +func (x *HistoricalEventInsurrectionStarted) Html(c *Context) string { e := util.If(x.TargetCivId != -1, c.entity(x.TargetCivId), "the local government") switch x.Outcome { case HistoricalEventInsurrectionStartedOutcome_LeadershipOverthrown: @@ -1232,7 +1232,7 @@ func (x *HistoricalEventInsurrectionStarted) Html(c *context) string { return "an insurrection against " + e + " began " + c.site(x.SiteId, "in") } } -func (x *HistoricalEventItemStolen) Html(c *context) string { +func (x *HistoricalEventItemStolen) Html(c *Context) string { i := util.If(x.Item != -1, c.artifact(x.Item), articled(x.Mat+" "+x.ItemType)) circumstance := "" if x.Circumstance != nil { @@ -1257,35 +1257,35 @@ func (x *HistoricalEventItemStolen) Html(c *context) string { util.If(x.StashSite != -1, " and brought "+c.site(x.StashSite, "to"), "") } -func (x *HistoricalEventKnowledgeDiscovered) Html(c *context) string { +func (x *HistoricalEventKnowledgeDiscovered) Html(c *Context) string { return c.hf(x.Hfid) + util.If(x.First, " was the very first to discover ", " independently discovered ") + x.Knowledge } -func (x *HistoricalEventMasterpieceArchConstructed) Html(c *context) string { +func (x *HistoricalEventMasterpieceArchConstructed) Html(c *Context) string { return c.hf(x.Hfid) + " constructed a masterful " + util.If(x.BuildingSubtype != HistoricalEventMasterpieceArchConstructedBuildingSubtype_Unknown, x.BuildingSubtype.String(), x.BuildingType.String()) + " for " + c.entity(x.EntityId) + c.site(x.SiteId, " in") } -func (x *HistoricalEventMasterpieceDye) Html(c *context) string { +func (x *HistoricalEventMasterpieceDye) Html(c *Context) string { return c.hf(x.Hfid) + " masterfully dyed a " + x.Mat.String() + " " + x.ItemType.String() + " with " + x.DyeMat + " for " + c.entity(x.EntityId) + c.site(x.SiteId, " in") } -func (x *HistoricalEventMasterpieceEngraving) Html(c *context) string { +func (x *HistoricalEventMasterpieceEngraving) Html(c *Context) string { return c.hf(x.Hfid) + " created a masterful " + "engraving" + " for " + c.entity(x.EntityId) + c.site(x.SiteId, " in") } -func (x *HistoricalEventMasterpieceFood) Html(c *context) string { +func (x *HistoricalEventMasterpieceFood) Html(c *Context) string { return c.hf(x.Hfid) + " prepared a masterful " + x.ItemSubtype.String() + " for " + c.entity(x.EntityId) + c.site(x.SiteId, " in") } -func (x *HistoricalEventMasterpieceItem) Html(c *context) string { +func (x *HistoricalEventMasterpieceItem) Html(c *Context) string { return c.hf(x.Hfid) + " created a masterful " + x.Mat + " " + util.If(x.ItemSubtype != "", x.ItemSubtype, x.ItemType) + " for " + c.entity(x.EntityId) + c.site(x.SiteId, " in") } -func (x *HistoricalEventMasterpieceItemImprovement) Html(c *context) string { +func (x *HistoricalEventMasterpieceItemImprovement) Html(c *Context) string { i := "" switch x.ImprovementType { case HistoricalEventMasterpieceItemImprovementImprovementType_ArtImage: @@ -1305,8 +1305,8 @@ func (x *HistoricalEventMasterpieceItemImprovement) Html(c *context) string { articled(x.Mat+" "+util.If(x.ItemSubtype != "", x.ItemSubtype, x.ItemType)) + " for " + c.entity(x.EntityId) + c.site(x.SiteId, " in") } -func (x *HistoricalEventMasterpieceLost) Html(c *context) string { - if e, ok := c.world.HistoricalEvents[x.CreationEvent]; ok { +func (x *HistoricalEventMasterpieceLost) Html(c *Context) string { + if e, ok := c.World.HistoricalEvents[x.CreationEvent]; ok { switch y := e.Details.(type) { case *HistoricalEventMasterpieceArchConstructed: return "the " + util.If(y.BuildingSubtype != HistoricalEventMasterpieceArchConstructedBuildingSubtype_Unknown, y.BuildingSubtype.String(), y.BuildingType.String()) + @@ -1330,17 +1330,17 @@ func (x *HistoricalEventMasterpieceLost) Html(c *context) string { return c.hf(x.Histfig) + " destroyed a masterful item" + c.site(x.Site, " in") } -func (x *HistoricalEventMerchant) Html(c *context) string { +func (x *HistoricalEventMerchant) Html(c *Context) string { return "merchants from " + c.entity(x.TraderEntityId) + " visited " + c.entity(x.DepotEntityId) + c.site(x.SiteId, " at") + util.If(x.Hardship, " and suffered great hardship", "") + util.If(x.LostValue, ". They reported irregularities with their goods", "") } -func (x *HistoricalEventModifiedBuilding) Html(c *context) string { +func (x *HistoricalEventModifiedBuilding) Html(c *Context) string { return c.hf(x.ModifierHfid) + " had " + articled(x.Modification.String()) + " added " + c.siteStructure(x.SiteId, x.StructureId, "to") } -func (x *HistoricalEventMusicalFormCreated) Html(c *context) string { +func (x *HistoricalEventMusicalFormCreated) Html(c *Context) string { reason := "" switch x.Reason { case HistoricalEventMusicalFormCreatedReason_GlorifyHf: @@ -1360,22 +1360,22 @@ func (x *HistoricalEventMusicalFormCreated) Html(c *context) string { return c.musicalForm(x.FormId) + " was created by " + c.hf(x.HistFigureId) + c.site(x.SiteId, " in") + reason + circumstance } -func (x *HistoricalEventNewSiteLeader) Html(c *context) string { +func (x *HistoricalEventNewSiteLeader) Html(c *Context) string { return c.entity(x.AttackerCivId) + " defeated " + c.siteCiv(x.SiteCivId, x.DefenderCivId) + " and placed " + c.hf(x.NewLeaderHfid) + " in charge of" + c.site(x.SiteId, "") + ". The new government was called " + c.entity(x.NewSiteCivId) } -func (x *HistoricalEventPeaceAccepted) Html(c *context) string { +func (x *HistoricalEventPeaceAccepted) Html(c *Context) string { return c.entity(x.Destination) + " accepted an offer of peace from " + c.entity(x.Source) } -func (x *HistoricalEventPeaceRejected) Html(c *context) string { +func (x *HistoricalEventPeaceRejected) Html(c *Context) string { return c.entity(x.Destination) + " rejected an offer of peace from " + c.entity(x.Source) } -func (x *HistoricalEventPerformance) Html(c *context) string { +func (x *HistoricalEventPerformance) Html(c *Context) string { r := c.entity(x.CivId) + " held " - if e, ok := c.world.Entities[x.CivId]; ok { + if e, ok := c.World.Entities[x.CivId]; ok { o := e.Occasion[x.OccasionId] s := o.Schedule[x.ScheduleId] r += c.schedule(s) @@ -1386,11 +1386,11 @@ func (x *HistoricalEventPerformance) Html(c *context) string { return r } -func (x *HistoricalEventPlunderedSite) Html(c *context) string { // TODO no_defeat_mention, took_items, took_livestock, was_raid +func (x *HistoricalEventPlunderedSite) Html(c *Context) string { // TODO no_defeat_mention, took_items, took_livestock, was_raid return c.entity(x.AttackerCivId) + " defeated " + c.siteCiv(x.SiteCivId, x.DefenderCivId) + " and pillaged " + c.site(x.SiteId, "") } -func (x *HistoricalEventPoeticFormCreated) Html(c *context) string { +func (x *HistoricalEventPoeticFormCreated) Html(c *Context) string { circumstance := "" switch x.Circumstance { case HistoricalEventPoeticFormCreatedCircumstance_Dream: @@ -1401,9 +1401,9 @@ func (x *HistoricalEventPoeticFormCreated) Html(c *context) string { return c.poeticForm(x.FormId) + " was created by " + c.hf(x.HistFigureId) + c.site(x.SiteId, " in") + circumstance } -func (x *HistoricalEventProcession) Html(c *context) string { +func (x *HistoricalEventProcession) Html(c *Context) string { r := c.entity(x.CivId) + " held a procession in " + c.site(x.SiteId, "") - if e, ok := c.world.Entities[x.CivId]; ok { + if e, ok := c.World.Entities[x.CivId]; ok { o := e.Occasion[x.OccasionId] r += " as part of " + o.Name() s := o.Schedule[x.ScheduleId] @@ -1423,22 +1423,22 @@ func (x *HistoricalEventProcession) Html(c *context) string { return r } -func (x *HistoricalEventRazedStructure) Html(c *context) string { +func (x *HistoricalEventRazedStructure) Html(c *Context) string { return c.entity(x.CivId) + " razed " + c.siteStructure(x.SiteId, x.StructureId, "") } -func (x *HistoricalEventReclaimSite) Html(c *context) string { +func (x *HistoricalEventReclaimSite) Html(c *Context) string { if x.Unretire { return c.siteCiv(x.SiteCivId, x.CivId) + " were taken by a mood to act against their judgment " + c.site(x.SiteId, "at") } return c.siteCiv(x.SiteCivId, x.CivId) + " launched an expedition to reclaim " + c.site(x.SiteId, "") } -func (x *HistoricalEventRegionpopIncorporatedIntoEntity) Html(c *context) string { // TODO Race +func (x *HistoricalEventRegionpopIncorporatedIntoEntity) Html(c *Context) string { // TODO Race return strconv.Itoa(x.PopNumberMoved) + " of " + strconv.Itoa(x.PopRace) + " from " + c.region(x.PopSrid) + " joined with " + c.entity(x.JoinEntityId) + c.site(x.SiteId, " at") } -func (x *HistoricalEventRemoveHfEntityLink) Html(c *context) string { +func (x *HistoricalEventRemoveHfEntityLink) Html(c *Context) string { hf := c.hf(x.Hfid) civ := c.entity(x.CivId) switch x.Link { @@ -1454,11 +1454,11 @@ func (x *HistoricalEventRemoveHfEntityLink) Html(c *context) string { return hf + " left " + civ } -func (x *HistoricalEventRemoveHfHfLink) Html(c *context) string { // divorced +func (x *HistoricalEventRemoveHfHfLink) Html(c *Context) string { // divorced return c.hf(x.Hfid) + " and " + c.hfRelated(x.HfidTarget, x.Hfid) + " broke up" } -func (x *HistoricalEventRemoveHfSiteLink) Html(c *context) string { +func (x *HistoricalEventRemoveHfSiteLink) Html(c *Context) string { switch x.LinkType { case HistoricalEventRemoveHfSiteLinkLinkType_HomeSiteAbstractBuilding: return c.hf(x.Histfig) + " moved out " + c.siteStructure(x.SiteId, x.Structure, "of") @@ -1470,46 +1470,46 @@ func (x *HistoricalEventRemoveHfSiteLink) Html(c *context) string { return c.hf(x.Histfig) + " stopped working " + c.siteStructure(x.SiteId, x.Structure, "at") } -func (x *HistoricalEventReplacedStructure) Html(c *context) string { +func (x *HistoricalEventReplacedStructure) Html(c *Context) string { return c.siteCiv(x.SiteCivId, x.CivId) + " replaced " + c.siteStructure(x.SiteId, x.OldAbId, "") + " with " + c.structure(x.SiteId, x.NewAbId) } -func (x *HistoricalEventSiteDied) Html(c *context) string { +func (x *HistoricalEventSiteDied) Html(c *Context) string { return c.siteCiv(x.SiteCivId, x.CivId) + " abandonned the settlement of " + c.site(x.SiteId, "") } -func (x *HistoricalEventSiteDispute) Html(c *context) string { +func (x *HistoricalEventSiteDispute) Html(c *Context) string { return c.entity(x.EntityId1) + " of " + c.site(x.SiteId1, "") + " and " + c.entity(x.EntityId2) + " of " + c.site(x.SiteId2, "") + " became embroiled in a dispute over " + x.Dispute.String() } -func (x *HistoricalEventSiteRetired) Html(c *context) string { +func (x *HistoricalEventSiteRetired) Html(c *Context) string { return c.siteCiv(x.SiteCivId, x.CivId) + " at the settlement " + c.site(x.SiteId, "of") + " regained their senses after " + util.If(x.First, "an initial", "another") + " period of questionable judgment" } -func (x *HistoricalEventSiteSurrendered) Html(c *context) string { +func (x *HistoricalEventSiteSurrendered) Html(c *Context) string { return c.siteCiv(x.SiteCivId, x.DefenderCivId) + " surrendered " + c.site(x.SiteId, "") + " to " + c.entity(x.AttackerCivId) } -func (x *HistoricalEventSiteTakenOver) Html(c *context) string { +func (x *HistoricalEventSiteTakenOver) Html(c *Context) string { return c.entity(x.AttackerCivId) + " defeated " + c.siteCiv(x.SiteCivId, x.DefenderCivId) + " and took over " + c.site(x.SiteId, "") + ". The new government was called " + c.entity(x.NewSiteCivId) } -func (x *HistoricalEventSiteTributeForced) Html(c *context) string { +func (x *HistoricalEventSiteTributeForced) Html(c *Context) string { return c.entity(x.AttackerCivId) + " secured tribute from " + c.siteCiv(x.SiteCivId, x.DefenderCivId) + util.If(x.SiteId != -1, ", to be delivered"+c.site(x.SiteId, " from"), "") + util.If(x.Season != HistoricalEventSiteTributeForcedSeason_Unknown, " every "+x.Season.String(), "") } -func (x *HistoricalEventSneakIntoSite) Html(c *context) string { +func (x *HistoricalEventSneakIntoSite) Html(c *Context) string { return util.If(x.AttackerCivId != -1, c.entity(x.AttackerCivId), "an unknown civilization") + " slipped " + c.site(x.SiteId, "into") + util.If(x.SiteCivId != -1 || x.DefenderCivId != -1, ", undetected by "+c.siteCiv(x.SiteCivId, x.DefenderCivId), "") } -func (x *HistoricalEventSpottedLeavingSite) Html(c *context) string { +func (x *HistoricalEventSpottedLeavingSite) Html(c *Context) string { return c.hf(x.SpotterHfid) + " of " + c.entity(x.SiteCivId) + " spotted the forces of " + util.If(x.LeaverCivId != -1, c.entity(x.LeaverCivId), "an unknown civilization") + " slipping out of " + c.site(x.SiteId, "") } -func (x *HistoricalEventSquadVsSquad) Html(c *context) string { // TODO a_leader_hfid +func (x *HistoricalEventSquadVsSquad) Html(c *Context) string { // TODO a_leader_hfid return c.hfList(x.AHfid) + " clashed with " + util.If(len(x.DHfid) > 0, c.hfList(x.DHfid), fmt.Sprintf("%d race_%d", x.DNumber, x.DRace)) + c.site(x.SiteId, " in") + @@ -1533,7 +1533,7 @@ func plan(diff int) string { // TODO not exact } } -func (x *HistoricalEventTacticalSituation) Html(c *context) string { +func (x *HistoricalEventTacticalSituation) Html(c *Context) string { r := "" if x.ATacticianHfid == -1 && x.DTacticianHfid == -1 { r = "the forces shifted" @@ -1564,7 +1564,7 @@ func (x *HistoricalEventTacticalSituation) Html(c *context) string { return r + c.site(x.SiteId, " in") } -func (x *HistoricalEventTrade) Html(c *context) string { +func (x *HistoricalEventTrade) Html(c *Context) string { outcome := "" switch d := x.AccountShift; { case d > 1000: @@ -1577,7 +1577,7 @@ func (x *HistoricalEventTrade) Html(c *context) string { return c.hf(x.TraderHfid) + util.If(x.TraderEntityId != -1, " of "+c.entity(x.TraderEntityId), "") + outcome + " trading" + c.site(x.SourceSiteId, " from") + c.site(x.DestSiteId, " to") } -func (x *HistoricalEventWrittenContentComposed) Html(c *context) string { +func (x *HistoricalEventWrittenContentComposed) Html(c *Context) string { reason := "" switch x.Reason { case HistoricalEventWrittenContentComposedReason_GlorifyHf: diff --git a/backend/model/functions.go b/backend/model/functions.go index 1643b4a..63ac9e4 100644 --- a/backend/model/functions.go +++ b/backend/model/functions.go @@ -7,10 +7,10 @@ import ( "strings" ) -var LinkHf = func(w *DfWorld, id int) template.HTML { return template.HTML((&context{world: w}).hf(id)) } -var LinkEntity = func(w *DfWorld, id int) template.HTML { return template.HTML((&context{world: w}).entity(id)) } -var LinkSite = func(w *DfWorld, id int) template.HTML { return template.HTML((&context{world: w}).site(id, "")) } -var LinkRegion = func(w *DfWorld, id int) template.HTML { return template.HTML((&context{world: w}).region(id)) } +var LinkHf = func(w *DfWorld, id int) template.HTML { return template.HTML((&Context{World: w}).hf(id)) } +var LinkEntity = func(w *DfWorld, id int) template.HTML { return template.HTML((&Context{World: w}).entity(id)) } +var LinkSite = func(w *DfWorld, id int) template.HTML { return template.HTML((&Context{World: w}).site(id, "")) } +var LinkRegion = func(w *DfWorld, id int) template.HTML { return template.HTML((&Context{World: w}).region(id)) } func andList(list []string) string { if len(list) > 1 { diff --git a/backend/server/resource.go b/backend/server/resource.go index 2c85332..6ec2cbc 100644 --- a/backend/server/resource.go +++ b/backend/server/resource.go @@ -2,17 +2,42 @@ package server import ( "fmt" + "io/fs" "net/http" "strconv" "github.com/gorilla/mux" + "github.com/robertjanetzko/LegendsBrowser2/backend/model" + "github.com/robertjanetzko/LegendsBrowser2/backend/templates" ) type Parms map[string]string -func (srv *DfServer) RegisterPage(path string, template string, accessor func(Parms) any) { +// func (srv *DfServer) RegisterPage(path string, template string, accessor func(Parms) any) { +// get := func(w http.ResponseWriter, r *http.Request) { +// err := srv.templates.Render(w, template, accessor(mux.Vars(r))) +// if err != nil { +// fmt.Fprintln(w, err) +// fmt.Println(err) +// } +// } + +// srv.router.HandleFunc(path, get).Methods("GET") +// } + +func (srv *DfServer) RegisterWorldPage(path string, template string, accessor func(Parms) any) { get := func(w http.ResponseWriter, r *http.Request) { - err := srv.templates.Render(w, template, accessor(mux.Vars(r))) + if srv.context.world == nil { + srv.renderLoading(w, r) + return + } + + td := &templates.TemplateData{ + Context: &model.Context{World: srv.context.world}, + Data: accessor(mux.Vars(r)), + } + + err := srv.templates.Render(w, template, td) if err != nil { fmt.Fprintln(w, err) fmt.Println(err) @@ -22,9 +47,26 @@ func (srv *DfServer) RegisterPage(path string, template string, accessor func(Pa srv.router.HandleFunc(path, get).Methods("GET") } -func (srv *DfServer) RegisterResourcePage(path string, template string, accessor func(int) any) { - srv.RegisterPage(path, template, func(params Parms) any { +func (srv *DfServer) RegisterWorldResourcePage(path string, template string, accessor func(int) any) { + srv.RegisterWorldPage(path, template, func(params Parms) any { id, _ := strconv.Atoi(params["id"]) return accessor(id) }) } + +type paths struct { + Current string + List []fs.FileInfo +} + +func (srv *DfServer) renderLoading(w http.ResponseWriter, r *http.Request) { + if srv.context.isLoading { + err := srv.templates.Render(w, "loading.html", nil) + if err != nil { + fmt.Fprintln(w, err) + fmt.Println(err) + } + } else { + http.Redirect(w, r, "/load", http.StatusSeeOther) + } +} diff --git a/backend/server/server.go b/backend/server/server.go index 658be8f..ab92f71 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -4,34 +4,48 @@ import ( "embed" "fmt" "io/fs" + "io/ioutil" "net/http" "os" + "strings" "github.com/gorilla/mux" "github.com/robertjanetzko/LegendsBrowser2/backend/model" "github.com/robertjanetzko/LegendsBrowser2/backend/templates" ) +type DfServerContext struct { + world *model.DfWorld + isLoading bool +} + type DfServer struct { router *mux.Router templates *templates.Template - world *model.DfWorld + context *DfServerContext } func StartServer(world *model.DfWorld, static embed.FS) { srv := &DfServer{ router: mux.NewRouter().StrictSlash(true), - world: world, + context: &DfServerContext{ + world: world, + isLoading: false, + }, } srv.LoadTemplates() - srv.RegisterResourcePage("/entity/{id}", "entity.html", func(id int) any { return srv.world.Entities[id] }) - srv.RegisterResourcePage("/hf/{id}", "hf.html", func(id int) any { return srv.world.HistoricalFigures[id] }) - srv.RegisterResourcePage("/region/{id}", "region.html", func(id int) any { return srv.world.Regions[id] }) - srv.RegisterResourcePage("/site/{id}", "site.html", func(id int) any { return srv.world.Sites[id] }) - srv.RegisterResourcePage("/artifact/{id}", "artifact.html", func(id int) any { return srv.world.Artifacts[id] }) - srv.RegisterPage("/events", "eventTypes.html", func(p Parms) any { return srv.world.AllEventTypes() }) - srv.RegisterPage("/events/{type}", "eventType.html", func(p Parms) any { return srv.world.EventsOfType(p["type"]) }) + srv.RegisterWorldResourcePage("/entity/{id}", "entity.html", func(id int) any { return srv.context.world.Entities[id] }) + srv.RegisterWorldResourcePage("/hf/{id}", "hf.html", func(id int) any { return srv.context.world.HistoricalFigures[id] }) + srv.RegisterWorldResourcePage("/region/{id}", "region.html", func(id int) any { return srv.context.world.Regions[id] }) + srv.RegisterWorldResourcePage("/site/{id}", "site.html", func(id int) any { return srv.context.world.Sites[id] }) + srv.RegisterWorldResourcePage("/artifact/{id}", "artifact.html", func(id int) any { return srv.context.world.Artifacts[id] }) + + srv.RegisterWorldPage("/", "eventTypes.html", func(p Parms) any { return srv.context.world.AllEventTypes() }) + srv.RegisterWorldPage("/events", "eventTypes.html", func(p Parms) any { return srv.context.world.AllEventTypes() }) + srv.RegisterWorldPage("/events/{type}", "eventType.html", func(p Parms) any { return srv.context.world.EventsOfType(p["type"]) }) + + srv.router.PathPrefix("/load").Handler(loadHandler{server: srv}) spa := spaHandler{staticFS: static, staticPath: "static", indexPath: "index.html"} srv.router.PathPrefix("/").Handler(spa) @@ -88,3 +102,46 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // otherwise, use http.FileServer to serve the static dir http.FileServer(http.FS(statics)).ServeHTTP(w, r) } + +type loadHandler struct { + server *DfServer +} + +func (h loadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + path := r.URL.Query().Get("p") + + p := &paths{ + Current: path, + } + if p.Current == "" { + p.Current = "." + } + + if f, err := os.Stat(p.Current); err == nil { + if f.IsDir() { + p.List, err = ioutil.ReadDir(p.Current) + if err != nil { + fmt.Fprintln(w, err) + fmt.Println(err) + } + + err = h.server.templates.Render(w, "load.html", &templates.TemplateData{Data: p}) + if err != nil { + fmt.Fprintln(w, err) + fmt.Println(err) + } + return + } else { + h.server.context.isLoading = true + wrld, _ := model.Parse(p.Current) + h.server.context.world = wrld + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + } + http.Redirect(w, r, "/load", http.StatusSeeOther) +} + +func isLegendsXml(f fs.FileInfo) bool { + return strings.HasSuffix(f.Name(), "-legends.xml") +} diff --git a/backend/server/templates.go b/backend/server/templates.go index 4151d3a..796fa1c 100644 --- a/backend/server/templates.go +++ b/backend/server/templates.go @@ -3,6 +3,7 @@ package server import ( "fmt" "html/template" + "net/url" "github.com/robertjanetzko/LegendsBrowser2/backend/model" "github.com/robertjanetzko/LegendsBrowser2/backend/templates" @@ -19,17 +20,23 @@ func (srv *DfServer) LoadTemplates() { return nil }, "title": util.Title, - "hf": func(id int) template.HTML { return model.LinkHf(srv.world, id) }, - "getHf": func(id int) *model.HistoricalFigure { return srv.world.HistoricalFigures[id] }, - "entity": func(id int) template.HTML { return model.LinkEntity(srv.world, id) }, - "getEntity": func(id int) *model.Entity { return srv.world.Entities[id] }, - "site": func(id int) template.HTML { return model.LinkSite(srv.world, id) }, - "getSite": func(id int) *model.Site { return srv.world.Sites[id] }, - "region": func(id int) template.HTML { return model.LinkRegion(srv.world, id) }, - "getRegion": func(id int) *model.Region { return srv.world.Regions[id] }, - "events": func(obj any) *model.EventList { return model.NewEventList(srv.world, obj) }, - "season": model.Season, - "time": model.Time, + "hf": func(id int) template.HTML { return model.LinkHf(srv.context.world, id) }, + "getHf": func(id int) *model.HistoricalFigure { return srv.context.world.HistoricalFigures[id] }, + "entity": func(id int) template.HTML { return model.LinkEntity(srv.context.world, id) }, + "getEntity": func(id int) *model.Entity { return srv.context.world.Entities[id] }, + "site": func(id int) template.HTML { return model.LinkSite(srv.context.world, id) }, + "getSite": func(id int) *model.Site { return srv.context.world.Sites[id] }, + "region": func(id int) template.HTML { return model.LinkRegion(srv.context.world, id) }, + "getRegion": func(id int) *model.Region { return srv.context.world.Regions[id] }, + "events": func(obj any) *model.EventList { + fmt.Println("W", srv.context.world) + return model.NewEventList(srv.context.world, obj) + }, + "season": model.Season, + "time": model.Time, + "url": url.PathEscape, + "query": url.QueryEscape, + "isLegendsXml": isLegendsXml, "html": func(value any) template.HTML { return template.HTML(fmt.Sprint(value)) }, diff --git a/backend/templates/events.html b/backend/templates/events.html index c2322b4..8714faf 100644 --- a/backend/templates/events.html +++ b/backend/templates/events.html @@ -1,5 +1,8 @@ +Eventss +{{ json .Context }} +