/// <summary> /// Sets info about discovered TaskField /// </summary> /// <param name="location"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <param name="field"></param> /// <param name="TaskFieldList"></param> public void SetInfoAboutDiscoveredTaskField(GameObjects.Location location, int dx, int dy, GameObjects.Field field, List <GameObjects.TaskField> TaskFieldList) { //basic information GameObjects.TaskField responseField = new GameObjects.TaskField(location.X + dx, location.Y + dy, DateTime.Now) { TimeStamp = DateTime.Now, DistanceToPiece = (field as GameObjects.TaskField).DistanceToPiece }; //anoter Player on the field if (field.HasPlayer()) { responseField.PlayerId = (long)field.Player.ID; responseField.Player = field.Player; } else { responseField.PlayerId = -1; responseField.Player = null; } //piece on the field GameObjects.Piece piece = (field as GameObjects.TaskField).Piece; if (piece != null) { responseField.Piece = piece; } else { responseField.Piece = null; } TaskFieldList.Add(responseField); }
// -------------------------------------- API #region API /// <summary> /// Method to request a Test 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 HandleTestPieceRequest(TestPieceMessage msg) { ConsoleWriter.Show("Received TestingPiece..."); string playerGuid = msg.PlayerGUID; ulong gameId = msg.GameId; Monitor.Enter(lockObject); GameObjects.Piece pieceDataToSend = null; var Player = Players.Where(q => q.GUID == playerGuid).First(); try { if (Player.GetPiece != null) { pieceDataToSend = new GameObjects.Piece(Player.GetPiece.ID, DateTime.Now, pieces.Where(p => p.ID == Player.GetPiece.ID).First().Type, (long)Player.ID); } } finally { Monitor.Exit(lockObject); } PrintBoardState(); Thread.Sleep((int)GetCosts.TestDelay); if (msg.ReceiveDate > GameStartDate) { return new DataMessage(Player.ID) { GameFinished = IsGameFinished, Pieces = new GameObjects.Piece[] { pieceDataToSend } }; } else return null; //obsluga wiadomosci ktore jeszcze nie zostaly wyslane ze wzgledu na delay }
/// <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; }
/// <summary> /// FOR UNIT TESTING - set a piece of a given type in a given location /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="type"></param> /// <param name="id"></param> /// <returns></returns> public bool SetPieceInLocation(int x, int y, TeamColour team, PieceType type, ulong id) { var piece = new GameObjects.Piece(id, DateTime.Now, type); if (ValidateFieldPosition(x, y, team) && actualBoard.GetField(x, y) is GameObjects.TaskField) { (actualBoard.GetField(x, y) as GameObjects.TaskField).Piece = piece; UpdateDistancesFromAllPieces(); return(true); } else { return(false); } }
private GameObjects.Piece CreatePiece() { var possibleSham = random.NextDouble(); GameArea.GameObjects.Piece piece; if (possibleSham <= GetGameDefinition.ShamProbability) { piece = new GameObjects.Piece(nextPieceId++, DateTime.Now, PieceType.sham); } else { piece = new GameObjects.Piece(nextPieceId++, DateTime.Now, PieceType.normal); } pieces.Add(piece); return(piece); }