diff options
Diffstat (limited to 'internal/game/core_command_queue.go')
| -rw-r--r-- | internal/game/core_command_queue.go | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/internal/game/core_command_queue.go b/internal/game/core_command_queue.go index 28b7a36..8fce71d 100644 --- a/internal/game/core_command_queue.go +++ b/internal/game/core_command_queue.go @@ -106,23 +106,32 @@ func (q *CommandQueue) DeleteActive(name string) { delete(q.activeQueue, name) } -// ActivePositions returns the sorted names of every player holding an active -// command, and the position (1-indexed) of the named player among them, or 0 -// when that player has no active command. Used by the `queued` command to -// show queue order. +// ActivePositions returns the names of every player holding an active command +// in actual resolution order (earliest Timestamp first, matching +// ProcessQueuedCommands), and the 1-based position of the named player among +// them, or 0 when that player has no active command. Used by the `queued` +// command to show queue order. func (q *CommandQueue) ActivePositions(name string) (names []string, pos int) { q.mu.Lock() defer q.mu.Unlock() - for n := range q.activeQueue { - names = append(names, n) + type entry struct { + name string + at time.Time } - sort.Strings(names) - for i, n := range names { - if n == name { - return names, i + 1 + entries := make([]entry, 0, len(q.activeQueue)) + for n, qc := range q.activeQueue { + entries = append(entries, entry{name: n, at: qc.Timestamp}) + } + sort.SliceStable(entries, func(i, j int) bool { + return entries[i].at.Before(entries[j].at) + }) + for _, e := range entries { + names = append(names, e.name) + if e.name == name { + pos = len(names) } } - return names, 0 + return names, pos } // ClearPlayer drops every queued command for the player: free, active, and @@ -145,11 +154,3 @@ func (q *CommandQueue) DrainConsume(name string) (*QueuedCommand, bool) { delete(q.consumeQueue, name) return qc, ok } - -// ActiveCount returns the number of players with active commands. Used by the -// `queued` command to show position. -func (q *CommandQueue) ActiveCount() int { - q.mu.Lock() - defer q.mu.Unlock() - return len(q.activeQueue) -} |
