/// <summary> /// Fill an array with null value /// </summary> /// <param name="array">The array to fill</param> public static void FillArrayWithNull(ref Player[] array) { for (int i = 0; i < array.Length; i++) { array[i] = null; } }
/// <summary> /// Get a client from his TcpClient /// </summary> /// <param name="client">TcpClient</param> /// <param name="array">Player list</param> /// <returns></returns> public static Player GetPlayerFromTcpClient(TcpClient client, Player[] array) { foreach (Player p in array) { if (p.TcpClient == client) return p; } return null; }
/// <summary> /// Get the first available slot in array /// </summary> /// <param name="array">Player list</param> /// <returns></returns> public static int FirstIndexAvailable(Player[] array) { for (int i = 0; i < array.Length; i++) { if (array[i] == null) return i; } return -1; }
/// <summary> /// Check if the array contains a TcpClient /// </summary> /// <param name="client">TcpClient to check</param> /// <param name="array">Player list</param> /// <returns></returns> public static bool ArrayContains(TcpClient client, Player[] array) { foreach(Player p in array) { if (p.TcpClient == client) return true; } return false; }
/// <summary> /// When client disconnect /// </summary> /// <param name="tcpClient">TcpClient</param> private void tcpServer_OnPlayerDisconnect(TcpClient tcpClient) { Log(Config.Lang.GetString("playerDisconnected") + tcpClient.Client.RemoteEndPoint); Player tempPlayer = new Player(tcpClient); players[tempPlayer.Slot] = null; }
/// <summary> /// When client connect /// </summary> /// <param name="tcpClient">TcpClient</param> private void tcpServer_OnPlayerConnect(TcpClient tcpClient) { Log(Config.Lang.GetString("playerConnected") + tcpClient.Client.RemoteEndPoint); Player tempPlayer = new Player(tcpClient); if (ListHelper.FirstIndexAvailable(players) != -1) { tempPlayer.Slot = (Byte)ListHelper.FirstIndexAvailable(players); players[tempPlayer.Slot] = tempPlayer; } else { string message = Config.Lang.GetString("serverFull"); byte[] messageArray = new byte[Encoding.UTF8.GetBytes(message).Length + 3]; using (MemoryStream ms = new MemoryStream(messageArray)) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write(message); } } tempPlayer.TcpClient.Send(new Packet(2, messageArray).ToByteArray()); } }