From 075af61c9f9cd9fc518480335f54432b1c892fa5 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Sat, 20 Apr 2024 19:42:22 +0200 Subject: [PATCH] Fetch all my-mc servers --- MyMcRealms/Controllers/WorldsController.cs | 93 +++++-------------- .../MyMcAPI/Responses/AllServersResponse.cs | 19 ++++ MyMcRealms/MyMcAPI/Wrapper.cs | 26 +++++- MyMcRealms/Program.cs | 4 - MyMcRealms/Responses/ConnectionResponse.cs | 8 ++ 5 files changed, 73 insertions(+), 77 deletions(-) create mode 100644 MyMcRealms/MyMcAPI/Responses/AllServersResponse.cs create mode 100644 MyMcRealms/Responses/ConnectionResponse.cs diff --git a/MyMcRealms/Controllers/WorldsController.cs b/MyMcRealms/Controllers/WorldsController.cs index f93af64..6e62f82 100644 --- a/MyMcRealms/Controllers/WorldsController.cs +++ b/MyMcRealms/Controllers/WorldsController.cs @@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore; using MyMcRealms.Attributes; using MyMcRealms.Data; using MyMcRealms.Entities; +using MyMcRealms.MyMcAPI; +using MyMcRealms.MyMcAPI.Responses; using MyMcRealms.Requests; using MyMcRealms.Responses; using Newtonsoft.Json; @@ -29,85 +31,30 @@ namespace MyMcRealms.Controllers string playerUUID = cookie.Split(";")[0].Split(":")[2]; string playerName = cookie.Split(";")[1].Split("=")[1]; - var ownedWorlds = await _context.Worlds.Where(w => w.OwnerUUID == playerUUID).Include(w => w.Subscription).ToListAsync(); - var memberWorlds = await _context.Players.Where(p => p.Uuid == playerUUID && p.Accepted).Include(p => p.World.Subscription).Select(p => p.World).ToListAsync(); - List allWorlds = []; - if (ownedWorlds.ToArray().Length == 0) + AllServersResponse AllServers = await new MyMcAPI.MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY")).GetAllServers(); + + foreach (var world in AllServers.Servers) { - var world = new World + WorldResponse response = new() { - Owner = playerName, - OwnerUUID = playerUUID, - Name = null, - Motd = null, - State = "UNINITIALIZED", + Id = AllServers.Servers.IndexOf(world), + Owner = "Owner", + OwnerUUID = "87a2931cf37f4867b6dd1f0699e138d3s", + Name = "my-mc.link world", + Motd = "A world hosted on my-mc.link", + State = "OPEN", WorldType = "NORMAL", MaxPlayers = 10, MinigameId = null, MinigameName = null, MinigameImage = null, ActiveSlot = 1, - Member = false - }; - - ownedWorlds.Add(world); - _context.Worlds.Add(world); - - _context.SaveChanges(); - } - - foreach (var world in ownedWorlds) - { - WorldResponse response = new() - { - Id = world.Id, - Owner = world.Owner, - OwnerUUID = world.OwnerUUID, - Name = world.Name, - Motd = world.Motd, - State = world.State, - WorldType = world.WorldType, - MaxPlayers = world.MaxPlayers, - MinigameId = world.MinigameId, - MinigameName = world.MinigameName, - MinigameImage = world.MinigameImage, - ActiveSlot = world.ActiveSlot, - Member = world.Member, - Players = world.Players - }; - - if (world.Subscription != null) - { - response.DaysLeft = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days; - response.Expired = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days < 0; - response.ExpiredTrial = false; - } - - allWorlds.Add(response); - } - - foreach (var world in memberWorlds) - { - WorldResponse response = new() - { - Id = world.Id, - Owner = world.Owner, - OwnerUUID = world.OwnerUUID, - Name = world.Name, - Motd = world.Motd, - State = world.State, - WorldType = world.WorldType, - MaxPlayers = world.MaxPlayers, - MinigameId = world.MinigameId, - MinigameName = world.MinigameName, - MinigameImage = world.MinigameImage, - ActiveSlot = world.ActiveSlot, - Member = world.Member, - Players = world.Players, + Member = false, + Players = [], DaysLeft = 0, - Expired = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days < 0, + Expired = false, ExpiredTrial = false }; @@ -269,9 +216,15 @@ namespace MyMcRealms.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); + AllServersResponse AllServers = await new MyMcAPI.MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY")).GetAllServers(); + + ConnectionResponse connection = new() + { + Address = AllServers.Servers[wId].Connect, + PendingUpdate = false + }; return Ok(connection); } diff --git a/MyMcRealms/MyMcAPI/Responses/AllServersResponse.cs b/MyMcRealms/MyMcAPI/Responses/AllServersResponse.cs new file mode 100644 index 0000000..af4b1bc --- /dev/null +++ b/MyMcRealms/MyMcAPI/Responses/AllServersResponse.cs @@ -0,0 +1,19 @@ + +namespace MyMcRealms.MyMcAPI.Responses +{ + public class AllServersResponse + { + public bool Success { get; set; } + public List Servers { get; set; } + + public static implicit operator Task(AllServersResponse? v) + { + throw new NotImplementedException(); + } + } + + public class Server + { + public string Connect { get; set; } = string.Empty; + } +} diff --git a/MyMcRealms/MyMcAPI/Wrapper.cs b/MyMcRealms/MyMcAPI/Wrapper.cs index 782982f..2b8ce1a 100644 --- a/MyMcRealms/MyMcAPI/Wrapper.cs +++ b/MyMcRealms/MyMcAPI/Wrapper.cs @@ -13,10 +13,30 @@ namespace MyMcRealms.MyMcAPI httpClient.BaseAddress = new Uri(ApiUrl); } - public async void GetHello() + public async Task GetHello() { - var response = await httpClient.GetFromJsonAsync("hello"); - Console.WriteLine(response.Message); + HelloResponse response = await httpClient.GetFromJsonAsync("hello"); + + if (response == null) + { + Console.WriteLine($"error while doing GET /hello"); + return null; + } + + return response; + } + + public async Task GetAllServers() + { + AllServersResponse response = await httpClient.GetFromJsonAsync($"list_all_servers/{Environment.GetEnvironmentVariable("MYMC_SERVER_LIST_KEY")}"); + + if (response == null) + { + Console.WriteLine("error while doing GET/list_all_servers"); + return null; + } + + return response; } } } diff --git a/MyMcRealms/Program.cs b/MyMcRealms/Program.cs index 201ef37..ad96af4 100644 --- a/MyMcRealms/Program.cs +++ b/MyMcRealms/Program.cs @@ -2,7 +2,6 @@ using Microsoft.EntityFrameworkCore; using MyMcRealms.Data; using MyMcRealms.Helpers; using MyMcRealms.Middlewares; -using MyMcRealms.MyMcAPI; using Npgsql; var builder = WebApplication.CreateBuilder(args); @@ -52,7 +51,4 @@ app.UseMiddleware(); app.MapControllers(); -var mymc = new MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY")); -mymc.GetHello(); - app.Run(); diff --git a/MyMcRealms/Responses/ConnectionResponse.cs b/MyMcRealms/Responses/ConnectionResponse.cs new file mode 100644 index 0000000..79c37f6 --- /dev/null +++ b/MyMcRealms/Responses/ConnectionResponse.cs @@ -0,0 +1,8 @@ +namespace MyMcRealms.Responses +{ + public class ConnectionResponse + { + public string Address { get; set; } = string.Empty; + public bool PendingUpdate { get; set; } + } +}