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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using Ragon.Protocol;
|
2023-04-09 11:06:52 +04:00
|
|
|
using Ragon.Server.Entity;
|
2023-04-13 20:42:05 +04:00
|
|
|
using Ragon.Server.IO;
|
2023-04-09 10:52:18 +04:00
|
|
|
using Ragon.Server.Plugin;
|
|
|
|
|
using Ragon.Server.Time;
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2023-04-09 10:52:18 +04:00
|
|
|
namespace Ragon.Server.Room;
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2023-04-13 20:42:05 +04:00
|
|
|
public class RagonRoom : IRagonRoom, IRagonAction
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
|
|
|
|
public string Id { get; private set; }
|
2023-07-30 21:14:14 +03:00
|
|
|
public string Scene { get; private set; }
|
2023-04-09 10:52:18 +04:00
|
|
|
public int PlayerMax { get; private set; }
|
|
|
|
|
public int PlayerMin { get; private set; }
|
|
|
|
|
public int PlayerCount => WaitPlayersList.Count;
|
2023-07-30 21:14:14 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
public RagonRoomPlayer Owner { get; private set; }
|
2023-04-09 10:52:18 +04:00
|
|
|
public RagonBuffer Writer { get; }
|
|
|
|
|
public IRoomPlugin Plugin { get; private set; }
|
2023-07-30 21:14:14 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
public Dictionary<ushort, RagonRoomPlayer> Players { get; private set; }
|
|
|
|
|
public List<RagonRoomPlayer> WaitPlayersList { get; private set; }
|
|
|
|
|
public List<RagonRoomPlayer> ReadyPlayersList { get; private set; }
|
|
|
|
|
public List<RagonRoomPlayer> PlayerList { get; private set; }
|
|
|
|
|
|
|
|
|
|
public Dictionary<ushort, RagonEntity> Entities { get; private set; }
|
|
|
|
|
public List<RagonEntity> DynamicEntitiesList { get; private set; }
|
|
|
|
|
public List<RagonEntity> StaticEntitiesList { get; private set; }
|
|
|
|
|
public List<RagonEntity> EntityList { get; private set; }
|
|
|
|
|
|
|
|
|
|
private readonly HashSet<RagonEntity> _entitiesDirtySet;
|
2023-07-30 21:14:14 +03:00
|
|
|
|
2023-04-09 10:52:18 +04:00
|
|
|
public RagonRoom(string roomId, RoomInformation info, IRoomPlugin roomPlugin)
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
|
|
|
|
Id = roomId;
|
2023-07-30 21:14:14 +03:00
|
|
|
Scene = info.Scene;
|
2023-04-09 10:52:18 +04:00
|
|
|
PlayerMax = info.Max;
|
|
|
|
|
PlayerMin = info.Min;
|
|
|
|
|
Plugin = roomPlugin;
|
2023-03-06 10:06:43 +04:00
|
|
|
|
|
|
|
|
Players = new Dictionary<ushort, RagonRoomPlayer>(info.Max);
|
|
|
|
|
WaitPlayersList = new List<RagonRoomPlayer>(info.Max);
|
|
|
|
|
ReadyPlayersList = new List<RagonRoomPlayer>(info.Max);
|
|
|
|
|
PlayerList = new List<RagonRoomPlayer>(info.Max);
|
|
|
|
|
|
|
|
|
|
Entities = new Dictionary<ushort, RagonEntity>();
|
|
|
|
|
DynamicEntitiesList = new List<RagonEntity>();
|
|
|
|
|
StaticEntitiesList = new List<RagonEntity>();
|
|
|
|
|
EntityList = new List<RagonEntity>();
|
|
|
|
|
|
|
|
|
|
_entitiesDirtySet = new HashSet<RagonEntity>();
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
Writer = new RagonBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AttachEntity(RagonEntity entity)
|
|
|
|
|
{
|
|
|
|
|
Entities.Add(entity.Id, entity);
|
|
|
|
|
EntityList.Add(entity);
|
|
|
|
|
|
|
|
|
|
if (entity.StaticId == 0)
|
|
|
|
|
DynamicEntitiesList.Add(entity);
|
|
|
|
|
else
|
|
|
|
|
StaticEntitiesList.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DetachEntity(RagonEntity entity)
|
|
|
|
|
{
|
|
|
|
|
Entities.Remove(entity.Id);
|
|
|
|
|
EntityList.Remove(entity);
|
|
|
|
|
StaticEntitiesList.Remove(entity);
|
|
|
|
|
DynamicEntitiesList.Remove(entity);
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
_entitiesDirtySet.Remove(entity);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 10:52:18 +04:00
|
|
|
public void Tick(float dt)
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
2023-04-09 10:52:18 +04:00
|
|
|
var entities = (ushort)_entitiesDirtySet.Count;
|
2023-03-06 10:06:43 +04:00
|
|
|
if (entities > 0)
|
|
|
|
|
{
|
|
|
|
|
Writer.Clear();
|
|
|
|
|
Writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
|
|
|
|
Writer.WriteUShort(entities);
|
|
|
|
|
|
|
|
|
|
foreach (var entity in _entitiesDirtySet)
|
2023-04-14 14:32:04 +04:00
|
|
|
entity.WriteState(Writer);
|
2023-03-06 10:06:43 +04:00
|
|
|
|
|
|
|
|
_entitiesDirtySet.Clear();
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
var sendData = Writer.ToArray();
|
|
|
|
|
foreach (var roomPlayer in ReadyPlayersList)
|
|
|
|
|
roomPlayer.Connection.Unreliable.Send(sendData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AttachPlayer(RagonRoomPlayer player)
|
|
|
|
|
{
|
|
|
|
|
if (Players.Count == 0)
|
|
|
|
|
Owner = player;
|
|
|
|
|
|
|
|
|
|
player.OnAttached(this);
|
|
|
|
|
|
|
|
|
|
PlayerList.Add(player);
|
|
|
|
|
Players.Add(player.Connection.Id, player);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DetachPlayer(RagonRoomPlayer roomPlayer)
|
|
|
|
|
{
|
|
|
|
|
if (Players.Remove(roomPlayer.Connection.Id, out var player))
|
|
|
|
|
{
|
|
|
|
|
PlayerList.Remove(player);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Writer.Clear();
|
|
|
|
|
Writer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
|
|
|
|
Writer.WriteString(player.Id);
|
|
|
|
|
|
|
|
|
|
var entitiesToDelete = player.Entities.DynamicList;
|
2023-04-09 10:52:18 +04:00
|
|
|
Writer.WriteUShort((ushort)entitiesToDelete.Count);
|
2023-03-06 10:06:43 +04:00
|
|
|
foreach (var entity in entitiesToDelete)
|
|
|
|
|
{
|
|
|
|
|
Writer.WriteUShort(entity.Id);
|
|
|
|
|
DetachEntity(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sendData = Writer.ToArray();
|
|
|
|
|
Broadcast(sendData);
|
|
|
|
|
}
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
if (roomPlayer.Connection.Id == Owner.Connection.Id && PlayerList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var nextOwner = PlayerList[0];
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
Owner = nextOwner;
|
2023-04-09 10:52:18 +04:00
|
|
|
|
|
|
|
|
var entitiesToUpdate = roomPlayer.Entities.StaticList;
|
|
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
Writer.Clear();
|
2023-06-27 23:41:30 +03:00
|
|
|
Writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED);
|
2023-07-01 08:12:40 +03:00
|
|
|
Writer.WriteUShort(Owner.Connection.Id);
|
2023-04-09 10:52:18 +04:00
|
|
|
Writer.WriteUShort((ushort)entitiesToUpdate.Count);
|
|
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
foreach (var entity in entitiesToUpdate)
|
|
|
|
|
{
|
|
|
|
|
Writer.WriteUShort(entity.Id);
|
2023-04-09 10:52:18 +04:00
|
|
|
|
|
|
|
|
entity.Attach(nextOwner);
|
2023-03-06 10:06:43 +04:00
|
|
|
nextOwner.Entities.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sendData = Writer.ToArray();
|
|
|
|
|
Broadcast(sendData);
|
|
|
|
|
}
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
player.OnDetached();
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
UpdateReadyPlayerList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateReadyPlayerList()
|
|
|
|
|
{
|
|
|
|
|
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateMap(string sceneName)
|
|
|
|
|
{
|
2023-07-30 21:14:14 +03:00
|
|
|
Scene = sceneName;
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
DynamicEntitiesList.Clear();
|
|
|
|
|
StaticEntitiesList.Clear();
|
|
|
|
|
Entities.Clear();
|
|
|
|
|
EntityList.Clear();
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
foreach (var player in PlayerList)
|
|
|
|
|
player.UnsetReady();
|
2023-04-09 10:52:18 +04:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
UpdateReadyPlayerList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Track(RagonEntity entity)
|
|
|
|
|
{
|
|
|
|
|
_entitiesDirtySet.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 19:38:26 +03:00
|
|
|
public void Broadcast(byte[] data, NetworkChannel channel = NetworkChannel.RELIABLE)
|
2023-03-06 10:06:43 +04:00
|
|
|
{
|
2023-10-11 19:38:26 +03:00
|
|
|
if (channel == NetworkChannel.RELIABLE)
|
|
|
|
|
{
|
|
|
|
|
foreach (var readyPlayer in ReadyPlayersList)
|
|
|
|
|
readyPlayer.Connection.Reliable.Send(data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var readyPlayer in ReadyPlayersList)
|
|
|
|
|
readyPlayer.Connection.Unreliable.Send(data);
|
|
|
|
|
}
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
2023-04-14 14:32:04 +04:00
|
|
|
|
|
|
|
|
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection)
|
|
|
|
|
{
|
|
|
|
|
return Players[connection.Id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RagonRoomPlayer? GetPlayerById(string id)
|
|
|
|
|
{
|
|
|
|
|
return PlayerList.FirstOrDefault(p => p.Id == id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IRagonEntity? GetEntityById(ushort id)
|
|
|
|
|
{
|
2023-07-30 21:14:14 +03:00
|
|
|
return Entities.TryGetValue(id, out var entity) ? entity : null;
|
2023-04-14 14:32:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer player)
|
|
|
|
|
{
|
|
|
|
|
return EntityList.Where(e => e.Owner.Connection.Id == player.Connection.Id).ToArray();
|
|
|
|
|
}
|
2022-12-16 00:05:46 +04:00
|
|
|
}
|