diff --git a/backend/main.go b/backend/main.go index dd76a1c..75f619d 100644 --- a/backend/main.go +++ b/backend/main.go @@ -19,6 +19,7 @@ import ( "github.com/pkg/profile" "github.com/robertjanetzko/LegendsBrowser2/backend/model" "github.com/robertjanetzko/LegendsBrowser2/backend/templates" + "github.com/robertjanetzko/LegendsBrowser2/backend/util" ) var world *model.DfWorld @@ -165,9 +166,10 @@ func main() { // server.RegisterResource(router, "poeticForm", world.PoeticForms) // server.RegisterResource(router, "written", world.WrittenContents) - RegisterPage(router, "/entity/{id}", t, "entity.html", func(id int) any { return world.Entities[id] }) - RegisterPage(router, "/hf/{id}", t, "hf.html", func(id int) any { return world.HistoricalFigures[id] }) - + RegisterResourcePage(router, "/entity/{id}", t, "entity.html", func(id int) any { return world.Entities[id] }) + RegisterResourcePage(router, "/hf/{id}", t, "hf.html", func(id int) any { return world.HistoricalFigures[id] }) + RegisterPage(router, "/events", t, "eventTypes.html", func(p Parms) any { return allEventTypes() }) + RegisterPage(router, "/events/{type}", t, "eventType.html", func(p Parms) any { return eventsOfType(p["type"]) }) } spa := spaHandler{staticFS: static, staticPath: "static", indexPath: "index.html"} @@ -175,18 +177,42 @@ func main() { fmt.Println("Serving at :8080") http.ListenAndServe(":8080", router) - } -func RegisterPage(router *mux.Router, path string, templates *templates.Template, template string, accessor func(int) any) { - get := func(w http.ResponseWriter, r *http.Request) { - id, err := strconv.Atoi(mux.Vars(r)["id"]) - if err != nil { - fmt.Fprintln(w, err) - fmt.Println(err) +func allEventTypes() []string { + types := make(map[string]bool) + for _, e := range world.HistoricalEvents { + types[e.Details.Type()] = true + } + var list = util.Keys(types) + sort.Strings(list) + return list +} + +func eventsOfType(t string) any { + var list []*model.HistoricalEvent + for _, e := range world.HistoricalEvents { + if e.Details.Type() == t { + list = append(list, e) } - fmt.Println("render", template, id) - err = templates.Render(w, template, accessor(id)) + } + + sort.Slice(list, func(i, j int) bool { return list[i].Id_ < list[j].Id_ }) + + return struct { + Type string + Events []*model.HistoricalEvent + }{ + Type: t, + Events: list, + } +} + +type Parms map[string]string + +func RegisterPage(router *mux.Router, path string, templates *templates.Template, template string, accessor func(Parms) any) { + get := func(w http.ResponseWriter, r *http.Request) { + err := templates.Render(w, template, accessor(mux.Vars(r))) if err != nil { fmt.Fprintln(w, err) fmt.Println(err) @@ -196,6 +222,13 @@ func RegisterPage(router *mux.Router, path string, templates *templates.Template router.HandleFunc(path, get).Methods("GET") } +func RegisterResourcePage(router *mux.Router, path string, templates *templates.Template, template string, accessor func(int) any) { + RegisterPage(router, path, templates, template, func(params Parms) any { + id, _ := strconv.Atoi(params["id"]) + return accessor(id) + }) +} + type spaHandler struct { staticFS embed.FS staticPath string diff --git a/backend/model/events.go b/backend/model/events.go index 58ab311..51e00bb 100644 --- a/backend/model/events.go +++ b/backend/model/events.go @@ -1,11 +1,69 @@ package model +import ( + "fmt" + "strings" +) + +func andList(list []string) string { + if len(list) > 1 { + return strings.Join(list[:len(list)-1], ", ") + " and " + list[len(list)-1] + } + return strings.Join(list, ", ") +} + +func (x *Honor) Requirement() string { + var list []string + if x.GrantedToEverybody { + list = append(list, "attaining sufficent skill with a weapon or technique") + } + // if x.RequiredSkill { // TODO + + // } + if x.RequiredBattles == 1 { + list = append(list, "serving in combat") + } + if x.RequiredBattles > 1 { + list = append(list, fmt.Sprintf("participating in %d battles", x.RequiredBattles)) + } + if x.RequiredYears >= 1 { + list = append(list, fmt.Sprintf("%d years of membership", x.RequiredYears)) + } + if x.RequiredKills >= 1 { + list = append(list, fmt.Sprintf("%d KILLS", x.RequiredKills)) // TODO + } + + return " after " + andList(list) +} + func (x *HistoricalEventAddHfEntityHonor) Html() string { - return "UNKNWON HistoricalEventAddHfEntityHonor" + e := world.Entities[x.EntityId] + h := e.Honor[x.HonorId] + return fmt.Sprintf("%s received the title %s of %s%s", hf(x.Hfid), h.Name(), entity(x.EntityId), h.Requirement()) } func (x *HistoricalEventAddHfEntityLink) Html() string { - return "UNKNWON HistoricalEventAddHfEntityLink" + h := hf(x.Hfid) + c := entity(x.CivId) + if x.AppointerHfid != -1 { + c += fmt.Sprintf(", appointed by %s", hf(x.AppointerHfid)) + } + switch x.Link { + case HistoricalEventAddHfEntityLinkLink_Enemy: + return h + " became an enemy of " + c + case HistoricalEventAddHfEntityLinkLink_Member: + return h + " became a member of " + c + case HistoricalEventAddHfEntityLinkLink_Position: + return h + " became " + world.Entities[x.CivId].Position(x.PositionId).GenderName(world.HistoricalFigures[x.Hfid]) + " of " + c + case HistoricalEventAddHfEntityLinkLink_Prisoner: + return h + " was imprisoned by " + c + case HistoricalEventAddHfEntityLinkLink_Slave: + return h + " was enslaved by " + c + case HistoricalEventAddHfEntityLinkLink_Squad: + return h + " SQUAD " + c // TODO + } + return h + " became SOMETHING of " + c } + func (x *HistoricalEventAddHfHfLink) Html() string { return "UNKNWON HistoricalEventAddHfHfLink" } func (x *HistoricalEventAddHfSiteLink) Html() string { return "UNKNWON HistoricalEventAddHfSiteLink" } func (x *HistoricalEventAgreementFormed) Html() string { diff --git a/backend/model/extensions.go b/backend/model/extensions.go index 3e52756..9477f9f 100644 --- a/backend/model/extensions.go +++ b/backend/model/extensions.go @@ -11,10 +11,29 @@ func (e *Entity) Position(id int) *EntityPosition { return nil } +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" +} + type HistoricalEventDetails interface { RelatedToEntity(int) bool RelatedToHf(int) bool Html() string + Type() string } type HistoricalEventCollectionDetails interface { @@ -31,6 +50,13 @@ func containsInt(list []int, id int) bool { var world *DfWorld +func entity(id int) string { + if x, ok := world.Entities[id]; ok { + return fmt.Sprintf(`%s`, x.Id(), x.Name()) + } + return "UNKNOWN ENTITY" +} + func hf(id int) string { if x, ok := world.HistoricalFigures[id]; ok { return fmt.Sprintf(`%s`, x.Id(), x.Name()) diff --git a/backend/same.json b/backend/same.json index 8181616..18bffaf 100644 --- a/backend/same.json +++ b/backend/same.json @@ -1,7 +1,4 @@ { - "Artifact": { - "Writing": "StructureLocalId" - }, "HistoricalEventAddHfEntityLink": { "LinkType": "Link" }, @@ -16,19 +13,6 @@ "Involved": "SiteId", "Site": "SiteId" }, - "HistoricalEventHfDoesInteraction": { - "InteractionAction": "Interaction" - }, - "HistoricalEventMasterpieceItem": { - "Maker": "Hfid", - "MakerEntity": "EntityId", - "Site": "SiteId" - }, - "HistoricalEventMerchant": { - "Destination": "DepotEntityId", - "Site": "SiteId", - "Source": "TraderEntityId" - }, "HistoricalEventPeaceAccepted": { "Site": "SiteId" }, diff --git a/backend/templates/entity.html b/backend/templates/entity.html index f8cc1ce..47593f1 100644 --- a/backend/templates/entity.html +++ b/backend/templates/entity.html @@ -8,7 +8,7 @@

Events

-{{ template "events.html" . }} +{{ template "events.html" events . }}

{{ json . }}

{{- end }} \ No newline at end of file diff --git a/backend/templates/eventType.html b/backend/templates/eventType.html new file mode 100644 index 0000000..d91f98b --- /dev/null +++ b/backend/templates/eventType.html @@ -0,0 +1,9 @@ +{{template "layout.html" .}} + +{{define "title"}}{{ title .Type }}{{end}} + +{{define "content"}} + +{{ template "events.html" .Events }} + +{{- end }} \ No newline at end of file diff --git a/backend/templates/eventTypes.html b/backend/templates/eventTypes.html new file mode 100644 index 0000000..fc12f8e --- /dev/null +++ b/backend/templates/eventTypes.html @@ -0,0 +1,11 @@ +{{template "layout.html" .}} + +{{define "title"}}Historical Events{{end}} + +{{define "content"}} + +{{- end }} \ No newline at end of file diff --git a/backend/templates/events.html b/backend/templates/events.html index 5779e18..6e87fcc 100644 --- a/backend/templates/events.html +++ b/backend/templates/events.html @@ -1,5 +1,5 @@