コード例 #1
0
        /// <summary>
        /// Method to request a Pick Up Piece action
        /// </summary>
        /// <param name="playerGuid">guid of player requesting an action</param>
        /// <param name="gameId">id of current game</param>
        /// <returns></returns>
        public DataMessage HandlePickUpPieceRequest(PickUpPieceMessage msg)
        {
            ConsoleWriter.Show("Received PickUpPiece...");
            string playerGuid = msg.PlayerGUID;
            ulong gameId = msg.GameId;
            var location = Players.Where(a => a.GUID == playerGuid).First().Location;
            var Player = Players.Where(q => q.GUID == playerGuid).First();
            GameObjects.Piece[] pieces = new GameObjects.Piece[] { null };

            var response = new DataMessage(Player.ID)
            {
                Pieces = pieces
            };

            Monitor.Enter(lockObject);
            try
            {
                var currentTaskField = actualBoard.GetField(location.X, location.Y) as GameObjects.TaskField;
                // the TaskField contains a piece
                if (currentTaskField != null && currentTaskField.Piece != null && Player.GetPiece == null)
                {
                    GameObjects.Piece pieceDataToSend = new GameObjects.Piece(currentTaskField.Piece.ID, DateTime.Now, playerId: (long)Player.ID);

                    response.Pieces[0] = pieceDataToSend;

                    var piece = currentTaskField.Piece;
                    piece.PlayerId = (long)Player.ID;
                    Player.SetPiece(piece); // Player picks up a piece
                    currentTaskField.Piece =null; // the piece is no longer on the field  
                    UpdateDistancesFromAllPieces();
                }
                // Player holds a piece and tries to pick up from empty Field - he gets a remainder that he holds a piece
                else if (currentTaskField != null && currentTaskField.Piece == null && Player.GetPiece != null)
                {
                    GameObjects.Piece pieceDataToSend = new GameObjects.Piece(Player.GetPiece.ID,
                                                                              Player.GetPiece.TimeStamp,
                                                                              Player.GetPiece.Type,
                                                                              Player.GetPiece.PlayerId);
                    response.Pieces[0] = pieceDataToSend;
                }

                // player is either on an empty TaskField or on a GoalField
            }
            finally
            {
                Monitor.Exit(lockObject);
            }
            PrintBoardState();
            Thread.Sleep(GetCosts.PickUpDelay);
            response.GameFinished = IsGameFinished;
            if (msg.ReceiveDate > GameStartDate)
                return response;
            else
                return null;
        }
コード例 #2
0
 public bool PickUpPiece()
 {
     if (gameFinished != true)
     {
         ConsoleWriter.Show(GUID + " picks up piece on location: " + Location);
         PickUpPieceMessage msg = new PickUpPieceMessage(GUID, GameId);
         LastActionTaken = ActionToComplete = ActionType.PickUpPiece;
         Controller.BeginSend(msg.Serialize()); //każda akcja od razu się wysyła, ustawia również LastActionTaken i dla move LastMoveTaken !!!!!
         WaitForActionComplete();
         return(HasPiece);
     }
     return(false);
 }