This commit is contained in:
Robert Janetzko 2022-04-21 14:25:35 +00:00
parent e136b2f8e6
commit 10a9024b08
6 changed files with 158 additions and 34 deletions

View file

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

View file

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

View file

@ -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(`<a href="/artifact/%d">%s</a>`, x.Id(), x.Name())
return fmt.Sprintf(`<a class="artifact" href="/artifact/%d">%s</a>`, x.Id(), util.Title(x.Name()))
}
return "UNKNOWN ARTIFACT"
}
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 fmt.Sprintf(`<a class="entity" href="/entity/%d">%s</a>`, x.Id(), util.Title(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())
return fmt.Sprintf(`the %s <a class="hf" href="/hf/%d">%s</a>`, 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 <a href="/site/%d">%s</a>`, prefix, x.Id(), x.Name())
return fmt.Sprintf(`%s <a class="site" href="/site/%d">%s</a>`, 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(`<a href="/site/%d/structure/%d">%s</a>`, siteId, structureId, y.Name())
return fmt.Sprintf(`<a class="structure" href="/site/%d/structure/%d">%s</a>`, 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(`<a href="/region/%d">%s</a>`, x.Id(), x.Name())
return fmt.Sprintf(`<a class="region" href="/region/%d">%s</a>`, x.Id(), util.Title(x.Name()))
}
return "UNKNOWN REGION"
}
func identity(id int) string {
if x, ok := world.Identities[id]; ok {
return fmt.Sprintf(`<a class="identity" href="/region/%d">%s</a>`, x.Id(), util.Title(x.Name()))
}
return "UNKNOWN IDENTITY"
}

View file

@ -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;
}
} */

View file

@ -7,6 +7,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{block "title" .}}{{end}}</title>
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/legends.css" rel="stylesheet">
<script src="/js/bootstrap.min.js"></script>
</head>

View file

@ -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(`<span class="json">` + string(b) + `</span>`)
}