mirror of
https://github.com/CyberL1/Minecraft-Realms-Emulator.git
synced 2024-11-23 22:58:21 -05:00
feat: error class, world checking middleware
This commit is contained in:
parent
4a27cc028e
commit
26fa8813dd
@ -0,0 +1,13 @@
|
|||||||
|
using Minecraft_Realms_Emulator.Entities;
|
||||||
|
|
||||||
|
namespace Minecraft_Realms_Emulator.Attributes
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public class CheckForWorldAttribute : Attribute
|
||||||
|
{
|
||||||
|
public bool WorldExists(World world)
|
||||||
|
{
|
||||||
|
return world != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -27,6 +27,7 @@ namespace Minecraft_Realms_Emulator.Controllers.Admin
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{wId}")]
|
[HttpGet("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
public ActionResult<World> GetWorld(int wId) {
|
public ActionResult<World> GetWorld(int wId) {
|
||||||
var world = _context.Worlds.ToList().Find(w => w.Id == wId);
|
var world = _context.Worlds.ToList().Find(w => w.Id == wId);
|
||||||
|
|
||||||
@ -59,12 +60,10 @@ namespace Minecraft_Realms_Emulator.Controllers.Admin
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{wId}/open")]
|
[HttpPut("{wId}/open")]
|
||||||
|
[CheckForWorld]
|
||||||
public ActionResult<bool> OpenServer(int wId)
|
public ActionResult<bool> OpenServer(int wId)
|
||||||
{
|
{
|
||||||
var world = _context.Worlds.ToList().Find(w => w.Id == wId);
|
var world = _context.Worlds.ToList().Find(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return BadRequest("World not found");
|
|
||||||
|
|
||||||
world.State = "OPEN";
|
world.State = "OPEN";
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
|
|
||||||
@ -74,12 +73,11 @@ namespace Minecraft_Realms_Emulator.Controllers.Admin
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{wId}/close")]
|
[HttpPut("{wId}/close")]
|
||||||
|
[CheckForWorld]
|
||||||
public ActionResult<bool> CloseServer(int wId)
|
public ActionResult<bool> CloseServer(int wId)
|
||||||
{
|
{
|
||||||
var world = _context.Worlds.ToList().Find(w => w.Id == wId);
|
var world = _context.Worlds.ToList().Find(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return BadRequest("World not found");
|
|
||||||
|
|
||||||
world.State = "CLOSED";
|
world.State = "CLOSED";
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
|
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
using Minecraft_Realms_Emulator.Attributes;
|
||||||
|
using Minecraft_Realms_Emulator.Data;
|
||||||
|
using Minecraft_Realms_Emulator.Entities;
|
||||||
|
using Minecraft_Realms_Emulator.Responses;
|
||||||
|
|
||||||
|
namespace Minecraft_Realms_Emulator.Middlewares
|
||||||
|
{
|
||||||
|
public class CheckForWorldMiddleware(RequestDelegate next)
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next = next;
|
||||||
|
|
||||||
|
public async Task Invoke(HttpContext httpContext, DataContext db)
|
||||||
|
{
|
||||||
|
var endpoint = httpContext.GetEndpoint();
|
||||||
|
var attribute = endpoint?.Metadata.GetMetadata<CheckForWorldAttribute>();
|
||||||
|
|
||||||
|
if (attribute == null)
|
||||||
|
{
|
||||||
|
await _next(httpContext);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
World world = db.Worlds.FirstOrDefault(w => w.Id == int.Parse(httpContext.Request.RouteValues["wId"].ToString()));
|
||||||
|
|
||||||
|
if (!attribute.WorldExists(world))
|
||||||
|
{
|
||||||
|
ErrorResponse errorResponse = new()
|
||||||
|
{
|
||||||
|
ErrorCode = 404,
|
||||||
|
ErrorMsg = "World not found"
|
||||||
|
};
|
||||||
|
|
||||||
|
httpContext.Response.StatusCode = 404;
|
||||||
|
await httpContext.Response.WriteAsJsonAsync(errorResponse);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _next(httpContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -94,6 +94,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}")]
|
[HttpPost("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<World>> InvitePlayer(int wId, PlayerRequest body)
|
public async Task<ActionResult<World>> InvitePlayer(int wId, PlayerRequest body)
|
||||||
{
|
{
|
||||||
@ -104,8 +105,6 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
|
|
||||||
var world = await _context.Worlds.Include(w => w.Players).FirstOrDefaultAsync(w => w.Id == wId);
|
var world = await _context.Worlds.Include(w => w.Players).FirstOrDefaultAsync(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
// Get player UUID
|
// Get player UUID
|
||||||
var playerInfo = await new HttpClient().GetFromJsonAsync<MinecraftPlayerInfo>($"https://api.mojang.com/users/profiles/minecraft/{body.Name}");
|
var playerInfo = await new HttpClient().GetFromJsonAsync<MinecraftPlayerInfo>($"https://api.mojang.com/users/profiles/minecraft/{body.Name}");
|
||||||
|
|
||||||
@ -138,13 +137,12 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{wId}/invite/{uuid}")]
|
[HttpDelete("{wId}/invite/{uuid}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<bool>> DeleteInvite(int wId, string uuid)
|
public async Task<ActionResult<bool>> DeleteInvite(int wId, string uuid)
|
||||||
{
|
{
|
||||||
var world = await _context.Worlds.FirstOrDefaultAsync(w => w.Id == wId);
|
var world = await _context.Worlds.FirstOrDefaultAsync(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
var player = _context.Players.Where(p => p.World.Id == wId).FirstOrDefault(p => p.Uuid == uuid);
|
var player = _context.Players.Where(p => p.World.Id == wId).FirstOrDefault(p => p.Uuid == uuid);
|
||||||
|
|
||||||
_context.Players.Remove(player);
|
_context.Players.Remove(player);
|
||||||
@ -159,6 +157,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{wId}")]
|
[HttpDelete("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
public async Task<ActionResult<bool>> LeaveWorld(int wId)
|
public async Task<ActionResult<bool>> LeaveWorld(int wId)
|
||||||
{
|
{
|
||||||
string cookie = Request.Headers.Cookie;
|
string cookie = Request.Headers.Cookie;
|
||||||
@ -166,8 +165,6 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
|
|
||||||
var world = await _context.Worlds.FirstOrDefaultAsync(w => w.Id == wId);
|
var world = await _context.Worlds.FirstOrDefaultAsync(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
var player = _context.Players.Where(p => p.World.Id == wId).FirstOrDefault(p => p.Uuid == playerUUID);
|
var player = _context.Players.Where(p => p.World.Id == wId).FirstOrDefault(p => p.Uuid == playerUUID);
|
||||||
|
|
||||||
_context.Players.Remove(player);
|
_context.Players.Remove(player);
|
||||||
|
@ -18,6 +18,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}/{uuid}")]
|
[HttpPost("{wId}/{uuid}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public ActionResult<OpsResponse> OpPlayer(int wId, string uuid)
|
public ActionResult<OpsResponse> OpPlayer(int wId, string uuid)
|
||||||
{
|
{
|
||||||
@ -47,6 +48,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{wId}/{uuid}")]
|
[HttpDelete("{wId}/{uuid}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public ActionResult<OpsResponse> DeopPlayer(int wId, string uuid)
|
public ActionResult<OpsResponse> DeopPlayer(int wId, string uuid)
|
||||||
{
|
{
|
||||||
|
@ -18,6 +18,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
[HttpGet("{wId}")]
|
[HttpGet("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<SubscriptionResponse>> Get(int wId)
|
public async Task<ActionResult<SubscriptionResponse>> Get(int wId)
|
||||||
{
|
{
|
||||||
|
@ -141,6 +141,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{wId}")]
|
[HttpGet("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<WorldResponse>> GetWorldById(int wId)
|
public async Task<ActionResult<WorldResponse>> GetWorldById(int wId)
|
||||||
{
|
{
|
||||||
@ -149,8 +150,6 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
|
|
||||||
var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).Include(w => w.Slots).FirstOrDefaultAsync(w => w.Id == wId);
|
var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).Include(w => w.Slots).FirstOrDefaultAsync(w => w.Id == wId);
|
||||||
|
|
||||||
if (world?.Subscription == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
|
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
|
||||||
|
|
||||||
List<SlotResponse> slots = [];
|
List<SlotResponse> slots = [];
|
||||||
@ -211,6 +210,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}/initialize")]
|
[HttpPost("{wId}/initialize")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<World>> Initialize(int wId, WorldCreateRequest body)
|
public async Task<ActionResult<World>> Initialize(int wId, WorldCreateRequest body)
|
||||||
{
|
{
|
||||||
@ -221,8 +221,16 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
|
|
||||||
var world = worlds.Find(w => w.Id == wId);
|
var world = worlds.Find(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
if (world.State != nameof(StateEnum.UNINITIALIZED))
|
||||||
if (world.State != nameof(StateEnum.UNINITIALIZED)) return NotFound("World already initialized");
|
{
|
||||||
|
ErrorResponse errorResponse = new()
|
||||||
|
{
|
||||||
|
ErrorCode = 401,
|
||||||
|
ErrorMsg = "World already initialized",
|
||||||
|
};
|
||||||
|
|
||||||
|
return StatusCode(401, errorResponse);
|
||||||
|
}
|
||||||
|
|
||||||
var subscription = new Subscription
|
var subscription = new Subscription
|
||||||
{
|
{
|
||||||
@ -273,6 +281,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}/reset")]
|
[HttpPost("{wId}/reset")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public ActionResult<bool> Reset(int wId)
|
public ActionResult<bool> Reset(int wId)
|
||||||
{
|
{
|
||||||
@ -281,6 +290,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{wId}/open")]
|
[HttpPut("{wId}/open")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<bool>> Open(int wId)
|
public async Task<ActionResult<bool>> Open(int wId)
|
||||||
{
|
{
|
||||||
@ -288,8 +298,6 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
|
|
||||||
var world = worlds.Find(w => w.Id == wId);
|
var world = worlds.Find(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
world.State = nameof(StateEnum.OPEN);
|
world.State = nameof(StateEnum.OPEN);
|
||||||
|
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
@ -298,6 +306,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{wId}/close")]
|
[HttpPut("{wId}/close")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<bool>> Close(int wId)
|
public async Task<ActionResult<bool>> Close(int wId)
|
||||||
{
|
{
|
||||||
@ -305,8 +314,6 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
|
|
||||||
var world = worlds.FirstOrDefault(w => w.Id == wId);
|
var world = worlds.FirstOrDefault(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
world.State = nameof(StateEnum.CLOSED);
|
world.State = nameof(StateEnum.CLOSED);
|
||||||
|
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
@ -315,6 +322,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}")]
|
[HttpPost("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<bool>> UpdateWorld(int wId, WorldCreateRequest body)
|
public async Task<ActionResult<bool>> UpdateWorld(int wId, WorldCreateRequest body)
|
||||||
{
|
{
|
||||||
@ -322,8 +330,6 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
|
|
||||||
var world = worlds.Find(w => w.Id == wId);
|
var world = worlds.Find(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
world.Name = body.Name;
|
world.Name = body.Name;
|
||||||
world.Motd = body.Description;
|
world.Motd = body.Description;
|
||||||
|
|
||||||
@ -333,6 +339,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}/slot/{sId}")]
|
[HttpPost("{wId}/slot/{sId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<bool>> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body)
|
public async Task<ActionResult<bool>> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body)
|
||||||
{
|
{
|
||||||
@ -356,13 +363,12 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{wId}/slot/{sId}")]
|
[HttpPut("{wId}/slot/{sId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public ActionResult<bool> SwitchSlot(int wId, int sId)
|
public ActionResult<bool> SwitchSlot(int wId, int sId)
|
||||||
{
|
{
|
||||||
var world = _context.Worlds.Find(wId);
|
var world = _context.Worlds.Find(wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
var slot = _context.Slots.Where(s => s.World.Id == wId).Where(s => s.SlotId == sId).Any();
|
var slot = _context.Slots.Where(s => s.World.Id == wId).Where(s => s.SlotId == sId).Any();
|
||||||
|
|
||||||
if (!slot)
|
if (!slot)
|
||||||
@ -397,6 +403,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{wId}/backups")]
|
[HttpGet("{wId}/backups")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<BackupsResponse>> GetBackups(int wId)
|
public async Task<ActionResult<BackupsResponse>> GetBackups(int wId)
|
||||||
{
|
{
|
||||||
@ -419,13 +426,12 @@ namespace Minecraft_Realms_Emulator.Modes.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{wId}")]
|
[HttpDelete("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public ActionResult<bool> DeleteRealm(int wId)
|
public ActionResult<bool> DeleteRealm(int wId)
|
||||||
{
|
{
|
||||||
var world = _context.Worlds.Find(wId);
|
var world = _context.Worlds.Find(wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
_context.Worlds.Remove(world);
|
_context.Worlds.Remove(world);
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
|
|
||||||
|
@ -94,6 +94,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}")]
|
[HttpPost("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public async Task<ActionResult<World>> InvitePlayer(int wId, PlayerRequest body)
|
public async Task<ActionResult<World>> InvitePlayer(int wId, PlayerRequest body)
|
||||||
@ -139,6 +140,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{wId}/invite/{uuid}")]
|
[HttpDelete("{wId}/invite/{uuid}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public async Task<ActionResult<bool>> DeleteInvite(int wId, string uuid)
|
public async Task<ActionResult<bool>> DeleteInvite(int wId, string uuid)
|
||||||
@ -161,6 +163,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{wId}")]
|
[HttpDelete("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
public async Task<ActionResult<bool>> LeaveWorld(int wId)
|
public async Task<ActionResult<bool>> LeaveWorld(int wId)
|
||||||
{
|
{
|
||||||
string cookie = Request.Headers.Cookie;
|
string cookie = Request.Headers.Cookie;
|
||||||
|
@ -18,6 +18,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}/{uuid}")]
|
[HttpPost("{wId}/{uuid}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public ActionResult<OpsResponse> OpPlayer(int wId, string uuid)
|
public ActionResult<OpsResponse> OpPlayer(int wId, string uuid)
|
||||||
@ -48,6 +49,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{wId}/{uuid}")]
|
[HttpDelete("{wId}/{uuid}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public ActionResult<OpsResponse> DeopPlayer(int wId, string uuid)
|
public ActionResult<OpsResponse> DeopPlayer(int wId, string uuid)
|
||||||
|
@ -18,6 +18,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
[HttpGet("{wId}")]
|
[HttpGet("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<SubscriptionResponse>> Get(int wId)
|
public async Task<ActionResult<SubscriptionResponse>> Get(int wId)
|
||||||
{
|
{
|
||||||
|
@ -144,6 +144,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{wId}")]
|
[HttpGet("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<WorldResponse>> GetWorldById(int wId)
|
public async Task<ActionResult<WorldResponse>> GetWorldById(int wId)
|
||||||
{
|
{
|
||||||
@ -152,8 +153,6 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
|
|
||||||
var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).Include(w => w.Slots).FirstOrDefaultAsync(w => w.Id == wId);
|
var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).Include(w => w.Slots).FirstOrDefaultAsync(w => w.Id == wId);
|
||||||
|
|
||||||
if (world?.Subscription == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
|
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
|
||||||
|
|
||||||
List<SlotResponse> slots = [];
|
List<SlotResponse> slots = [];
|
||||||
@ -214,6 +213,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}/initialize")]
|
[HttpPost("{wId}/initialize")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<World>> Initialize(int wId, WorldCreateRequest body)
|
public async Task<ActionResult<World>> Initialize(int wId, WorldCreateRequest body)
|
||||||
{
|
{
|
||||||
@ -223,9 +223,16 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
var worlds = await _context.Worlds.ToListAsync();
|
var worlds = await _context.Worlds.ToListAsync();
|
||||||
|
|
||||||
var world = worlds.Find(w => w.Id == wId);
|
var world = worlds.Find(w => w.Id == wId);
|
||||||
|
if (world.State != nameof(StateEnum.UNINITIALIZED))
|
||||||
|
{
|
||||||
|
ErrorResponse errorResponse = new()
|
||||||
|
{
|
||||||
|
ErrorCode = 401,
|
||||||
|
ErrorMsg = "World already initialized",
|
||||||
|
};
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
return StatusCode(401, errorResponse);
|
||||||
if (world.State != nameof(StateEnum.UNINITIALIZED)) return NotFound("World already initialized");
|
}
|
||||||
|
|
||||||
var subscription = new Subscription
|
var subscription = new Subscription
|
||||||
{
|
{
|
||||||
@ -289,6 +296,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}/reset")]
|
[HttpPost("{wId}/reset")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public ActionResult<bool> Reset(int wId)
|
public ActionResult<bool> Reset(int wId)
|
||||||
@ -298,6 +306,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{wId}/open")]
|
[HttpPut("{wId}/open")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public async Task<ActionResult<bool>> Open(int wId)
|
public async Task<ActionResult<bool>> Open(int wId)
|
||||||
@ -306,8 +315,6 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
|
|
||||||
var world = worlds.Find(w => w.Id == wId);
|
var world = worlds.Find(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
new DockerHelper(world).StartServer();
|
new DockerHelper(world).StartServer();
|
||||||
|
|
||||||
world.State = nameof(StateEnum.OPEN);
|
world.State = nameof(StateEnum.OPEN);
|
||||||
@ -318,6 +325,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{wId}/close")]
|
[HttpPut("{wId}/close")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public async Task<ActionResult<bool>> Close(int wId)
|
public async Task<ActionResult<bool>> Close(int wId)
|
||||||
@ -326,8 +334,6 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
|
|
||||||
var world = worlds.FirstOrDefault(w => w.Id == wId);
|
var world = worlds.FirstOrDefault(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
new DockerHelper(world).StopServer();
|
new DockerHelper(world).StopServer();
|
||||||
|
|
||||||
world.State = nameof(StateEnum.CLOSED);
|
world.State = nameof(StateEnum.CLOSED);
|
||||||
@ -338,6 +344,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}")]
|
[HttpPost("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public async Task<ActionResult<bool>> UpdateWorld(int wId, WorldCreateRequest body)
|
public async Task<ActionResult<bool>> UpdateWorld(int wId, WorldCreateRequest body)
|
||||||
@ -346,8 +353,6 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
|
|
||||||
var world = worlds.Find(w => w.Id == wId);
|
var world = worlds.Find(w => w.Id == wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
world.Name = body.Name;
|
world.Name = body.Name;
|
||||||
world.Motd = body.Description;
|
world.Motd = body.Description;
|
||||||
|
|
||||||
@ -357,6 +362,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("{wId}/slot/{sId}")]
|
[HttpPost("{wId}/slot/{sId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public async Task<ActionResult<bool>> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body)
|
public async Task<ActionResult<bool>> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body)
|
||||||
@ -381,14 +387,13 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{wId}/slot/{sId}")]
|
[HttpPut("{wId}/slot/{sId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
[CheckActiveSubscription]
|
[CheckActiveSubscription]
|
||||||
public ActionResult<bool> SwitchSlot(int wId, int sId)
|
public ActionResult<bool> SwitchSlot(int wId, int sId)
|
||||||
{
|
{
|
||||||
var world = _context.Worlds.Find(wId);
|
var world = _context.Worlds.Find(wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
var slot = _context.Slots.Where(s => s.World.Id == wId).Where(s => s.SlotId == sId).Any();
|
var slot = _context.Slots.Where(s => s.World.Id == wId).Where(s => s.SlotId == sId).Any();
|
||||||
|
|
||||||
if (!slot)
|
if (!slot)
|
||||||
@ -423,6 +428,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{wId}/backups")]
|
[HttpGet("{wId}/backups")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public async Task<ActionResult<BackupsResponse>> GetBackups(int wId)
|
public async Task<ActionResult<BackupsResponse>> GetBackups(int wId)
|
||||||
{
|
{
|
||||||
@ -445,13 +451,12 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{wId}")]
|
[HttpDelete("{wId}")]
|
||||||
|
[CheckForWorld]
|
||||||
[CheckRealmOwner]
|
[CheckRealmOwner]
|
||||||
public ActionResult<bool> DeleteRealm(int wId)
|
public ActionResult<bool> DeleteRealm(int wId)
|
||||||
{
|
{
|
||||||
var world = _context.Worlds.Find(wId);
|
var world = _context.Worlds.Find(wId);
|
||||||
|
|
||||||
if (world == null) return NotFound("World not found");
|
|
||||||
|
|
||||||
new DockerHelper(world).DeleteServer();
|
new DockerHelper(world).DeleteServer();
|
||||||
|
|
||||||
_context.Worlds.Remove(world);
|
_context.Worlds.Remove(world);
|
||||||
|
@ -4,7 +4,6 @@ using Minecraft_Realms_Emulator.Data;
|
|||||||
using Minecraft_Realms_Emulator.Enums;
|
using Minecraft_Realms_Emulator.Enums;
|
||||||
using Minecraft_Realms_Emulator.Helpers;
|
using Minecraft_Realms_Emulator.Helpers;
|
||||||
using Minecraft_Realms_Emulator.Middlewares;
|
using Minecraft_Realms_Emulator.Middlewares;
|
||||||
using Minecraft_Realms_Emulator.Modes.Realms.Helpers;
|
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@ -125,6 +124,7 @@ app.UseMiddleware<MinecraftCookieMiddleware>();
|
|||||||
app.UseMiddleware<CheckRealmOwnerMiddleware>();
|
app.UseMiddleware<CheckRealmOwnerMiddleware>();
|
||||||
app.UseMiddleware<ActiveSubscriptionMiddleware>();
|
app.UseMiddleware<ActiveSubscriptionMiddleware>();
|
||||||
app.UseMiddleware<AdminKeyMiddleware>();
|
app.UseMiddleware<AdminKeyMiddleware>();
|
||||||
|
app.UseMiddleware<CheckForWorldMiddleware>();
|
||||||
|
|
||||||
Console.WriteLine($"Running in {mode.Value} mode");
|
Console.WriteLine($"Running in {mode.Value} mode");
|
||||||
app.Run();
|
app.Run();
|
||||||
|
8
Minecraft-Realms-Emulator/Responses/ErrorResponse.cs
Normal file
8
Minecraft-Realms-Emulator/Responses/ErrorResponse.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Minecraft_Realms_Emulator.Responses
|
||||||
|
{
|
||||||
|
public class ErrorResponse
|
||||||
|
{
|
||||||
|
public int ErrorCode { get; set; }
|
||||||
|
public string ErrorMsg { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user