Cached what room each player is in, removing need for a loop

This commit is contained in:
Derek S 2021-04-09 02:04:51 -05:00
parent bf39a95962
commit 3c6b314c0c
4 changed files with 12 additions and 24 deletions

View file

@ -23,12 +23,10 @@ namespace LightReflectiveMirror
/// <param name="sendTo">Who to relay the data to</param>
void ProcessData(int clientId, byte[] clientData, int channel, int sendTo = -1)
{
Room playersRoom = GetRoomForPlayer(clientId);
Room room = _cachedClientRooms[clientId];
if(playersRoom != null)
if(room != null)
{
Room room = playersRoom;
if(room.hostId == clientId)
{
if (room.clients.Contains(sendTo))

View file

@ -71,7 +71,7 @@ namespace LightReflectiveMirror
ProcessData(clientId, data.ReadBytes(ref pos), channel, data.ReadInt(ref pos));
break;
case OpCodes.UpdateRoomData:
var plyRoom = GetRoomForPlayer(clientId);
var plyRoom = _cachedClientRooms[clientId];
if (plyRoom == null)
return;

View file

@ -7,24 +7,6 @@ namespace LightReflectiveMirror
{
public partial class RelayHandler
{
/// <summary>
/// Returns the current room the client is in, null if client is not in a room.
/// </summary>
/// <param name="clientId">The client we are getting the room for</param>
/// <returns></returns>
Room GetRoomForPlayer(int clientId)
{
for (int i = 0; i < rooms.Count; i++)
{
if (rooms[i].hostId == clientId)
return rooms[i];
if (rooms[i].clients.Contains(clientId))
return rooms[i];
}
return null;
}
/// <summary>
/// Attempts to join a room for a client.
@ -44,6 +26,7 @@ namespace LightReflectiveMirror
if (rooms[i].clients.Count < rooms[i].maxPlayers)
{
rooms[i].clients.Add(clientId);
_cachedClientRooms.Add(clientId, rooms[i]);
int sendJoinPos = 0;
byte[] sendJoinBuffer = _sendBuffers.Rent(500);
@ -143,6 +126,7 @@ namespace LightReflectiveMirror
};
rooms.Add(room);
_cachedClientRooms.Add(clientId, room);
int pos = 0;
byte[] sendBuffer = _sendBuffers.Rent(5);
@ -172,17 +156,21 @@ namespace LightReflectiveMirror
sendBuffer.WriteByte(ref pos, (byte)OpCodes.ServerLeft);
for (int x = 0; x < rooms[i].clients.Count; x++)
{
Program.transport.ServerSend(rooms[i].clients[x], 0, new ArraySegment<byte>(sendBuffer, 0, pos));
_cachedClientRooms.Remove(rooms[i].clients[x]);
}
_sendBuffers.Return(sendBuffer);
rooms[i].clients.Clear();
rooms.RemoveAt(i);
_cachedClientRooms.Remove(clientId);
Endpoint.RoomsModified();
return;
}
else
{
if (requiredHostId >= 0 && rooms[i].hostId != requiredHostId)
if (requiredHostId != -1 && rooms[i].hostId != requiredHostId)
continue;
if (rooms[i].clients.RemoveAll(x => x == clientId) > 0)
@ -196,6 +184,7 @@ namespace LightReflectiveMirror
Program.transport.ServerSend(rooms[i].hostId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
_sendBuffers.Return(sendBuffer);
Endpoint.RoomsModified();
_cachedClientRooms.Remove(clientId);
}
}
}

View file

@ -9,5 +9,6 @@ namespace LightReflectiveMirror
private List<int> _pendingAuthentication = new List<int>();
private ArrayPool<byte> _sendBuffers;
private int _maxPacketSize = 0;
private Dictionary<int, Room> _cachedClientRooms = new Dictionary<int, Room>();
}
}