events
This commit is contained in:
parent
ca9325b93a
commit
d29ab0dce1
|
@ -19,6 +19,7 @@ import (
|
||||||
"github.com/pkg/profile"
|
"github.com/pkg/profile"
|
||||||
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
|
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
|
||||||
"github.com/robertjanetzko/LegendsBrowser2/backend/templates"
|
"github.com/robertjanetzko/LegendsBrowser2/backend/templates"
|
||||||
|
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var world *model.DfWorld
|
var world *model.DfWorld
|
||||||
|
@ -165,9 +166,10 @@ func main() {
|
||||||
// server.RegisterResource(router, "poeticForm", world.PoeticForms)
|
// server.RegisterResource(router, "poeticForm", world.PoeticForms)
|
||||||
// server.RegisterResource(router, "written", world.WrittenContents)
|
// server.RegisterResource(router, "written", world.WrittenContents)
|
||||||
|
|
||||||
RegisterPage(router, "/entity/{id}", t, "entity.html", func(id int) any { return world.Entities[id] })
|
RegisterResourcePage(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, "/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"}
|
spa := spaHandler{staticFS: static, staticPath: "static", indexPath: "index.html"}
|
||||||
|
@ -175,18 +177,42 @@ func main() {
|
||||||
|
|
||||||
fmt.Println("Serving at :8080")
|
fmt.Println("Serving at :8080")
|
||||||
http.ListenAndServe(":8080", router)
|
http.ListenAndServe(":8080", router)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterPage(router *mux.Router, path string, templates *templates.Template, template string, accessor func(int) any) {
|
func allEventTypes() []string {
|
||||||
get := func(w http.ResponseWriter, r *http.Request) {
|
types := make(map[string]bool)
|
||||||
id, err := strconv.Atoi(mux.Vars(r)["id"])
|
for _, e := range world.HistoricalEvents {
|
||||||
if err != nil {
|
types[e.Details.Type()] = true
|
||||||
fmt.Fprintln(w, err)
|
}
|
||||||
fmt.Println(err)
|
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 {
|
if err != nil {
|
||||||
fmt.Fprintln(w, err)
|
fmt.Fprintln(w, err)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
@ -196,6 +222,13 @@ func RegisterPage(router *mux.Router, path string, templates *templates.Template
|
||||||
router.HandleFunc(path, get).Methods("GET")
|
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 {
|
type spaHandler struct {
|
||||||
staticFS embed.FS
|
staticFS embed.FS
|
||||||
staticPath string
|
staticPath string
|
||||||
|
|
|
@ -1,11 +1,69 @@
|
||||||
package model
|
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 {
|
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 {
|
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 *HistoricalEventAddHfHfLink) Html() string { return "UNKNWON HistoricalEventAddHfHfLink" }
|
||||||
func (x *HistoricalEventAddHfSiteLink) Html() string { return "UNKNWON HistoricalEventAddHfSiteLink" }
|
func (x *HistoricalEventAddHfSiteLink) Html() string { return "UNKNWON HistoricalEventAddHfSiteLink" }
|
||||||
func (x *HistoricalEventAgreementFormed) Html() string {
|
func (x *HistoricalEventAgreementFormed) Html() string {
|
||||||
|
|
|
@ -11,10 +11,29 @@ func (e *Entity) Position(id int) *EntityPosition {
|
||||||
return nil
|
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 {
|
type HistoricalEventDetails interface {
|
||||||
RelatedToEntity(int) bool
|
RelatedToEntity(int) bool
|
||||||
RelatedToHf(int) bool
|
RelatedToHf(int) bool
|
||||||
Html() string
|
Html() string
|
||||||
|
Type() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HistoricalEventCollectionDetails interface {
|
type HistoricalEventCollectionDetails interface {
|
||||||
|
@ -31,6 +50,13 @@ func containsInt(list []int, id int) bool {
|
||||||
|
|
||||||
var world *DfWorld
|
var world *DfWorld
|
||||||
|
|
||||||
|
func entity(id int) string {
|
||||||
|
if x, ok := world.Entities[id]; ok {
|
||||||
|
return fmt.Sprintf(`<a href="/entity/%d">%s</a>`, x.Id(), x.Name())
|
||||||
|
}
|
||||||
|
return "UNKNOWN ENTITY"
|
||||||
|
}
|
||||||
|
|
||||||
func hf(id int) string {
|
func hf(id int) string {
|
||||||
if x, ok := world.HistoricalFigures[id]; ok {
|
if x, ok := world.HistoricalFigures[id]; ok {
|
||||||
return fmt.Sprintf(`<a href="/hf/%d">%s</a>`, x.Id(), x.Name())
|
return fmt.Sprintf(`<a href="/hf/%d">%s</a>`, x.Id(), x.Name())
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
{
|
{
|
||||||
"Artifact": {
|
|
||||||
"Writing": "StructureLocalId"
|
|
||||||
},
|
|
||||||
"HistoricalEventAddHfEntityLink": {
|
"HistoricalEventAddHfEntityLink": {
|
||||||
"LinkType": "Link"
|
"LinkType": "Link"
|
||||||
},
|
},
|
||||||
|
@ -16,19 +13,6 @@
|
||||||
"Involved": "SiteId",
|
"Involved": "SiteId",
|
||||||
"Site": "SiteId"
|
"Site": "SiteId"
|
||||||
},
|
},
|
||||||
"HistoricalEventHfDoesInteraction": {
|
|
||||||
"InteractionAction": "Interaction"
|
|
||||||
},
|
|
||||||
"HistoricalEventMasterpieceItem": {
|
|
||||||
"Maker": "Hfid",
|
|
||||||
"MakerEntity": "EntityId",
|
|
||||||
"Site": "SiteId"
|
|
||||||
},
|
|
||||||
"HistoricalEventMerchant": {
|
|
||||||
"Destination": "DepotEntityId",
|
|
||||||
"Site": "SiteId",
|
|
||||||
"Source": "TraderEntityId"
|
|
||||||
},
|
|
||||||
"HistoricalEventPeaceAccepted": {
|
"HistoricalEventPeaceAccepted": {
|
||||||
"Site": "SiteId"
|
"Site": "SiteId"
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
<h3>Events</h3>
|
<h3>Events</h3>
|
||||||
|
|
||||||
{{ template "events.html" . }}
|
{{ template "events.html" events . }}
|
||||||
|
|
||||||
<p>{{ json . }}</p>
|
<p>{{ json . }}</p>
|
||||||
{{- end }}
|
{{- end }}
|
|
@ -0,0 +1,9 @@
|
||||||
|
{{template "layout.html" .}}
|
||||||
|
|
||||||
|
{{define "title"}}{{ title .Type }}{{end}}
|
||||||
|
|
||||||
|
{{define "content"}}
|
||||||
|
|
||||||
|
{{ template "events.html" .Events }}
|
||||||
|
|
||||||
|
{{- end }}
|
|
@ -0,0 +1,11 @@
|
||||||
|
{{template "layout.html" .}}
|
||||||
|
|
||||||
|
{{define "title"}}Historical Events{{end}}
|
||||||
|
|
||||||
|
{{define "content"}}
|
||||||
|
<ul>
|
||||||
|
{{- range $t := . }}
|
||||||
|
<li><a href="/events/{{ $t }}">{{ $t }}</a></li>
|
||||||
|
{{- end }}
|
||||||
|
</ul>
|
||||||
|
{{- end }}
|
|
@ -1,5 +1,5 @@
|
||||||
<ul>
|
<ul>
|
||||||
{{- range $event := events . }}
|
{{- range $event := . }}
|
||||||
<li>
|
<li>
|
||||||
[{{ $event.Id }}] In {{ if gt $event.Seconds72 -1 }}{{ season $event.Seconds72 }} of {{ end }}{{ $event.Year }} {{
|
[{{ $event.Id }}] In {{ if gt $event.Seconds72 -1 }}{{ season $event.Seconds72 }} of {{ end }}{{ $event.Year }} {{
|
||||||
html $event.Details.Html }} {{ json $event.Details }}
|
html $event.Details.Html }} {{ json $event.Details }}
|
||||||
|
|
|
@ -48,6 +48,6 @@
|
||||||
|
|
||||||
<h3>Events</h3>
|
<h3>Events</h3>
|
||||||
|
|
||||||
{{ template "events.html" . }}
|
{{ template "events.html" events . }}
|
||||||
|
|
||||||
{{ json . }}
|
{{ json . }}
|
Loading…
Reference in New Issue