diff --git a/backend/main.go b/backend/main.go index f56a27e..19299e4 100644 --- a/backend/main.go +++ b/backend/main.go @@ -2,7 +2,6 @@ package main import ( "embed" - "encoding/json" "flag" "fmt" "html/template" @@ -13,7 +12,6 @@ import ( "runtime" "sort" "strconv" - "strings" "github.com/gorilla/mux" "github.com/pkg/profile" @@ -35,33 +33,14 @@ func main() { router := mux.NewRouter().StrictSlash(true) functions := template.FuncMap{ - "json": func(obj any) string { - b, err := json.MarshalIndent(obj, "", " ") - if err != nil { - fmt.Println(err) - return "" - } - return string(b) - }, + "json": util.Json, "check": func(condition bool, v any) any { if condition { return v } return nil }, - "title": func(input string) string { - words := strings.Split(input, " ") - smallwords := " a an on the to of " - - for index, word := range words { - if strings.Contains(smallwords, " "+word+" ") && index > 0 { - words[index] = word - } else { - words[index] = strings.Title(word) - } - } - return strings.Join(words, " ") - }, + "title": util.Title, "getHf": func(id int) *model.HistoricalFigure { return world.HistoricalFigures[id] }, "getEntity": func(id int) *model.Entity { return world.Entities[id] }, "events": func(obj model.Identifiable) []*model.HistoricalEvent { diff --git a/backend/model/events.go b/backend/model/events.go index e309641..b96b1b4 100644 --- a/backend/model/events.go +++ b/backend/model/events.go @@ -277,9 +277,18 @@ func (x *HistoricalEventArtifactTransformed) Html() string { return fmt.Sprintf("%s was made from %s by %s in %s", artifact(x.NewArtifactId), artifact(x.OldArtifactId), hf(x.HistFigureId), site(x.SiteId, "")) // TODO wording } -func (x *HistoricalEventAssumeIdentity) Html() string { return "UNKNWON HistoricalEventAssumeIdentity" } -func (x *HistoricalEventAttackedSite) Html() string { return "UNKNWON HistoricalEventAttackedSite" } -func (x *HistoricalEventBodyAbused) Html() string { return "UNKNWON HistoricalEventBodyAbused" } +func (x *HistoricalEventAssumeIdentity) Html() string { + h := hf(x.TricksterHfid) + i := identity(x.IdentityId) + if x.TargetEnid == -1 { + return fmt.Sprintf(`%s assumed the identity "%s"`, h, i) + } else { + return fmt.Sprintf(`%s fooled %s into believing %s was "%s"`, h, entity(x.TargetEnid), pronoun(x.TricksterHfid), i) + } +} + +func (x *HistoricalEventAttackedSite) Html() string { return "UNKNWON HistoricalEventAttackedSite" } +func (x *HistoricalEventBodyAbused) Html() string { return "UNKNWON HistoricalEventBodyAbused" } func (x *HistoricalEventBuildingProfileAcquired) Html() string { return "UNKNWON HistoricalEventBuildingProfileAcquired" } diff --git a/backend/model/extensions.go b/backend/model/extensions.go index 8f432d4..8c63936 100644 --- a/backend/model/extensions.go +++ b/backend/model/extensions.go @@ -1,6 +1,10 @@ package model -import "fmt" +import ( + "fmt" + + "github.com/robertjanetzko/LegendsBrowser2/backend/util" +) func (e *Entity) Position(id int) *EntityPosition { for _, p := range e.EntityPosition { @@ -52,28 +56,37 @@ var world *DfWorld func artifact(id int) string { if x, ok := world.Artifacts[id]; ok { - return fmt.Sprintf(`%s`, x.Id(), x.Name()) + return fmt.Sprintf(`%s`, x.Id(), util.Title(x.Name())) } return "UNKNOWN ARTIFACT" } func entity(id int) string { if x, ok := world.Entities[id]; ok { - return fmt.Sprintf(`%s`, x.Id(), x.Name()) + return fmt.Sprintf(`%s`, x.Id(), util.Title(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()) + return fmt.Sprintf(`the %s %s`, x.Race, x.Id(), util.Title(x.Name())) } return "UNKNOWN HISTORICAL FIGURE" } +func pronoun(id int) string { + if x, ok := world.HistoricalFigures[id]; ok { + if x.Female() { + return "she" + } + } + return "he" +} + func site(id int, prefix string) string { if x, ok := world.Sites[id]; ok { - return fmt.Sprintf(`%s %s`, prefix, x.Id(), x.Name()) + return fmt.Sprintf(`%s %s`, prefix, x.Id(), util.Title(x.Name())) } return "UNKNOWN SITE" } @@ -81,7 +94,7 @@ func site(id int, prefix string) string { func structure(siteId, structureId int) string { if x, ok := world.Sites[siteId]; ok { if y, ok := x.Structures[structureId]; ok { - return fmt.Sprintf(`%s`, siteId, structureId, y.Name()) + return fmt.Sprintf(`%s`, siteId, structureId, util.Title(y.Name())) } } return "UNKNOWN STRUCTURE" @@ -89,7 +102,14 @@ func structure(siteId, structureId int) string { func region(id int) string { if x, ok := world.Regions[id]; ok { - return fmt.Sprintf(`%s`, x.Id(), x.Name()) + return fmt.Sprintf(`%s`, x.Id(), util.Title(x.Name())) } return "UNKNOWN REGION" } + +func identity(id int) string { + if x, ok := world.Identities[id]; ok { + return fmt.Sprintf(`%s`, x.Id(), util.Title(x.Name())) + } + return "UNKNOWN IDENTITY" +} diff --git a/backend/static/css/legends.css b/backend/static/css/legends.css new file mode 100644 index 0000000..a8ae341 --- /dev/null +++ b/backend/static/css/legends.css @@ -0,0 +1,87 @@ +a { + text-decoration: none; +} + +.hf { + color: #3366CC; +} + +.entity { + color: #CC33FF; +} + +.site { + color: #FFCC00; +} + +.structure { + color: #FFAA00; +} + +.ruin { + color: #bbb; +} + +.worldconstruction { + color: #777777; +} + +.region { + color: #009933; +} + +.artifact { + color: #993300; +} + +.writtencontent { + color: #999999; +} + +.artform { + color: #993399; +} + +.occasion { + color: #993399; +} + +.war { + color: #600000; +} + +.battle { + color: #900000; +} + +.json { + color: #ddd; + font-size: 50%; +} + +/* +@media (prefers-color-scheme: dark) { + a { + color: #337ab7; + } + + .artifact { + color: #ea4e00; + } + + .historical-figure { + color: #5c93ff; + } + + .war { + color: #a00000; + } + + .battle { + color: #c70303; + } + + .art-form { + color: #cc45cc; + } +} */ \ No newline at end of file diff --git a/backend/templates/layout.html b/backend/templates/layout.html index 991c9fd..1d522b3 100644 --- a/backend/templates/layout.html +++ b/backend/templates/layout.html @@ -7,6 +7,7 @@ {{block "title" .}}{{end}} + diff --git a/backend/util/util.go b/backend/util/util.go index b74eefe..217ead8 100644 --- a/backend/util/util.go +++ b/backend/util/util.go @@ -1,6 +1,11 @@ package util -import "strings" +import ( + "encoding/json" + "fmt" + "html/template" + "strings" +) func Keys[K comparable, V any](input map[K]V) []K { keys := make([]K, 0, len(input)) @@ -26,3 +31,26 @@ func ContainsAny(s string, substrings ...string) bool { } return false } + +func Title(input string) string { + words := strings.Split(input, " ") + smallwords := " a an on the to of " + + for index, word := range words { + if strings.Contains(smallwords, " "+word+" ") && index > 0 { + words[index] = word + } else { + words[index] = strings.Title(word) + } + } + return strings.Join(words, " ") +} + +func Json(obj any) template.HTML { + b, err := json.MarshalIndent(obj, "", " ") + if err != nil { + fmt.Println(err) + return "" + } + return template.HTML(`` + string(b) + ``) +}