Files
Ragon/Ragon.Client/Sources/RagonRoom.cs
T

177 lines
5.9 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
2024-05-09 10:50:59 +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.
*/
2023-10-04 14:42:59 +03:00
using Ragon.Protocol;
2023-03-06 10:06:43 +04:00
namespace Ragon.Client
{
public class RagonRoom : IDisposable
2023-03-06 10:06:43 +04:00
{
2024-04-07 17:15:52 +03:00
private class EventSubscription : IDisposable
{
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
private Action<RagonPlayer, IRagonEvent> _callback;
public EventSubscription(
List<Action<RagonPlayer, IRagonEvent>> callbacks,
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
Action<RagonPlayer, IRagonEvent> callback)
{
_callbacks = callbacks;
_localCallbacks = localCallbacks;
_callback = callback;
}
public void Dispose()
{
_callbacks?.Remove(_callback);
_localCallbacks?.Remove(_callback);
_callbacks = null!;
_localCallbacks = null!;
_callback = null!;
}
}
2023-10-04 14:42:59 +03:00
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
2023-10-07 20:20:02 +03:00
2024-05-05 15:45:28 +03:00
private readonly RagonClient _client;
private readonly RagonScene _scene;
private readonly RagonEntityCache _entityCache;
private readonly RagonPlayerCache _playerCache;
private readonly RoomParameters _parameters;
private readonly RagonUserData _userData;
2024-04-13 16:17:31 +03:00
public string Id => _parameters.RoomId;
public int MinPlayers => _parameters.Min;
public int MaxPlayers => _parameters.Max;
2023-07-30 21:14:14 +03:00
public string Scene => _scene.Name;
2023-06-27 23:41:30 +03:00
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
public RagonPlayer Local => _playerCache.Local;
2023-03-06 10:06:43 +04:00
public RagonPlayer Owner => _playerCache.Owner;
public RagonUserData UserData => _userData;
2023-10-07 20:20:02 +03:00
2023-10-04 14:42:59 +03:00
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners =
new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners =
new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
2023-10-07 20:20:02 +03:00
2023-03-06 10:06:43 +04:00
public RagonRoom(RagonClient client,
RagonEntityCache entityCache,
RagonPlayerCache playerCache,
2024-04-13 16:17:31 +03:00
RoomParameters parameters,
2023-03-06 10:06:43 +04:00
RagonScene scene)
{
_client = client;
2024-04-13 16:17:31 +03:00
_parameters = parameters;
2023-03-06 10:06:43 +04:00
_entityCache = entityCache;
_playerCache = playerCache;
_scene = scene;
_userData = new RagonUserData();
2023-03-06 10:06:43 +04:00
}
internal void Cleanup()
{
_entityCache.Cleanup();
_playerCache.Cleanup();
}
2023-07-30 21:14:14 +03:00
internal void Update(string sceneName)
{
_scene.Update(sceneName);
}
internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
2023-10-04 14:42:59 +03:00
{
if (_events.TryGetValue(eventCode, out var evnt))
evnt?.Invoke(caller, buffer);
else
RagonLog.Warn($"Handler event {Id} with eventCode {eventCode} not defined");
2023-10-04 14:42:59 +03:00
}
2023-10-07 20:20:02 +03:00
2024-05-09 10:50:59 +03:00
internal void HandleUserData(RagonBuffer buffer)
2024-05-05 15:45:28 +03:00
{
_userData.Read(buffer);
2024-05-05 15:45:28 +03:00
}
2024-04-07 17:15:52 +03:00
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
2023-10-04 14:42:59 +03:00
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
2023-10-07 20:20:02 +03:00
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
2023-10-11 19:37:50 +03:00
if (!_listeners.TryGetValue(eventCode, out var callbacks))
2023-10-04 14:42:59 +03:00
{
2023-10-07 20:20:02 +03:00
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
_listeners.Add(eventCode, callbacks);
}
2023-10-11 19:37:50 +03:00
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
2023-10-07 20:20:02 +03:00
{
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
2023-10-11 19:37:50 +03:00
_localListeners.Add(eventCode, localCallbacks);
2023-10-04 14:42:59 +03:00
}
2023-10-11 19:37:50 +03:00
2023-10-07 20:20:02 +03:00
callbacks.Add(action);
localCallbacks.Add(action);
2023-10-04 14:42:59 +03:00
2023-10-11 19:37:50 +03:00
if (!_events.ContainsKey(eventCode))
2023-10-04 14:42:59 +03:00
{
2023-10-11 19:37:50 +03:00
_events.Add(eventCode, (player, serializer) =>
{
t.Deserialize(serializer);
2023-10-07 20:20:02 +03:00
2023-10-11 19:37:50 +03:00
foreach (var callbackListener in callbacks)
callbackListener.Invoke(player, t);
});
}
2023-10-07 20:20:02 +03:00
2024-04-07 17:15:52 +03:00
return new EventSubscription(callbacks, localCallbacks, action);
2023-10-04 14:42:59 +03:00
}
2023-07-30 21:14:14 +03:00
public void LoadScene(string sceneName) => _scene.Load(sceneName);
2023-03-06 10:06:43 +04:00
public void SceneLoaded() => _scene.SceneLoaded();
2023-10-07 20:20:02 +03:00
public void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode mode)
where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode mode)
where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
2023-10-11 19:37:50 +03:00
public void ReplicateData(byte[] data, bool reliable = false) => _scene.ReplicateData(data, reliable);
2023-03-06 10:06:43 +04:00
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
2023-07-29 10:58:06 +03:00
public void CreateEntity(RagonEntity entity, RagonPayload payload) => _entityCache.Create(entity, payload);
2023-06-27 23:41:30 +03:00
public void TransferEntity(RagonEntity entity, RagonPlayer player) => _entityCache.Transfer(entity, player);
2023-03-06 10:06:43 +04:00
public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null);
2023-07-29 10:58:06 +03:00
public void DestroyEntity(RagonEntity entityId, RagonPayload payload) => _entityCache.Destroy(entityId, payload);
2023-10-09 09:17:43 +03:00
public void Dispose()
{
Cleanup();
_events.Clear();
_listeners.Clear();
_localListeners.Clear();
}
2023-03-06 10:06:43 +04:00
}
}