http-commands

This commit is contained in:
2023-04-13 20:42:05 +04:00
parent 24c9aa2043
commit e1a9ad476c
31 changed files with 428 additions and 104 deletions
@@ -0,0 +1,53 @@
using Ragon.Server.Entity;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Room;
using Ragon.Server.Time;
namespace Ragon.Server.Plugin;
public class BaseRoomPlugin: IRoomPlugin
{
private IRagonRoom _ragonRoom;
public RagonRoomPlayer GetPlayerById(string id)
{
var player = _ragonRoom.GetPlayerById(id);
return player;
}
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection)
{
var player = _ragonRoom.GetPlayerByConnection(connection);
return player;
}
public virtual void OnAttached(IRagonRoom room)
{
_ragonRoom = room;
}
public virtual void OnDetached()
{
}
#region VIRTUAL
public virtual void Tick(float dt)
{
}
public virtual bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity)
{
return true;
}
public virtual bool OnEntityRemove(RagonRoomPlayer remover, RagonEntity entity)
{
return true;
}
#endregion
}
@@ -0,0 +1,62 @@
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Room;
namespace Ragon.Server.Plugin;
public class BaseServerPlugin: IServerPlugin
{
private IRagonServer _ragonServer;
public RagonLobbyPlayer? GetPlayerById(string id)
{
var context = _ragonServer.ResolveContext(id);
return context?.LobbyPlayer;
}
public RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection)
{
var context = _ragonServer.ResolveContext(connection);
return context?.LobbyPlayer;
}
public void OnAttached(IRagonServer server)
{
_ragonServer = server;
}
public void OnDetached()
{
}
public virtual bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room)
{
return true;
}
public virtual bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room)
{
return true;
}
public virtual bool OnRoomLeave(RagonRoomPlayer player, RagonRoom room)
{
return true;
}
public virtual bool OnRoomJoin(RagonRoomPlayer player, RagonRoom room)
{
return true;
}
public virtual bool OnCommand(string command, string payload)
{
return true;
}
public IRoomPlugin CreateRoomPlugin(RoomInformation information)
{
return new BaseRoomPlugin();
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ namespace Ragon.Server.Plugin;
public interface IRoomPlugin
{
void Tick(float dt);
void OnAttached();
void OnAttached(IRagonRoom room);
void OnDetached();
bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity);
bool OnEntityRemove(RagonRoomPlayer remover, RagonEntity entity);
+4 -1
View File
@@ -14,6 +14,7 @@
* limitations under the License.
*/
using Ragon.Server.Http;
using Ragon.Server.Lobby;
using Ragon.Server.Room;
@@ -21,10 +22,12 @@ namespace Ragon.Server.Plugin;
public interface IServerPlugin
{
void OnAttached(IRagonServer server);
void OnDetached();
bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room);
bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room);
bool OnRoomLeave(RagonRoomPlayer player, RagonRoom room);
bool OnRoomJoin(RagonRoomPlayer player, RagonRoom room);
bool OnCommand(string command, string payload);
IRoomPlugin CreateRoomPlugin(RoomInformation information);
}
@@ -0,0 +1,16 @@
using Ragon.Server.Room;
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class PlayerDto
{
public string Id { get; set;}
public string Name { get; set; }
public PlayerDto(RagonRoomPlayer ragonRoomPlayer)
{
Id = ragonRoomPlayer.Id;
Name = ragonRoomPlayer.Name;
}
}
@@ -0,0 +1,23 @@
using Ragon.Server.Room;
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class RoomDto
{
public string Id { get; set;}
public int PlayerMin { get; set; }
public int PlayerMax { get; set; }
public int PlayerCount { get; set; }
public PlayerDto[] Players { get; set; }
public RoomDto(RagonRoom room)
{
Id = room.Id;
PlayerMin = room.PlayerMin;
PlayerMax = room.PlayerMax;
PlayerCount = room.PlayerCount;
Players = room.PlayerList.Select(p => new PlayerDto(p)).ToArray();
}
}
@@ -8,14 +8,14 @@ using Ragon.Server.Room;
namespace Ragon.Server.Plugin.Web;
public class WebHookPlugin
public class RagonWebHookPlugin
{
private Dictionary<string, string> _webHooks;
private RagonServer _server;
private HttpClient _httpClient;
public WebHookPlugin(RagonServer server, Configuration configuration)
public RagonWebHookPlugin(RagonServer server, Configuration configuration)
{
_webHooks = new Dictionary<string, string>(configuration.WebHooks);
_httpClient = new HttpClient();
@@ -30,7 +30,7 @@ public class WebHookPlugin
var executor = context.Executor;
executor.Run(async () =>
{
var authorizationOperation = (AuthorizationOperation) _server.Resolve(RagonOperation.AUTHORIZE);
var authorizationOperation = (AuthorizationOperation) _server.ResolveOperation(RagonOperation.AUTHORIZE);
var response = await _httpClient.PostAsync(new Uri(value), httpContent);
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -42,7 +42,7 @@ public class WebHookPlugin
var authorizationResponse = JsonConvert.DeserializeObject<AuthorizationResponse>(content);
if (authorizationResponse != null)
{
var lobbyPlayer = new RagonLobbyPlayer(authorizationResponse.Id, authorizationResponse.Name, authorizationResponse.Payload);
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, authorizationResponse.Id, authorizationResponse.Name, authorizationResponse.Payload);
context.SetPlayer(lobbyPlayer);
authorizationOperation.Approve(context);
@@ -58,12 +58,14 @@ public class WebHookPlugin
return false;
}
public void RoomCreated(RagonContext context, RagonRoom room)
public void RoomCreated(RagonContext context, RagonRoom room, RagonRoomPlayer player)
{
if (_webHooks.TryGetValue("room-created", out var value) && !string.IsNullOrEmpty(value))
{
var request = new RoomCreatedRequest()
{
Room = new RoomDto(room),
Player = new PlayerDto(player)
};
var content = JsonContent.Create(request);
var executor = context.Executor;
@@ -75,8 +77,9 @@ public class WebHookPlugin
{
if (_webHooks.TryGetValue("room-removed", out var value) && !string.IsNullOrEmpty(value))
{
var request = new RoomCreatedRequest()
var request = new RoomRemovedRequest()
{
Room = new RoomDto(ragonRoom)
};
var content = JsonContent.Create(request);
var executor = context.Executor;
@@ -88,8 +91,10 @@ public class WebHookPlugin
{
if (_webHooks.TryGetValue("room-joined", out var value) && !string.IsNullOrEmpty(value))
{
var request = new RoomCreatedRequest()
var request = new RoomJoinedRequest()
{
Room = new RoomDto(existsRoom),
Player = new PlayerDto(player)
};
var content = JsonContent.Create(request);
var executor = context.Executor;
@@ -101,8 +106,10 @@ public class WebHookPlugin
{
if (_webHooks.TryGetValue("room-leaved", out var value) && !string.IsNullOrEmpty(value))
{
var request = new RoomCreatedRequest()
var request = new RoomLeavedRequest()
{
Room = new RoomDto(room),
Player = new PlayerDto(roomPlayer)
};
var content = JsonContent.Create(request);
var executor = context.Executor;
@@ -1,7 +1,9 @@
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class RoomCreatedRequest
{
public RoomDto Room { get; set; }
public PlayerDto Player { get; set; }
}
@@ -2,5 +2,6 @@ namespace Ragon.Server.Plugin.Web;
public class RoomJoinedRequest
{
}
public RoomDto Room { get; set; }
public PlayerDto Player { get; set; }
}
@@ -3,5 +3,6 @@ namespace Ragon.Server.Plugin.Web;
[Serializable]
public class RoomLeavedRequest
{
public RoomDto Room { get; set; }
public PlayerDto Player { get; set; }
}
@@ -1,7 +1,9 @@
using Ragon.Server.Plugin.Web;
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class RoomRemovedRequest
{
public RoomDto Room { get; set; }
}