Files
Ragon/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs
T

164 lines
5.1 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-12-16 00:05:46 +04:00
using NLog;
2023-03-06 10:06:43 +04:00
using Ragon.Protocol;
2023-04-09 10:52:18 +04:00
using Ragon.Server.Entity;
2023-04-09 11:06:52 +04:00
using Ragon.Server.Lobby;
2023-04-09 10:52:18 +04:00
using Ragon.Server.Room;
2022-12-16 00:05:46 +04:00
2023-04-09 10:52:18 +04:00
namespace Ragon.Server.Handler;
2022-12-16 00:05:46 +04:00
2023-10-07 19:30:52 +03:00
public sealed class SceneLoadedOperation : BaseOperation
2022-12-16 00:05:46 +04:00
{
2023-04-09 10:52:18 +04:00
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
2023-10-07 19:30:52 +03:00
public SceneLoadedOperation(RagonBuffer reader, RagonBuffer writer): base(reader, writer)
2023-04-09 10:52:18 +04:00
{
}
2023-10-07 19:30:52 +03:00
public override void Handle(RagonContext context, byte[] data)
2022-12-16 00:05:46 +04:00
{
2023-04-09 10:52:18 +04:00
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
2022-12-16 00:05:46 +04:00
return;
var owner = context.Room.Owner;
var player = context.RoomPlayer;
var room = context.Room;
if (player == owner)
{
2023-10-07 19:30:52 +03:00
var statics = Reader.ReadUShort();
2022-12-16 00:05:46 +04:00
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
{
2023-10-07 19:30:52 +03:00
var entityType = Reader.ReadUShort();
var eventAuthority = (RagonAuthority)Reader.ReadByte();
var staticId = Reader.ReadUShort();
var propertiesCount = Reader.ReadUShort();
2023-04-09 10:52:18 +04:00
var entityParameters = new RagonEntityParameters()
{
Type = entityType,
Authority = eventAuthority,
AttachId = 0,
StaticId = staticId,
2023-09-03 13:29:37 +03:00
BufferedEvents = context.Configuration.LimitBufferedEvents,
2023-04-09 10:52:18 +04:00
};
var entity = new RagonEntity(entityParameters);
2022-12-16 00:05:46 +04:00
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
{
2023-10-07 19:30:52 +03:00
var propertyType = Reader.ReadBool();
var propertySize = Reader.ReadUShort();
2023-04-14 14:32:04 +04:00
entity.AddProperty(new RagonProperty(propertySize, propertyType));
2022-12-16 00:05:46 +04:00
}
2023-03-06 10:06:43 +04:00
2023-04-09 10:52:18 +04:00
var roomPlugin = room.Plugin;
2023-04-14 17:42:27 +04:00
if (!roomPlugin.OnEntityCreate(player, entity)) continue;
2022-12-16 00:05:46 +04:00
2023-04-09 10:52:18 +04:00
var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}";
var entityInfo = $"{entity.Id}:{entity.Type}";
2023-07-30 16:56:11 +03:00
_logger.Trace($"{playerInfo} created static entity {entityInfo}");
2023-04-09 10:52:18 +04:00
entity.Attach(player);
2023-03-06 10:06:43 +04:00
room.AttachEntity(entity);
player.AttachEntity(entity);
2022-12-16 00:05:46 +04:00
}
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} loaded");
2023-03-06 10:06:43 +04:00
2022-12-16 00:05:46 +04:00
room.WaitPlayersList.Add(player);
foreach (var roomPlayer in room.WaitPlayersList)
{
2023-10-07 19:30:52 +03:00
DispatchPlayerJoinExcludePlayer(room, roomPlayer, Writer);
2023-03-06 10:06:43 +04:00
2022-12-16 00:05:46 +04:00
roomPlayer.SetReady();
}
2023-03-06 10:06:43 +04:00
2022-12-16 00:05:46 +04:00
room.UpdateReadyPlayerList();
2023-03-06 10:06:43 +04:00
2023-10-07 19:30:52 +03:00
DispatchSnapshot(room, room.WaitPlayersList, Writer);
2023-03-06 10:06:43 +04:00
2022-12-16 00:05:46 +04:00
room.WaitPlayersList.Clear();
}
else if (owner.IsLoaded)
{
player.SetReady();
2023-03-06 10:06:43 +04:00
2023-10-07 19:30:52 +03:00
DispatchPlayerJoinExcludePlayer(room, player, Writer);
2023-03-06 10:06:43 +04:00
2022-12-16 00:05:46 +04:00
room.UpdateReadyPlayerList();
2023-03-06 10:06:43 +04:00
2023-10-07 19:30:52 +03:00
DispatchSnapshot(room, new List<RagonRoomPlayer>() { player }, Writer);
2022-12-16 00:05:46 +04:00
foreach (var entity in room.EntityList)
2023-10-07 19:30:52 +03:00
entity.RestoreBufferedEvents(player, Writer);
2022-12-16 00:05:46 +04:00
}
else
{
_logger.Trace($"Player {player.Connection.Id}|{context.LobbyPlayer.Name} waiting owner of room");
room.WaitPlayersList.Add(player);
}
}
2023-03-06 10:06:43 +04:00
private void DispatchPlayerJoinExcludePlayer(RagonRoom room, RagonRoomPlayer roomPlayer, RagonBuffer writer)
2022-12-16 00:05:46 +04:00
{
writer.Clear();
writer.WriteOperation(RagonOperation.PLAYER_JOINED);
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
2023-03-06 10:06:43 +04:00
2022-12-16 00:05:46 +04:00
var sendData = writer.ToArray();
foreach (var awaiter in room.ReadyPlayersList)
{
if (awaiter != roomPlayer)
2022-12-20 12:20:52 -08:00
awaiter.Connection.Reliable.Send(sendData);
2022-12-16 00:05:46 +04:00
}
}
2023-03-06 10:06:43 +04:00
private void DispatchSnapshot(RagonRoom room, List<RagonRoomPlayer> receviersList, RagonBuffer writer)
2022-12-16 00:05:46 +04:00
{
writer.Clear();
writer.WriteOperation(RagonOperation.SNAPSHOT);
2023-03-06 10:06:43 +04:00
writer.WriteUShort((ushort)room.ReadyPlayersList.Count);
2022-12-16 00:05:46 +04:00
foreach (var roomPlayer in room.ReadyPlayersList)
{
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
}
2023-04-09 10:52:18 +04:00
2022-12-16 00:05:46 +04:00
var dynamicEntities = room.DynamicEntitiesList;
2023-03-06 10:06:43 +04:00
var dynamicEntitiesCount = (ushort)dynamicEntities.Count;
2022-12-16 00:05:46 +04:00
writer.WriteUShort(dynamicEntitiesCount);
foreach (var entity in dynamicEntities)
2023-03-06 10:06:43 +04:00
entity.Snapshot(writer);
2022-12-16 00:05:46 +04:00
var staticEntities = room.StaticEntitiesList;
2023-03-06 10:06:43 +04:00
var staticEntitiesCount = (ushort)staticEntities.Count;
2022-12-16 00:05:46 +04:00
writer.WriteUShort(staticEntitiesCount);
foreach (var entity in staticEntities)
2023-03-06 10:06:43 +04:00
entity.Snapshot(writer);
2023-04-09 10:52:18 +04:00
2022-12-16 00:05:46 +04:00
var sendData = writer.ToArray();
foreach (var player in receviersList)
2022-12-20 12:20:52 -08:00
player.Connection.Reliable.Send(sendData);
2022-12-16 00:05:46 +04:00
}
}