simplify and improve faction master list validation
This commit is contained in:
parent
d817080bc9
commit
e210141b8f
@ -435,7 +435,6 @@ func TestDeadlockPrevention(t *testing.T) {
|
|||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
t.Error("Potential deadlock detected - test timed out")
|
t.Error("Potential deadlock detected - test timed out")
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -393,7 +393,7 @@ func (m *Manager) handleInfoCommand(args []string) (string, error) {
|
|||||||
return fmt.Sprintf("Faction '%s' not found.", args[0]), nil
|
return fmt.Sprintf("Faction '%s' not found.", args[0]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result := fmt.Sprintf("Faction Information:\n")
|
result := "Faction Information:\n"
|
||||||
result += fmt.Sprintf("ID: %d\n", faction.ID)
|
result += fmt.Sprintf("ID: %d\n", faction.ID)
|
||||||
result += fmt.Sprintf("Name: %s\n", faction.Name)
|
result += fmt.Sprintf("Name: %s\n", faction.Name)
|
||||||
result += fmt.Sprintf("Type: %s\n", faction.Type)
|
result += fmt.Sprintf("Type: %s\n", faction.Type)
|
||||||
|
@ -317,30 +317,30 @@ func (mfl *MasterFactionList) ValidateFactions() []string {
|
|||||||
mfl.mutex.RLock()
|
mfl.mutex.RLock()
|
||||||
defer mfl.mutex.RUnlock()
|
defer mfl.mutex.RUnlock()
|
||||||
|
|
||||||
// Pre-allocate slice with reasonable capacity to avoid repeated allocations
|
|
||||||
issues := make([]string, 0, 10)
|
issues := make([]string, 0, 10)
|
||||||
|
|
||||||
// Single pass through globalFactionList - check faction validity and build seen set
|
seenIDs := make(map[int32]*Faction, len(mfl.globalFactionList))
|
||||||
seenInGlobal := make(map[int32]bool, len(mfl.globalFactionList))
|
seenNames := make(map[string]*Faction, len(mfl.factionNameList))
|
||||||
for id, faction := range mfl.globalFactionList {
|
|
||||||
seenInGlobal[id] = true
|
|
||||||
|
|
||||||
|
// Pass 1: Validate globalFactionList and build seenID map
|
||||||
|
for id, faction := range mfl.globalFactionList {
|
||||||
if faction == nil {
|
if faction == nil {
|
||||||
issues = append(issues, fmt.Sprintf("Faction ID %d is nil", id))
|
issues = append(issues, fmt.Sprintf("Faction ID %d is nil", id))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine multiple faction checks in one pass (inlined IsValid())
|
if faction.ID <= 0 || faction.Name == "" {
|
||||||
if faction.ID <= 0 || len(faction.Name) == 0 {
|
issues = append(issues, fmt.Sprintf("Faction ID %d is invalid or unnamed", id))
|
||||||
issues = append(issues, fmt.Sprintf("Faction ID %d is invalid", id))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if faction.ID != id {
|
if faction.ID != id {
|
||||||
issues = append(issues, fmt.Sprintf("Faction ID mismatch: map key %d != faction ID %d", id, faction.ID))
|
issues = append(issues, fmt.Sprintf("Faction ID mismatch: map key %d != faction ID %d", id, faction.ID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
seenIDs[id] = faction
|
||||||
}
|
}
|
||||||
|
|
||||||
// Single pass through factionNameList
|
// Pass 2: Validate factionNameList and build seenName map
|
||||||
for name, faction := range mfl.factionNameList {
|
for name, faction := range mfl.factionNameList {
|
||||||
if faction == nil {
|
if faction == nil {
|
||||||
issues = append(issues, fmt.Sprintf("Faction name '%s' maps to nil", name))
|
issues = append(issues, fmt.Sprintf("Faction name '%s' maps to nil", name))
|
||||||
@ -351,36 +351,29 @@ func (mfl *MasterFactionList) ValidateFactions() []string {
|
|||||||
issues = append(issues, fmt.Sprintf("Faction name mismatch: map key '%s' != faction name '%s'", name, faction.Name))
|
issues = append(issues, fmt.Sprintf("Faction name mismatch: map key '%s' != faction name '%s'", name, faction.Name))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the pre-built set instead of map lookup
|
if _, ok := seenIDs[faction.ID]; !ok {
|
||||||
if !seenInGlobal[faction.ID] {
|
|
||||||
issues = append(issues, fmt.Sprintf("Faction '%s' (ID %d) exists in name map but not in ID map", name, faction.ID))
|
issues = append(issues, fmt.Sprintf("Faction '%s' (ID %d) exists in name map but not in ID map", name, faction.ID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
seenNames[name] = faction
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check relationship consistency - use the pre-built set for faster lookups
|
// Pass 3: Validate relationships using prebuilt seenIDs
|
||||||
for factionID, hostiles := range mfl.hostileFactions {
|
validateRelations := func(relations map[int32][]int32, relType string) {
|
||||||
if !seenInGlobal[factionID] {
|
for sourceID, targets := range relations {
|
||||||
issues = append(issues, fmt.Sprintf("Hostile relationship defined for non-existent faction %d", factionID))
|
if _, ok := seenIDs[sourceID]; !ok {
|
||||||
|
issues = append(issues, fmt.Sprintf("%s relationship defined for non-existent faction %d", relType, sourceID))
|
||||||
|
}
|
||||||
|
for _, targetID := range targets {
|
||||||
|
if _, ok := seenIDs[targetID]; !ok {
|
||||||
|
issues = append(issues, fmt.Sprintf("Faction %d has %s relationship with non-existent faction %d", sourceID, relType, targetID))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, hostileID := range hostiles {
|
|
||||||
if !seenInGlobal[hostileID] {
|
|
||||||
issues = append(issues, fmt.Sprintf("Faction %d has hostile relationship with non-existent faction %d", factionID, hostileID))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for factionID, friendlies := range mfl.friendlyFactions {
|
validateRelations(mfl.hostileFactions, "Hostile")
|
||||||
if !seenInGlobal[factionID] {
|
validateRelations(mfl.friendlyFactions, "Friendly")
|
||||||
issues = append(issues, fmt.Sprintf("Friendly relationship defined for non-existent faction %d", factionID))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, friendlyID := range friendlies {
|
|
||||||
if !seenInGlobal[friendlyID] {
|
|
||||||
issues = append(issues, fmt.Sprintf("Faction %d has friendly relationship with non-existent faction %d", factionID, friendlyID))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return issues
|
return issues
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user