Faster way to get client by username
This commit is contained in:
@ -192,7 +192,8 @@ namespace RageCoop.Server.Scripting
|
|||||||
/// <returns>The Client from this user or null</returns>
|
/// <returns>The Client from this user or null</returns>
|
||||||
public Client GetClientByUsername(string username)
|
public Client GetClientByUsername(string username)
|
||||||
{
|
{
|
||||||
return Server.Clients.Values.FirstOrDefault(x => x.Username.ToLower() == username.ToLower());
|
Server.ClientsByName.TryGetValue(username, out Client c);
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -212,7 +213,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
targets ??= new(Server.Clients.Values);
|
targets ??= new(Server.ClientsByNetHandle.Values);
|
||||||
foreach(Client client in targets)
|
foreach(Client client in targets)
|
||||||
{
|
{
|
||||||
Server.SendChatMessage(username, message, client);
|
Server.SendChatMessage(username, message, client);
|
||||||
@ -317,7 +318,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
public void SendCustomEvent(List<Client> targets, int eventHash, params object[] args)
|
public void SendCustomEvent(List<Client> targets, int eventHash, params object[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
targets ??= new(Server.Clients.Values);
|
targets ??= new(Server.ClientsByNetHandle.Values);
|
||||||
var p = new Packets.CustomEvent()
|
var p = new Packets.CustomEvent()
|
||||||
{
|
{
|
||||||
Args=args,
|
Args=args,
|
||||||
@ -338,7 +339,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
public void SendCustomEventQueued(List<Client> targets, int eventHash, params object[] args)
|
public void SendCustomEventQueued(List<Client> targets, int eventHash, params object[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
targets ??= new(Server.Clients.Values);
|
targets ??= new(Server.ClientsByNetHandle.Values);
|
||||||
var p = new Packets.CustomEvent(null,true)
|
var p = new Packets.CustomEvent(null,true)
|
||||||
{
|
{
|
||||||
Args=args,
|
Args=args,
|
||||||
|
@ -44,7 +44,7 @@ namespace RageCoop.Server
|
|||||||
internal ServerEntities Entities;
|
internal ServerEntities Entities;
|
||||||
|
|
||||||
internal readonly Dictionary<Command, Action<CommandContext>> Commands = new();
|
internal readonly Dictionary<Command, Action<CommandContext>> Commands = new();
|
||||||
internal readonly Dictionary<long,Client> Clients = new();
|
internal readonly Dictionary<long,Client> ClientsByNetHandle = new();
|
||||||
internal readonly Dictionary<string, Client> ClientsByName = new();
|
internal readonly Dictionary<string, Client> ClientsByName = new();
|
||||||
internal Client _hostClient;
|
internal Client _hostClient;
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ namespace RageCoop.Server
|
|||||||
{
|
{
|
||||||
while (!_stopping)
|
while (!_stopping)
|
||||||
{
|
{
|
||||||
foreach(var c in Clients.Values.ToArray())
|
foreach(var c in ClientsByNetHandle.Values.ToArray())
|
||||||
{
|
{
|
||||||
c.UpdateLatency();
|
c.UpdateLatency();
|
||||||
}
|
}
|
||||||
@ -316,7 +316,7 @@ namespace RageCoop.Server
|
|||||||
case NetIncomingMessageType.StatusChanged:
|
case NetIncomingMessageType.StatusChanged:
|
||||||
{
|
{
|
||||||
// Get sender client
|
// Get sender client
|
||||||
if (!Clients.TryGetValue(message.SenderConnection.RemoteUniqueIdentifier, out sender))
|
if (!ClientsByNetHandle.TryGetValue(message.SenderConnection.RemoteUniqueIdentifier, out sender))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -339,7 +339,7 @@ namespace RageCoop.Server
|
|||||||
{
|
{
|
||||||
|
|
||||||
// Get sender client
|
// Get sender client
|
||||||
if (Clients.TryGetValue(message.SenderConnection.RemoteUniqueIdentifier, out sender))
|
if (ClientsByNetHandle.TryGetValue(message.SenderConnection.RemoteUniqueIdentifier, out sender))
|
||||||
{
|
{
|
||||||
// Get packet type
|
// Get packet type
|
||||||
var type = (PacketType)message.ReadByte();
|
var type = (PacketType)message.ReadByte();
|
||||||
@ -531,7 +531,7 @@ namespace RageCoop.Server
|
|||||||
connection.Deny("Username contains special chars!");
|
connection.Deny("Username contains special chars!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Clients.Values.Any(x => x.Username.ToLower() == packet.Username.ToLower()))
|
if (ClientsByNetHandle.Values.Any(x => x.Username.ToLower() == packet.Username.ToLower()))
|
||||||
{
|
{
|
||||||
connection.Deny("Username is already taken!");
|
connection.Deny("Username is already taken!");
|
||||||
return;
|
return;
|
||||||
@ -566,14 +566,14 @@ namespace RageCoop.Server
|
|||||||
Client tmpClient;
|
Client tmpClient;
|
||||||
|
|
||||||
// Add the player to Players
|
// Add the player to Players
|
||||||
lock (Clients)
|
lock (ClientsByNetHandle)
|
||||||
{
|
{
|
||||||
var player = new ServerPed(this)
|
var player = new ServerPed(this)
|
||||||
{
|
{
|
||||||
ID= packet.PedID,
|
ID= packet.PedID,
|
||||||
};
|
};
|
||||||
Entities.Add(player);
|
Entities.Add(player);
|
||||||
Clients.Add(connection.RemoteUniqueIdentifier,
|
ClientsByNetHandle.Add(connection.RemoteUniqueIdentifier,
|
||||||
tmpClient = new Client(this)
|
tmpClient = new Client(this)
|
||||||
{
|
{
|
||||||
NetID = connection.RemoteUniqueIdentifier,
|
NetID = connection.RemoteUniqueIdentifier,
|
||||||
@ -582,8 +582,8 @@ namespace RageCoop.Server
|
|||||||
Player = player
|
Player = player
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
ClientsByName.Add(packet.Username, tmpClient);
|
ClientsByName.Add(packet.Username.ToLower(), tmpClient);
|
||||||
if (Clients.Count==1) {
|
if (ClientsByNetHandle.Count==1) {
|
||||||
_hostClient=tmpClient;
|
_hostClient=tmpClient;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -601,7 +601,7 @@ namespace RageCoop.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send other players to this client
|
// Send other players to this client
|
||||||
Clients.Values.ForEach(target =>
|
ClientsByNetHandle.Values.ForEach(target =>
|
||||||
{
|
{
|
||||||
if (target==newClient) { return; }
|
if (target==newClient) { return; }
|
||||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||||
@ -661,12 +661,12 @@ namespace RageCoop.Server
|
|||||||
Entities.CleanUp(localClient);
|
Entities.CleanUp(localClient);
|
||||||
_worker.QueueJob(() => API.Events.InvokePlayerDisconnected(localClient));
|
_worker.QueueJob(() => API.Events.InvokePlayerDisconnected(localClient));
|
||||||
Logger?.Info($"Player {localClient.Username} disconnected! ID:{localClient.Player.ID}");
|
Logger?.Info($"Player {localClient.Username} disconnected! ID:{localClient.Player.ID}");
|
||||||
Clients.Remove(localClient.NetID);
|
ClientsByNetHandle.Remove(localClient.NetID);
|
||||||
ClientsByName.Remove(localClient.Username);
|
ClientsByName.Remove(localClient.Username.ToLower());
|
||||||
if (localClient==_hostClient)
|
if (localClient==_hostClient)
|
||||||
{
|
{
|
||||||
|
|
||||||
_hostClient = Clients.Values.FirstOrDefault();
|
_hostClient = ClientsByNetHandle.Values.FirstOrDefault();
|
||||||
_hostClient?.SendCustomEvent(CustomEvents.IsHost, true);
|
_hostClient?.SendCustomEvent(CustomEvents.IsHost, true);
|
||||||
}
|
}
|
||||||
Security.RemoveConnection(localClient.Connection.RemoteEndPoint);
|
Security.RemoveConnection(localClient.Connection.RemoteEndPoint);
|
||||||
@ -685,7 +685,7 @@ namespace RageCoop.Server
|
|||||||
_worker.QueueJob(() => API.Events.InvokePlayerUpdate(client));
|
_worker.QueueJob(() => API.Events.InvokePlayerUpdate(client));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var c in Clients.Values)
|
foreach (var c in ClientsByNetHandle.Values)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Don't send data back
|
// Don't send data back
|
||||||
@ -713,7 +713,7 @@ namespace RageCoop.Server
|
|||||||
{
|
{
|
||||||
_worker.QueueJob(() => Entities.Update(packet, client));
|
_worker.QueueJob(() => Entities.Update(packet, client));
|
||||||
bool isPlayer = packet.ID==client.Player?.LastVehicle?.ID;
|
bool isPlayer = packet.ID==client.Player?.LastVehicle?.ID;
|
||||||
foreach (var c in Clients.Values)
|
foreach (var c in ClientsByNetHandle.Values)
|
||||||
{
|
{
|
||||||
if (c.NetID==client.NetID) { continue; }
|
if (c.NetID==client.NetID) { continue; }
|
||||||
if (isPlayer)
|
if (isPlayer)
|
||||||
@ -737,7 +737,7 @@ namespace RageCoop.Server
|
|||||||
private void ProjectileSync(Packets.ProjectileSync packet, Client client)
|
private void ProjectileSync(Packets.ProjectileSync packet, Client client)
|
||||||
{
|
{
|
||||||
|
|
||||||
foreach (var c in Clients.Values)
|
foreach (var c in ClientsByNetHandle.Values)
|
||||||
{
|
{
|
||||||
if (c.NetID==client.NetID) { continue; }
|
if (c.NetID==client.NetID) { continue; }
|
||||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||||
@ -761,7 +761,7 @@ namespace RageCoop.Server
|
|||||||
|
|
||||||
_worker.QueueJob(() => API.Events.InvokeOnChatMessage(message, sender));
|
_worker.QueueJob(() => API.Events.InvokeOnChatMessage(message, sender));
|
||||||
|
|
||||||
foreach(var c in Clients.Values)
|
foreach(var c in ClientsByNetHandle.Values)
|
||||||
{
|
{
|
||||||
var msg = MainNetServer.CreateMessage();
|
var msg = MainNetServer.CreateMessage();
|
||||||
var crypt = new Func<string, byte[]>((s) =>
|
var crypt = new Func<string, byte[]>((s) =>
|
||||||
|
Reference in New Issue
Block a user