/// <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); }
/// <summary> /// Method to request a Discover action /// </summary> /// <param name="playerGuid">guid of player requesting an action</param> /// <param name="gameId">id of current game</param> /// <returns></returns> public DataMessage HandleDiscoverRequest(DiscoverMessage msg) { ConsoleWriter.Show("Received Discover ..."); string playerGuid = msg.PlayerGUID; ulong gameId = msg.GameId; var location = Players.Where(a => a.GUID == playerGuid).First().Location; var team = Players.Where(q => q.GUID == playerGuid).First().Team; List<GameObjects.TaskField> TaskFieldList = new List<GameObjects.TaskField>(); List<GameObjects.GoalField> GoalFieldList = new List<GameObjects.GoalField>(); Monitor.Enter(lockObject); try { for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { if (dx == 0 && dy == 0) continue; if (ValidateFieldPosition((location.X + dx),(location.Y + dy), team)) { GameObjects.Field field = actualBoard.GetField(location.X + dx, location.Y + dy); // discovered field is a TaskField - can contain players and pieces if (field is GameObjects.TaskField) { SetInfoAboutDiscoveredTaskField(location, dx, dy, field, TaskFieldList); } //// discovered field is a GoalField - can contain players //else if (field is GameObjects.GoalField) //{ // SetInfoAboutDiscoveredGoalField(location, dx, dy, field, GoalFieldList); //} } } } } finally { Monitor.Exit(lockObject); } PrintBoardState(); Thread.Sleep((int)GetCosts.DiscoverDelay); if (msg.ReceiveDate > GameStartDate) { return new DataMessage(Players.Where(q => q.GUID == playerGuid).First().ID) { GameFinished = IsGameFinished, Tasks = TaskFieldList.ToArray(), Goals = GoalFieldList.ToArray() }; } else return null; }
/// <summary> /// Sets info about discovered GoalField /// </summary> /// <param name="location"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <param name="field"></param> /// <param name="GoalFieldList"></param> public void SetInfoAboutDiscoveredGoalField(GameObjects.Location location, int dx, int dy, GameObjects.Field field, List <GameObjects.GoalField> GoalFieldList) { GameObjects.GoalField responseField = new GameObjects.GoalField(location.X + dx, location.Y + dy, DateTime.Now, (field as GameObjects.GoalField).Team, GoalFieldType.unknown) { TimeStamp = DateTime.Now }; if (field.HasPlayer()) { responseField.PlayerId = (long)field.Player.ID; responseField.Player = field.Player; } else { responseField.PlayerId = -1; responseField.Player = null; } GoalFieldList.Add(responseField); }
/// <summary> /// Method to request a Move action /// </summary> /// <param name="direction">direction requested by a Player</param> /// <param name="playerGuid">guid of player requesting an action</param> /// <param name="gameId">id of current game</param> /// <returns></returns> public DataMessage HandleMoveRequest(MoveMessage msg) { ConsoleWriter.Show("Received Move ..."); string playerGuid = msg.PlayerGUID; ulong gameId = msg.GameId; MoveType direction = (MoveType)msg.Direction; var currentLocation = Players.Where(a => a.GUID == playerGuid).First().Location; var team = Players.Where(a => a.GUID == playerGuid).First().Team; var Player = Players.Where(q => q.GUID == playerGuid).First(); // perform location delta and get future field var futureLocation = PerformLocationDelta(direction, currentLocation, team); var futureBoardField = actualBoard.GetField(futureLocation.X, futureLocation.Y); //basic info for response DataMessage response = new DataMessage(Player.ID) { Tasks = new GameObjects.TaskField[] { }, Goals = null, PlayerLocation = currentLocation, }; //player tried to step out of the board or enetr wrong GoalArea if (!ValidateFieldPosition(futureLocation.X, futureLocation.Y, team)) return response; GameObjects.Field responseField; GameObjects.Field field = actualBoard.GetField(futureLocation.X, futureLocation.Y); Monitor.Enter(lockObject); try { // what type of field are we trying to enter - Task or Goal? // we set info about a FutureField if (futureBoardField is GameObjects.TaskField) { //TryMovePlayerToTaskField(response, futureLocation, playerGuid, out piece, out field); List<GameObjects.TaskField> tempList = new List<GameObjects.TaskField>(); SetInfoAboutDiscoveredTaskField(futureLocation, 0, 0, field, tempList); response.Tasks = tempList.ToArray(); responseField = response.Tasks[0]; } else //if (futureBoardField is GameArea.GoalField) { //TryMovePlayerToGoalField(response, futureLocation, playerGuid, out field); response.Goals = new GameObjects.GoalField[] { }; response.Tasks = null; List<GameObjects.GoalField> tempList = new List<GameObjects.GoalField>(); SetInfoAboutDiscoveredGoalField(futureLocation, 0, 0, field, tempList); response.Goals = tempList.ToArray(); responseField = response.Goals[0]; (responseField as GameObjects.GoalField).Type = GoalFieldType.unknown; // after move request Player receives no info about GoalField type } // check if there is another Player on the field we're trying to enter // if so, we don't actually move, just get an update on the field if (responseField.Player == null) { // perform move action PerformMoveAction(currentLocation, futureLocation, playerGuid, response); } } finally { Monitor.Exit(lockObject); } PrintBoardState(); Thread.Sleep((int)GetCosts.MoveDelay); response.GameFinished = IsGameFinished; if (msg.ReceiveDate > GameStartDate) return response; else return null; }