refactoring: removed external dependencies from server, removed webhooks from server
This commit is contained in:
@@ -18,39 +18,68 @@ using Ragon.Server.IO;
|
||||
using Ragon.Server.Lobby;
|
||||
using Ragon.Server.Room;
|
||||
|
||||
namespace Ragon.Server.Plugin;
|
||||
|
||||
public class BaseServerPlugin: IServerPlugin
|
||||
namespace Ragon.Server.Plugin
|
||||
{
|
||||
public IRagonServer Server { get; protected set; }
|
||||
|
||||
public virtual void OnAttached(IRagonServer server)
|
||||
public class ConnectionRequest
|
||||
{
|
||||
Server = server;
|
||||
public int PeerID { get; set; }
|
||||
|
||||
public void Approve()
|
||||
{
|
||||
}
|
||||
|
||||
public void Reject()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnDetached()
|
||||
public class BaseServerPlugin : IServerPlugin
|
||||
{
|
||||
|
||||
}
|
||||
public IRagonServer Server { get; protected set; }
|
||||
|
||||
public virtual bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
public virtual void OnAttached(IRagonServer server)
|
||||
{
|
||||
Server = server;
|
||||
}
|
||||
|
||||
public virtual bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
public virtual void OnDetached()
|
||||
{
|
||||
|
||||
public virtual bool OnCommand(string command, string payload)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual IRoomPlugin CreateRoomPlugin(RoomInformation information)
|
||||
{
|
||||
return new BaseRoomPlugin();
|
||||
public virtual bool OnAuthorize(ConnectionRequest request)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool OnRoomJoined(RagonRoomPlayer player, RagonRoom room)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool OnRoomLeaved(RagonRoomPlayer player, RagonRoom room)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool OnCommand(string command, string payload)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual IRoomPlugin CreateRoomPlugin(RoomInformation information)
|
||||
{
|
||||
return new BaseRoomPlugin();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,18 +14,21 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using Ragon.Server.Http;
|
||||
using Ragon.Server.Lobby;
|
||||
using Ragon.Server.Room;
|
||||
|
||||
namespace Ragon.Server.Plugin;
|
||||
|
||||
public interface IServerPlugin
|
||||
namespace Ragon.Server.Plugin
|
||||
{
|
||||
void OnAttached(IRagonServer server);
|
||||
void OnDetached();
|
||||
bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room);
|
||||
bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room);
|
||||
bool OnCommand(string command, string payload);
|
||||
IRoomPlugin CreateRoomPlugin(RoomInformation information);
|
||||
public interface IServerPlugin
|
||||
{
|
||||
void OnAttached(IRagonServer server);
|
||||
void OnDetached();
|
||||
bool OnAuthorize(ConnectionRequest request);
|
||||
bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room);
|
||||
bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room);
|
||||
bool OnRoomJoined(RagonRoomPlayer player, RagonRoom room);
|
||||
bool OnRoomLeaved(RagonRoomPlayer player, RagonRoom room);
|
||||
bool OnCommand(string command, string payload);
|
||||
IRoomPlugin CreateRoomPlugin(RoomInformation information);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.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;
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* 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.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();
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023-2024 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 System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Ragon.Protocol;
|
||||
using Ragon.Server.Handler;
|
||||
using Ragon.Server.Lobby;
|
||||
using Ragon.Server.Room;
|
||||
|
||||
namespace Ragon.Server.Plugin.Web;
|
||||
|
||||
public class RagonWebHookPlugin
|
||||
{
|
||||
private Dictionary<string, string> _webHooks;
|
||||
|
||||
private IRagonServer _server;
|
||||
private HttpClient _httpClient;
|
||||
|
||||
public RagonWebHookPlugin(IRagonServer server, RagonServerConfiguration configuration)
|
||||
{
|
||||
_webHooks = new Dictionary<string, string>(configuration.WebHooks);
|
||||
_httpClient = new HttpClient();
|
||||
_server = server;
|
||||
}
|
||||
|
||||
public bool RequestAuthorization(RagonContext context, string payload)
|
||||
{
|
||||
if (_webHooks.TryGetValue("authorization-request", out var value))
|
||||
{
|
||||
var httpContent = new StringContent(payload, Encoding.UTF8, "application/json");
|
||||
var executor = context.Executor;
|
||||
executor.Run(async () =>
|
||||
{
|
||||
var authorizationOperation = (AuthorizationOperation) _server.ResolveHandler(RagonOperation.AUTHORIZE);
|
||||
var response = await _httpClient.PostAsync(new Uri(value), httpContent);
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
authorizationOperation.Reject(context);
|
||||
return;
|
||||
}
|
||||
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var authorizationResponse = JsonConvert.DeserializeObject<AuthorizationResponse>(content);
|
||||
if (authorizationResponse != null)
|
||||
{
|
||||
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, authorizationResponse.Id, authorizationResponse.Name, authorizationResponse.Payload);
|
||||
|
||||
context.SetPlayer(lobbyPlayer);
|
||||
authorizationOperation.Approve(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
authorizationOperation.Reject(context);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
|
||||
}
|
||||
}
|
||||
|
||||
public void RoomRemoved(RagonContext context, RagonRoom ragonRoom)
|
||||
{
|
||||
if (_webHooks.TryGetValue("room-removed", out var value) && !string.IsNullOrEmpty(value))
|
||||
{
|
||||
var request = new RoomRemovedRequest()
|
||||
{
|
||||
Room = new RoomDto(ragonRoom)
|
||||
};
|
||||
var content = JsonContent.Create(request);
|
||||
var executor = context.Executor;
|
||||
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
|
||||
}
|
||||
}
|
||||
|
||||
public void RoomJoined(RagonContext context, RagonRoom existsRoom, RagonRoomPlayer player)
|
||||
{
|
||||
if (_webHooks.TryGetValue("room-joined", out var value) && !string.IsNullOrEmpty(value))
|
||||
{
|
||||
var request = new RoomJoinedRequest()
|
||||
{
|
||||
Room = new RoomDto(existsRoom),
|
||||
Player = new PlayerDto(player)
|
||||
};
|
||||
var content = JsonContent.Create(request);
|
||||
var executor = context.Executor;
|
||||
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
|
||||
}
|
||||
}
|
||||
|
||||
public void RoomLeaved(RagonContext context, RagonRoom room, RagonRoomPlayer roomPlayer)
|
||||
{
|
||||
if (_webHooks.TryGetValue("room-leaved", out var value) && !string.IsNullOrEmpty(value))
|
||||
{
|
||||
var request = new RoomLeavedRequest()
|
||||
{
|
||||
Room = new RoomDto(room),
|
||||
Player = new PlayerDto(roomPlayer)
|
||||
};
|
||||
var content = JsonContent.Create(request);
|
||||
var executor = context.Executor;
|
||||
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Server.Plugin.Web;
|
||||
|
||||
[Serializable]
|
||||
public class AuthorizationRequest
|
||||
{
|
||||
public string Name;
|
||||
public string Token;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Server.Plugin.Web;
|
||||
|
||||
[Serializable]
|
||||
public class RoomCreatedRequest
|
||||
{
|
||||
public RoomDto Room { get; set; }
|
||||
public PlayerDto Player { get; set; }
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Server.Plugin.Web;
|
||||
|
||||
public class RoomJoinedRequest
|
||||
{
|
||||
public RoomDto Room { get; set; }
|
||||
public PlayerDto Player { get; set; }
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Server.Plugin.Web;
|
||||
|
||||
[Serializable]
|
||||
public class RoomLeavedRequest
|
||||
{
|
||||
public RoomDto Room { get; set; }
|
||||
public PlayerDto Player { get; set; }
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
namespace Ragon.Server.Plugin.Web;
|
||||
|
||||
[Serializable]
|
||||
public class RoomRemovedRequest
|
||||
{
|
||||
public RoomDto Room { get; set; }
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Server.Plugin.Web;
|
||||
|
||||
[Serializable]
|
||||
public class AuthorizationResponse
|
||||
{
|
||||
public string Id;
|
||||
public string Name;
|
||||
public string Payload;
|
||||
}
|
||||
Reference in New Issue
Block a user