From 2ee68e74acae7892c8ce2d4fe7a6e2cb6ca46065 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Tue, 9 Jun 2020 20:44:25 +0100 Subject: [PATCH] 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 --- pkg/game/command_test.go | 1 + pkg/game/world.go | 26 ++++++++++++++++++++++---- pkg/server/routes_test.go | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/game/command_test.go b/pkg/game/command_test.go index ebc528c..dcdd9a1 100644 --- a/pkg/game/command_test.go +++ b/pkg/game/command_test.go @@ -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) diff --git a/pkg/game/world.go b/pkg/game/world.go index 150a9b1..6002b25 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -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 diff --git a/pkg/server/routes_test.go b/pkg/server/routes_test.go index 46776d2..e4f16af 100644 --- a/pkg/server/routes_test.go +++ b/pkg/server/routes_test.go @@ -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)