|
| 1 | +# Bad Input Tracker Context |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `internal/badinputtracker` package provides a thread-safe system for tracking and analyzing invalid or unrecognized user commands in the GoMud game engine. This helps administrators identify common user input patterns that might indicate missing commands or user confusion. |
| 6 | + |
| 7 | +## Key Components |
| 8 | + |
| 9 | +### Core Files |
| 10 | +- **badinputtracker.go**: Main bad input tracking functionality |
| 11 | +- **badinputtracker_test.go**: Comprehensive unit tests for all tracking functions |
| 12 | + |
| 13 | +### Key Functions |
| 14 | + |
| 15 | +#### Command Tracking |
| 16 | +- **TrackBadCommand(cmd string, rest string)**: Records an invalid command attempt |
| 17 | + - Thread-safe using mutex locking |
| 18 | + - Tracks both the command and its arguments separately |
| 19 | + - Increments counter for repeated identical bad commands |
| 20 | + - Creates nested map structure: `badCommands[cmd][rest]` |
| 21 | + |
| 22 | +#### Data Retrieval |
| 23 | +- **GetBadCommands() map[string]int**: Returns flattened view of all tracked bad commands |
| 24 | + - Thread-safe read operation |
| 25 | + - Combines command and arguments into single string key format: `"cmd rest"` |
| 26 | + - Returns copy of data with occurrence counts |
| 27 | + - Safe for concurrent access |
| 28 | + |
| 29 | +#### Data Management |
| 30 | +- **Clear()**: Resets all tracked bad command data |
| 31 | + - Thread-safe operation using mutex |
| 32 | + - Completely reinitializes the badCommands map |
| 33 | + - Used for periodic cleanup or testing |
| 34 | + |
| 35 | +### Global State |
| 36 | +- **badCommands**: `map[string]map[string]int` - Nested map tracking command failures |
| 37 | + - First level key: command name |
| 38 | + - Second level key: command arguments/rest |
| 39 | + - Value: occurrence count |
| 40 | +- **lock**: `sync.Mutex` - Ensures thread-safe access to badCommands map |
| 41 | + |
| 42 | +## Data Structure Design |
| 43 | + |
| 44 | +### Nested Map Structure |
| 45 | +```go |
| 46 | +badCommands = map[string]map[string]int{ |
| 47 | + "unknowncmd": { |
| 48 | + "arg1 arg2": 3, |
| 49 | + "different args": 1, |
| 50 | + }, |
| 51 | + "anotherbad": { |
| 52 | + "some args": 2, |
| 53 | + }, |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### Flattened Output Format |
| 58 | +```go |
| 59 | +map[string]int{ |
| 60 | + "unknowncmd arg1 arg2": 3, |
| 61 | + "unknowncmd different args": 1, |
| 62 | + "anotherbad some args": 2, |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Thread Safety |
| 67 | + |
| 68 | +### Concurrency Design |
| 69 | +- **Mutex Protection**: All operations protected by sync.Mutex |
| 70 | +- **Read/Write Safety**: Prevents data races between tracking and retrieval |
| 71 | +- **Lock Scope**: Minimal lock duration to reduce contention |
| 72 | +- **Safe Iteration**: GetBadCommands creates a copy to avoid holding locks during iteration |
| 73 | + |
| 74 | +## Usage Patterns |
| 75 | + |
| 76 | +### Tracking Bad Commands |
| 77 | +```go |
| 78 | +// Track a bad command attempt |
| 79 | +badinputtracker.TrackBadCommand("unknowncmd", "some arguments") |
| 80 | + |
| 81 | +// Track repeated attempts (increments counter) |
| 82 | +badinputtracker.TrackBadCommand("unknowncmd", "some arguments") |
| 83 | +``` |
| 84 | + |
| 85 | +### Retrieving Analytics |
| 86 | +```go |
| 87 | +// Get all bad commands with counts |
| 88 | +badCommands := badinputtracker.GetBadCommands() |
| 89 | +for cmdWithArgs, count := range badCommands { |
| 90 | + log.Printf("Bad command '%s' attempted %d times", cmdWithArgs, count) |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Periodic Cleanup |
| 95 | +```go |
| 96 | +// Clear tracking data (e.g., daily reset) |
| 97 | +badinputtracker.Clear() |
| 98 | +``` |
| 99 | + |
| 100 | +## Integration Points |
| 101 | + |
| 102 | +### Command Processing |
| 103 | +- Called when user input doesn't match any valid command |
| 104 | +- Integrated into main command parsing pipeline |
| 105 | +- Helps identify gaps in command coverage |
| 106 | + |
| 107 | +### Administrative Tools |
| 108 | +- Data accessible via admin commands for analysis |
| 109 | +- Used in web admin interface for usage statistics |
| 110 | +- Helps guide development of new commands |
| 111 | + |
| 112 | +### Logging and Monitoring |
| 113 | +- Provides data for system monitoring |
| 114 | +- Helps identify user experience issues |
| 115 | +- Supports usage pattern analysis |
| 116 | + |
| 117 | +## Testing Coverage |
| 118 | + |
| 119 | +### Comprehensive Test Suite |
| 120 | +- **Single Command Tracking**: Basic functionality verification |
| 121 | +- **Multiple Command Handling**: Different commands and arguments |
| 122 | +- **Counter Increment**: Repeated command tracking |
| 123 | +- **Complex Scenarios**: Mixed command patterns |
| 124 | +- **Clear Functionality**: Data reset verification |
| 125 | +- **Thread Safety**: Concurrent access patterns |
| 126 | + |
| 127 | +### Test Scenarios |
| 128 | +- Empty state handling |
| 129 | +- Single vs multiple command tracking |
| 130 | +- Increment behavior for identical commands |
| 131 | +- Mixed command and argument combinations |
| 132 | +- Clear operation verification |
| 133 | + |
| 134 | +## Performance Considerations |
| 135 | + |
| 136 | +### Memory Usage |
| 137 | +- **Bounded Growth**: Limited by unique command/argument combinations |
| 138 | +- **Efficient Storage**: Nested map structure minimizes memory overhead |
| 139 | +- **Cleanup Strategy**: Manual clearing prevents unbounded growth |
| 140 | + |
| 141 | +### Lookup Performance |
| 142 | +- **O(1) Access**: Map-based storage for fast lookups |
| 143 | +- **Minimal Locking**: Short critical sections for good concurrency |
| 144 | +- **Copy Strategy**: GetBadCommands creates copy to minimize lock time |
| 145 | + |
| 146 | +## Error Handling |
| 147 | + |
| 148 | +### Graceful Degradation |
| 149 | +- **No Panics**: All operations handle edge cases gracefully |
| 150 | +- **Empty State**: Safe handling of empty or uninitialized state |
| 151 | +- **Concurrent Safety**: No data corruption under concurrent access |
| 152 | + |
| 153 | +## Administrative Use Cases |
| 154 | + |
| 155 | +### Usage Analysis |
| 156 | +- Identify commonly mistyped commands |
| 157 | +- Discover missing command aliases |
| 158 | +- Analyze user behavior patterns |
| 159 | +- Guide user interface improvements |
| 160 | + |
| 161 | +### System Monitoring |
| 162 | +- Track system usage patterns |
| 163 | +- Identify potential bot or automated activity |
| 164 | +- Monitor user experience quality |
| 165 | +- Support capacity planning |
| 166 | + |
| 167 | +## Future Enhancements |
| 168 | + |
| 169 | +- Time-based tracking for temporal analysis |
| 170 | +- Integration with user session tracking |
| 171 | +- Automatic suggestion system for similar commands |
| 172 | +- Configurable retention policies |
| 173 | +- Export functionality for external analysis |
0 commit comments