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:
Marc Di Luzio 2020-06-09 20:44:25 +01:00
parent 217e579cec
commit 2ee68e74ac
3 changed files with 24 additions and 4 deletions

View file

@ -30,6 +30,7 @@ func TestCommand_Move(t *testing.T) {
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to execute move command")
// Tick the world
world.EnqueueAllIncoming()
world.ExecuteCommandQueues()
newatributes, err := world.RoverAttributes(a)

View file

@ -28,6 +28,9 @@ type World struct {
// Commands is the set of currently executing command streams per rover
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
cmdMutex sync.RWMutex
}
@ -37,6 +40,7 @@ func NewWorld(size int, chunkSize int) *World {
return &World{
Rovers: make(map[uuid.UUID]Rover),
CommandQueue: make(map[uuid.UUID]CommandStream),
Incoming: make(map[uuid.UUID]CommandStream),
Atlas: NewAtlas(size, chunkSize),
}
}
@ -289,19 +293,30 @@ func (w *World) Enqueue(rover uuid.UUID, commands ...Command) error {
w.cmdMutex.Lock()
defer w.cmdMutex.Unlock()
// Append the commands to the current set
cmds := w.CommandQueue[rover]
w.CommandQueue[rover] = append(cmds, commands...)
// Append the commands to the incoming set
cmds := w.Incoming[rover]
w.Incoming[rover] = append(cmds, commands...)
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
func (w *World) ExecuteCommandQueues() {
w.cmdMutex.Lock()
defer w.cmdMutex.Unlock()
// Iterate through all commands
// Iterate through all the current commands
for rover, cmds := range w.CommandQueue {
if len(cmds) != 0 {
// Extract the first command in the queue
@ -324,6 +339,9 @@ func (w *World) ExecuteCommandQueues() {
delete(w.CommandQueue, rover)
}
}
// Add any incoming commands from this tick and clear that queue
w.EnqueueAllIncoming()
}
// ExecuteCommand will execute a single command

View file

@ -127,6 +127,7 @@ func TestHandleCommand(t *testing.T) {
assert.NoError(t, err, "Couldn't get rover attribs")
// Tick the command queues to progress the move command
s.world.EnqueueAllIncoming()
s.world.ExecuteCommandQueues()
attribs2, err := s.world.RoverAttributes(inst)