Skip to content

Commit

Permalink
Added score sync state packet and lowered the frequency of sync state…
Browse files Browse the repository at this point in the history
… packets
  • Loading branch information
cubicgraphics committed May 14, 2024
1 parent 3628d6b commit e251152
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public interface IPlayerRegistry
bool TryGetPlayer(EndPoint remoteEndPoint, [MaybeNullWhen(false)] out IPlayer player);
bool TryGetPlayer(byte connectionId, [MaybeNullWhen(false)] out IPlayer player);
bool TryGetPlayer(string userId, [MaybeNullWhen(false)] out IPlayer player);
int GetMillisBetweenSyncStatePackets();
long GetMillisBetweenPoseSyncStateDeltaPackets();
long GetMillisBetweenScoreSyncStateDeltaPackets();

public void AddExtraPlayerSessionData(Core.Abstractions.IPlayer playerSessionData);
public bool RemoveExtraPlayerSessionDataAndApply(Core.Abstractions.IPlayer playerSessionData/*out string ClientVersion, out byte Platform, out string PlayerPlatformUserId*/);
Expand Down
33 changes: 25 additions & 8 deletions BeatTogether.DedicatedServer.Kernel/DedicatedInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,14 @@ public override void OnConnect(EndPoint endPoint)
},
new MpNodePoseSyncStatePacket
{
fullStateUpdateFrequency = 100L,
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenSyncStatePackets()
fullStateUpdateFrequency = Math.Max(_playerRegistry.GetMillisBetweenPoseSyncStateDeltaPackets(), 100L),
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenPoseSyncStateDeltaPackets()
},
new MpScoreSyncStatePacket
{
fullStateUpdateFrequency = Math.Max(_playerRegistry.GetMillisBetweenScoreSyncStateDeltaPackets(), 500L),
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenScoreSyncStateDeltaPackets()
}
}, IgnoranceChannelTypes.Reliable);

//Send server infomation to player
Expand Down Expand Up @@ -417,9 +422,14 @@ public override void OnConnect(EndPoint endPoint)
},
new MpNodePoseSyncStatePacket
{
fullStateUpdateFrequency = 100L,
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenSyncStatePackets()
fullStateUpdateFrequency = Math.Max(_playerRegistry.GetMillisBetweenPoseSyncStateDeltaPackets(), 100L),
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenPoseSyncStateDeltaPackets()
},
new MpScoreSyncStatePacket
{
fullStateUpdateFrequency = Math.Max(_playerRegistry.GetMillisBetweenScoreSyncStateDeltaPackets(), 500L),
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenScoreSyncStateDeltaPackets()
}
};

PacketDispatcher.SendToPlayer(player, Player_ConnectPacket, IgnoranceChannelTypes.Reliable);
Expand Down Expand Up @@ -572,10 +582,17 @@ public override void OnDisconnect(EndPoint endPoint)
// Update permissions
SetNewServerOwner(_playerRegistry.Players[0]);
}
PacketDispatcher.SendToNearbyPlayers(new MpNodePoseSyncStatePacket
{
fullStateUpdateFrequency = 100L,
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenSyncStatePackets()
PacketDispatcher.SendToNearbyPlayers(new INetSerializable[] {
new MpNodePoseSyncStatePacket
{
fullStateUpdateFrequency = Math.Max(_playerRegistry.GetMillisBetweenPoseSyncStateDeltaPackets(), 100L),
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenPoseSyncStateDeltaPackets()
},
new MpScoreSyncStatePacket
{
fullStateUpdateFrequency = Math.Max(_playerRegistry.GetMillisBetweenScoreSyncStateDeltaPackets(), 500L),
deltaUpdateFrequency = _playerRegistry.GetMillisBetweenScoreSyncStateDeltaPackets()
}
}, IgnoranceChannelTypes.Reliable);
}
else
Expand Down
20 changes: 15 additions & 5 deletions BeatTogether.DedicatedServer.Kernel/PlayerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public bool AddPlayer(IPlayer player)
_playersByRemoteEndPoint.TryAdd(player.Endpoint, player);
_playersByConnectionId.TryAdd(player.ConnectionId, player);
_PlayerCount++;
MillisBetweenSyncStatePackets = (int)(0.84 * _PlayerCount + 15.789);
MillisBetweenPoseSyncStatePackets = (long)(0.94 * _PlayerCount + 15);
MillisBetweenScoreSyncStatePackets = (long)(1.5 * _PlayerCount + 20);
return true;
}
}
Expand All @@ -92,7 +93,8 @@ public void RemovePlayer(IPlayer player)
_playersByRemoteEndPoint.Remove(player.Endpoint, out _);
_playersByConnectionId.Remove(player.ConnectionId, out _);
_PlayerCount--;
MillisBetweenSyncStatePackets = (int)(0.84 * _PlayerCount + 15.789);
MillisBetweenPoseSyncStatePackets = (long)(0.94 * _PlayerCount + 15);
MillisBetweenScoreSyncStatePackets = (long)(1.5 * _PlayerCount + 20);
}
}
}
Expand All @@ -119,10 +121,18 @@ public bool TryGetPlayer(string userId, [MaybeNullWhen(false)] out IPlayer playe
}
}

private int MillisBetweenSyncStatePackets = 0;
public int GetMillisBetweenSyncStatePackets()


private long MillisBetweenPoseSyncStatePackets = 0;
public long GetMillisBetweenPoseSyncStateDeltaPackets()
{
return MillisBetweenPoseSyncStatePackets;
}

private long MillisBetweenScoreSyncStatePackets = 0;
public long GetMillisBetweenScoreSyncStateDeltaPackets()
{
return MillisBetweenSyncStatePackets;
return MillisBetweenScoreSyncStatePackets;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using BeatTogether.Extensions;
using BeatTogether.DedicatedServer.Messaging.Abstractions;
using BeatTogether.DedicatedServer.Messaging.Util;

namespace BeatTogether.DedicatedServer.Messaging.Packets.MultiplayerSession.MpCorePackets
{
public sealed class MpScoreSyncStatePacket : INetSerializable
{
public long deltaUpdateFrequency;
public long fullStateUpdateFrequency;

public void WriteTo(ref SpanBuffer bufferWriter)
{
bufferWriter.WriteVarLong(deltaUpdateFrequency);
bufferWriter.WriteVarLong(fullStateUpdateFrequency);
}
public void ReadFrom(ref SpanBuffer bufferReader)
{
deltaUpdateFrequency = bufferReader.ReadVarLong();
fullStateUpdateFrequency = bufferReader.ReadVarLong();
}
}

}

0 comments on commit e251152

Please sign in to comment.