command yay!

This commit is contained in:
rfi
2023-11-13 19:10:02 +07:00
parent 21856d353b
commit 6c5aca3c3a
12 changed files with 335 additions and 36 deletions

View File

@@ -13,6 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\AscNet.Common\AscNet.Common.csproj" />
<ProjectReference Include="..\AscNet.GameServer\AscNet.GameServer.csproj" />
<ProjectReference Include="..\AscNet.Logging\AscNet.Logging.csproj" />
</ItemGroup>

View File

@@ -0,0 +1,69 @@
using AscNet.Common.Database;
using AscNet.GameServer;
using AscNet.GameServer.Commands;
using AscNet.SDKServer.Models ;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace AscNet.SDKServer.Controllers
{
public class CommandController : IRegisterable
{
public static void Register(WebApplication app)
{
app.MapGet("/api/AscNet/commands", (HttpContext ctx) =>
{
List<object> commands = new();
foreach (var command in CommandFactory.commands.Keys)
{
Command? cmd = CommandFactory.CreateCommand(command, null!, Array.Empty<string>(), false);
if (cmd is not null)
commands.Add(new
{
name = command,
help = cmd.Help
});
}
return ctx.Response.WriteAsync(JsonConvert.SerializeObject(new { code = 0, msg = "OK", data = commands }));
});
app.MapPost("/api/AscNet/command/{command}", ([FromHeader] string authorization, [FromBody] ExecuteCommandBody body, string command, HttpContext ctx) =>
{
Player? player = Player.FromToken(authorization);
if (player is null)
{
ctx.Response.StatusCode = 401;
return ctx.Response.WriteAsync(JsonConvert.SerializeObject(new { msg = "Invalid token!" }));
}
Session? session = Server.Instance.SessionFromUID(player.PlayerData.Id);
if (session is null)
{
ctx.Response.StatusCode = 400;
return ctx.Response.WriteAsync(JsonConvert.SerializeObject(new { msg = "Player is offline!" }));
}
try
{
Command? cmd = CommandFactory.CreateCommand(command, session, body.Args);
if (cmd is null)
{
ctx.Response.StatusCode = 404;
return ctx.Response.WriteAsync(JsonConvert.SerializeObject(new { msg = "Command does not exists!" }));
}
cmd.Execute();
}
catch (Exception ex)
{
ctx.Response.StatusCode = 400;
return ctx.Response.WriteAsync(JsonConvert.SerializeObject(new { msg = "Execution error!", err = ex.InnerException?.Message ?? ex.Message }));
}
return ctx.Response.WriteAsync(JsonConvert.SerializeObject(new { msg = "OK" }));
});
}
}
}

View File

@@ -0,0 +1,11 @@
using Newtonsoft.Json;
namespace AscNet.SDKServer.Models
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class ExecuteCommandBody
{
[JsonProperty("args", NullValueHandling = NullValueHandling.Ignore)]
public string[] Args { get; set; }
}
}