dorfylegends/backend/model/extensions.go

203 lines
4.8 KiB
Go
Raw Normal View History

2022-04-16 23:12:23 +03:00
package model
2022-04-21 17:25:35 +03:00
import (
"fmt"
2022-04-21 23:01:18 +03:00
"regexp"
"strings"
2022-04-21 17:25:35 +03:00
2022-04-21 23:01:18 +03:00
"github.com/iancoleman/strcase"
2022-04-21 17:25:35 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
)
2022-04-19 12:46:43 +03:00
2022-04-16 23:12:23 +03:00
func (e *Entity) Position(id int) *EntityPosition {
for _, p := range e.EntityPosition {
if p.Id_ == id {
return p
}
}
2022-04-20 00:54:29 +03:00
return &EntityPosition{Name_: "UNKNOWN POSITION"}
2022-04-16 23:12:23 +03:00
}
2022-04-19 18:46:11 +03:00
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"
}
2022-04-16 23:12:23 +03:00
type HistoricalEventDetails interface {
2022-04-18 11:36:29 +03:00
RelatedToEntity(int) bool
2022-04-16 23:12:23 +03:00
RelatedToHf(int) bool
2022-04-18 11:36:29 +03:00
Html() string
2022-04-19 18:46:11 +03:00
Type() string
2022-04-16 23:12:23 +03:00
}
type HistoricalEventCollectionDetails interface {
}
2022-04-18 11:36:29 +03:00
func containsInt(list []int, id int) bool {
for _, v := range list {
if v == id {
return true
}
}
return false
}
2022-04-19 12:46:43 +03:00
var world *DfWorld
2022-04-21 23:01:18 +03:00
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 articled(s string) string {
if ok, _ := regexp.MatchString("^([aeio]|un|ul).*", s); ok {
return "an " + s
}
return "a " + s
}
2022-04-20 00:54:29 +03:00
func artifact(id int) string {
if x, ok := world.Artifacts[id]; ok {
2022-04-21 17:25:35 +03:00
return fmt.Sprintf(`<a class="artifact" href="/artifact/%d">%s</a>`, x.Id(), util.Title(x.Name()))
2022-04-20 00:54:29 +03:00
}
return "UNKNOWN ARTIFACT"
}
2022-04-19 18:46:11 +03:00
func entity(id int) string {
if x, ok := world.Entities[id]; ok {
2022-04-21 17:25:35 +03:00
return fmt.Sprintf(`<a class="entity" href="/entity/%d">%s</a>`, x.Id(), util.Title(x.Name()))
2022-04-19 18:46:11 +03:00
}
return "UNKNOWN ENTITY"
}
2022-04-19 12:46:43 +03:00
func hf(id int) string {
if x, ok := world.HistoricalFigures[id]; ok {
2022-04-21 17:25:35 +03:00
return fmt.Sprintf(`the %s <a class="hf" href="/hf/%d">%s</a>`, x.Race, x.Id(), util.Title(x.Name()))
2022-04-19 12:46:43 +03:00
}
return "UNKNOWN HISTORICAL FIGURE"
}
2022-04-21 23:01:18 +03:00
func hfList(ids []int) string {
return andList(util.Map(ids, hf))
}
2022-04-21 17:25:35 +03:00
func pronoun(id int) string {
if x, ok := world.HistoricalFigures[id]; ok {
if x.Female() {
return "she"
}
}
return "he"
}
2022-04-19 12:46:43 +03:00
func site(id int, prefix string) string {
if x, ok := world.Sites[id]; ok {
2022-04-21 17:25:35 +03:00
return fmt.Sprintf(`%s <a class="site" href="/site/%d">%s</a>`, prefix, x.Id(), util.Title(x.Name()))
2022-04-19 12:46:43 +03:00
}
return "UNKNOWN SITE"
}
2022-04-20 00:54:29 +03:00
func structure(siteId, structureId int) string {
if x, ok := world.Sites[siteId]; ok {
if y, ok := x.Structures[structureId]; ok {
2022-04-21 17:25:35 +03:00
return fmt.Sprintf(`<a class="structure" href="/site/%d/structure/%d">%s</a>`, siteId, structureId, util.Title(y.Name()))
2022-04-20 00:54:29 +03:00
}
}
return "UNKNOWN STRUCTURE"
}
2022-04-20 22:38:31 +03:00
2022-04-21 23:01:18 +03:00
func property(siteId, propertyId int) string {
if x, ok := world.Sites[siteId]; ok {
if y, ok := x.SiteProperties[propertyId]; ok {
if y.StructureId != -1 {
return structure(siteId, y.StructureId)
}
return articled(y.Type.String())
}
}
return "UNKNOWN PROPERTY"
}
2022-04-20 22:38:31 +03:00
func region(id int) string {
if x, ok := world.Regions[id]; ok {
2022-04-21 17:25:35 +03:00
return fmt.Sprintf(`<a class="region" href="/region/%d">%s</a>`, x.Id(), util.Title(x.Name()))
2022-04-20 22:38:31 +03:00
}
return "UNKNOWN REGION"
}
2022-04-21 17:25:35 +03:00
2022-04-21 23:01:18 +03:00
func location(siteId int, sitePrefix string, regionId int, regionPrefix string) string {
if siteId != -1 {
return site(siteId, sitePrefix)
}
if regionId != -1 {
return regionPrefix + " " + region(regionId)
}
return ""
}
2022-04-21 17:25:35 +03:00
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"
}
2022-04-21 23:01:18 +03:00
func feature(x *Feature) string {
switch x.Type {
case FeatureType_DancePerformance:
return "a perfomance of " + danceForm(x.Reference)
case FeatureType_Images:
if x.Reference != -1 {
return "images of " + hf(x.Reference)
}
return "images"
case FeatureType_MusicalPerformance:
return "a perfomance of " + musicalForm(x.Reference)
case FeatureType_PoetryRecital:
return "a recital of " + poeticForm(x.Reference)
case FeatureType_Storytelling:
if x.Reference != -1 {
return "a telling of the story of " + hf(x.Reference)
}
return "a story recital"
default:
return strcase.ToDelimited(x.Type.String(), ' ')
}
}
func danceForm(id int) string {
if x, ok := world.DanceForms[id]; ok {
return fmt.Sprintf(`<a class="artform" href="/danceForm/%d">%s</a>`, id, util.Title(x.Name()))
}
return "UNKNOWN DANCE FORM"
}
func musicalForm(id int) string {
if x, ok := world.MusicalForms[id]; ok {
return fmt.Sprintf(`<a class="artform" href="/musicalForm/%d">%s</a>`, id, util.Title(x.Name()))
}
return "UNKNOWN MUSICAL FORM"
}
func poeticForm(id int) string {
if x, ok := world.PoeticForms[id]; ok {
return fmt.Sprintf(`<a class="artform" href="/poeticForm/%d">%s</a>`, id, util.Title(x.Name()))
}
return "UNKNOWN POETIC FORM"
}