Introduce typed D&D spell artifacts
This commit is contained in:
@@ -3,15 +3,17 @@ 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, sourceID string) {
|
||||
func canonicalizeResponse(response *extractionResponse) {
|
||||
if response == nil {
|
||||
return
|
||||
}
|
||||
for index := range response.SpellCasts {
|
||||
canonicalizeSpellCast(&response.SpellCasts[index], sourceID)
|
||||
canonicalizeSpellCast(&response.SpellCasts[index])
|
||||
}
|
||||
sort.SliceStable(response.SpellCasts, func(i, j int) bool {
|
||||
left, leftOK := earliestSourceUnit(response.SpellCasts[i])
|
||||
@@ -26,9 +28,8 @@ func canonicalizeResponse(response *extractionResponse, sourceID string) {
|
||||
})
|
||||
}
|
||||
|
||||
func canonicalizeSpellCast(spell *spellCastResponse, sourceID string) {
|
||||
func canonicalizeSpellCast(spell *spellCastResponse) {
|
||||
for index := range spell.SourceRefs {
|
||||
spell.SourceRefs[index].SourceID = sourceID
|
||||
spell.SourceRefs[index].StartUnitID = canonicalUnitRef(spell.SourceRefs[index].StartUnitID)
|
||||
spell.SourceRefs[index].EndUnitID = canonicalUnitRef(spell.SourceRefs[index].EndUnitID)
|
||||
}
|
||||
@@ -51,12 +52,12 @@ func canonicalUnitRef(ref shared.UnitRef) shared.UnitRef {
|
||||
return shared.UnitRefFromInt(value)
|
||||
}
|
||||
|
||||
func dedupeSourceRefs(refs []shared.SourceRefResponse) []shared.SourceRefResponse {
|
||||
func dedupeSourceRefs(refs []spellSourceRefResponse) []spellSourceRefResponse {
|
||||
if len(refs) < 2 {
|
||||
return refs
|
||||
}
|
||||
out := refs[:0]
|
||||
var previous shared.SourceRefResponse
|
||||
var previous spellSourceRefResponse
|
||||
for index, ref := range refs {
|
||||
if index > 0 && sameSourceRef(previous, ref) {
|
||||
continue
|
||||
@@ -67,9 +68,8 @@ func dedupeSourceRefs(refs []shared.SourceRefResponse) []shared.SourceRefRespons
|
||||
return out
|
||||
}
|
||||
|
||||
func sameSourceRef(left shared.SourceRefResponse, right shared.SourceRefResponse) bool {
|
||||
return left.SourceID == right.SourceID &&
|
||||
left.StartUnitID.Int() == right.StartUnitID.Int() &&
|
||||
func sameSourceRef(left spellSourceRefResponse, right spellSourceRefResponse) bool {
|
||||
return left.StartUnitID.Int() == right.StartUnitID.Int() &&
|
||||
left.EndUnitID.Int() == right.EndUnitID.Int()
|
||||
}
|
||||
|
||||
@@ -90,3 +90,28 @@ func unitSortValue(ref shared.UnitRef) int {
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
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.Int(),
|
||||
EndUnitID: ref.EndUnitID.Int(),
|
||||
}
|
||||
}
|
||||
spellCasts[index] = dnd.SpellCast{
|
||||
Caster: spell.Caster,
|
||||
Spell: spell.Spell,
|
||||
Effect: spell.Effect,
|
||||
NarrativeDescription: spell.NarrativeDescription,
|
||||
SourceRefs: refs,
|
||||
}
|
||||
}
|
||||
if response.SpellCasts == nil {
|
||||
spellCasts = nil
|
||||
}
|
||||
return dnd.SpellList{SpellCasts: spellCasts}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user