feat(realms): docker helper

This commit is contained in:
CyberL1 2024-05-25 19:58:05 +02:00
parent ab237667a3
commit 8e51111564
7 changed files with 58 additions and 38 deletions

View File

@ -6,7 +6,7 @@ using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Requests; using Minecraft_Realms_Emulator.Requests;
using Minecraft_Realms_Emulator.Responses; using Minecraft_Realms_Emulator.Responses;
namespace Minecraft_Realms_Emulator.Modes.Realms namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
{ {
[Route("modes/realms/[controller]")] [Route("modes/realms/[controller]")]
[ApiController] [ApiController]
@ -29,7 +29,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
var invites = await _context.Invites.Where(i => i.RecipeintUUID == playerUUID).Include(i => i.World).ToListAsync(); var invites = await _context.Invites.Where(i => i.RecipeintUUID == playerUUID).Include(i => i.World).ToListAsync();
List<InviteResponse> invitesList = []; List<InviteResponse> invitesList = [];
foreach (var invite in invites) foreach (var invite in invites)
{ {
InviteResponse inv = new() InviteResponse inv = new()
@ -38,7 +38,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
WorldName = invite.World.Name, WorldName = invite.World.Name,
WorldOwnerName = invite.World.Owner, WorldOwnerName = invite.World.Owner,
WorldOwnerUuid = invite.World.OwnerUUID, WorldOwnerUuid = invite.World.OwnerUUID,
Date = ((DateTimeOffset) invite.Date).ToUnixTimeMilliseconds(), Date = ((DateTimeOffset)invite.Date).ToUnixTimeMilliseconds(),
}; };
invitesList.Add(inv); invitesList.Add(inv);
@ -78,7 +78,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
var invite = _context.Invites.Include(i => i.World).FirstOrDefault(i => i.InvitationId == id); var invite = _context.Invites.Include(i => i.World).FirstOrDefault(i => i.InvitationId == id);
if (invite == null) return NotFound("Invite not found"); if (invite == null) return NotFound("Invite not found");
_context.Invites.Remove(invite); _context.Invites.Remove(invite);
string cookie = Request.Headers.Cookie; string cookie = Request.Headers.Cookie;

View File

@ -5,7 +5,7 @@ using Minecraft_Realms_Emulator.Enums;
using Minecraft_Realms_Emulator.Helpers; using Minecraft_Realms_Emulator.Helpers;
using Minecraft_Realms_Emulator.Responses; using Minecraft_Realms_Emulator.Responses;
namespace Minecraft_Realms_Emulator.Modes.Realms namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
{ {
[Route("modes/realms/[controller]")] [Route("modes/realms/[controller]")]
[ApiController] [ApiController]

View File

@ -3,7 +3,7 @@ using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Responses; using Minecraft_Realms_Emulator.Responses;
namespace Minecraft_Realms_Emulator.Modes.Realms namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
{ {
[Route("modes/realms/[controller]")] [Route("modes/realms/[controller]")]
[ApiController] [ApiController]

View File

@ -4,7 +4,7 @@ using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Responses; using Minecraft_Realms_Emulator.Responses;
namespace Minecraft_Realms_Emulator.Modes.Realms namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
{ {
[Route("modes/realms/[controller]")] [Route("modes/realms/[controller]")]
[ApiController] [ApiController]
@ -28,7 +28,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
var sub = new SubscriptionResponse var sub = new SubscriptionResponse
{ {
StartDate = ((DateTimeOffset)world.Subscription.StartDate).ToUnixTimeMilliseconds(), StartDate = ((DateTimeOffset)world.Subscription.StartDate).ToUnixTimeMilliseconds(),
DaysLeft = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days, DaysLeft = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days,
SubscriptionType = world.Subscription.SubscriptionType SubscriptionType = world.Subscription.SubscriptionType
}; };

View File

@ -4,7 +4,7 @@ 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;
namespace Minecraft_Realms_Emulator.Modes.Realms namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
{ {
[Route("modes/realms/[controller]")] [Route("modes/realms/[controller]")]
[ApiController] [ApiController]

View File

@ -5,6 +5,7 @@ using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities; using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Enums; using Minecraft_Realms_Emulator.Enums;
using Minecraft_Realms_Emulator.Helpers; using Minecraft_Realms_Emulator.Helpers;
using Minecraft_Realms_Emulator.Modes.Realms.Helpers;
using Minecraft_Realms_Emulator.Requests; using Minecraft_Realms_Emulator.Requests;
using Minecraft_Realms_Emulator.Responses; using Minecraft_Realms_Emulator.Responses;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -13,7 +14,7 @@ using System.Diagnostics;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
namespace Minecraft_Realms_Emulator.Modes.Realms namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
{ {
[Route("modes/realms/[controller]")] [Route("modes/realms/[controller]")]
[ApiController] [ApiController]
@ -275,15 +276,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
CommandBlocks = false CommandBlocks = false
}; };
// Run docker container new DockerHelper().CreateServer(world.Id, port);
ProcessStartInfo serverProcessInfo = new();
serverProcessInfo.FileName = "docker";
serverProcessInfo.Arguments = $"run -d --name realm-server-{world.Id} -p {port}:25565 realm-server";
Process serverProcess = new();
serverProcess.StartInfo = serverProcessInfo;
serverProcess.Start();
_context.Worlds.Update(world); _context.Worlds.Update(world);
@ -316,15 +309,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
if (world == null) return NotFound("World not found"); if (world == null) return NotFound("World not found");
// Start the server new DockerHelper().StartServer(wId);
ProcessStartInfo serverProcessInfo = new();
serverProcessInfo.FileName = "docker";
serverProcessInfo.Arguments = $"container start realm-server-{world.Id}";
Process serverProcess = new();
serverProcess.StartInfo = serverProcessInfo;
serverProcess.Start();
world.State = nameof(StateEnum.OPEN); world.State = nameof(StateEnum.OPEN);
@ -400,7 +385,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
slot.CommandBlocks = body.CommandBlocks; slot.CommandBlocks = body.CommandBlocks;
_context.SaveChanges(); _context.SaveChanges();
return Ok(true); return Ok(true);
} }
@ -476,15 +461,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
if (world == null) return NotFound("World not found"); if (world == null) return NotFound("World not found");
// Remove docker container new DockerHelper().DeleteServer(wId);
ProcessStartInfo serverProcessInfo = new();
serverProcessInfo.FileName = "docker";
serverProcessInfo.Arguments = $"container rm realm-server-{world.Id}";
Process serverProcess = new();
serverProcess.StartInfo = serverProcessInfo;
serverProcess.Start();
_context.Worlds.Remove(world); _context.Worlds.Remove(world);
_context.SaveChanges(); _context.SaveChanges();

View File

@ -0,0 +1,43 @@
using System.Diagnostics;
namespace Minecraft_Realms_Emulator.Modes.Realms.Helpers
{
public class DockerHelper
{
public void CreateServer(int id, int port)
{
ProcessStartInfo serverProcessInfo = new();
serverProcessInfo.FileName = "docker";
serverProcessInfo.Arguments = $"run -d --name realm-server-{id} -p {port}:25565 realm-server";
Process serverProcess = new();
serverProcess.StartInfo = serverProcessInfo;
serverProcess.Start();
}
public void StartServer(int id)
{
ProcessStartInfo serverProcessInfo = new();
serverProcessInfo.FileName = "docker";
serverProcessInfo.Arguments = $"container start realm-server-{id}";
Process serverProcess = new();
serverProcess.StartInfo = serverProcessInfo;
serverProcess.Start();
}
public void DeleteServer(int id)
{
ProcessStartInfo serverProcessInfo = new();
serverProcessInfo.FileName = "docker";
serverProcessInfo.Arguments = $"container rm realm-server-{id}";
Process serverProcess = new();
serverProcess.StartInfo = serverProcessInfo;
serverProcess.Start();
}
}
}