Tag rovers by the controlling account

This commit is contained in:
Marc Di Luzio 2020-07-24 22:22:32 +01:00
parent e840b3e47b
commit 6f2d67bd7c
5 changed files with 34 additions and 23 deletions

View file

@ -205,7 +205,7 @@ func (s *Server) LoadWorld() error {
// SpawnRoverForAccount spawns the rover rover for an account // SpawnRoverForAccount spawns the rover rover for an account
func (s *Server) SpawnRoverForAccount(account string) (string, error) { func (s *Server) SpawnRoverForAccount(account string) (string, error) {
inst, err := s.world.SpawnRover() inst, err := s.world.SpawnRover(account)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -10,7 +10,7 @@ import (
func TestCommand_Toggle(t *testing.T) { func TestCommand_Toggle(t *testing.T) {
w := NewWorld(8) w := NewWorld(8)
a, err := w.SpawnRover() a, err := w.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
r, err := w.GetRover(a) r, err := w.GetRover(a)
@ -36,7 +36,7 @@ func TestCommand_Toggle(t *testing.T) {
func TestCommand_Turn(t *testing.T) { func TestCommand_Turn(t *testing.T) {
w := NewWorld(8) w := NewWorld(8)
a, err := w.SpawnRover() a, err := w.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_turn, Bearing: roveapi.Bearing_NorthWest}) err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_turn, Bearing: roveapi.Bearing_NorthWest})
@ -50,7 +50,7 @@ func TestCommand_Turn(t *testing.T) {
func TestCommand_Stash(t *testing.T) { func TestCommand_Stash(t *testing.T) {
w := NewWorld(8) w := NewWorld(8)
name, err := w.SpawnRover() name, err := w.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
info, err := w.GetRover(name) info, err := w.GetRover(name)
@ -78,7 +78,7 @@ func TestCommand_Stash(t *testing.T) {
func TestCommand_Repair(t *testing.T) { func TestCommand_Repair(t *testing.T) {
w := NewWorld(8) w := NewWorld(8)
name, err := w.SpawnRover() name, err := w.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
info, err := w.GetRover(name) info, err := w.GetRover(name)
@ -118,7 +118,7 @@ func TestCommand_Repair(t *testing.T) {
func TestCommand_Broadcast(t *testing.T) { func TestCommand_Broadcast(t *testing.T) {
w := NewWorld(8) w := NewWorld(8)
name, err := w.SpawnRover() name, err := w.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
// Enqueue the broadcast and tick // Enqueue the broadcast and tick
@ -133,7 +133,7 @@ func TestCommand_Broadcast(t *testing.T) {
func TestCommand_Salvage(t *testing.T) { func TestCommand_Salvage(t *testing.T) {
w := NewWorld(8) w := NewWorld(8)
name, err := w.SpawnRover() name, err := w.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
info, err := w.GetRover(name) info, err := w.GetRover(name)
@ -161,7 +161,7 @@ func TestCommand_Salvage(t *testing.T) {
func TestCommand_Invalid(t *testing.T) { func TestCommand_Invalid(t *testing.T) {
w := NewWorld(8) w := NewWorld(8)
name, err := w.SpawnRover() name, err := w.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_none}) err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_none})

View file

@ -66,6 +66,9 @@ type Rover struct {
// Logs Stores log of information // Logs Stores log of information
Logs []RoverLogEntry Logs []RoverLogEntry
// The account that owns this rover
Owner string
} }
// DefaultRover returns a default rover object with default settings // DefaultRover returns a default rover object with default settings

View file

@ -63,13 +63,16 @@ func NewWorld(chunkSize int) *World {
} }
// SpawnRover adds an rover to the game // SpawnRover adds an rover to the game
func (w *World) SpawnRover() (string, error) { func (w *World) SpawnRover(account string) (string, error) {
w.worldMutex.Lock() w.worldMutex.Lock()
defer w.worldMutex.Unlock() defer w.worldMutex.Unlock()
// Initialise the rover // Initialise the rover
rover := DefaultRover() rover := DefaultRover()
// Assign the owner
rover.Owner = account
// Spawn in a random place near the origin // Spawn in a random place near the origin
rover.Pos = maths.Vector{ rover.Pos = maths.Vector{
X: 10 - rand.Intn(20), X: 10 - rand.Intn(20),
@ -375,6 +378,9 @@ func (w *World) RoverTransfer(rover string) (string, error) {
oldRover.AddLogEntryf("transferring to dormant rover %s", newRover.Name) oldRover.AddLogEntryf("transferring to dormant rover %s", newRover.Name)
newRover.AddLogEntryf("transferred from rover %s", oldRover.Name) newRover.AddLogEntryf("transferred from rover %s", oldRover.Name)
// Clear the old owner
oldRover.Owner = ""
// Marshal old rover // Marshal old rover
oldRoverData, err := json.Marshal(oldRover) oldRoverData, err := json.Marshal(oldRover)
if err != nil { if err != nil {
@ -384,7 +390,9 @@ func (w *World) RoverTransfer(rover string) (string, error) {
// Add this new rover to tracking // Add this new rover to tracking
w.Rovers[newRover.Name] = &newRover w.Rovers[newRover.Name] = &newRover
// TODO: Swap account rover to the dormant one // Swap account rover to the dormant one
newRover.Owner = oldRover.Owner
w.Accountant.AssignData(oldRover.Owner, "rover", newRover.Name)
// Place the old rover into the world // Place the old rover into the world
w.Atlas.SetObject(oldRover.Pos, Object{Type: roveapi.Object_RoverDormant, Data: oldRoverData}) w.Atlas.SetObject(oldRover.Pos, Object{Type: roveapi.Object_RoverDormant, Data: oldRoverData})

View file

@ -18,9 +18,9 @@ func TestNewWorld(t *testing.T) {
func TestWorld_CreateRover(t *testing.T) { func TestWorld_CreateRover(t *testing.T) {
world := NewWorld(8) world := NewWorld(8)
a, err := world.SpawnRover() a, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
b, err := world.SpawnRover() b, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
// Basic duplicate check // Basic duplicate check
@ -33,7 +33,7 @@ func TestWorld_CreateRover(t *testing.T) {
func TestWorld_GetRover(t *testing.T) { func TestWorld_GetRover(t *testing.T) {
world := NewWorld(4) world := NewWorld(4)
a, err := world.SpawnRover() a, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
rover, err := world.GetRover(a) rover, err := world.GetRover(a)
@ -44,9 +44,9 @@ func TestWorld_GetRover(t *testing.T) {
func TestWorld_DestroyRover(t *testing.T) { func TestWorld_DestroyRover(t *testing.T) {
world := NewWorld(1) world := NewWorld(1)
a, err := world.SpawnRover() a, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
b, err := world.SpawnRover() b, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
err = world.DestroyRover(a) err = world.DestroyRover(a)
@ -62,7 +62,7 @@ func TestWorld_DestroyRover(t *testing.T) {
func TestWorld_GetSetMovePosition(t *testing.T) { func TestWorld_GetSetMovePosition(t *testing.T) {
world := NewWorld(4) world := NewWorld(4)
a, err := world.SpawnRover() a, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
pos := maths.Vector{ pos := maths.Vector{
@ -97,9 +97,9 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
func TestWorld_RadarFromRover(t *testing.T) { func TestWorld_RadarFromRover(t *testing.T) {
// Create world that should have visible walls on the radar // Create world that should have visible walls on the radar
world := NewWorld(2) world := NewWorld(2)
a, err := world.SpawnRover() a, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
b, err := world.SpawnRover() b, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
// Warp the rovers into position // Warp the rovers into position
@ -130,7 +130,7 @@ func TestWorld_RadarFromRover(t *testing.T) {
func TestWorld_RoverDamage(t *testing.T) { func TestWorld_RoverDamage(t *testing.T) {
world := NewWorld(2) world := NewWorld(2)
a, err := world.SpawnRover() a, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
pos := maths.Vector{ pos := maths.Vector{
@ -160,7 +160,7 @@ func TestWorld_RoverDamage(t *testing.T) {
func TestWorld_Daytime(t *testing.T) { func TestWorld_Daytime(t *testing.T) {
world := NewWorld(1) world := NewWorld(1)
a, err := world.SpawnRover() a, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
// Remove rover charge // Remove rover charge
@ -199,10 +199,10 @@ func TestWorld_Daytime(t *testing.T) {
func TestWorld_Broadcast(t *testing.T) { func TestWorld_Broadcast(t *testing.T) {
world := NewWorld(8) world := NewWorld(8)
a, err := world.SpawnRover() a, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
b, err := world.SpawnRover() b, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
// Warp rovers near to eachother // Warp rovers near to eachother
@ -265,7 +265,7 @@ func TestWorld_Sailing(t *testing.T) {
world.Tick() // One initial tick to set the wind direction the first time world.Tick() // One initial tick to set the wind direction the first time
world.Wind = roveapi.Bearing_North // Set the wind direction to north world.Wind = roveapi.Bearing_North // Set the wind direction to north
name, err := world.SpawnRover() name, err := world.SpawnRover("tmp")
assert.NoError(t, err) assert.NoError(t, err)
// Warp the rover to 0,0 after clearing it // Warp the rover to 0,0 after clearing it