major update
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class AuthorizationOperation: IRagonOperation
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Authorized)
|
||||
{
|
||||
_logger.Warn("Player already authorized");
|
||||
return;
|
||||
}
|
||||
|
||||
var key = reader.ReadString();
|
||||
var playerName = reader.ReadString();
|
||||
var additionalPayload = new RagonPayload();
|
||||
additionalPayload.Read(reader);
|
||||
|
||||
context.LobbyPlayer.Name = playerName;
|
||||
context.LobbyPlayer.AdditionalData = Array.Empty<byte>();
|
||||
context.LobbyPlayer.Status = LobbyPlayerStatus.Authorized;
|
||||
|
||||
var playerId = context.LobbyPlayer.Id;
|
||||
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
|
||||
writer.WriteString(playerId);
|
||||
writer.WriteString(playerName);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
context.Connection.Reliable.Send(sendData);
|
||||
|
||||
_logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class EntityCreateOperation : IRagonOperation
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
var player = context.RoomPlayer;
|
||||
var room = context.Room;
|
||||
var attachId = reader.ReadUShort();
|
||||
var entityType = reader.ReadUShort();
|
||||
var eventAuthority = (RagonAuthority) reader.ReadByte();
|
||||
var propertiesCount = reader.ReadUShort();
|
||||
|
||||
var entity = new RagonEntity(player, entityType, 0, attachId, eventAuthority);
|
||||
for (var i = 0; i < propertiesCount; i++)
|
||||
{
|
||||
var propertyType = reader.ReadBool();
|
||||
var propertySize = reader.ReadUShort();
|
||||
|
||||
entity.State.AddProperty(new RagonProperty(propertySize, propertyType));
|
||||
}
|
||||
|
||||
if (reader.Capacity > 0)
|
||||
entity.Payload.Read(reader);
|
||||
|
||||
room.AttachEntity(entity);
|
||||
player.AttachEntity(entity);
|
||||
|
||||
entity.Create();
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class EntityDestroyOperation: IRagonOperation
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
var player = context.RoomPlayer;
|
||||
var room = context.Room;
|
||||
var entityId = reader.ReadUShort();
|
||||
|
||||
if (room.Entities.TryGetValue(entityId, out var entity))
|
||||
{
|
||||
var payload = new RagonPayload();
|
||||
payload.Read(reader);
|
||||
|
||||
room.DetachEntity(entity);
|
||||
player.DetachEntity(entity);
|
||||
|
||||
entity.Destroy();
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} destoyed entity {entity.Id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class EntityEventOperation : IRagonOperation
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
var player = context.RoomPlayer;
|
||||
var room = context.Room;
|
||||
var entityId = reader.ReadUShort();
|
||||
|
||||
if (!room.Entities.TryGetValue(entityId, out var ent))
|
||||
{
|
||||
_logger.Warn($"Entity not found for event with Id {entityId}");
|
||||
return;
|
||||
}
|
||||
|
||||
var eventId = reader.ReadUShort();
|
||||
var eventMode = (RagonReplicationMode)reader.ReadByte();
|
||||
var targetMode = (RagonTarget)reader.ReadByte();
|
||||
var targetPlayerPeerId = (ushort)0;
|
||||
|
||||
if (targetMode == RagonTarget.Player)
|
||||
targetPlayerPeerId = reader.ReadUShort();
|
||||
|
||||
var ragonEvent = new RagonEvent(player, eventId);
|
||||
ragonEvent.Read(reader);
|
||||
|
||||
if (targetMode == RagonTarget.Player &&
|
||||
context.Room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
|
||||
{
|
||||
ent.ReplicateEvent(player, ragonEvent, eventMode, targetPlayer);
|
||||
return;
|
||||
}
|
||||
|
||||
ent.ReplicateEvent(player, ragonEvent, eventMode, targetMode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class EntityStateOperation: IRagonOperation
|
||||
{
|
||||
private ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
var room = context.Room;
|
||||
var entitiesCount = reader.ReadUShort();
|
||||
|
||||
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
|
||||
{
|
||||
var entityId = reader.ReadUShort();
|
||||
if (room.Entities.TryGetValue(entityId, out var entity))
|
||||
{
|
||||
entity.State.Read(reader);
|
||||
room.Track(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Error($"Entity with Id {entityId} not found, replication interrupted");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class RoomCreateOperation: IRagonOperation
|
||||
{
|
||||
private RagonRoomParameters _roomParameters = new();
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
|
||||
{
|
||||
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
|
||||
return;
|
||||
}
|
||||
|
||||
var custom = reader.ReadBool();
|
||||
var roomId = Guid.NewGuid().ToString();
|
||||
|
||||
if (custom)
|
||||
{
|
||||
roomId = reader.ReadString();
|
||||
if (context.Lobby.FindRoomById(roomId, out _))
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_FAILED);
|
||||
writer.WriteString($"Room with id {roomId} already exists");
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
context.Connection.Reliable.Send(sendData);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} join failed to room {roomId}, room already exist");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_roomParameters.Deserialize(reader);
|
||||
|
||||
var information = new RoomInformation()
|
||||
{
|
||||
Map = _roomParameters.Map,
|
||||
Max = _roomParameters.Max,
|
||||
Min = _roomParameters.Min,
|
||||
};
|
||||
|
||||
var lobbyPlayer = context.LobbyPlayer;
|
||||
|
||||
var room = new RagonRoom(roomId, information);
|
||||
context.Scheduler.Run(room);
|
||||
context.Lobby.Persist(room);
|
||||
|
||||
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
context.SetRoom(room, player);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}");
|
||||
|
||||
JoinSuccess(player, room, writer);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
|
||||
}
|
||||
|
||||
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort) room.Info.Min);
|
||||
writer.WriteUShort((ushort) room.Info.Max);
|
||||
writer.WriteString(room.Info.Map);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class RoomJoinOperation : IRagonOperation
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
var roomId = reader.ReadString();
|
||||
var lobbyPlayer = context.LobbyPlayer;
|
||||
|
||||
if (!context.Lobby.FindRoomById(roomId, out var existsRoom))
|
||||
{
|
||||
JoinFailed(lobbyPlayer, writer);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} failed to join room {roomId}");
|
||||
return;
|
||||
}
|
||||
|
||||
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
context.SetRoom(existsRoom, player);
|
||||
|
||||
JoinSuccess(player, existsRoom, writer);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}");
|
||||
}
|
||||
|
||||
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort) room.Info.Min);
|
||||
writer.WriteUShort((ushort) room.Info.Max);
|
||||
writer.WriteString(room.Info.Map);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
private void JoinFailed(RagonLobbyPlayer player, RagonBuffer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_FAILED);
|
||||
writer.WriteString($"Room not exists");
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class RoomJoinOrCreateOperation : IRagonOperation
|
||||
{
|
||||
private RagonRoomParameters _roomParameters = new();
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
|
||||
{
|
||||
_logger.Warn("Player not authorized for this request");
|
||||
return;
|
||||
}
|
||||
|
||||
var roomId = Guid.NewGuid().ToString();
|
||||
var lobbyPlayer = context.LobbyPlayer;
|
||||
|
||||
_roomParameters.Deserialize(reader);
|
||||
|
||||
if (context.Lobby.FindRoomByMap(_roomParameters.Map, out var existsRoom))
|
||||
{
|
||||
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
context.SetRoom(existsRoom, player);
|
||||
|
||||
JoinSuccess(player, existsRoom, writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
var information = new RoomInformation()
|
||||
{
|
||||
Map = _roomParameters.Map,
|
||||
Max = _roomParameters.Max,
|
||||
Min = _roomParameters.Min,
|
||||
};
|
||||
|
||||
var room = new RagonRoom(roomId, information);
|
||||
context.Lobby.Persist(room);
|
||||
context.Scheduler.Run(room);
|
||||
|
||||
var roomPlayer = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
||||
context.SetRoom(room, roomPlayer);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}");
|
||||
|
||||
JoinSuccess(roomPlayer, room, writer);
|
||||
}
|
||||
}
|
||||
|
||||
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||
writer.WriteString(room.Id);
|
||||
writer.WriteString(player.Id);
|
||||
writer.WriteString(room.Owner.Id);
|
||||
writer.WriteUShort((ushort) room.Info.Min);
|
||||
writer.WriteUShort((ushort) room.Info.Max);
|
||||
writer.WriteString(room.Info.Map);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
|
||||
_logger.Trace($"{player.Connection.Id}|{player.Name} joined to room {room.Id}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class RoomLeaveOperation: IRagonOperation
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
var room = context.Room;
|
||||
var roomPlayer = context.RoomPlayer;
|
||||
if (room != null)
|
||||
{
|
||||
context.Room?.DetachPlayer(roomPlayer);
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public class SceneLoadOperation: IRagonOperation
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
var roomOwner = context.Room.Owner;
|
||||
var currentPlayer = context.RoomPlayer;
|
||||
var room = context.Room;
|
||||
var sceneName = reader.ReadString();
|
||||
|
||||
if (roomOwner.Connection.Id != currentPlayer.Connection.Id)
|
||||
{
|
||||
_logger.Warn("Only owner can change map!");
|
||||
return;
|
||||
}
|
||||
|
||||
room.UpdateMap(sceneName);
|
||||
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.LOAD_SCENE);
|
||||
writer.WriteString(sceneName);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
foreach (var player in room.PlayerList)
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* 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 NLog;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Server;
|
||||
|
||||
public sealed class SceneLoadedOperation : IRagonOperation
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
|
||||
return;
|
||||
|
||||
var owner = context.Room.Owner;
|
||||
var player = context.RoomPlayer;
|
||||
var room = context.Room;
|
||||
|
||||
if (player == owner)
|
||||
{
|
||||
var statics = reader.ReadUShort();
|
||||
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
|
||||
{
|
||||
var entityType = reader.ReadUShort();
|
||||
var eventAuthority = (RagonAuthority)reader.ReadByte();
|
||||
var staticId = reader.ReadUShort();
|
||||
var propertiesCount = reader.ReadUShort();
|
||||
|
||||
var entity = new RagonEntity(player, entityType, staticId, 0, eventAuthority);
|
||||
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
|
||||
{
|
||||
var propertyType = reader.ReadBool();
|
||||
var propertySize = reader.ReadUShort();
|
||||
entity.State.AddProperty(new RagonProperty(propertySize, propertyType));
|
||||
}
|
||||
|
||||
var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}";
|
||||
var entityInfo = $"{entity.Id}:{entity.Type}";
|
||||
|
||||
_logger.Trace($"{playerInfo} created entity {entityInfo}");
|
||||
|
||||
room.AttachEntity(entity);
|
||||
player.AttachEntity(entity);
|
||||
}
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} loaded");
|
||||
|
||||
room.WaitPlayersList.Add(player);
|
||||
|
||||
foreach (var roomPlayer in room.WaitPlayersList)
|
||||
{
|
||||
DispatchPlayerJoinExcludePlayer(room, roomPlayer, writer);
|
||||
|
||||
roomPlayer.SetReady();
|
||||
}
|
||||
|
||||
room.UpdateReadyPlayerList();
|
||||
|
||||
DispatchSnapshot(room, room.WaitPlayersList, writer);
|
||||
|
||||
room.WaitPlayersList.Clear();
|
||||
}
|
||||
else if (owner.IsLoaded)
|
||||
{
|
||||
player.SetReady();
|
||||
|
||||
DispatchPlayerJoinExcludePlayer(room, player, writer);
|
||||
|
||||
room.UpdateReadyPlayerList();
|
||||
|
||||
DispatchSnapshot(room, new List<RagonRoomPlayer>() { player }, writer);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void DispatchPlayerJoinExcludePlayer(RagonRoom room, RagonRoomPlayer roomPlayer, RagonBuffer writer)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void DispatchSnapshot(RagonRoom room, List<RagonRoomPlayer> receviersList, RagonBuffer writer)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.SNAPSHOT);
|
||||
writer.WriteUShort((ushort)room.ReadyPlayersList.Count);
|
||||
foreach (var roomPlayer in room.ReadyPlayersList)
|
||||
{
|
||||
writer.WriteUShort(roomPlayer.Connection.Id);
|
||||
writer.WriteString(roomPlayer.Id);
|
||||
writer.WriteString(roomPlayer.Name);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user