Merge pull request #29 from Makinolo/main

Adds connection and disconnection callbacks for all players
This commit is contained in:
Nick-I. A
2021-11-20 03:20:28 +01:00
committed by GitHub
2 changed files with 28 additions and 3 deletions

View File

@ -7,7 +7,7 @@ namespace CoopClient
public static class Interface public static class Interface
{ {
#region DELEGATES #region DELEGATES
public delegate void ConnectEvent(bool connected, string reason = null); public delegate void ConnectEvent(bool connected, long fromId, string reason = null);
public delegate void ChatMessage(string from, string message, CancelEventArgs args); public delegate void ChatMessage(string from, string message, CancelEventArgs args);
public delegate void ModEvent(long from, string mod, byte customID, byte[] bytes); public delegate void ModEvent(long from, string mod, byte customID, byte[] bytes);
#endregion #endregion
@ -19,12 +19,22 @@ namespace CoopClient
internal static void Connected() internal static void Connected()
{ {
OnConnection?.Invoke(true); OnConnection?.Invoke(true, GetLocalID());
} }
internal static void Disconnected(string reason) internal static void Disconnected(string reason)
{ {
OnConnection?.Invoke(false, reason); OnConnection?.Invoke(false, GetLocalID(), reason);
}
internal static void Connected(long userId)
{
OnConnection?.Invoke(true, userId);
}
internal static void Disconnected(long userId)
{
OnConnection?.Invoke(false, userId);
} }
internal static void ModPacketReceived(long from, string mod, byte customID, byte[] bytes) internal static void ModPacketReceived(long from, string mod, byte customID, byte[] bytes)
@ -78,6 +88,14 @@ namespace CoopClient
return result; return result;
} }
public static Entities.EntitiesPlayer GetPlayer( long playerId )
{
if (Main.Players.ContainsKey(playerId))
return Main.Players[playerId];
else
return null;
}
public static bool IsMenuVisible() public static bool IsMenuVisible()
{ {
#if NON_INTERACTIVE #if NON_INTERACTIVE

View File

@ -46,6 +46,11 @@ namespace CoopClient
string[] ip = address.Split(':'); string[] ip = address.Split(':');
if(ip.Length != 2)
{
throw new Exception("Malformed URL");
}
// Send HandshakePacket // Send HandshakePacket
NetOutgoingMessage outgoingMessage = Client.CreateMessage(); NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new HandshakePacket() new HandshakePacket()
@ -260,6 +265,7 @@ namespace CoopClient
}; };
Main.Players.Add(packet.ID, player); Main.Players.Add(packet.ID, player);
Interface.Connected(packet.ID);
} }
private void PlayerDisconnect(PlayerDisconnectPacket packet) private void PlayerDisconnect(PlayerDisconnectPacket packet)
@ -275,6 +281,7 @@ namespace CoopClient
player.PedBlip?.Delete(); player.PedBlip?.Delete();
Interface.Disconnected(packet.ID);
Main.Players.Remove(packet.ID); Main.Players.Remove(packet.ID);
} }
} }