Files
Ragon/Ragon.Server/Sources/Room/RagonRoom.cs
T

264 lines
6.6 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
2024-05-19 08:00:56 +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.
*/
using Ragon.Protocol;
2024-04-29 09:12:42 +03:00
using Ragon.Server.Data;
using Ragon.Server.Event;
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-04-09 10:52:18 +04:00
public int PlayerMax { get; private set; }
public int PlayerMin { get; private set; }
public int PlayerCount => WaitPlayersList.Count;
2025-01-18 17:30:03 +03:00
public IReadOnlyList<RagonRoomPlayer> GetPlayers() => PlayerList;
2024-05-19 08:00:56 +03:00
public bool IsDone { get; private set; }
2024-05-05 15:45:28 +03:00
2024-04-29 09:12:42 +03:00
public RagonData UserData { get; set; }
2023-03-06 10:06:43 +04:00
public RagonRoomPlayer Owner { get; private set; }
2024-09-28 20:11:56 +03:00
public RagonStream Writer { get; }
2023-04-09 10:52:18 +04:00
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; }
private readonly List<RagonEvent> _bufferedEvents;
private readonly int _limitBufferedEvents;
2024-05-16 21:28:47 +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-04-09 10:52:18 +04:00
PlayerMax = info.Max;
PlayerMin = info.Min;
Plugin = roomPlugin;
2024-05-16 21:28:47 +03:00
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);
_bufferedEvents = new List<RagonEvent>();
_limitBufferedEvents = 1000;
2023-04-09 10:52:18 +04:00
UserData = new RagonData();
2024-09-28 20:11:56 +03:00
Writer = new RagonStream();
2023-03-06 10:06:43 +04:00
}
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer)
{
foreach (var evnt in _bufferedEvents)
{
Writer.Clear();
Writer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
Writer.WriteUShort(evnt.EventCode);
Writer.WriteUShort(evnt.Invoker.Connection.Id);
Writer.WriteByte((byte)RagonReplicationMode.Server);
evnt.Write(Writer);
var sendData = Writer.ToArray();
roomPlayer.Connection.Reliable.Send(sendData);
}
}
2024-05-16 21:28:47 +03:00
public void ReplicateEvent(
RagonRoomPlayer invoker,
RagonEvent evnt,
RagonReplicationMode eventMode,
RagonRoomPlayer targetPlayer
)
{
var room = Owner.Room;
var buffer = room.Writer;
2024-05-16 21:28:47 +03:00
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evnt.EventCode);
buffer.WriteUShort(invoker.Connection.Id);
buffer.WriteByte((byte)eventMode);
evnt.Write(buffer);
var sendData = buffer.ToArray();
targetPlayer.Connection.Reliable.Send(sendData);
}
public void ReplicateEvent(
RagonRoomPlayer invoker,
RagonEvent evnt,
RagonReplicationMode eventMode,
RagonTarget targetMode
)
{
2024-05-16 21:28:47 +03:00
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner &&
_bufferedEvents.Count < _limitBufferedEvents)
{
_bufferedEvents.Add(evnt);
}
2024-05-16 21:28:47 +03:00
Writer.Clear();
Writer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
Writer.WriteUShort(evnt.EventCode);
Writer.WriteUShort(invoker.Connection.Id);
Writer.WriteByte((byte)eventMode);
2024-05-16 21:28:47 +03:00
evnt.Write(Writer);
var sendData = Writer.ToArray();
switch (targetMode)
{
case RagonTarget.Owner:
{
Owner.Connection.Reliable.Send(sendData);
break;
}
case RagonTarget.ExceptOwner:
{
foreach (var roomPlayer in ReadyPlayersList)
{
if (roomPlayer.Connection.Id != Owner.Connection.Id)
roomPlayer.Connection.Reliable.Send(sendData);
}
break;
}
case RagonTarget.ExceptInvoker:
{
foreach (var roomPlayer in ReadyPlayersList)
{
if (roomPlayer.Connection.Id != invoker.Connection.Id)
roomPlayer.Connection.Reliable.Send(sendData);
}
break;
}
case RagonTarget.All:
{
Broadcast(sendData);
break;
}
}
}
2024-05-18 17:24:57 +03:00
2023-04-09 10:52:18 +04:00
public void Tick(float dt)
2023-03-06 10:06:43 +04:00
{
2024-09-28 20:11:56 +03:00
2023-03-06 10:06:43 +04:00
}
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);
}
2023-04-09 10:52:18 +04:00
2024-09-28 20:11:56 +03:00
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();
2024-05-16 21:28:47 +03:00
Plugin.OnPlayerLeaved(player);
2023-03-06 10:06:43 +04:00
}
}
public void UpdateReadyPlayerList()
{
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
}
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
2024-05-16 21:28:47 +03:00
public void Broadcast(byte[] data, List<RagonRoomPlayer> players, NetworkChannel channel = NetworkChannel.RELIABLE)
{
if (channel == NetworkChannel.RELIABLE)
{
foreach (var p in players)
p.Connection.Reliable.Send(data);
}
else
{
foreach (var p in players)
p.Connection.Unreliable.Send(data);
}
}
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);
}
2024-09-28 20:11:56 +03:00
2024-05-19 08:00:56 +03:00
public void Attach()
{
Plugin.OnAttached(this);
}
public void Detach()
{
Plugin.OnDetached(this);
Players.Clear();
WaitPlayersList.Clear();
ReadyPlayersList.Clear();
PlayerList.Clear();
_bufferedEvents.Clear();
IsDone = true;
}
2022-12-16 00:05:46 +04:00
}