From 63808713582a9869f17489701e7a960a0ad18728 Mon Sep 17 00:00:00 2001 From: Robert Janetzko Date: Sat, 7 May 2022 20:19:30 +0000 Subject: [PATCH] years --- analyze/overwrites.json | 4 ++++ backend/model/extensions.go | 11 +++++++++++ backend/model/model.go | 2 ++ backend/model/process.go | 4 ++++ backend/server/server.go | 31 ++++++++++++++++++++++++++++--- backend/server/templates.go | 14 ++++++++------ backend/templates/hfs.html | 21 ++++++++++++--------- backend/templates/year.html | 12 ++++++++++++ backend/templates/years.html | 25 +++++++++++++++++++++++++ backend/util/util.go | 11 +++++++++++ 10 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 backend/templates/year.html create mode 100644 backend/templates/years.html diff --git a/analyze/overwrites.json b/analyze/overwrites.json index 2089c8d..0e58de7 100644 --- a/analyze/overwrites.json +++ b/analyze/overwrites.json @@ -85,6 +85,10 @@ { "Name": "NecromancerSince", "Type": "int" + }, + { + "Name": "Kills", + "Type": "[]int" } ], "Entity": [ diff --git a/backend/model/extensions.go b/backend/model/extensions.go index 1ea27b9..b76411e 100644 --- a/backend/model/extensions.go +++ b/backend/model/extensions.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/robertjanetzko/LegendsBrowser2/backend/util" + "golang.org/x/exp/maps" ) func (w *DfWorld) AllEventTypes() []string { @@ -63,6 +64,16 @@ func (w *DfWorld) SiteHistory(siteId int) []*HistoricalEvent { return list } +func (w *DfWorld) Races() []string { + races := make(map[string]bool) + for _, hf := range w.HistoricalFigures { + races[hf.Race] = true + } + list := maps.Keys(races) + sort.Strings(list) + return list +} + func (c *HistoricalEventCollection) Type() string { if c.Details == nil { return "unk" diff --git a/backend/model/model.go b/backend/model/model.go index f82665e..aa450bd 100644 --- a/backend/model/model.go +++ b/backend/model/model.go @@ -19059,6 +19059,7 @@ type HistoricalFigure struct { Sphere []string `json:"sphere" legend:"base" related:""` // sphere UsedIdentityId []int `json:"usedIdentityId" legend:"base" related:""` // used_identity_id VagueRelationship []*VagueRelationship `json:"vagueRelationship" legend:"base" related:""` // vague_relationship + Kills []int `json:"kills" legend:"add" related:""` // Kills Leader bool `json:"leader" legend:"add" related:""` // Leader Necromancer bool `json:"necromancer" legend:"add" related:""` // Necromancer NecromancerSince int `json:"necromancerSince" legend:"add" related:""` // NecromancerSince @@ -19182,6 +19183,7 @@ func (x *HistoricalFigure) MarshalJSON() ([]byte, error) { d["sphere"] = x.Sphere d["usedIdentityId"] = x.UsedIdentityId d["vagueRelationship"] = x.VagueRelationship + d["kills"] = x.Kills d["leader"] = x.Leader d["necromancer"] = x.Necromancer if x.NecromancerSince != -1 { diff --git a/backend/model/process.go b/backend/model/process.go index 65b10b6..2f6b562 100644 --- a/backend/model/process.go +++ b/backend/model/process.go @@ -150,6 +150,10 @@ func (w *DfWorld) processEvents() { id.HistfigId = hf.Id_ } } + case *HistoricalEventHfDied: + if hf, ok := w.HistoricalFigures[d.SlayerHfid]; ok { + hf.Kills = append(hf.Kills, d.Hfid) + } } } } diff --git a/backend/server/server.go b/backend/server/server.go index 9aa557a..8052a8e 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -12,6 +12,7 @@ import ( "github.com/robertjanetzko/LegendsBrowser2/backend/model" "github.com/robertjanetzko/LegendsBrowser2/backend/templates" "github.com/robertjanetzko/LegendsBrowser2/backend/util" + "golang.org/x/exp/constraints" ) type DfServerContext struct { @@ -119,6 +120,19 @@ func StartServer(config *Config, world *model.DfWorld, static embed.FS) error { srv.RegisterWorldResourcePage("/identity/{id}", "identity.html", func(id int) any { return srv.context.world.Identities[id] }) srv.RegisterWorldResourcePage("/popover/identity/{id}", "popoverIdentity.html", func(id int) any { return srv.context.world.Identities[id] }) + srv.RegisterWorldPage("/years", "years.html", func(p Parms) any { + return groupBy(srv.context.world.HistoricalEvents, + func(e *model.HistoricalEvent) int { return e.Year }, + func(e *model.HistoricalEvent) bool { return true }, + func(e *model.HistoricalEvent) int { return e.Id_ }) + }) + srv.RegisterWorldResourcePage("/year/{id}", "year.html", func(id int) any { + return util.FilterMap(srv.context.world.HistoricalEvents, + func(v *model.HistoricalEvent) bool { return v.Year == id }, + func(a, b *model.HistoricalEvent) bool { return a.Id_ < b.Id_ }, + ) + }) + 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.RegisterWorldResourcePage("/event/{id}", "event.html", func(id int) any { return srv.context.world.HistoricalEvents[id] }) @@ -236,7 +250,18 @@ func (srv *DfServer) searchHf(p Parms) any { list = append(list, hf) } - sort.Slice(list, func(i, j int) bool { return list[i].Name_ < list[j].Name_ }) + switch p["sort"] { + case "race": + sort.Slice(list, func(i, j int) bool { return list[i].Race < list[j].Race }) + case "birth": + sort.Slice(list, func(i, j int) bool { return list[i].BirthYear < list[j].BirthYear }) + case "death": + sort.Slice(list, func(i, j int) bool { return list[i].DeathYear < list[j].DeathYear }) + case "kills": + sort.Slice(list, func(i, j int) bool { return len(list[i].Kills) > len(list[j].Kills) }) + default: + sort.Slice(list, func(i, j int) bool { return list[i].Name_ < list[j].Name_ }) + } return map[string]any{ "Params": p, @@ -288,8 +313,8 @@ func groupByType[K comparable, T namedTyped](input map[K]T) map[string][]T { return groupBy(input, func(t T) string { return t.Type() }, func(t T) bool { return t.Name() != "" }, func(t T) string { return t.Name() }) } -func groupBy[K comparable, T any](input map[K]T, mapper func(T) string, filter func(T) bool, sortMapper func(T) string) map[string][]T { - output := make(map[string][]T) +func groupBy[K comparable, N comparable, T any, S constraints.Ordered](input map[K]T, mapper func(T) N, filter func(T) bool, sortMapper func(T) S) map[N][]T { + output := make(map[N][]T) for _, v := range input { k := mapper(v) diff --git a/backend/server/templates.go b/backend/server/templates.go index 16baa53..283133e 100644 --- a/backend/server/templates.go +++ b/backend/server/templates.go @@ -90,12 +90,14 @@ func (srv *DfServer) LoadTemplates() { "html": func(value any) template.HTML { return template.HTML(fmt.Sprint(value)) }, - "bytes": func(s int64) string { return humanize.Bytes(uint64(s)) }, - "first": util.FirstInMap, - "ifFirst": func(m any, k string, r string) string { return util.If(util.FirstInMap(m, k), r, "") }, - "strip": util.Strip, - "string": util.String, - "capitalize": util.Capitalize, + "bytes": func(s int64) string { return humanize.Bytes(uint64(s)) }, + "first": util.FirstInMap, + "ifFirst": func(m any, k string, r string) string { return util.If(util.FirstInMap(m, k), r, "") }, + "strip": util.Strip, + "string": util.String, + "capitalize": util.Capitalize, + "add": func(a, b int) int { return a + b }, + "breakYearColumn": func(c, m int) bool { return (c % ((m + 2) / 4)) == 0 }, } srv.templates = templates.New(functions) } diff --git a/backend/templates/hfs.html b/backend/templates/hfs.html index 5135919..3117319 100644 --- a/backend/templates/hfs.html +++ b/backend/templates/hfs.html @@ -14,6 +14,7 @@ Name Race Lived + Kills {{- range .Hfs }}{{- if not (eq .Name "") }} @@ -26,6 +27,7 @@ from {{ .BirthYear }} till {{ .DeathYear }} {{- end }} + {{ len .Kills }} {{- end}}{{- end}} @@ -51,25 +53,26 @@ }}checked{{end}}> Ghost
-
+

Sorting

- +
diff --git a/backend/templates/year.html b/backend/templates/year.html new file mode 100644 index 0000000..6dd57d4 --- /dev/null +++ b/backend/templates/year.html @@ -0,0 +1,12 @@ +{{template "layout.html" .}} + +{{define "title"}}Year {{(index . 0).Year}}{{end}} + +{{define "content"}} + +

Year {{(index . 0).Year}}

+ +
Events
+{{ template "events.html" events . }} + +{{- end }} \ No newline at end of file diff --git a/backend/templates/years.html b/backend/templates/years.html new file mode 100644 index 0000000..35a08ae --- /dev/null +++ b/backend/templates/years.html @@ -0,0 +1,25 @@ +{{template "layout.html" .}} + +{{define "title"}}Years{{end}} + +{{define "content"}} +

Years

+
+
+
    + {{ $c := 0}} + {{ range $y, $e := . }} + {{ $c = add $c 1 }} + {{ if breakYearColumn $c (len $) }} +
+
+
+ +
+
+ +{{- end }} \ No newline at end of file diff --git a/backend/util/util.go b/backend/util/util.go index 3723040..4246fcb 100644 --- a/backend/util/util.go +++ b/backend/util/util.go @@ -90,6 +90,17 @@ func Map[U, V any](list []U, mapper func(U) V) []V { return newList } +func FilterMap[K comparable, V any](input map[K]V, predicate func(V) bool, sorter func(V, V) bool) []V { + var list []V + for _, v := range input { + if predicate(v) { + list = append(list, v) + } + } + sort.Slice(list, func(i, j int) bool { return sorter(list[i], list[j]) }) + return list +} + type Identifiable interface { Id() int }