public void TestMoveToInstruction() { var drone = CreateDrone(); var date = new DateTime(2018, 1, 1); var dest = new CoordinateInt2D() { X = 45, Y = 45 }; var source = new CoordinateInt2D() { X = 1, Y = 1 }; var globalInstruction = new GlobalInstruction() { TYPE = "MoveTo", Destination = dest, DroneName = "Drone_1" }; var playerCtx = new PlayerContext() { Level = 1 }; var mapper = new InstructionMapper(playerCtx); var expectedInstruction = new MoveTo(playerCtx, drone, date, source, dest); var actualInstruction = mapper.ToSpecificInstruction(globalInstruction, drone, date); actualInstruction.Should().BeEquivalentTo(expectedInstruction); }
public void TestUnloadInstruction() { var drone = new Drone(new CoordinateInt2D() { X = 4, Y = 4 }) { Name = "Drone_1", Speed = 1, StorageSize = 10 }; var globalInstruction = new GlobalInstruction() { TYPE = "Unload", Destination = null, DroneName = "Drone_1" }; var playerCtx = new PlayerContext() { Level = 1 }; var mapper = new InstructionMapper(playerCtx); var date = new DateTime(2018, 1, 1); var expectedInstruction = new Unload(playerCtx, drone, date); var actualInstruction = mapper.ToSpecificInstruction(globalInstruction, drone, date); actualInstruction.Should().BeEquivalentTo(expectedInstruction); }
public IInstruction ToSpecificInstruction( GlobalInstruction globalInstruction, IDrone drone, DateTime startedAt) { if (globalInstruction == null) { throw new ArgumentException("globalInstruction is null"); } if (drone == null) { throw new ArgumentException("drone is null"); } switch (globalInstruction.TYPE) { case "Collect": return(new Collect(PlayerContext, drone, startedAt)); case "MoveTo": return(new MoveTo(PlayerContext, drone, startedAt, drone.CurrentPosition, globalInstruction.Destination)); case "Unload": return(new Unload(PlayerContext, drone, startedAt)); default: throw new ArgumentException("Unknown instruction type " + globalInstruction.TYPE); } }
public void TestUnknownInstruction() { var drone = CreateDrone(); var globalInstruction = new GlobalInstruction() { TYPE = "Poulet", Destination = null, DroneName = "Drone_1" }; var playerCtx = new PlayerContext() { Level = 1 }; var mapper = new InstructionMapper(playerCtx); var date = new DateTime(2018, 1, 1); Action toSpecificInstructionAction = () => { mapper.ToSpecificInstruction(globalInstruction, drone, date); }; toSpecificInstructionAction.Should().Throw <ArgumentException>() .WithMessage("Unknown instruction type Poulet"); }
private string ProcessInstruction(GlobalInstruction globInstruction) { try { var drone = PlayerContext.Drones.First( dr => dr.Name == globInstruction.DroneName ); if (drone.State == DroneState.ExecutionInstruction) { return("drone is already doing an action " + drone.GetLastValidInstruction().GetType().Name); } var mapper = new InstructionMapper(PlayerContext); drone.AddInstruction(mapper.ToSpecificInstruction(globInstruction, drone, DateTime.Now)); return("OK, drone will do " + drone.GetLastValidInstruction().GetType().Name); } catch (InvalidInstructionException e) { Console.WriteLine(e.Message); return("Invalid Instruction: " + e.Message + " for instruction: " + globInstruction); } catch (InvalidOperationException e) { Console.WriteLine(e.Message); return("no such drone named: " + globInstruction.DroneName); } }