dorfylegends/backend/server/server.go

338 lines
13 KiB
Go
Raw Permalink Normal View History

2022-04-26 10:24:16 +03:00
package server
import (
"embed"
"fmt"
"net/http"
2022-05-03 15:59:47 +03:00
"os"
2022-04-28 21:14:33 +03:00
"sort"
2022-04-30 10:45:31 +03:00
"strconv"
2022-04-26 10:24:16 +03:00
"github.com/gorilla/mux"
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
"github.com/robertjanetzko/LegendsBrowser2/backend/templates"
2022-04-27 22:44:39 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
2022-05-07 23:19:30 +03:00
"golang.org/x/exp/constraints"
2022-04-26 10:24:16 +03:00
)
2022-04-26 18:39:24 +03:00
type DfServerContext struct {
2022-05-01 13:29:39 +03:00
config *Config
2022-04-26 18:39:24 +03:00
world *model.DfWorld
isLoading bool
2022-04-26 22:00:09 +03:00
progress *model.LoadProgress
2022-04-26 18:39:24 +03:00
}
2022-04-26 10:24:16 +03:00
type DfServer struct {
router *mux.Router
2022-04-26 22:00:09 +03:00
loader *loadHandler
2022-04-26 10:24:16 +03:00
templates *templates.Template
2022-04-26 18:39:24 +03:00
context *DfServerContext
2022-04-26 10:24:16 +03:00
}
2022-05-05 13:55:33 +03:00
func StartServer(config *Config, world *model.DfWorld, static embed.FS) error {
2022-04-26 10:24:16 +03:00
srv := &DfServer{
router: mux.NewRouter().StrictSlash(true),
2022-04-26 18:39:24 +03:00
context: &DfServerContext{
2022-05-01 13:29:39 +03:00
config: config,
2022-04-26 18:39:24 +03:00
world: world,
isLoading: false,
2022-04-26 22:00:09 +03:00
progress: &model.LoadProgress{},
2022-04-26 18:39:24 +03:00
},
2022-04-26 10:24:16 +03:00
}
2022-05-09 18:20:31 +03:00
root := srv.router
if srv.context.config.SubUri != "" {
srv.router = srv.router.PathPrefix("/legends").Subrouter()
}
2022-04-26 22:00:09 +03:00
srv.loader = &loadHandler{server: srv}
2022-04-26 10:24:16 +03:00
srv.LoadTemplates()
2022-05-01 10:32:14 +03:00
srv.RegisterWorldPage("/entities", "entities.html", func(p Parms) any { return groupByType(srv.context.world.Entities) })
2022-04-26 18:39:24 +03:00
srv.RegisterWorldResourcePage("/entity/{id}", "entity.html", func(id int) any { return srv.context.world.Entities[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/entity/{id}", "popoverEntity.html", func(id int) any { return srv.context.world.Entities[id] })
2022-04-27 22:44:39 +03:00
2022-05-04 12:42:02 +03:00
srv.RegisterWorldPage("/geography", "geography.html", func(p Parms) any {
return &struct {
Regions map[string][]*model.Region
Landmasses map[string][]*model.Landmass
MountainPeaks map[string][]*model.MountainPeak
Rivers map[string][]*model.River
}{
Regions: singleGroup(srv.context.world.Regions, "region"),
Landmasses: singleGroup(srv.context.world.Landmasses, "landmass"),
MountainPeaks: singleGroup(srv.context.world.MountainPeaks, "mountain"),
Rivers: map[string][]*model.River{
"rivers": srv.context.world.Rivers,
},
}
})
2022-05-04 15:07:35 +03:00
srv.RegisterWorldResourcePage("/landmass/{id}", "landmass.html", func(id int) any { return srv.context.world.Landmasses[id] })
2022-05-07 19:11:24 +03:00
srv.RegisterWorldResourcePage("/popover/landmass/{id}", "popoverLandmass.html", func(id int) any { return srv.context.world.Landmasses[id] })
2022-05-04 15:07:35 +03:00
srv.RegisterWorldResourcePage("/mountain/{id}", "mountain.html", func(id int) any { return srv.context.world.MountainPeaks[id] })
2022-05-07 19:11:24 +03:00
srv.RegisterWorldResourcePage("/popover/mountain/{id}", "popoverMountain.html", func(id int) any { return srv.context.world.MountainPeaks[id] })
2022-05-04 15:07:35 +03:00
srv.RegisterWorldResourcePage("/river/{id}", "river.html", func(id int) any { return srv.context.world.Rivers[id] })
2022-05-07 19:11:24 +03:00
srv.RegisterWorldResourcePage("/popover/river/{id}", "popoverRiver.html", func(id int) any { return srv.context.world.Rivers[id] })
2022-05-04 12:42:02 +03:00
2022-05-01 10:32:14 +03:00
srv.RegisterWorldPage("/regions", "regions.html", func(p Parms) any { return groupByType(srv.context.world.Regions) })
2022-04-26 18:39:24 +03:00
srv.RegisterWorldResourcePage("/region/{id}", "region.html", func(id int) any { return srv.context.world.Regions[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/region/{id}", "popoverRegion.html", func(id int) any { return srv.context.world.Regions[id] })
2022-04-28 22:24:55 +03:00
2022-05-01 10:32:14 +03:00
srv.RegisterWorldPage("/sites", "sites.html", func(p Parms) any { return groupByType(srv.context.world.Sites) })
2022-04-26 18:39:24 +03:00
srv.RegisterWorldResourcePage("/site/{id}", "site.html", func(id int) any { return srv.context.world.Sites[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/site/{id}", "popoverSite.html", func(id int) any { return srv.context.world.Sites[id] })
2022-04-28 22:24:55 +03:00
2022-04-29 15:21:27 +03:00
srv.RegisterWorldPage("/structures", "structures.html", func(p Parms) any {
return flatGrouped(srv.context.world.Sites, func(s *model.Site) []*model.Structure { return util.Values(s.Structures) })
})
2022-04-30 15:27:42 +03:00
srv.RegisterWorldPage("/site/{siteId}/structure/{id}", "structure.html", srv.findStructure)
srv.RegisterWorldPage("/popover/site/{siteId}/structure/{id}", "popoverStructure.html", srv.findStructure)
2022-04-29 15:21:27 +03:00
2022-05-01 10:32:14 +03:00
srv.RegisterWorldPage("/worldconstructions", "worldconstructions.html", func(p Parms) any { return groupByType(srv.context.world.WorldConstructions) })
2022-04-28 22:24:55 +03:00
srv.RegisterWorldResourcePage("/worldconstruction/{id}", "worldconstruction.html", func(id int) any { return srv.context.world.WorldConstructions[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/worldconstruction/{id}", "popoverWorldconstruction.html", func(id int) any { return srv.context.world.WorldConstructions[id] })
2022-04-28 22:24:55 +03:00
2022-05-01 10:32:14 +03:00
srv.RegisterWorldPage("/artifacts", "artifacts.html", func(p Parms) any { return groupByType(srv.context.world.Artifacts) })
2022-04-26 18:39:24 +03:00
srv.RegisterWorldResourcePage("/artifact/{id}", "artifact.html", func(id int) any { return srv.context.world.Artifacts[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/artifact/{id}", "popoverArtifact.html", func(id int) any { return srv.context.world.Artifacts[id] })
2022-04-26 18:39:24 +03:00
2022-04-28 22:24:55 +03:00
srv.RegisterWorldPage("/artforms", "artforms.html", func(p Parms) any {
2022-04-30 10:45:31 +03:00
return &struct {
2022-04-28 22:24:55 +03:00
DanceForms map[string][]*model.DanceForm
MusicalForms map[string][]*model.MusicalForm
PoeticForms map[string][]*model.PoeticForm
}{
2022-05-01 10:32:14 +03:00
DanceForms: groupByType(srv.context.world.DanceForms),
MusicalForms: groupByType(srv.context.world.MusicalForms),
PoeticForms: groupByType(srv.context.world.PoeticForms),
2022-04-28 22:24:55 +03:00
}
})
2022-05-03 22:39:00 +03:00
srv.RegisterWorldResourcePage("/danceform/{id}", "artform.html", func(id int) any { return srv.context.world.DanceForms[id] })
srv.RegisterWorldResourcePage("/musicalform/{id}", "artform.html", func(id int) any { return srv.context.world.MusicalForms[id] })
srv.RegisterWorldResourcePage("/poeticform/{id}", "artform.html", func(id int) any { return srv.context.world.PoeticForms[id] })
2022-05-01 10:32:14 +03:00
srv.RegisterWorldPage("/writtencontents", "writtencontents.html", func(p Parms) any { return groupByType(srv.context.world.WrittenContents) })
2022-04-28 22:24:55 +03:00
srv.RegisterWorldResourcePage("/writtencontent/{id}", "writtencontent.html", func(id int) any { return srv.context.world.WrittenContents[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/writtencontent/{id}", "popoverWrittencontent.html", func(id int) any { return srv.context.world.WrittenContents[id] })
2022-04-28 22:24:55 +03:00
2022-05-05 23:18:31 +03:00
srv.RegisterWorldPage("/hfs", "hfs.html", srv.searchHf)
2022-04-28 22:24:55 +03:00
srv.RegisterWorldResourcePage("/hf/{id}", "hf.html", func(id int) any { return srv.context.world.HistoricalFigures[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/hf/{id}", "popoverHf.html", func(id int) any { return srv.context.world.HistoricalFigures[id] })
2022-04-28 22:24:55 +03:00
2022-05-07 22:17:05 +03:00
srv.RegisterWorldPage("/identities", "identities.html", func(p Parms) any { return srv.context.world.Identities })
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] })
2022-05-07 23:19:30 +03:00
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_ },
)
})
2022-04-26 18:39:24 +03:00
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"]) })
2022-05-04 07:46:21 +03:00
srv.RegisterWorldResourcePage("/event/{id}", "event.html", func(id int) any { return srv.context.world.HistoricalEvents[id] })
2022-04-26 18:39:24 +03:00
2022-05-03 15:59:47 +03:00
srv.RegisterWorldPage("/collections", "collections.html", func(p Parms) any {
return groupBy(srv.context.world.HistoricalEventCollections,
func(e *model.HistoricalEventCollection) string { return e.Type() },
func(e *model.HistoricalEventCollection) bool { return true },
func(e *model.HistoricalEventCollection) string { return model.Time(e.StartYear, e.StartSeconds72) },
)
})
srv.RegisterWorldResourcePage("/collection/{id}", "collection.html", func(id int) any { return srv.context.world.HistoricalEventCollections[id] })
srv.RegisterWorldResourcePage("/popover/collection/{id}", "popoverCollection.html", func(id int) any { return srv.context.world.HistoricalEventCollections[id] })
2022-05-07 18:08:42 +03:00
srv.RegisterWorldPage("/worldmap", "worldMap.html", func(p Parms) any {
return &struct {
Landmasses map[int]*model.Landmass
Regions map[int]*model.Region
Sites map[int]*model.Site
MountainPeaks map[int]*model.MountainPeak
WorldConstructions map[int]*model.WorldConstruction
Rivers []*model.River
}{
Landmasses: srv.context.world.Landmasses,
Regions: srv.context.world.Regions,
Sites: srv.context.world.Sites,
MountainPeaks: srv.context.world.MountainPeaks,
WorldConstructions: srv.context.world.WorldConstructions,
Rivers: srv.context.world.Rivers,
}
})
2022-05-01 10:32:14 +03:00
srv.RegisterWorldPage("/", "index.html", func(p Parms) any {
return &struct {
Civilizations map[string][]*model.Entity
}{
Civilizations: groupBy(srv.context.world.Entities,
2022-05-08 20:09:55 +03:00
func(e *model.Entity) string {
return util.If(e.Necromancer, "necromancer", util.If(e.Race == "", "unknown", e.Race))
},
2022-05-06 14:11:12 +03:00
func(e *model.Entity) bool {
return e.Name() != "" && (e.Type_ == model.EntityType_Civilization || e.Necromancer)
},
2022-05-01 10:32:14 +03:00
func(e *model.Entity) string { return e.Name() }),
}
})
2022-05-01 13:29:39 +03:00
srv.router.HandleFunc("/map", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
w.Write(srv.loader.server.context.world.MapData)
})
2022-04-29 21:23:00 +03:00
srv.router.PathPrefix("/search").Handler(searchHandler{server: srv})
2022-04-26 22:00:09 +03:00
srv.router.PathPrefix("/load").Handler(srv.loader)
2022-04-26 10:24:16 +03:00
2022-04-29 12:39:39 +03:00
spa := spaHandler{server: srv, staticFS: static, staticPath: "static", indexPath: "index.html"}
2022-05-03 15:59:47 +03:00
if templates.DebugTemplates {
spa.staticFS = os.DirFS(".")
}
2022-04-26 10:24:16 +03:00
srv.router.PathPrefix("/").Handler(spa)
2022-05-09 18:20:31 +03:00
OpenBrowser(fmt.Sprintf("http://localhost:%d", config.Port))
http.ListenAndServe(fmt.Sprintf(":%d", config.Port), root)
2022-05-01 13:29:39 +03:00
return nil
2022-04-26 10:24:16 +03:00
}
2022-04-30 15:27:42 +03:00
func (srv *DfServer) findStructure(p Parms) any {
siteId, err := strconv.Atoi(p["siteId"])
if err != nil {
return nil
}
structureId, err := strconv.Atoi(p["id"])
if err != nil {
return nil
}
if site, ok := srv.context.world.Sites[siteId]; ok {
return site.Structures[structureId]
}
return nil
}
2022-05-05 23:18:31 +03:00
func (srv *DfServer) searchHf(p Parms) any {
var list []*model.HistoricalFigure
for _, hf := range srv.context.world.HistoricalFigures {
if p["leader"] == "1" && !hf.Leader {
continue
}
if p["deity"] == "1" && !hf.Deity {
continue
}
if p["force"] == "1" && !hf.Force {
continue
}
if p["vampire"] == "1" && !hf.Vampire {
continue
}
if p["werebeast"] == "1" && !hf.Werebeast {
continue
}
if p["necromancer"] == "1" && !hf.Necromancer {
continue
}
if p["alive"] == "1" && hf.DeathYear != -1 {
continue
}
if p["ghost"] == "1" && false { // TODO ghost
continue
}
if p["adventurer"] == "1" && !hf.Adventurer {
continue
}
list = append(list, hf)
}
2022-05-07 23:19:30 +03:00
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_ })
}
2022-05-05 23:18:31 +03:00
return map[string]any{
"Params": p,
"Hfs": list,
}
}
2022-04-30 10:45:31 +03:00
func (srv *DfServer) notFound(w http.ResponseWriter) {
err := srv.templates.Render(w, "notFound.html", nil)
2022-04-27 22:44:39 +03:00
if err != nil {
httpError(w, err)
2022-04-26 22:00:09 +03:00
}
}
2022-04-27 22:44:39 +03:00
func httpError(w http.ResponseWriter, err error) {
fmt.Fprintln(w, err)
fmt.Println(err)
}
2022-04-28 21:14:33 +03:00
type namedTyped interface {
model.Named
model.Typed
}
2022-04-29 15:21:27 +03:00
func flatGrouped[K comparable, U any, V namedTyped](input map[K]U, mapper func(U) []V) map[string][]V {
output := make(map[string][]V)
for _, x := range input {
for _, v := range mapper(x) {
k := v.Type()
if v.Name() != "" {
output[k] = append(output[k], v)
}
}
}
for _, v := range output {
sort.Slice(v, func(i, j int) bool { return v[i].Name() < v[j].Name() })
}
return output
}
2022-05-04 12:42:02 +03:00
func singleGroup[K comparable, T model.Named](input map[K]T, group string) map[string][]T {
return groupBy(input, func(t T) string { return group }, func(t T) bool { return t.Name() != "" }, func(t T) string { return t.Name() })
}
2022-05-01 10:32:14 +03:00
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() })
}
2022-05-07 23:19:30 +03:00
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)
2022-04-27 22:44:39 +03:00
for _, v := range input {
2022-05-01 10:32:14 +03:00
k := mapper(v)
if filter(v) {
2022-04-29 12:31:05 +03:00
output[k] = append(output[k], v)
}
2022-04-27 22:44:39 +03:00
}
2022-04-28 21:14:33 +03:00
for _, v := range output {
2022-05-01 10:32:14 +03:00
sort.Slice(v, func(i, j int) bool { return sortMapper(v[i]) < sortMapper(v[j]) })
2022-04-28 21:14:33 +03:00
}
2022-04-27 22:44:39 +03:00
return output
}