public void GetBoardDim() { Packet boardRequest = new Packet(Id, gameMasterId, RequestType.Send); boardRequest.AddArgument(ServerConstants.ArgumentNames.GameBoardSize, null); SendPacket(boardRequest); }
/// <summary> /// return a pair of the new position, /// the method should test every possible position whether it is ocupied by other player or not, by asking the server about it /// </summary> /// <returns></returns> public void Move() { int[] dx = { -1, 0, 1, 0 }; int[] dy = { 0, 1, 0, -1 }; Random r = new Random(); while (true) { int idx = r.Next(4); int newX = location.Item1 + dx[idx]; int newY = location.Item2 + dy[idx]; if (newX >= 0 && newX < Board.Width && newY >= 0 && newY < Board .Height) { Tuple <int, int> testLocation = new Tuple <int, int>(newX, newY); Packet toSend = new Packet(Id, gameMasterId, RequestType.Send); toSend.AddArgument(ServerConstants.ArgumentNames.CheckMove, testLocation); SendPacket(toSend); Thread.Sleep(900); } } }
public override void HandleReceivePacket(Packet receivedPacket) { switch (receivedPacket.RequestType) { case RequestType.Register: SetId(int.Parse(receivedPacket.Arguments[ServerConstants.ArgumentNames.Id])); Console.WriteLine($"Player ID set to {Id}"); var connetToGame = new Packet(Id, -1, RequestType.ConnectToGame); connetToGame.AddArgument(ServerConstants.ArgumentNames.SenderType, ClientType.Agent); SendPacket(connetToGame); break; case RequestType.Send: if (receivedPacket.Arguments.ContainsKey("TeamLeaderId") && receivedPacket.SenderId == gameMasterId) { teamLeaderId = (int)receivedPacket.Arguments["TeamLeaderId"]; Console.WriteLine($"Player: {Id} received Team Leader's id: {teamLeaderId} "); } else if (receivedPacket.Arguments.ContainsKey("GameBoardSize") && receivedPacket.SenderId == gameMasterId) { var k = receivedPacket.Arguments[ServerConstants.ArgumentNames.GameBoardSize]; Board = new GameBoard((int)k.Width, (int)k.Height, (int)k.GoalAreaHeight); Console.WriteLine($"game board of size {k.Width}x{k.Height}x{k.GoalAreaHeight} added to {GetId()}"); } else if (receivedPacket.Arguments.ContainsKey("CheckMove") && receivedPacket.SenderId == gameMasterId) { JObject firstCoordinate = receivedPacket.Arguments.Values.First(); if (firstCoordinate != null) { Int32.TryParse(((JValue)firstCoordinate.First.Last).Value.ToString(), out int frist); Int32.TryParse(((JValue)firstCoordinate.Last.Last).Value.ToString(), out int second); location = new Tuple <int, int>(frist, second); } } break; case RequestType.ConnectToGame: gameMasterId = (int)receivedPacket.Arguments[ServerConstants.ArgumentNames.GameMasterId]; Console.WriteLine($"Player: {Id} received Game Master's id: {gameMasterId} "); break; default: Console.WriteLine("Player received packet of unknown type, do nothing"); break; } }