Enqueue the incoming commands at the next tick
This sync commands for all users and in the future will let you view which moves and commands are currently being executed
This commit is contained in:
parent
217e579cec
commit
2ee68e74ac
3 changed files with 24 additions and 4 deletions
|
@ -30,6 +30,7 @@ func TestCommand_Move(t *testing.T) {
|
||||||
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to execute move command")
|
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to execute move command")
|
||||||
|
|
||||||
// Tick the world
|
// Tick the world
|
||||||
|
world.EnqueueAllIncoming()
|
||||||
world.ExecuteCommandQueues()
|
world.ExecuteCommandQueues()
|
||||||
|
|
||||||
newatributes, err := world.RoverAttributes(a)
|
newatributes, err := world.RoverAttributes(a)
|
||||||
|
|
|
@ -28,6 +28,9 @@ type World struct {
|
||||||
// Commands is the set of currently executing command streams per rover
|
// Commands is the set of currently executing command streams per rover
|
||||||
CommandQueue map[uuid.UUID]CommandStream `json:"commands"`
|
CommandQueue map[uuid.UUID]CommandStream `json:"commands"`
|
||||||
|
|
||||||
|
// Incoming represents the set of commands to add to the queue at the end of the current tick
|
||||||
|
Incoming map[uuid.UUID]CommandStream `json:"incoming"`
|
||||||
|
|
||||||
// Mutex to lock around command operations
|
// Mutex to lock around command operations
|
||||||
cmdMutex sync.RWMutex
|
cmdMutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
@ -37,6 +40,7 @@ func NewWorld(size int, chunkSize int) *World {
|
||||||
return &World{
|
return &World{
|
||||||
Rovers: make(map[uuid.UUID]Rover),
|
Rovers: make(map[uuid.UUID]Rover),
|
||||||
CommandQueue: make(map[uuid.UUID]CommandStream),
|
CommandQueue: make(map[uuid.UUID]CommandStream),
|
||||||
|
Incoming: make(map[uuid.UUID]CommandStream),
|
||||||
Atlas: NewAtlas(size, chunkSize),
|
Atlas: NewAtlas(size, chunkSize),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -289,19 +293,30 @@ func (w *World) Enqueue(rover uuid.UUID, commands ...Command) error {
|
||||||
w.cmdMutex.Lock()
|
w.cmdMutex.Lock()
|
||||||
defer w.cmdMutex.Unlock()
|
defer w.cmdMutex.Unlock()
|
||||||
|
|
||||||
// Append the commands to the current set
|
// Append the commands to the incoming set
|
||||||
cmds := w.CommandQueue[rover]
|
cmds := w.Incoming[rover]
|
||||||
w.CommandQueue[rover] = append(cmds, commands...)
|
w.Incoming[rover] = append(cmds, commands...)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnqueueAllIncoming will enqueue the incoming commands
|
||||||
|
func (w *World) EnqueueAllIncoming() {
|
||||||
|
// Add any incoming commands from this tick and clear that queue
|
||||||
|
for id, incoming := range w.Incoming {
|
||||||
|
commands := w.CommandQueue[id]
|
||||||
|
commands = append(commands, incoming...)
|
||||||
|
w.CommandQueue[id] = commands
|
||||||
|
}
|
||||||
|
w.Incoming = make(map[uuid.UUID]CommandStream)
|
||||||
|
}
|
||||||
|
|
||||||
// Execute will execute any commands in the current command queue
|
// Execute will execute any commands in the current command queue
|
||||||
func (w *World) ExecuteCommandQueues() {
|
func (w *World) ExecuteCommandQueues() {
|
||||||
w.cmdMutex.Lock()
|
w.cmdMutex.Lock()
|
||||||
defer w.cmdMutex.Unlock()
|
defer w.cmdMutex.Unlock()
|
||||||
|
|
||||||
// Iterate through all commands
|
// Iterate through all the current commands
|
||||||
for rover, cmds := range w.CommandQueue {
|
for rover, cmds := range w.CommandQueue {
|
||||||
if len(cmds) != 0 {
|
if len(cmds) != 0 {
|
||||||
// Extract the first command in the queue
|
// Extract the first command in the queue
|
||||||
|
@ -324,6 +339,9 @@ func (w *World) ExecuteCommandQueues() {
|
||||||
delete(w.CommandQueue, rover)
|
delete(w.CommandQueue, rover)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add any incoming commands from this tick and clear that queue
|
||||||
|
w.EnqueueAllIncoming()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecuteCommand will execute a single command
|
// ExecuteCommand will execute a single command
|
||||||
|
|
|
@ -127,6 +127,7 @@ func TestHandleCommand(t *testing.T) {
|
||||||
assert.NoError(t, err, "Couldn't get rover attribs")
|
assert.NoError(t, err, "Couldn't get rover attribs")
|
||||||
|
|
||||||
// Tick the command queues to progress the move command
|
// Tick the command queues to progress the move command
|
||||||
|
s.world.EnqueueAllIncoming()
|
||||||
s.world.ExecuteCommandQueues()
|
s.world.ExecuteCommandQueues()
|
||||||
|
|
||||||
attribs2, err := s.world.RoverAttributes(inst)
|
attribs2, err := s.world.RoverAttributes(inst)
|
||||||
|
|
Loading…
Add table
Reference in a new issue