74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
package combatturns
|
|
|
|
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.CombatTurns {
|
|
canonicalizeCombatTurn(&response.CombatTurns[index], order, sourceID)
|
|
}
|
|
sort.SliceStable(response.CombatTurns, func(i, j int) bool {
|
|
left, leftOK := order.EarliestValid(canonicalSourceRefs(response.CombatTurns[i].SourceRefs, sourceID))
|
|
right, rightOK := order.EarliestValid(canonicalSourceRefs(response.CombatTurns[j].SourceRefs, sourceID))
|
|
if leftOK != rightOK {
|
|
return leftOK
|
|
}
|
|
if !leftOK {
|
|
return false
|
|
}
|
|
return left < right
|
|
})
|
|
}
|
|
|
|
func canonicalizeCombatTurn(turn *combatTurnResponse, order shared.SourceRefOrder, sourceID string) {
|
|
if turn == nil {
|
|
return
|
|
}
|
|
turn.SourceRefs = combatResponseRefs(order.Canonicalize(canonicalSourceRefs(turn.SourceRefs, sourceID)))
|
|
}
|
|
|
|
func canonicalCombatTurnList(response extractionResponse, sourceID string) dnd.CombatTurnList {
|
|
turns := make([]dnd.CombatTurn, len(response.CombatTurns))
|
|
for index, turn := range response.CombatTurns {
|
|
turns[index] = dnd.CombatTurn{
|
|
Actor: turn.Actor,
|
|
TurnKind: dnd.CombatTurnKind(turn.TurnKind),
|
|
SourceRefs: canonicalSourceRefs(turn.SourceRefs, sourceID),
|
|
}
|
|
}
|
|
if response.CombatTurns == nil {
|
|
turns = nil
|
|
}
|
|
return dnd.CombatTurnList{CombatTurns: turns}
|
|
}
|
|
|
|
func canonicalSourceRefs(refs []combatSourceRefResponse, sourceID string) []source.SourceRef {
|
|
if refs == nil {
|
|
return nil
|
|
}
|
|
out := make([]source.SourceRef, len(refs))
|
|
for index, ref := range refs {
|
|
out[index] = source.SourceRef{SourceID: sourceID, StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func combatResponseRefs(refs []source.SourceRef) []combatSourceRefResponse {
|
|
if refs == nil {
|
|
return nil
|
|
}
|
|
values := make([]combatSourceRefResponse, len(refs))
|
|
for index, ref := range refs {
|
|
values[index] = combatSourceRefResponse{StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
|
|
}
|
|
return values
|
|
}
|