🚧 plugin system, webhook system

This commit is contained in:
2023-04-09 10:52:18 +04:00
parent f2edc94958
commit bfd6c1b54b
60 changed files with 762 additions and 267 deletions
@@ -0,0 +1,26 @@
/*
* 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;
public interface IRoomPlugin
{
void Tick(float dt);
void OnAttached();
void OnDetached();
bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity);
bool OnEntityRemove(RagonRoomPlayer remover, RagonEntity entity);
}
@@ -0,0 +1,30 @@
/*
* 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.Lobby;
using Ragon.Server.Room;
namespace Ragon.Server.Plugin;
public interface IServerPlugin
{
bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room);
bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room);
bool OnRoomLeave(RagonRoomPlayer player, RagonRoom room);
bool OnRoomJoin(RagonRoomPlayer player, RagonRoom room);
IRoomPlugin CreateRoomPlugin(RoomInformation information);
}
@@ -0,0 +1,8 @@
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class AuthorizationRequest
{
public string Name;
public string Token;
}
@@ -0,0 +1,7 @@
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class RoomCreatedRequest
{
}
@@ -0,0 +1,6 @@
namespace Ragon.Server.Plugin.Web;
public class RoomJoinedRequest
{
}
@@ -0,0 +1,7 @@
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class RoomLeavedRequest
{
}
@@ -0,0 +1,7 @@
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class RoomRemovedRequest
{
}
@@ -0,0 +1,9 @@
namespace Ragon.Server.Plugin.Web;
[Serializable]
public class AuthorizationResponse
{
public string Id;
public string Name;
public string Payload;
}
@@ -0,0 +1,112 @@
using System.Net;
using System.Net.Http.Json;
using Newtonsoft.Json;
using Ragon.Protocol;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Room;
namespace Ragon.Server.Plugin;
public class WebHookPlugin
{
private Dictionary<string, string> _webHooks;
private RagonServer _server;
private HttpClient _httpClient;
public WebHookPlugin(RagonServer server, Configuration configuration)
{
_webHooks = new Dictionary<string, string>(configuration.WebHooks);
_httpClient = new HttpClient();
_server = server;
}
public bool RequestAuthorization(RagonContext context, string name, string password)
{
if (_webHooks.TryGetValue("authorization-request", out var value))
{
var httpContent = new StringContent("");
var executor = context.Executor;
executor.Run(async () =>
{
var authorizationOperation = (AuthorizationOperation) _server.Resolve(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(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)
{
if (_webHooks.TryGetValue("room-created", out var value) && !string.IsNullOrEmpty(value))
{
var request = new RoomCreatedRequest()
{
};
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 RoomCreatedRequest()
{
};
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 RoomCreatedRequest()
{
};
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 RoomCreatedRequest()
{
};
var content = JsonContent.Create(request);
var executor = context.Executor;
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
}
}
}