82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package spells
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
|
)
|
|
|
|
func canonicalizeResponse(response *extractionResponse, order shared.SourceRefOrder, sourceID string) {
|
|
if response == nil {
|
|
return
|
|
}
|
|
for index := range response.SpellCasts {
|
|
canonicalizeSpellCast(&response.SpellCasts[index], order, sourceID)
|
|
}
|
|
sort.SliceStable(response.SpellCasts, func(i, j int) bool {
|
|
left, leftOK := order.EarliestValid(spellSourceRefs(response.SpellCasts[i].SourceRefs, sourceID))
|
|
right, rightOK := order.EarliestValid(spellSourceRefs(response.SpellCasts[j].SourceRefs, sourceID))
|
|
if leftOK != rightOK {
|
|
return leftOK
|
|
}
|
|
if !leftOK {
|
|
return false
|
|
}
|
|
return left < right
|
|
})
|
|
}
|
|
|
|
func canonicalizeSpellCast(spell *spellCastResponse, order shared.SourceRefOrder, sourceID string) {
|
|
if spell == nil {
|
|
return
|
|
}
|
|
spell.SourceRefs = spellResponseRefs(order.Canonicalize(spellSourceRefs(spell.SourceRefs, sourceID)))
|
|
}
|
|
|
|
func spellSourceRefs(refs []spellSourceRefResponse, sourceID string) []source.SourceRef {
|
|
if refs == nil {
|
|
return nil
|
|
}
|
|
values := make([]source.SourceRef, len(refs))
|
|
for index, ref := range refs {
|
|
values[index] = source.SourceRef{SourceID: sourceID, StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
|
|
}
|
|
return values
|
|
}
|
|
|
|
func spellResponseRefs(refs []source.SourceRef) []spellSourceRefResponse {
|
|
if refs == nil {
|
|
return nil
|
|
}
|
|
values := make([]spellSourceRefResponse, len(refs))
|
|
for index, ref := range refs {
|
|
values[index] = spellSourceRefResponse{StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
|
|
}
|
|
return values
|
|
}
|
|
|
|
func canonicalSpellList(response extractionResponse, sourceID string) dnd.SpellList {
|
|
spellCasts := make([]dnd.SpellCast, len(response.SpellCasts))
|
|
for index, spell := range response.SpellCasts {
|
|
refs := make([]source.SourceRef, len(spell.SourceRefs))
|
|
for refIndex, ref := range spell.SourceRefs {
|
|
refs[refIndex] = source.SourceRef{
|
|
SourceID: sourceID,
|
|
StartUnitID: ref.StartUnitID,
|
|
EndUnitID: ref.EndUnitID,
|
|
}
|
|
}
|
|
spellCasts[index] = dnd.SpellCast{
|
|
Caster: spell.Caster,
|
|
Spell: spell.Spell,
|
|
SourceRefs: refs,
|
|
}
|
|
}
|
|
if response.SpellCasts == nil {
|
|
spellCasts = nil
|
|
}
|
|
return dnd.SpellList{SpellCasts: spellCasts}
|
|
}
|