simplify and improve faction master list validation

This commit is contained in:
Sky Johnson 2025-08-02 12:18:01 -05:00
parent d817080bc9
commit e210141b8f
3 changed files with 135 additions and 143 deletions

View File

@ -29,7 +29,7 @@ func TestMasterFactionListConcurrency(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
factionID := int32((goroutineID%10) + 1) factionID := int32((goroutineID % 10) + 1)
// Mix of read operations // Mix of read operations
faction := mfl.GetFaction(factionID) faction := mfl.GetFaction(factionID)
@ -58,8 +58,8 @@ func TestMasterFactionListConcurrency(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
factionID := int32((goroutineID%10) + 1) factionID := int32((goroutineID % 10) + 1)
targetID := int32(((goroutineID+1)%10) + 1) targetID := int32(((goroutineID + 1) % 10) + 1)
// Concurrent relationship operations // Concurrent relationship operations
if goroutineID%2 == 0 { if goroutineID%2 == 0 {
@ -132,7 +132,7 @@ func TestPlayerFactionConcurrency(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
factionID := int32((goroutineID%10) + 1) factionID := int32((goroutineID % 10) + 1)
// Mix of faction value operations // Mix of faction value operations
switch j % 4 { switch j % 4 {
@ -162,7 +162,7 @@ func TestPlayerFactionConcurrency(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
factionID := int32((goroutineID%10) + 1) factionID := int32((goroutineID % 10) + 1)
// Operations that trigger updates // Operations that trigger updates
pf.IncreaseFaction(factionID, 1) pf.IncreaseFaction(factionID, 1)
@ -191,7 +191,7 @@ func TestPlayerFactionConcurrency(t *testing.T) {
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
// Trigger faction updates // Trigger faction updates
factionID := int32((goroutineID%10) + 1) factionID := int32((goroutineID % 10) + 1)
pf.IncreaseFaction(factionID, 1) pf.IncreaseFaction(factionID, 1)
// Generate packets concurrently // Generate packets concurrently
@ -224,7 +224,7 @@ func TestManagerConcurrency(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
factionID := int32((goroutineID%10) + 1) factionID := int32((goroutineID % 10) + 1)
// Mix of statistics operations // Mix of statistics operations
switch j % 4 { switch j % 4 {
@ -334,7 +334,7 @@ func TestEntityFactionAdapterConcurrency(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
factionID := int32((goroutineID%10) + 1) factionID := int32((goroutineID % 10) + 1)
// Mix of faction ID operations // Mix of faction ID operations
switch j % 3 { switch j % 3 {
@ -374,7 +374,7 @@ func TestEntityFactionAdapterConcurrency(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
otherFactionID := int32((goroutineID%10) + 1) otherFactionID := int32((goroutineID % 10) + 1)
// Concurrent relationship checks // Concurrent relationship checks
_ = adapter.IsHostileToFaction(otherFactionID) _ = adapter.IsHostileToFaction(otherFactionID)
@ -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()
} }
}() }()
@ -446,7 +445,7 @@ func TestDeadlockPrevention(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < 100; j++ { for j := 0; j < 100; j++ {
factionID := int32((goroutineID%10) + 1) factionID := int32((goroutineID % 10) + 1)
// Mix operations that could potentially deadlock // Mix operations that could potentially deadlock
switch j % 6 { switch j % 6 {
@ -501,7 +500,7 @@ func TestRaceConditions(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
factionID := int32((goroutineID%20) + 1) factionID := int32((goroutineID % 20) + 1)
// Rapid-fire operations // Rapid-fire operations
faction := NewFaction(factionID, "Race Test", "Test", "Race test") faction := NewFaction(factionID, "Race Test", "Test", "Race test")

View File

@ -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)

View File

@ -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
} }