public MoveResult Move(MoveCommand move) { if (IsValidMoveRequest(move)) { return _gameManager.Execute(move); } return null; }
public MoveResult Execute(MoveCommand command) { // TODO Validate Command authToken var game = Games[command.GameId]; // TODO Execute command against game var result = game.MoveElevator(command); // TODO Return result return result; }
public void PlayerCanIssueMoveDownCommand() { var game = new Game(); var logonResult = game.LogonPlayer("MyCoolAgent"); var command = new MoveCommand() { AuthToken = logonResult.AuthToken, Direction = "Up", ElevatorId = 0 }; game.Update(Game.START_DELAY); var result = game.MoveElevator(command); Assert.IsTrue(result.Success); Elevator elevator; game.Elevators.TryGetValue(0, out elevator); //Assert.AreEqual(elevator.Floor, 1); // elevators now start on a random floor. int oldFloor = elevator.Floor; game.Update(Game.TURN_DURATION); game.Update(Game.SERVER_PROCESSING); command.Direction = "Down"; result = game.MoveElevator(command); Assert.IsTrue(result.Success); game.Elevators.TryGetValue(0, out elevator); if (oldFloor > 0) { Assert.AreEqual(elevator.Floor, oldFloor - 1); } else if (oldFloor == 0) { Assert.AreEqual(elevator.Floor, 0); } else { // not possible Assert.Fail(); } }
private MoveResult _validateMoveElevatorErrors(MoveCommand command) { var token = command.AuthToken; var id = command.ElevatorId; Elevator elevator; var exists = Elevators.TryGetValue(id, out elevator); if (!exists) { return new MoveResult() { Success = false, Message = string.Format("Elevator for id {0} does not exist", id) }; } if (string.IsNullOrEmpty(command.Direction)) { return new MoveResult() { Success = false, Message = string.Format("Could not move elevator on empty direction") }; } if (command.Direction.ToLower() != "up" && command.Direction.ToLower() != "down" && command.Direction.ToLower() != "stop") { return new MoveResult() { Success = false, Message = string.Format("Could not move elevator on invalid direction {0}", command.Direction) }; } if (elevator.PlayerToken != token) { return new MoveResult() { Success = false, Message = string.Format("Can't move elevators you don't own ;) ID: {0}", command.ElevatorId) }; } if (elevator.Done) { return new MoveResult() { Success = false, Message = string.Format("Elevator has already been moved") }; } if (Processing) { return new MoveResult() { Success = false, Message = string.Format("Server is processing, please try again soon") }; } return null; }
public MoveResult MoveElevator(MoveCommand command) { var result = new MoveResult(); //var token = command.AuthToken; var id = command.ElevatorId; Elevator elevator; var exists = Elevators.TryGetValue(id, out elevator); var error = _validateMoveElevatorErrors(command); if (error == null && elevator != null) { elevator.Done = true; // we have validated control and existance of the elevator if (command.Direction.ToLower() == "up") { elevator.Floor = Math.Min(elevator.Floor + 1, NUMBER_OF_FLOORS - 1); elevator.IsStopped = false; result.Message = string.Format("Moved elevator {0} up successfully", command.ElevatorId); } else if (command.Direction.ToLower() == "down") { elevator.Floor = Math.Max(elevator.Floor - 1, 0); elevator.IsStopped = false; result.Message = string.Format("Moved elevator {0} down successfully", command.ElevatorId); } else if (command.Direction.ToLower() == "stop") { elevator.IsStopped = true; result.Message = string.Format("Stopped elevator {0} successfully", command.ElevatorId); } result.Success = true; } else { System.Diagnostics.Debug.WriteLine(error.Message); return error; } return result; }
private bool IsValidMoveRequest(MoveCommand move) { if (move != null && !string.IsNullOrWhiteSpace(move.AuthToken) && move.GameId >= 0) { if (move.Direction.Equals("up", StringComparison.InvariantCultureIgnoreCase) || move.Direction.Equals("down", StringComparison.InvariantCultureIgnoreCase) || move.Direction.Equals("stop", StringComparison.InvariantCultureIgnoreCase)) { return true; } } return false; }
public void PlayersCanIssueMoveUpCommands() { var game = new Game(); var logonResult = game.LogonPlayer("MyCoolAgent"); var command = new MoveCommand() { AuthToken = logonResult.AuthToken, Direction = "UP", ElevatorId = 0 }; Elevator elevator; game.Elevators.TryGetValue(0, out elevator); elevator.Floor = 3; var result = game.MoveElevator(command); Assert.IsTrue(result.Success); game.Elevators.TryGetValue(0, out elevator); Assert.AreEqual(elevator.Floor, 4); }
public void PlayersCantMoveElevatorsTheyDontOwn() { var game = new Game(); var logonResult = game.LogonPlayer("MyCoolAgent"); var command = new MoveCommand() { AuthToken = logonResult.AuthToken, Direction = "up", ElevatorId = 2 }; var result = game.MoveElevator(command); Assert.IsFalse(result.Success); }
public void PlayersCanLoadElevatorsFairly() { var game = new Game(); var logonResult = game.LogonPlayer("MyCoolAgent"); var command = new MoveCommand() { AuthToken = logonResult.AuthToken, Direction = "stop", ElevatorId = 0 }; game.Update(Game.START_DELAY); game.Elevators[0].Floor = 0; game.Elevators[1].Floor = 0; var e0 = game.Elevators[0]; var e1 = game.Elevators[1]; e0.Meeples.Add(new Meeple(game, game.Floors[0])); e0.Meeples.Add(new Meeple(game, game.Floors[0])); game.Elevators[2].Floor = 2; game.Elevators[3].Floor = 2; var meeples = new List<Meeple>() { new Meeple(game, game.Floors[0]){Destination = 10}, new Meeple(game, game.Floors[0]){Destination = 7} }; game.Floors[0].Meeples = meeples; var result = game.MoveElevator(command); command.ElevatorId = 1; game.MoveElevator(command); foreach (var meeple in meeples) { Assert.IsFalse(meeple.InElevator); } game.Update(Game.TURN_DURATION); game.Update(Game.SERVER_PROCESSING); Assert.AreEqual(game.Floors[0].Number, 0); Assert.IsTrue(result.Success); Assert.AreEqual(e0.IsStopped, true); Assert.AreEqual(e0.Floor, 0); Assert.AreEqual(e0.Meeples.Count, 2); Assert.AreEqual(e1.IsStopped, true); Assert.AreEqual(e1.Floor, 0); Assert.AreEqual(e1.Meeples.Count, 2); foreach (var meeple in e1.Meeples) { Assert.IsTrue(meeple.InElevator); } Assert.AreEqual(game.Floors[0].Meeples.Count, 0); }
public void PlayersCanIssueOneCommandPerElevatorPerTurn() { var game = new Game(); var logonResult = game.LogonPlayer("MyCoolAgent"); var command = new MoveCommand() { AuthToken = logonResult.AuthToken, Direction = "UP", ElevatorId = 0 }; Elevator elevator; game.Elevators.TryGetValue(0, out elevator); elevator.Floor = 0; var result = game.MoveElevator(command); Assert.IsTrue(result.Success); game.Elevators.TryGetValue(0, out elevator); Assert.AreEqual(elevator.Floor, 1); // should fail on the second attempt and not advance the elevator result = game.MoveElevator(command); Assert.IsFalse(result.Success); Assert.AreEqual(elevator.Floor, 1); // should work on the other elevator game.Elevators.TryGetValue(1, out elevator); command.ElevatorId = 1; elevator.Floor = 0; result = game.MoveElevator(command); Assert.IsTrue(result.Success); Assert.AreEqual(elevator.Floor, 1); }