public void PickUpPiece() { Common.Schema.PickUpPiece p = new PickUpPiece() { playerGuid = game.Guid, gameId = game.GameId }; Send(XmlMessageConverter.ToXml(p)); }
public void GivenANewGame_WhenPickingUpPiece_PieceDisappearsFromBoardAndGetsPlayerId() { //Arrange GameMaster.Net.Game game = new GameMaster.Net.Game(); var gm = newGameMaster(game); var location = new Common.Schema.Location() { x = 1, y = 4 }; var player = addPlayer(gm, 1, PlayerType.leader, Common.Schema.TeamColour.blue, location, game); var piece = addPiece(location, game); var message = new PickUpPiece() { playerGuid = player.Guid }; //Act gm.MessageHandler.HandleMessage(message, new Socket(new SocketType(), new ProtocolType())).Wait(); //Assert Assert.IsNull((game.Board.Fields[location.x, location.y] as Common.SchemaWrapper.TaskField).PieceId); Assert.AreEqual(game.Pieces.Where(p => p.Id == piece.Id).Single().PlayerId, player.Id); }
public async Task HandleMessage(PickUpPiece message, Socket handler) { if (!ValidateMessage(message)) { return; } string resp = ""; await Task.Delay((int)gameMaster.Settings.ActionCosts.PickUpDelay); Wrapper.Player currentPlayer = Players.Single(p => p.Guid == message.playerGuid); gameMaster.Logger.Log(message, currentPlayer); lock (BoardLock) { Wrapper.Piece piece = Pieces.FirstOrDefault( pc => pc.Location.x == currentPlayer.Location.x && pc.Location.y == currentPlayer.Location.y && !pc.PlayerId.HasValue); if (piece == null || Pieces.Any(pc => pc.PlayerId == currentPlayer.Id)) { ConsoleDebug.Warning("No piece here or you have already a piece!"); //send empty piece collection resp = new DataMessageBuilder(currentPlayer.Id, endGame) .SetPieces(new Piece[0]) .GetXml(); } else { ConsoleDebug.Warning("Piece picked up!"); piece.PlayerId = currentPlayer.Id; var taskField = Board.Fields[currentPlayer.X, currentPlayer.Y] as Wrapper.TaskField; if (taskField != null) //update taskField { ConsoleDebug.Warning("Updating TaskField"); taskField.PieceId = null; Board.UpdateDistanceToPiece(Pieces); //clocest neighbour to piece + 1 // taskField.DistanceToPiece = new[] // { // FieldAt(gameMaster.Board.Fields, currentPlayer.X + 1, currentPlayer.Y) // ?.DistanceToPiece, // FieldAt(gameMaster.Board.Fields, currentPlayer.X - 1, currentPlayer.Y) // ?.DistanceToPiece, // FieldAt(gameMaster.Board.Fields, currentPlayer.X, currentPlayer.Y + 1) // ?.DistanceToPiece, // FieldAt(gameMaster.Board.Fields, currentPlayer.X, currentPlayer.Y - 1) // ?.DistanceToPiece //}.Where(u => u.HasValue).Select(u => u.Value).Min() + 1; } resp = new DataMessageBuilder(currentPlayer.Id, endGame) .AddPiece(new Piece() { id = piece.Id, timestamp = piece.TimeStamp, playerId = currentPlayer.Id, playerIdSpecified = true, type = PieceType.unknown }) .GetXml(); } } GameMasterClient.Connection.SendFromClient(handler, resp); }