implement most of grids api methods

This commit is contained in:
zznty
2023-01-24 18:27:32 +07:00
parent 495d299c00
commit f66489ae9b
11 changed files with 165 additions and 12 deletions

View File

@@ -0,0 +1,121 @@
using System.Net;
using EmbedIO;
using EmbedIO.Routing;
using EmbedIO.WebApi;
using Sandbox.Game.Entities;
using Torch.API;
using TorchRemote.Models.Responses;
using TorchRemote.Plugin.Utils;
using VRageMath;
using Vector3 = TorchRemote.Models.Shared.Vector3;
namespace TorchRemote.Plugin.Controllers;
public class GridsController : WebApiController
{
private const string RootPath = "/grids";
[Route(HttpVerbs.Get, RootPath)]
public Task<IEnumerable<long>> GetAsync()
{
if (Statics.Torch.GameState is not TorchGameState.Loaded)
throw new HttpException(HttpStatusCode.ServiceUnavailable);
return Statics.Torch.InvokeAsync<IEnumerable<long>>(() =>
{
return MyCubeGridGroups.Static.Physical.Groups.SelectMany(b => b.Nodes.Select(n => n.NodeData.EntityId))
.ToArray();
});
}
[Route(HttpVerbs.Get, $"{RootPath}/{{id}}")]
public Task<GridInfo> GetAsync(long id)
{
if (Statics.Torch.GameState is not TorchGameState.Loaded)
throw new HttpException(HttpStatusCode.ServiceUnavailable);
return Statics.Torch.InvokeAsync(() =>
{
if (!MyEntities.TryGetEntityById(id, out MyCubeGrid grid))
throw HttpException.NotFound("Grid with given id does not exist", id);
return GetInfo(grid);
});
}
[Route(HttpVerbs.Post, $"{RootPath}/{{id}}/power")]
public Task PostPowerAsync(long id, [QueryField] bool powered)
{
if (Statics.Torch.GameState is not TorchGameState.Loaded)
throw new HttpException(HttpStatusCode.ServiceUnavailable);
return Statics.Torch.InvokeAsync(() =>
{
if (!MyEntities.TryGetEntityById(id, out MyCubeGrid grid))
throw HttpException.NotFound("Grid with given id does not exist", id);
if (grid.IsPowered != powered)
grid.SwitchPower();
});
}
[Route(HttpVerbs.Delete, $"{RootPath}/{{id}}")]
public Task DeleteAsync(long id)
{
if (Statics.Torch.GameState is not TorchGameState.Loaded)
throw new HttpException(HttpStatusCode.ServiceUnavailable);
return Statics.Torch.InvokeAsync(() =>
{
if (!MyEntities.TryGetEntityById(id, out MyCubeGrid grid))
throw HttpException.NotFound("Grid with given id does not exist", id);
grid.Close();
});
}
[Route(HttpVerbs.Delete, $"{RootPath}/{{id}}/group")]
public Task DeleteGroupAsync(long id)
{
if (Statics.Torch.GameState is not TorchGameState.Loaded)
throw new HttpException(HttpStatusCode.ServiceUnavailable);
return Statics.Torch.InvokeAsync(() =>
{
if (!MyEntities.TryGetEntityById(id, out MyCubeGrid grid))
throw HttpException.NotFound("Grid with given id does not exist", id);
foreach (var node in MyCubeGridGroups.Static.Physical.GetGroup(grid).Nodes.ToArray())
{
node.NodeData.Close();
}
});
}
[Route(HttpVerbs.Get, $"{RootPath}/{{id}}/group")]
public Task<IEnumerable<GridInfo>> GetGroupAsync(long id)
{
if (Statics.Torch.GameState is not TorchGameState.Loaded)
throw new HttpException(HttpStatusCode.ServiceUnavailable);
return Statics.Torch.InvokeAsync<IEnumerable<GridInfo>>(() =>
{
if (!MyEntities.TryGetEntityById(id, out MyCubeGrid grid))
throw HttpException.NotFound("Grid with given id does not exist", id);
return MyCubeGridGroups.Static.Physical.GetGroup(grid).Nodes.Select(n => GetInfo(n.NodeData)).ToArray();
});
}
private static GridInfo GetInfo(MyCubeGrid grid)
{
var matrixD = grid.PositionComp?.WorldMatrixRef ?? MatrixD.Zero;
return new GridInfo(grid.EntityId, grid.DisplayName,
new(matrixD.Translation.ToNumerics(), matrixD.Forward.ToNumerics(),
matrixD.Up.ToNumerics(), grid.Physics?.LinearVelocity.ToNumerics() ?? Vector3.Zero,
grid.Physics?.AngularVelocity.ToNumerics() ?? Vector3.Zero),
grid.BigOwners.Count > 0 ? grid.BigOwners[0] : null,
grid.BigOwners.Count + grid.SmallOwners.Count > 0 ? grid.BigOwners.Concat(grid.SmallOwners).ToArray() : null,
grid.BlocksCount, grid.BlocksPCU);
}
}

View File

@@ -65,7 +65,8 @@ public class ApiServerManager : Manager
.WithController<ChatController>()
.WithController<PluginsController>()
.WithController<PluginDownloadsController>()
.WithController<PlayersController>())
.WithController<PlayersController>()
.WithController<GridsController>())
.WithModule(new LogsModule("/api/live/logs", true))
.WithModule(chatModule);
}

View File

@@ -25,6 +25,10 @@
<PackageReference Include="JsonPointer.Net" Version="2.2.2" />
<PackageReference Include="JsonSchema.Net" Version="3.2.2" />
<PackageReference Include="JsonSchema.Net.Generation" Version="3.0.4" />
<PackageReference Include="PolySharp" Version="1.12.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PropertyChanged.Fody" Version="4.0.3" PrivateAssets="all" />
<PackageReference Include="Torch.Server.ReferenceAssemblies" Version="1.3.1.207-master" PrivateAssets="all" IncludeAssets="compile" />
<PackageReference Include="System.Text.Json" Version="7.0.0-rc.1.22426.10" />

View File

@@ -5,6 +5,7 @@ using Sandbox.Engine.Networking;
using Sandbox.Game;
using Sandbox.Game.World;
using TorchRemote.Models.Shared;
namespace TorchRemote.Plugin.Utils;
public static class Extensions
@@ -26,4 +27,10 @@ public static class Extensions
using var md5 = MD5.Create();
return new(md5.ComputeHash(Encoding.UTF8.GetBytes(s)));
}
public static Vector3 ToNumerics(this VRageMath.Vector3D vector3D) =>
new((float)vector3D.X, (float)vector3D.Y, (float)vector3D.Z);
public static Vector3 ToNumerics(this VRageMath.Vector3 vector3) =>
new(vector3.X, vector3.Y, vector3.Z);
}

View File

@@ -2,5 +2,5 @@
<PluginManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>Torch Remote</Name>
<Guid>284017F3-9682-4841-A544-EB04DB8CB9BA</Guid>
<Version>v1.0.7</Version>
<Version>v1.0.8</Version>
</PluginManifest>