Remove the incoming command streams, de-scopes and simplifies

This commit is contained in:
Marc Di Luzio 2020-07-23 00:13:28 +01:00
parent d49d034f0e
commit 2bc2477128
5 changed files with 94 additions and 134 deletions

View file

@ -18,7 +18,6 @@ func TestCommand_Toggle(t *testing.T) {
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_toggle})
assert.NoError(t, err)
w.EnqueueAllIncoming()
w.Tick()
r, err = w.GetRover(a)
@ -27,7 +26,6 @@ func TestCommand_Toggle(t *testing.T) {
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_toggle})
assert.NoError(t, err)
w.EnqueueAllIncoming()
w.Tick()
r, err = w.GetRover(a)
@ -42,7 +40,6 @@ func TestCommand_Turn(t *testing.T) {
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_turn, Turn: roveapi.Bearing_NorthWest})
assert.NoError(t, err)
w.EnqueueAllIncoming()
w.Tick()
r, err := w.GetRover(a)

View file

@ -37,8 +37,6 @@ type World struct {
// Commands is the set of currently executing command streams per rover
CommandQueue map[string]CommandStream
// Incoming represents the set of commands to add to the queue at the end of the current tick
CommandIncoming map[string]CommandStream
// Mutex to lock around all world operations
worldMutex sync.RWMutex
@ -49,12 +47,11 @@ type World struct {
// NewWorld creates a new world object
func NewWorld(chunkSize int) *World {
return &World{
Rovers: make(map[string]*Rover),
CommandQueue: make(map[string]CommandStream),
CommandIncoming: make(map[string]CommandStream),
Atlas: NewChunkAtlas(chunkSize),
TicksPerDay: 24,
CurrentTicks: 0,
Rovers: make(map[string]*Rover),
CommandQueue: make(map[string]CommandStream),
Atlas: NewChunkAtlas(chunkSize),
TicksPerDay: 24,
CurrentTicks: 0,
}
}
@ -423,10 +420,7 @@ func (w *World) RadarFromRover(rover string) (radar []roveapi.Tile, objs []rovea
}
// RoverCommands returns current commands for the given rover
func (w *World) RoverCommands(rover string) (incoming CommandStream, queued CommandStream) {
if c, ok := w.CommandIncoming[rover]; ok {
incoming = c
}
func (w *World) RoverCommands(rover string) (queued CommandStream) {
if c, ok := w.CommandQueue[rover]; ok {
queued = c
}
@ -465,22 +459,11 @@ func (w *World) Enqueue(rover string, commands ...*roveapi.Command) error {
w.cmdMutex.Lock()
defer w.cmdMutex.Unlock()
w.CommandIncoming[rover] = commands
w.CommandQueue[rover] = 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.CommandIncoming {
commands := w.CommandQueue[id]
commands = append(commands, incoming...)
w.CommandQueue[id] = commands
}
w.CommandIncoming = make(map[string]CommandStream)
}
// Tick will execute any commands in the current command queue and tick the world
func (w *World) Tick() {
w.cmdMutex.Lock()
@ -505,9 +488,6 @@ func (w *World) Tick() {
}
}
// Add any incoming commands from this tick and clear that queue
w.EnqueueAllIncoming()
// Change the wind every day
if (w.CurrentTicks % w.TicksPerDay) == 0 {
w.Wind = roveapi.Bearing((rand.Int() % 8) + 1) // Random cardinal bearing