dorfylegends/backend/model/process.go

435 lines
12 KiB
Go
Raw Normal View History

2022-04-29 15:21:27 +03:00
package model
2022-04-29 16:28:08 +03:00
import (
2022-05-05 17:55:57 +03:00
"sort"
2022-04-29 16:28:08 +03:00
"strings"
2022-04-29 16:33:21 +03:00
2022-05-08 17:38:38 +03:00
"github.com/iancoleman/strcase"
2022-05-03 15:59:47 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
2022-05-06 14:11:12 +03:00
"golang.org/x/exp/maps"
2022-04-29 16:33:21 +03:00
"golang.org/x/exp/slices"
2022-04-29 16:28:08 +03:00
)
func (w *DfWorld) process() {
2022-05-07 22:17:05 +03:00
for id, r := range w.Rivers {
r.Id_ = id
}
2022-05-04 10:26:26 +03:00
w.addRelationshipEvents()
2022-04-29 15:21:27 +03:00
// set site in structure
for _, site := range w.Sites {
for _, structure := range site.Structures {
structure.SiteId = site.Id_
}
}
2022-04-29 16:28:08 +03:00
w.processEvents()
2022-05-03 15:59:47 +03:00
w.processCollections()
2022-05-05 17:55:57 +03:00
w.processHistoricalFigures()
for _, e := range w.Entities {
2022-05-06 14:11:12 +03:00
if len(e.Sites) > 0 {
if site, ok := w.Sites[e.Sites[0]]; ok {
if site.Type_ == SiteType_Tower {
e.Necromancer = true
}
}
}
2022-05-06 16:16:16 +03:00
for _, l := range e.EntityLink {
if l.Type_ == EntityEntityLinkType_PARENT {
e.Parent = l.Target
}
}
2022-05-05 17:55:57 +03:00
idx := slices.Index(e.Child, e.Id_)
if idx != -1 {
e.Child = append(e.Child[:idx], e.Child[idx+1:]...)
}
sort.Slice(e.Wars, func(i, j int) bool { return e.Wars[i].Id_ < e.Wars[j].Id_ })
}
2022-04-29 16:28:08 +03:00
2022-05-08 17:38:38 +03:00
if !w.Plus {
2022-05-08 20:09:55 +03:00
trimRace := func(s string) string { return strings.Trim(strcase.ToDelimited(s, ' '), " 0123456789") }
2022-05-08 17:38:38 +03:00
for _, hf := range w.HistoricalFigures {
2022-05-08 20:09:55 +03:00
hf.Race = trimRace(hf.Race)
}
for _, e := range w.Entities {
if len(e.Leaders) > 0 {
switch r := e.Leaders[0].Hf.Race; {
case r == "demon":
e.Race = "goblin"
default:
e.Race = r
}
} else {
if !strings.Contains(e.Name_, " ") {
e.Race = "kobold"
}
}
2022-05-08 17:38:38 +03:00
}
for _, a := range w.DanceForms {
a.Name_ = a.Description[:strings.Index(a.Description, " is a ")]
}
for _, a := range w.MusicalForms {
a.Name_ = a.Description[:strings.Index(a.Description, " is a ")]
}
for _, a := range w.PoeticForms {
a.Name_ = a.Description[:strings.Index(a.Description, " is a ")]
}
2022-05-08 20:09:55 +03:00
setEntityType := func(id int, t EntityType) {
if c, ok := w.Entities[id]; ok {
if c.Type_ == EntityType_Unknown {
c.Type_ = t
}
}
}
setParent := func(id, parent int) {
if id == -1 || parent == -1 {
return
}
if c, ok := w.Entities[parent]; ok {
if c.Parent != -1 {
parent = c.Parent
}
}
if c, ok := w.Entities[id]; ok {
c.Parent = parent
if p, ok := w.Entities[parent]; ok {
c.Race = p.Race
}
}
if c, ok := w.Entities[parent]; ok {
c.Child = append(c.Child, id)
}
}
list := maps.Values(w.HistoricalEvents)
sort.Slice(list, func(i, j int) bool { return list[i].Id_ < list[j].Id_ })
for _, e := range list {
switch d := e.Details.(type) {
case *HistoricalEventCreatedSite:
setParent(d.SiteCivId, d.CivId)
setEntityType(d.CivId, EntityType_Civilization)
setEntityType(d.SiteCivId, EntityType_Sitegovernment)
case *HistoricalEventDestroyedSite:
setParent(d.SiteCivId, d.DefenderCivId)
setEntityType(d.AttackerCivId, EntityType_Civilization)
setEntityType(d.DefenderCivId, EntityType_Civilization)
setEntityType(d.SiteCivId, EntityType_Sitegovernment)
case *HistoricalEventSiteTakenOver:
setParent(d.SiteCivId, d.DefenderCivId)
setParent(d.NewSiteCivId, d.AttackerCivId)
setEntityType(d.DefenderCivId, EntityType_Civilization)
setEntityType(d.SiteCivId, EntityType_Sitegovernment)
setEntityType(d.AttackerCivId, EntityType_Civilization)
setEntityType(d.NewSiteCivId, EntityType_Sitegovernment)
case *HistoricalEventHfDestroyedSite:
setParent(d.SiteCivId, d.DefenderCivId)
setEntityType(d.DefenderCivId, EntityType_Civilization)
setEntityType(d.SiteCivId, EntityType_Sitegovernment)
case *HistoricalEventReclaimSite:
setParent(d.SiteCivId, d.CivId)
setEntityType(d.CivId, EntityType_Civilization)
setEntityType(d.SiteCivId, EntityType_Sitegovernment)
case *HistoricalEventCreatedStructure:
setParent(d.SiteCivId, d.CivId)
setEntityType(d.CivId, EntityType_Civilization)
setEntityType(d.SiteCivId, EntityType_Sitegovernment)
if site, ok := w.Sites[d.SiteId]; ok {
if structure, ok := site.Structures[d.StructureId]; ok {
if structure.Type_ == StructureType_Guildhall {
setEntityType(d.SiteCivId, EntityType_Guild)
}
}
}
case *HistoricalEventChangedCreatureType:
d.NewRace = trimRace(d.NewRace)
d.OldRace = trimRace(d.OldRace)
case *HistoricalEventCreatureDevoured:
if col, ok := w.HistoricalEventCollections[e.Collection]; ok {
if cd, ok := col.Details.(*HistoricalEventCollectionBeastAttack); ok {
if len(cd.AttackerHfIds) > 0 {
d.Eater = cd.AttackerHfIds[0]
}
}
}
d.Race = "creature"
case *HistoricalEventHfNewPet:
d.Pets = "creature" // TODO from hf pets?
case *HistoricalEventItemStolen:
if col, ok := w.HistoricalEventCollections[e.Collection]; ok {
if cd, ok := col.Details.(*HistoricalEventCollectionBeastAttack); ok {
if len(cd.AttackerHfIds) > 0 {
d.Histfig = cd.AttackerHfIds[0]
}
d.Site = cd.SiteId
}
}
d.ItemType = "item"
case *HistoricalEventMasterpieceItem:
d.ItemType = "item"
}
}
for _, e := range w.Entities {
switch e.Race {
case "dwarf":
e.EntityPosition = dwarfPositions
case "elf":
e.EntityPosition = elfPositions
case "human":
e.EntityPosition = humanPositions
case "goblin":
e.EntityPosition = goblinPositions
}
for i, p := range e.EntityPosition {
p.Id_ = i
}
}
2022-05-08 17:38:38 +03:00
}
2022-04-29 15:21:27 +03:00
// check events texts
2022-05-08 17:38:38 +03:00
for _, e := range w.HistoricalEvents {
e.Details.Html(&Context{World: w})
}
for _, e := range w.HistoricalEventCollections {
e.Details.Html(e, &Context{World: w})
}
2022-05-07 18:08:42 +03:00
2022-04-29 15:21:27 +03:00
}
2022-04-29 16:28:08 +03:00
func (w *DfWorld) processEvents() {
2022-05-06 14:11:12 +03:00
list := maps.Values(w.HistoricalEvents)
sort.Slice(list, func(i, j int) bool { return list[i].Id_ < list[j].Id_ })
for _, e := range list {
2022-04-29 16:28:08 +03:00
switch d := e.Details.(type) {
case *HistoricalEventHfDoesInteraction:
2022-05-08 20:09:55 +03:00
if hf, ok := w.HistoricalFigures[d.TargetHfid]; ok {
if strings.HasPrefix(d.Interaction, "DEITY_CURSE_WEREBEAST_") {
hf.Werebeast = true
hf.WerebeastSince = e.Year
}
if strings.HasPrefix(d.Interaction, "DEITY_CURSE_VAMPIRE_") {
hf.Vampire = true
hf.VampireSince = e.Year
}
2022-05-05 17:55:57 +03:00
}
case *HistoricalEventHfLearnsSecret:
if strings.HasPrefix(d.Interaction, "SECRET_") {
2022-05-08 20:09:55 +03:00
if hf, ok := w.HistoricalFigures[d.StudentHfid]; ok {
hf.Necromancer = true
hf.NecromancerSince = e.Year
}
2022-04-29 16:28:08 +03:00
}
case *HistoricalEventCreatedSite:
w.addEntitySite(d.CivId, d.SiteId)
w.addEntitySite(d.SiteCivId, d.SiteId)
2022-05-05 13:55:33 +03:00
w.Sites[d.SiteId].Ruin = false
2022-05-07 19:11:24 +03:00
w.Sites[d.SiteId].Owner = d.CivId
2022-04-29 16:28:08 +03:00
case *HistoricalEventDestroyedSite:
w.addEntitySite(d.DefenderCivId, d.SiteId)
w.addEntitySite(d.SiteCivId, d.SiteId)
w.Sites[d.SiteId].Ruin = true
case *HistoricalEventSiteTakenOver:
w.addEntitySite(d.AttackerCivId, d.SiteId)
w.addEntitySite(d.SiteCivId, d.SiteId)
w.addEntitySite(d.DefenderCivId, d.SiteId)
w.addEntitySite(d.NewSiteCivId, d.SiteId)
2022-05-05 13:55:33 +03:00
w.Sites[d.SiteId].Ruin = false
2022-05-07 19:11:24 +03:00
w.Sites[d.SiteId].Owner = d.AttackerCivId
2022-04-29 16:28:08 +03:00
case *HistoricalEventHfDestroyedSite:
w.addEntitySite(d.SiteCivId, d.SiteId)
w.addEntitySite(d.DefenderCivId, d.SiteId)
w.Sites[d.SiteId].Ruin = true
case *HistoricalEventReclaimSite:
w.addEntitySite(d.SiteCivId, d.SiteId)
w.addEntitySite(d.SiteCivId, d.SiteId)
w.Sites[d.SiteId].Ruin = false
2022-05-07 19:11:24 +03:00
w.Sites[d.SiteId].Owner = d.CivId
2022-05-04 12:42:02 +03:00
case *HistoricalEventAddHfEntityLink:
if d.Link == HistoricalEventAddHfEntityLinkLink_Position {
if hf, ok := w.HistoricalFigures[d.Hfid]; ok {
for _, l := range hf.EntityPositionLink {
if l.EntityId == d.CivId && l.StartYear == e.Year {
l.PositionProfileId = d.PositionId
}
}
for _, l := range hf.EntityFormerPositionLink {
if l.EntityId == d.CivId && l.StartYear == e.Year {
l.PositionProfileId = d.PositionId
}
}
}
}
2022-05-04 20:31:15 +03:00
case *HistoricalEventHfReachSummit:
id, _, _ := util.FindInMap(w.MountainPeaks, func(m *MountainPeak) bool { return m.Coords == d.Coords })
d.MountainPeakId = id
2022-05-05 13:55:33 +03:00
case *HistoricalEventCreatedWorldConstruction:
if master, ok := w.WorldConstructions[d.MasterWcid]; ok {
master.Parts = append(master.Parts, d.Wcid)
}
case *HistoricalEventBuildingProfileAcquired:
if site, ok := w.Sites[d.SiteId]; ok {
if property, ok := site.SiteProperties[d.BuildingProfileId]; ok {
if structure, ok := site.Structures[property.StructureId]; ok {
structure.Ruin = false
}
d.StructureId = property.StructureId
}
}
case *HistoricalEventRazedStructure:
if site, ok := w.Sites[d.SiteId]; ok {
if structure, ok := site.Structures[d.StructureId]; ok {
structure.Ruin = true
}
}
case *HistoricalEventReplacedStructure:
if site, ok := w.Sites[d.SiteId]; ok {
if structure, ok := site.Structures[d.OldAbId]; ok {
structure.Ruin = true
}
}
2022-05-07 22:17:05 +03:00
case *HistoricalEventAssumeIdentity:
if hf, ok := w.HistoricalFigures[d.TricksterHfid]; ok {
if id, ok := w.Identities[d.IdentityId]; ok {
id.HistfigId = hf.Id_
}
}
2022-05-07 23:19:30 +03:00
case *HistoricalEventHfDied:
if hf, ok := w.HistoricalFigures[d.SlayerHfid]; ok {
hf.Kills = append(hf.Kills, d.Hfid)
}
2022-04-29 16:28:08 +03:00
}
}
}
2022-05-03 15:59:47 +03:00
func (w *DfWorld) processCollections() {
2022-05-06 14:11:12 +03:00
list := maps.Values(w.HistoricalEventCollections)
sort.Slice(list, func(i, j int) bool { return list[i].Id_ < list[j].Id_ })
for _, col := range list {
2022-05-03 15:59:47 +03:00
for _, eventId := range col.Event {
if e, ok := w.HistoricalEvents[eventId]; ok {
e.Collection = col.Id_
}
}
switch cd := col.Details.(type) {
case *HistoricalEventCollectionAbduction:
targets := make(map[int]bool)
for _, eventId := range col.Event {
if e, ok := w.HistoricalEvents[eventId]; ok {
switch d := e.Details.(type) {
case *HistoricalEventHfAbducted:
targets[d.TargetHfid] = true
}
}
}
delete(targets, -1)
cd.TargetHfids = util.Keys(targets)
case *HistoricalEventCollectionBeastAttack:
attackers := make(map[int]bool)
for _, eventId := range col.Event {
if e, ok := w.HistoricalEvents[eventId]; ok {
switch d := e.Details.(type) {
case *HistoricalEventHfSimpleBattleEvent:
attackers[d.Group1Hfid] = true
case *HistoricalEventHfAttackedSite:
attackers[d.AttackerHfid] = true
case *HistoricalEventHfDestroyedSite:
attackers[d.AttackerHfid] = true
case *HistoricalEventAddHfEntityLink:
attackers[d.Hfid] = true
case *HistoricalEventCreatureDevoured:
attackers[d.Eater] = true
case *HistoricalEventItemStolen:
attackers[d.Histfig] = true
}
}
}
delete(attackers, -1)
cd.AttackerHfIds = util.Keys(attackers)
case *HistoricalEventCollectionJourney:
HistoricalEventCollectionJourneyLoop:
for _, eventId := range col.Event {
if e, ok := w.HistoricalEvents[eventId]; ok {
switch d := e.Details.(type) {
case *HistoricalEventHfTravel:
cd.TravellerHfIds = d.GroupHfid
break HistoricalEventCollectionJourneyLoop
}
}
}
case *HistoricalEventCollectionOccasion:
for _, eventcolId := range col.Eventcol {
if e, ok := w.HistoricalEventCollections[eventcolId]; ok {
switch d := e.Details.(type) {
case *HistoricalEventCollectionCeremony:
d.OccasionEventcol = col.Id_
case *HistoricalEventCollectionCompetition:
d.OccasionEventcol = col.Id_
case *HistoricalEventCollectionPerformance:
d.OccasionEventcol = col.Id_
case *HistoricalEventCollectionProcession:
d.OccasionEventcol = col.Id_
}
}
}
2022-05-05 17:55:57 +03:00
case *HistoricalEventCollectionWar:
if e, ok := w.Entities[cd.AggressorEntId]; ok {
e.Wars = append(e.Wars, col)
}
if e, ok := w.Entities[cd.DefenderEntId]; ok {
e.Wars = append(e.Wars, col)
}
2022-05-03 15:59:47 +03:00
}
}
}
2022-04-29 16:28:08 +03:00
func (w *DfWorld) addEntitySite(entityId, siteId int) {
if e, ok := w.Entities[entityId]; ok {
2022-04-29 16:33:21 +03:00
if !slices.Contains(e.Sites, siteId) {
e.Sites = append(e.Sites, siteId)
2022-04-29 16:28:08 +03:00
}
}
}
2022-05-04 10:26:26 +03:00
func (w *DfWorld) addRelationshipEvents() {
for _, r := range w.HistoricalEventRelationships {
w.HistoricalEvents[r.Event] = &HistoricalEvent{
2022-05-04 20:31:15 +03:00
Id_: r.Event,
Year: r.Year,
Collection: -1,
Seconds72: -1,
2022-05-04 10:26:26 +03:00
Details: &HistoricalEventAddHfHfLink{
Hfid: r.SourceHf,
HfidTarget: r.TargetHf,
Relationship: r.Relationship,
2022-05-04 20:31:15 +03:00
LinkType: HistoricalEventAddHfHfLinkLinkType_Unknown,
2022-05-04 10:26:26 +03:00
},
}
}
}
2022-05-05 17:55:57 +03:00
func (w *DfWorld) processHistoricalFigures() {
// for _, hf := range w.HistoricalFigures {
// for _, i := range hf.ActiveInteraction {
// if strings.HasPrefix(i, "DEITY_CURSE_WEREBEAST_") {
// hf.Werebeast = true
// }
// if strings.HasPrefix(i, "DEITY_CURSE_VAMPIRE_") {
// hf.Vampire = true
// }
// if strings.HasPrefix(i, "SECRET_") {
// hf.Necromancer = true
// }
// }
// }
}