public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { var msg = new Data(); IPlayer player = connection.GameState.BluePlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); if (player == null) { player = connection.GameState.RedPlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); } msg.playerId = player.Id; Piece[] pickedUpPiece = new Piece[1]; foreach (PieceInfo p in connection.GameState.Pieces) { if (player.Location.x == p.Location.x && player.Location.y == p.Location.y) { p.Piece.playerId = player.Id; p.Piece.playerIdSpecified = true; pickedUpPiece[0] = new Piece() { type = PieceType.unknown, playerId = player.Id, playerIdSpecified = true, id = p.Piece.id }; msg.Pieces = pickedUpPiece; connection.GameState.CalculateManhattanDistance(); } } msg.gameFinished = connection.GameState.GameFinished; var serializer = new Messages.XmlHandling.Serializer(msg); connection.DelayMessage(serializer, connection.GameState.ActionCosts.PickUpDelay); }
public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { IPlayer player = connection.GameState.BluePlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); if (player == null) { player = connection.GameState.RedPlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); } //TO DO:location of piece carried by player should also change var location = Validate(connection, player); var piece = connection.GameState.Pieces.FirstOrDefault(p => p.Piece.playerId == player.Id && p.Piece.playerIdSpecified == true); if (piece != null) { piece.Location = location; } var msg = new Data(); msg.PlayerLocation = player.Location = location; msg.playerId = player.Id; msg.gameFinished = connection.GameState.GameFinished; var serializer = new Messages.XmlHandling.Serializer(msg); Task delay = new Task(() => { System.Threading.Thread.Sleep((int)connection.GameState.ActionCosts.MoveDelay); connection.SendMessage(serializer.Serialize()); Console.WriteLine("I have waited for: " + (int)connection.GameState.ActionCosts.MoveDelay); }); delay.Start(); }
private TaskField[] GetSurroundingFields(Messages.GameMaster.IConnection connection, IPlayer player) { var gameState = connection.GameState; var location = player.Location; var taskFields = new List <TaskField>(); for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (location.x + i < 0 || location.x + i >= gameState.BoardWidth || location.y + j < 0 || location.y + j >= (gameState.TaskAreaLength + gameState.GoalAreaLength * 2)) { continue; } if (gameState.BoardList[(int)location.x + i][(int)location.y + j] is Xsd2.TaskField) { //Console.WriteLine(" my location x is: " + location.x + " y is: " + location.y); taskFields.Add(gameState.BoardList[(int)location.x + i][(int)location.y + j] as TaskField); } } } return(taskFields?.ToArray()); }
public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { var sender = connection.GameState.GetPlayerByGuid(this.playerGuid); if (connection.GameState.GetPlayerById(this.withPlayerId) == null) { RejectKnowledgeExchange msg = new RejectKnowledgeExchange(this.withPlayerId, sender.Id, true); var serializer = new Messages.XmlHandling.Serializer(msg); connection.SendMessage(serializer.Serialize()); } else { Tuple <ulong, ulong> pair = new Tuple <ulong, ulong>(sender.Id, this.withPlayerIdField); if (connection.GameState.PlayerPairs.FirstOrDefault(x => x.Item1 == sender.Id && x.Item2 == this.withPlayerId) == null) { //no pair yet KnowledgeExchangeRequest msg = new KnowledgeExchangeRequest(this.withPlayerId, sender.Id); var serializer = new Messages.XmlHandling.Serializer(msg); connection.GameState.PlayerPairs.Add(pair); connection.DelayMessage(serializer, connection.GameState.ActionCosts.KnowledgeExchangeDelay); } else { // pair exists connection.GameState.PlayerPairs.RemoveAll(x => x.Item1 == sender.Id && x.Item2 == this.withPlayerId); AcceptExchangeRequest msg = new AcceptExchangeRequest(this.withPlayerId, sender.Id); var serializer = new Messages.XmlHandling.Serializer(msg); connection.SendMessage(serializer.Serialize()); } } }
public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { var msg = new Data(); IPlayer player = connection.GameState.BluePlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); if (player == null) { player = connection.GameState.RedPlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); } msg.playerId = player.Id; Piece[] checkedPiece = new Piece[1]; foreach (PieceInfo p in connection.GameState.Pieces) { if (player.Id == p.Piece.playerId) { checkedPiece[0] = new Piece() { type = p.Piece.type, playerId = player.Id, playerIdSpecified = true, id = p.Piece.id }; msg.Pieces = checkedPiece; } } msg.gameFinished = connection.GameState.GameFinished; var serializer = new Messages.XmlHandling.Serializer(msg); connection.DelayMessage(serializer, connection.GameState.ActionCosts.TestDelay); }
public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { var guid = Guid.NewGuid(); var gameState = connection.GameState; var msg = new ConfirmJoiningGame(gameState.GameId, guid, this.playerId, new Player(this.playerId, this.preferredTeam, this.preferredRole)); connection.AddPlayer(guid, msg.playerId, this.preferredTeam, this.preferredRole); var serializer = new Messages.XmlHandling.Serializer(msg); connection.SendMessage(serializer.Serialize()); if (((ulong)gameState.RedPlayers.Count == gameState.RedTeamMax) && ((ulong)gameState.BluePlayers.Count == gameState.BlueTeamMax)) { gameState.GameStarted = true; var gameMsg = new Game(); gameMsg.Players = new Player[gameState.RedTeamMax + gameState.BlueTeamMax]; int index = 0; for (int i = 0; i < (int)gameState.RedTeamMax; i++, index++) { var p = gameState.RedPlayers[i]; gameMsg.Players[index] = new Player(p.Id, p.TeamColour, p.PlayerType); } for (int i = 0; i < (int)gameState.BlueTeamMax; i++, index++) { var p = gameState.BluePlayers[i]; gameMsg.Players[index] = new Player(p.Id, p.TeamColour, p.PlayerType); } connection.GameState.SetPlayerLocations(); Console.WriteLine("Sending game msg"); gameMsg.Board = new GameBoard(); gameMsg.Board.goalsHeight = (uint)connection.GameState.GoalAreaLength; gameMsg.Board.tasksHeight = (uint)connection.GameState.TaskAreaLength; gameMsg.Board.width = (uint)connection.GameState.BoardWidth; foreach (var player in connection.GameState.RedPlayers) { gameMsg.playerId = player.Id; gameMsg.PlayerLocation = player.Location; serializer = new Serializer(gameMsg); connection.SendMessage(serializer.Serialize()); } foreach (var player in connection.GameState.BluePlayers) { gameMsg.playerId = player.Id; gameMsg.PlayerLocation = player.Location; serializer = new Serializer(gameMsg); connection.SendMessage(serializer.Serialize()); } var gameStartedMsg = new GameStarted(connection.GameState.GameId); serializer = new Serializer(gameStartedMsg); connection.SendMessage(serializer.Serialize()); connection.GameState.StartSpawningPieces(); } }
private bool CheckIfEnding(Messages.GameMaster.IConnection connection) { //Console.WriteLine("i red goal values : " + connection.GameState.RedGoalsFinished + " blue goals finished " + connection.GameState.BlueGoalsFinished); //Console.WriteLine("i red goal TO BE DONE : " + connection.GameState.RedNumberOfGoals + " blue goals TO BE DONE " + connection.GameState.BlueNumberOfGoals); if (connection.GameState.RedGoalsFinished == connection.GameState.RedNumberOfGoals) { return(true); } if (connection.GameState.BlueGoalsFinished == connection.GameState.BlueNumberOfGoals) { return(true); } return(false); }
public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { var msg = new Data(); IPlayer player = connection.GameState.BluePlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); if (player == null) { player = connection.GameState.RedPlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); } msg.TaskFields = GetSurroundingFields(connection, player); msg.playerId = player.Id; msg.gameFinished = connection.GameState.GameFinished; var serializer = new Messages.XmlHandling.Serializer(msg); connection.DelayMessage(serializer, connection.GameState.ActionCosts.DiscoverDelay); }
private Location Validate(Messages.GameMaster.IConnection connection, IPlayer player) { var gameState = connection.GameState; var newLocation = GetNewPlayerLocation(player); if (player.TeamColour == TeamColour.red) { if (newLocation.y >= (gameState.GoalAreaLength + gameState.TaskAreaLength) || newLocation.y < 0) { return(player.Location); } } if (player.TeamColour == TeamColour.blue) { if (newLocation.y >= (gameState.GoalAreaLength * 2 + gameState.TaskAreaLength) || newLocation.y < (gameState.GoalAreaLength)) { return(player.Location); } } if (newLocation.x >= gameState.BoardWidth || newLocation.x < 0) { return(player.Location); } foreach (var p in gameState.RedPlayers) { if (p.Location.x == newLocation.x && p.Location.y == newLocation.y && p.Id != player.Id) { return(player.Location); } } foreach (var p in gameState.BluePlayers) { if (p.Location.x == newLocation.x && p.Location.y == newLocation.y && p.Id != player.Id) { return(player.Location); } } return(newLocation); }
public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { try { Console.WriteLine("Player id #{0} disconnected", this.playerId); connection.GameState.RemovePlayer(this.playerId); if (connection.GameState.GameStarted == true && connection.GameState.RedPlayers.Count == 0 && connection.GameState.BluePlayers.Count == 0) { connection.Disconnect(); Console.WriteLine("All players disconnected"); } } catch (ArgumentNullException) { Console.WriteLine("Invalid msg: {0}", this); } Thread.Sleep(2000); }
public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { connection.Disconnect(); }
public void HandleOnGameMaster(Messages.GameMaster.IConnection connection) { var msg = new Data(); IPlayer player = connection.GameState.BluePlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); if (player == null) { player = connection.GameState.RedPlayers.FirstOrDefault(p => p.Guid.ToString() == this.playerGuid); } msg.playerId = player.Id; Piece[] placedPiece = new Piece[1]; GoalField[] goalFields = new GoalField[1]; //we pick piece which shares the location with player. PieceInfo myPiece = connection.GameState.Pieces.First(p => p.Location.x == player.Location.x && p.Location.y == player.Location.y && p.Piece.playerId == player.Id && p.Piece.playerIdSpecified == true); if (myPiece.Piece.type != PieceType.sham) { //if piece is on place we are standing, foreach (PieceInfo p in connection.GameState.Pieces) { if (player.Location.x == p.Location.x && player.Location.y == p.Location.y) { if (player.Id != p.Piece.playerId) { placedPiece[0] = new Piece() { type = myPiece.Piece.type, playerId = player.Id, playerIdSpecified = true, id = myPiece.Piece.id }; } } } // this is overriding the lines before, we need to think through this process. placedPiece[0] = new Piece() { type = myPiece.Piece.type, id = myPiece.Piece.id }; foreach (List <Xsd2.Field> flist in connection.GameState.BoardList) { foreach (var f in flist) { if (f is GoalField) { if (f.x == player.Location.x && f.y == player.Location.y) { goalFields[0] = new GoalField() { playerId = player.Id, type = ((GoalField)f).type, playerIdSpecified = true, x = f.x, y = f.y }; Console.WriteLine(((GoalField)f).type); Console.WriteLine("f.x: " + f.x + ", f.y: " + f.y); Console.WriteLine(" x " + player.Location.x + " y " + player.Location.y); //testing whether piece is not sham. if (((GoalField)f).type == GoalFieldType.goal && myPiece.Piece.type == PieceType.normal) { if (player.TeamColour == TeamColour.red) { connection.GameState.RedGoalsFinished++; } else { connection.GameState.BlueGoalsFinished++; } } } } } } // why do we send here piece? msg.Pieces = placedPiece; // we are still or not holding it. msg.GoalFields = goalFields; if (connection.GameState.GameFinished != true) { connection.GameState.GameFinished = CheckIfEnding(connection); } //edit msg.gameFinished = connection.GameState.GameFinished; var serializer = new Messages.XmlHandling.Serializer(msg); connection.DelayMessage(serializer, connection.GameState.ActionCosts.PlacingDelay); } else { connection.GameState.Pieces.First(p => p.Location.x == player.Location.x && p.Location.y == player.Location.y && p.Piece.playerId == player.Id && p.Piece.playerIdSpecified == true).Piece.playerIdSpecified = false; //we are sending the keep alive back var serializer = new Messages.XmlHandling.Serializer(char.ToString((char)23)); } }