From c1353de5654f3c6af81902587154efb1f1d2df3c Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Sat, 6 Jul 2024 10:24:45 +0200 Subject: [PATCH] feat: start server if stopped on join --- .../Realms/Controllers/WorldsController.cs | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs b/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs index 5ad7652..51e25da 100644 --- a/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs +++ b/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs @@ -667,32 +667,27 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers [CheckActiveSubscription] public async Task> Open(int wId) { - 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); - new DockerHelper(world).StartServer(); + new DockerHelper(world).StartServer(); - world.State = nameof(StateEnum.OPEN); + world.State = nameof(StateEnum.OPEN); - _context.SaveChanges(); + _context.SaveChanges(); - var connection = _context.Connections.FirstOrDefault(c => c.World.Id == wId); - var query = new MinecraftServerQuery().Query(connection.Address); + var connection = _context.Connections.FirstOrDefault(c => c.World.Id == wId); + var query = new MinecraftServerQuery().Query(connection.Address); - while (query == null) - { - - var newQuery = new MinecraftServerQuery().Query(connection.Address); - - if (newQuery != null) + while (query == null) { - break; + await Task.Delay(1000); + query = new MinecraftServerQuery().Query(connection.Address); } - } - return Ok(true); - } + return Ok(true); + } [HttpPut("{wId}/close")] [CheckForWorld] @@ -909,9 +904,19 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers } [HttpGet("v1/{wId}/join/pc")] - public ActionResult Join(int wId) + public async Task> Join(int wId) { - var connection = _context.Connections.FirstOrDefault(x => x.World.Id == wId); + var connection = _context.Connections.Include(c => c.World).FirstOrDefault(x => x.World.Id == wId); + + var query = new MinecraftServerQuery().Query(connection.Address); + + if (query == null) new DockerHelper(connection.World).StartServer(); + + while (query == null) + { + await Task.Delay(1000); + query = new MinecraftServerQuery().Query(connection.Address); + } return Ok(connection); }