Pull the repair function out

This commit is contained in:
Marc Di Luzio 2020-07-22 19:25:47 +01:00
parent 9e42764398
commit 075a502103

View file

@ -338,6 +338,26 @@ func (w *World) RoverTurn(rover string, bearing roveapi.Bearing) (roveapi.Bearin
return r.Bearing, nil return r.Bearing, nil
} }
// RoverRepair will turn the rover
func (w *World) RoverRepair(rover string) (int, error) {
w.worldMutex.Lock()
defer w.worldMutex.Unlock()
r, ok := w.Rovers[rover]
if !ok {
return 0, fmt.Errorf("no rover matching id")
}
// Consume an inventory item to repair if possible
if len(r.Inventory) > 0 && r.Integrity < r.MaximumIntegrity {
r.Inventory = r.Inventory[:len(r.Inventory)-1]
r.Integrity = r.Integrity + 1
r.AddLogEntryf("repaired self to %d", r.Integrity)
}
return r.Integrity, nil
}
// RadarFromRover can be used to query what a rover can currently see // RadarFromRover can be used to query what a rover can currently see
func (w *World) RadarFromRover(rover string) (radar []roveapi.Tile, objs []roveapi.Object, err error) { func (w *World) RadarFromRover(rover string) (radar []roveapi.Tile, objs []roveapi.Object, err error) {
w.worldMutex.RLock() w.worldMutex.RLock()
@ -501,17 +521,9 @@ func (w *World) ExecuteCommand(c *roveapi.Command, rover string) (err error) {
} }
case roveapi.CommandType_repair: case roveapi.CommandType_repair:
r, err := w.GetRover(rover) if _, err := w.RoverRepair(rover); err != nil {
if err != nil {
return err return err
} }
// Consume an inventory item to repair if possible
if len(r.Inventory) > 0 && r.Integrity < r.MaximumIntegrity {
r.Inventory = r.Inventory[:len(r.Inventory)-1]
r.Integrity = r.Integrity + 1
r.AddLogEntryf("repaired self to %d", r.Integrity)
w.Rovers[rover] = r
}
case roveapi.CommandType_broadcast: case roveapi.CommandType_broadcast:
if err := w.RoverBroadcast(rover, c.GetBroadcast()); err != nil { if err := w.RoverBroadcast(rover, c.GetBroadcast()); err != nil {