Pull the repair function out
This commit is contained in:
parent
9e42764398
commit
075a502103
1 changed files with 21 additions and 9 deletions
|
@ -338,6 +338,26 @@ func (w *World) RoverTurn(rover string, bearing roveapi.Bearing) (roveapi.Bearin
|
|||
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
|
||||
func (w *World) RadarFromRover(rover string) (radar []roveapi.Tile, objs []roveapi.Object, err error) {
|
||||
w.worldMutex.RLock()
|
||||
|
@ -501,17 +521,9 @@ func (w *World) ExecuteCommand(c *roveapi.Command, rover string) (err error) {
|
|||
}
|
||||
|
||||
case roveapi.CommandType_repair:
|
||||
r, err := w.GetRover(rover)
|
||||
if err != nil {
|
||||
if _, err := w.RoverRepair(rover); err != nil {
|
||||
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:
|
||||
if err := w.RoverBroadcast(rover, c.GetBroadcast()); err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue