2023-03-06 10:06:43 +04:00
|
|
|
/*
|
2024-05-19 12:26:42 +03:00
|
|
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
2023-03-06 10:06:43 +04:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
using Ragon.Protocol;
|
2023-04-09 10:52:18 +04:00
|
|
|
using Ragon.Server.Entity;
|
2023-10-11 19:37:50 +03:00
|
|
|
using Ragon.Server.IO;
|
2023-04-09 11:06:52 +04:00
|
|
|
using Ragon.Server.Lobby;
|
2024-05-19 08:54:49 +03:00
|
|
|
using Ragon.Server.Logging;
|
2023-04-09 10:52:18 +04:00
|
|
|
using Ragon.Server.Room;
|
2022-12-16 00:05:46 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
namespace Ragon.Server.Handler
|
2022-12-16 00:05:46 +04:00
|
|
|
{
|
2024-05-19 08:54:49 +03:00
|
|
|
public sealed class SceneLoadedOperation : BaseOperation
|
2022-12-16 00:05:46 +04:00
|
|
|
{
|
2024-05-19 08:54:49 +03:00
|
|
|
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(SceneLoadedOperation));
|
2024-08-17 10:29:27 +03:00
|
|
|
private RagonServerConfiguration _configuration;
|
|
|
|
|
|
|
|
|
|
public SceneLoadedOperation(RagonBuffer reader, RagonBuffer writer, RagonServerConfiguration serverConfiguration) : base(reader, writer)
|
2023-10-11 20:49:00 +03:00
|
|
|
{
|
2024-08-17 10:29:27 +03:00
|
|
|
_configuration = serverConfiguration;
|
2023-10-11 20:49:00 +03:00
|
|
|
}
|
2022-12-16 00:05:46 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
public override void Handle(RagonContext context, NetworkChannel channel)
|
2022-12-16 00:05:46 +04:00
|
|
|
{
|
2024-05-19 08:54:49 +03:00
|
|
|
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var owner = context.Room.Owner;
|
|
|
|
|
var player = context.RoomPlayer;
|
|
|
|
|
var room = context.Room;
|
2024-08-17 10:29:27 +03:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
if (player.IsLoaded)
|
2022-12-16 00:05:46 +04:00
|
|
|
{
|
2024-05-19 08:54:49 +03:00
|
|
|
_logger.Warning($"Player {player.Name}:{player.Connection.Id} already ready");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
if (player == owner)
|
|
|
|
|
{
|
|
|
|
|
var statics = Reader.ReadUShort();
|
|
|
|
|
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
|
2022-12-16 00:05:46 +04:00
|
|
|
{
|
2024-05-19 08:54:49 +03:00
|
|
|
var entityType = Reader.ReadUShort();
|
|
|
|
|
var eventAuthority = (RagonAuthority)Reader.ReadByte();
|
|
|
|
|
var staticId = Reader.ReadUShort();
|
|
|
|
|
var propertiesCount = Reader.ReadUShort();
|
|
|
|
|
|
|
|
|
|
var entityParameters = new RagonEntityParameters()
|
|
|
|
|
{
|
|
|
|
|
Type = entityType,
|
|
|
|
|
Authority = eventAuthority,
|
|
|
|
|
AttachId = 0,
|
|
|
|
|
StaticId = staticId,
|
|
|
|
|
BufferedEvents = context.LimitBufferedEvents,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var entity = new RagonEntity(entityParameters);
|
|
|
|
|
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
|
|
|
|
|
{
|
|
|
|
|
var propertyType = Reader.ReadBool();
|
|
|
|
|
var propertySize = Reader.ReadUShort();
|
2024-08-17 10:29:27 +03:00
|
|
|
entity.AddProperty(new RagonProperty(propertySize, propertyType, _configuration.LimitPropertySize));
|
2024-05-19 08:54:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var roomPlugin = room.Plugin;
|
2024-08-17 10:29:27 +03:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
if (!roomPlugin.OnEntityCreate(player, entity)) continue;
|
|
|
|
|
|
|
|
|
|
var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}";
|
|
|
|
|
var entityInfo = $"{entity.Id}:{entity.Type}";
|
|
|
|
|
|
|
|
|
|
_logger.Trace($"{playerInfo} created static entity {entityInfo}");
|
|
|
|
|
|
|
|
|
|
entity.Attach(player);
|
|
|
|
|
room.AttachEntity(entity);
|
|
|
|
|
player.AttachEntity(entity);
|
2022-12-16 00:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} loaded");
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
room.WaitPlayersList.Add(player);
|
2022-12-16 00:05:46 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
foreach (var roomPlayer in room.WaitPlayersList)
|
|
|
|
|
{
|
|
|
|
|
DispatchPlayerJoinExcludePlayer(room, roomPlayer, Writer);
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
roomPlayer.SetReady();
|
|
|
|
|
}
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
room.UpdateReadyPlayerList();
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
DispatchSnapshot(room, room.WaitPlayersList, Writer);
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
room.WaitPlayersList.Clear();
|
|
|
|
|
}
|
|
|
|
|
else if (owner.IsLoaded)
|
|
|
|
|
{
|
|
|
|
|
player.SetReady();
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
DispatchPlayerJoinExcludePlayer(room, player, Writer);
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
room.UpdateReadyPlayerList();
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
DispatchSnapshot(room, new List<RagonRoomPlayer>() { player }, Writer);
|
2022-12-16 00:05:46 +04:00
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
foreach (var entity in room.EntityList)
|
|
|
|
|
entity.RestoreBufferedEvents(player, Writer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace($"Player {player.Connection.Id}|{context.LobbyPlayer.Name} waiting owner of room");
|
|
|
|
|
room.WaitPlayersList.Add(player);
|
|
|
|
|
}
|
2022-12-16 00:05:46 +04:00
|
|
|
}
|
2024-05-19 08:54:49 +03:00
|
|
|
|
|
|
|
|
private void DispatchPlayerJoinExcludePlayer(RagonRoom room, RagonRoomPlayer roomPlayer, RagonBuffer writer)
|
2022-12-16 00:05:46 +04:00
|
|
|
{
|
2024-05-19 08:54:49 +03:00
|
|
|
writer.Clear();
|
|
|
|
|
writer.WriteOperation(RagonOperation.PLAYER_JOINED);
|
|
|
|
|
writer.WriteUShort(roomPlayer.Connection.Id);
|
|
|
|
|
writer.WriteString(roomPlayer.Id);
|
|
|
|
|
writer.WriteString(roomPlayer.Name);
|
|
|
|
|
|
|
|
|
|
var sendData = writer.ToArray();
|
|
|
|
|
foreach (var awaiter in room.ReadyPlayersList)
|
|
|
|
|
{
|
|
|
|
|
if (awaiter != roomPlayer)
|
|
|
|
|
awaiter.Connection.Reliable.Send(sendData);
|
|
|
|
|
}
|
2022-12-16 00:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
2024-05-19 08:54:49 +03:00
|
|
|
private void DispatchSnapshot(RagonRoom room, List<RagonRoomPlayer> receviersList, RagonBuffer writer)
|
2022-12-16 00:05:46 +04:00
|
|
|
{
|
2024-05-19 08:54:49 +03:00
|
|
|
writer.Clear();
|
|
|
|
|
writer.WriteOperation(RagonOperation.SNAPSHOT);
|
|
|
|
|
|
|
|
|
|
var dynamicEntities = room.DynamicEntitiesList;
|
|
|
|
|
var dynamicEntitiesCount = (ushort)dynamicEntities.Count;
|
|
|
|
|
writer.WriteUShort(dynamicEntitiesCount);
|
|
|
|
|
foreach (var entity in dynamicEntities)
|
|
|
|
|
entity.Snapshot(writer);
|
|
|
|
|
|
|
|
|
|
var staticEntities = room.StaticEntitiesList;
|
|
|
|
|
var staticEntitiesCount = (ushort)staticEntities.Count;
|
|
|
|
|
writer.WriteUShort(staticEntitiesCount);
|
|
|
|
|
foreach (var entity in staticEntities)
|
|
|
|
|
entity.Snapshot(writer);
|
|
|
|
|
|
|
|
|
|
var sendData = writer.ToArray();
|
|
|
|
|
foreach (var player in receviersList)
|
|
|
|
|
player.Connection.Reliable.Send(sendData);
|
2022-12-16 00:05:46 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|