Files
Ragon/Ragon.Client/Sources/Handler/SnapshotHandler.cs
T

126 lines
3.8 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.
*/
2023-06-27 23:41:30 +03:00
using System.Diagnostics;
2023-03-06 10:06:43 +04:00
using Ragon.Protocol;
namespace Ragon.Client;
2023-10-04 14:42:59 +03:00
internal class SnapshotHandler : IHandler
2023-03-06 10:06:43 +04:00
{
2023-07-29 10:58:06 +03:00
private readonly IRagonEntityListener _entityListener;
private readonly RagonClient _client;
private readonly RagonListenerList _listenerList;
private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache;
2023-03-06 10:06:43 +04:00
public SnapshotHandler(
RagonClient ragonClient,
RagonListenerList listenerList,
RagonEntityCache entityCache,
2023-07-29 10:58:06 +03:00
RagonPlayerCache playerCache,
2023-07-30 16:56:11 +03:00
IRagonEntityListener entityListener
2023-03-06 10:06:43 +04:00
)
{
_client = ragonClient;
2023-07-29 10:58:06 +03:00
_entityListener = entityListener;
2023-03-06 10:06:43 +04:00
_listenerList = listenerList;
_entityCache = entityCache;
_playerCache = playerCache;
}
public void Handle(RagonBuffer buffer)
{
var playersCount = buffer.ReadUShort();
RagonLog.Trace("Players: " + playersCount);
for (var i = 0; i < playersCount; i++)
{
var playerPeerId = buffer.ReadUShort();
var playerId = buffer.ReadString();
var playerName = buffer.ReadString();
2023-07-30 16:56:11 +03:00
2023-03-06 10:06:43 +04:00
_playerCache.AddPlayer(playerPeerId, playerId, playerName);
2023-07-30 16:56:11 +03:00
2023-07-29 10:58:06 +03:00
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
2023-03-06 10:06:43 +04:00
}
2023-07-29 10:58:06 +03:00
2023-03-06 10:06:43 +04:00
var dynamicEntities = buffer.ReadUShort();
RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
for (var i = 0; i < dynamicEntities; i++)
{
var entityType = buffer.ReadUShort();
var entityId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort();
2023-07-30 17:44:51 +03:00
2023-07-29 10:58:06 +03:00
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null)
{
RagonLog.Error($"Player not found with peerId: ${ownerPeerId}");
return;
}
2023-03-06 10:06:43 +04:00
2023-06-27 23:41:30 +03:00
var hasAuthority = _playerCache.Local.Id == player.Id;
2023-07-29 10:58:06 +03:00
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
2023-03-06 10:06:43 +04:00
2023-07-30 16:56:11 +03:00
if (payloadSize > 0)
{
var payload = new RagonPayload(payloadSize);
payload.Read(buffer);
entity.AttachPayload(payload);
}
2023-07-29 10:58:06 +03:00
_entityListener.OnEntityCreated(entity);
2023-07-30 16:56:11 +03:00
entity.Read(buffer);
2023-07-29 10:58:06 +03:00
entity.Attach(_client, entityId, entityType, hasAuthority, player);
2023-03-06 10:06:43 +04:00
}
var staticEntities = buffer.ReadUShort();
RagonLog.Trace("Scene Entities: " + staticEntities);
for (var i = 0; i < staticEntities; i++)
{
var entityType = buffer.ReadUShort();
var entityId = buffer.ReadUShort();
var staticId = buffer.ReadUShort();
var ownerPeerId = buffer.ReadUShort();
var payloadSize = buffer.ReadUShort();
2023-07-30 16:56:11 +03:00
2023-07-29 10:58:06 +03:00
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
if (player == null)
{
RagonLog.Error($"Player not found with peerId: ${ownerPeerId}");
return;
}
2023-07-30 16:56:11 +03:00
2023-06-27 23:41:30 +03:00
var hasAuthority = _playerCache.Local.Id == player.Id;
2023-07-29 10:58:06 +03:00
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
2023-07-30 16:56:11 +03:00
entity.Read(buffer);
2023-07-29 10:58:06 +03:00
entity.Attach(_client, entityId, entityType, hasAuthority, player);
2023-03-06 10:06:43 +04:00
}
2023-07-29 10:58:06 +03:00
2023-07-30 21:46:42 +03:00
if (_client.Status == RagonStatus.LOBBY)
{
_client.SetStatus(RagonStatus.ROOM);
_listenerList.OnJoined();
}
2023-07-30 21:14:14 +03:00
_listenerList.OnSceneLoaded();
2023-03-06 10:06:43 +04:00
}
}