implement most of grids api methods
This commit is contained in:
11
README.md
11
README.md
@@ -41,10 +41,11 @@
|
||||
- [x] invoke command (with direct response for optional delay)
|
||||
- [x] send message (custom color author channel)
|
||||
- [ ] grids
|
||||
- [ ] get all (grouped by physical factor)
|
||||
- [ ] get by entity id (basic info + counts of each terminal block subtype)
|
||||
- [ ] toggle power state by entity id
|
||||
- [ ] delete by entity id
|
||||
- [ ] delete the entire group by entity id of one grid
|
||||
- [x] get all
|
||||
- [x] get grids in group by id
|
||||
- [x] get by entity id (basic info)
|
||||
- [x] toggle power state by entity id
|
||||
- [x] delete by entity id
|
||||
- [x] delete the entire group by entity id of one grid
|
||||
- [ ] get block list
|
||||
- [ ] filtering: all, terminal, functional (is enabled, is working), type, subtype
|
||||
|
5
TorchRemote.Models/Responses/EntityWorldData.cs
Normal file
5
TorchRemote.Models/Responses/EntityWorldData.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using TorchRemote.Models.Shared;
|
||||
|
||||
namespace TorchRemote.Models.Responses;
|
||||
|
||||
public record EntityWorldData(Vector3 Position, Vector3 Forward, Vector3 Up, Vector3 LinearVelocity, Vector3 AngularVelocity);
|
9
TorchRemote.Models/Responses/GridInfo.cs
Normal file
9
TorchRemote.Models/Responses/GridInfo.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace TorchRemote.Models.Responses;
|
||||
|
||||
public record GridInfo(long Id,
|
||||
string Name,
|
||||
EntityWorldData WorldData,
|
||||
long? BiggestOwner,
|
||||
ICollection<long>? Owners,
|
||||
int BlockCount,
|
||||
int Pcu);
|
6
TorchRemote.Models/Shared/Vector3.cs
Normal file
6
TorchRemote.Models/Shared/Vector3.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace TorchRemote.Models.Shared;
|
||||
|
||||
public record Vector3(float X, float Y, float Z)
|
||||
{
|
||||
public static Vector3 Zero => new Vector3(0, 0, 0);
|
||||
}
|
@@ -14,6 +14,10 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Json.More.Net" Version="1.7.1" />
|
||||
<PackageReference Include="JsonSchema.Net" Version="3.2.1" />
|
||||
<PackageReference Include="PolySharp" Version="1.12.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Text.Json" Version="7.0.0-rc.1.22426.10" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
@@ -1,5 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
[assembly: InternalsVisibleTo("TorchRemote.Plugin")]
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace System.Runtime.CompilerServices;
|
||||
internal class IsExternalInit { }
|
121
TorchRemote.Plugin/Controllers/GridsController.cs
Normal file
121
TorchRemote.Plugin/Controllers/GridsController.cs
Normal 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);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
@@ -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" />
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -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>
|
Reference in New Issue
Block a user