This commit is contained in:
Robert Janetzko 2022-04-19 15:46:11 +00:00
parent ca9325b93a
commit d29ab0dce1
9 changed files with 154 additions and 33 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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(`<a href="/entity/%d">%s</a>`, x.Id(), x.Name())
}
return "UNKNOWN ENTITY"
}
func hf(id int) string {
if x, ok := world.HistoricalFigures[id]; ok {
return fmt.Sprintf(`<a href="/hf/%d">%s</a>`, x.Id(), x.Name())

View File

@ -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"
},

View File

@ -8,7 +8,7 @@
<h3>Events</h3>
{{ template "events.html" . }}
{{ template "events.html" events . }}
<p>{{ json . }}</p>
{{- end }}

View File

@ -0,0 +1,9 @@
{{template "layout.html" .}}
{{define "title"}}{{ title .Type }}{{end}}
{{define "content"}}
{{ template "events.html" .Events }}
{{- end }}

View File

@ -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 }}

View File

@ -1,5 +1,5 @@
<ul>
{{- range $event := events . }}
{{- range $event := . }}
<li>
[{{ $event.Id }}] In {{ if gt $event.Seconds72 -1 }}{{ season $event.Seconds72 }} of {{ end }}{{ $event.Year }} {{
html $event.Details.Html }} {{ json $event.Details }}

View File

@ -48,6 +48,6 @@
<h3>Events</h3>
{{ template "events.html" . }}
{{ template "events.html" events . }}
{{ json . }}