feat: player/room user data now available on joined event
This commit is contained in:
@@ -26,15 +26,8 @@ namespace Ragon.Server.Handler
|
||||
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
|
||||
return;
|
||||
}
|
||||
|
||||
var playerUserData = Reader.ReadBytes(Reader.Capacity);
|
||||
if (playerUserData.Length > _userDataLimit)
|
||||
{
|
||||
_logger.Warn($"Player {context.Connection.Id} exceeded user data limit");
|
||||
return;
|
||||
}
|
||||
|
||||
context.UserData.Data = playerUserData;
|
||||
context.UserData.Read(Reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@ public sealed class RoomCreateOperation : BaseOperation
|
||||
};
|
||||
|
||||
var lobbyPlayer = context.LobbyPlayer;
|
||||
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
|
||||
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
||||
var room = new RagonRoom(roomId, information, roomPlugin);
|
||||
@@ -103,11 +103,23 @@ public sealed class RoomCreateOperation : BaseOperation
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort)room.PlayerMin);
|
||||
writer.WriteUShort((ushort)room.PlayerMax);
|
||||
writer.WriteString(room.Scene);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
|
||||
room.UserData.Snapshot(writer);
|
||||
|
||||
writer.WriteUShort((ushort)room.PlayerList.Count);
|
||||
foreach (var roomPlayer in room.PlayerList)
|
||||
{
|
||||
writer.WriteUShort(roomPlayer.Connection.Id);
|
||||
writer.WriteString(roomPlayer.Id);
|
||||
writer.WriteString(roomPlayer.Name);
|
||||
|
||||
roomPlayer.Context.UserData.Snapshot(writer);
|
||||
}
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
|
||||
@@ -1,45 +1,46 @@
|
||||
using NLog;
|
||||
using Ragon.Protocol;
|
||||
using Ragon.Server.Event;
|
||||
using Ragon.Server.IO;
|
||||
using Ragon.Server.Lobby;
|
||||
|
||||
namespace Ragon.Server.Handler;
|
||||
|
||||
public class RoomEventOperation : BaseOperation
|
||||
{
|
||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public RoomEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Handle(RagonContext context, NetworkChannel channel)
|
||||
{
|
||||
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
|
||||
{
|
||||
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
|
||||
return;
|
||||
}
|
||||
|
||||
var room = context.Room;
|
||||
var player = context.RoomPlayer;
|
||||
|
||||
var eventId = Reader.ReadUShort();
|
||||
var replicationMode = (RagonReplicationMode)Reader.ReadByte();
|
||||
var eventMode = (RagonReplicationMode)Reader.ReadByte();
|
||||
var targetMode = (RagonTarget)Reader.ReadByte();
|
||||
var targetPlayerPeerId = (ushort)0;
|
||||
|
||||
|
||||
if (targetMode == RagonTarget.Player)
|
||||
targetPlayerPeerId = Reader.ReadUShort();
|
||||
|
||||
var @event = new RagonEvent(player, eventId);
|
||||
@event.Read(Reader);
|
||||
|
||||
Writer.Clear();
|
||||
Writer.WriteUShort(eventId);
|
||||
Writer.WriteUShort(player.Connection.Id);
|
||||
Writer.WriteUShort((ushort) replicationMode);
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
|
||||
if (targetMode == RagonTarget.Player && room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
|
||||
{
|
||||
targetPlayer.Connection.Reliable.Send(sendData);
|
||||
room.ReplicateEvent(player, @event, eventMode, targetPlayer);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var roomPlayer in room.ReadyPlayersList)
|
||||
roomPlayer.Connection.Reliable.Send(sendData);
|
||||
room.ReplicateEvent(player, @event, eventMode, targetMode);
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public sealed class RoomJoinOperation : BaseOperation
|
||||
return;
|
||||
}
|
||||
|
||||
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
context.SetRoom(existsRoom, player);
|
||||
|
||||
if (!existsRoom.Plugin.OnPlayerJoined(player))
|
||||
@@ -56,6 +56,8 @@ public sealed class RoomJoinOperation : BaseOperation
|
||||
|
||||
JoinSuccess(context, existsRoom, Writer);
|
||||
|
||||
existsRoom.RestoreBufferedEvents(player);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}");
|
||||
}
|
||||
|
||||
@@ -64,12 +66,24 @@ public sealed class RoomJoinOperation : BaseOperation
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteString(context.RoomPlayer.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort)room.PlayerMin);
|
||||
writer.WriteUShort((ushort)room.PlayerMax);
|
||||
writer.WriteString(room.Scene);
|
||||
|
||||
writer.WriteString(context.RoomPlayer.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
|
||||
room.UserData.Snapshot(writer);
|
||||
|
||||
writer.WriteUShort((ushort)room.PlayerList.Count);
|
||||
foreach (var roomPlayer in room.PlayerList)
|
||||
{
|
||||
writer.WriteUShort(roomPlayer.Connection.Id);
|
||||
writer.WriteString(roomPlayer.Id);
|
||||
writer.WriteString(roomPlayer.Name);
|
||||
|
||||
roomPlayer.Context.UserData.Snapshot(writer);
|
||||
}
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
context.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
||||
|
||||
if (context.Lobby.FindRoomByScene(_roomPayload.Scene, out var existsRoom))
|
||||
{
|
||||
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
|
||||
if (!existsRoom.Plugin.OnPlayerJoined(player))
|
||||
return;
|
||||
@@ -62,6 +62,8 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
||||
_ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
|
||||
|
||||
JoinSuccess(player, existsRoom, Writer);
|
||||
|
||||
existsRoom.RestoreBufferedEvents(player);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -72,7 +74,7 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
||||
Min = _roomPayload.Min,
|
||||
};
|
||||
|
||||
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
|
||||
var room = new RagonRoom(roomId, information, roomPlugin);
|
||||
|
||||
@@ -96,12 +98,24 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteUShort((ushort)room.PlayerMin);
|
||||
writer.WriteUShort((ushort)room.PlayerMax);
|
||||
writer.WriteString(room.Scene);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort) room.PlayerMin);
|
||||
writer.WriteUShort((ushort) room.PlayerMax);
|
||||
writer.WriteString(room.Scene);
|
||||
|
||||
|
||||
room.UserData.Snapshot(writer);
|
||||
|
||||
writer.WriteUShort((ushort)room.PlayerList.Count);
|
||||
foreach (var roomPlayer in room.PlayerList)
|
||||
{
|
||||
writer.WriteUShort(roomPlayer.Connection.Id);
|
||||
writer.WriteString(roomPlayer.Id);
|
||||
writer.WriteString(roomPlayer.Name);
|
||||
|
||||
roomPlayer.Context.UserData.Snapshot(writer);
|
||||
}
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
|
||||
|
||||
@@ -42,16 +42,9 @@ public sealed class RoomUserDataOperation : BaseOperation
|
||||
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
|
||||
return;
|
||||
}
|
||||
|
||||
var roomUserData = Reader.ReadBytes(Reader.Capacity);
|
||||
if (roomUserData.Length > _userDataLimit)
|
||||
{
|
||||
_logger.Warn("Room user data is too big");
|
||||
return;
|
||||
}
|
||||
|
||||
var room = context.Room;
|
||||
if (room != null)
|
||||
room.UserData.Data = roomUserData;
|
||||
room.UserData.Read(Reader);
|
||||
}
|
||||
}
|
||||
@@ -143,13 +143,6 @@ public sealed class SceneLoadedOperation : BaseOperation
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.SNAPSHOT);
|
||||
writer.WriteUShort((ushort)room.ReadyPlayersList.Count);
|
||||
foreach (var roomPlayer in room.ReadyPlayersList)
|
||||
{
|
||||
writer.WriteUShort(roomPlayer.Connection.Id);
|
||||
writer.WriteString(roomPlayer.Id);
|
||||
writer.WriteString(roomPlayer.Name);
|
||||
}
|
||||
|
||||
var dynamicEntities = room.DynamicEntitiesList;
|
||||
var dynamicEntitiesCount = (ushort)dynamicEntities.Count;
|
||||
|
||||
Reference in New Issue
Block a user