1
0
mirror of https://github.com/CyberL1/MyMcRealms.git synced 2024-09-19 07:52:52 -04:00

Fetch all my-mc servers

This commit is contained in:
CyberL1 2024-04-20 19:42:22 +02:00
parent 1326184cc2
commit 075af61c9f
5 changed files with 73 additions and 77 deletions

View File

@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore;
using MyMcRealms.Attributes; using MyMcRealms.Attributes;
using MyMcRealms.Data; using MyMcRealms.Data;
using MyMcRealms.Entities; using MyMcRealms.Entities;
using MyMcRealms.MyMcAPI;
using MyMcRealms.MyMcAPI.Responses;
using MyMcRealms.Requests; using MyMcRealms.Requests;
using MyMcRealms.Responses; using MyMcRealms.Responses;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -29,85 +31,30 @@ namespace MyMcRealms.Controllers
string playerUUID = cookie.Split(";")[0].Split(":")[2]; string playerUUID = cookie.Split(";")[0].Split(":")[2];
string playerName = cookie.Split(";")[1].Split("=")[1]; 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<WorldResponse> allWorlds = []; List<WorldResponse> 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, Id = AllServers.Servers.IndexOf(world),
OwnerUUID = playerUUID, Owner = "Owner",
Name = null, OwnerUUID = "87a2931cf37f4867b6dd1f0699e138d3s",
Motd = null, Name = "my-mc.link world",
State = "UNINITIALIZED", Motd = "A world hosted on my-mc.link",
State = "OPEN",
WorldType = "NORMAL", WorldType = "NORMAL",
MaxPlayers = 10, MaxPlayers = 10,
MinigameId = null, MinigameId = null,
MinigameName = null, MinigameName = null,
MinigameImage = null, MinigameImage = null,
ActiveSlot = 1, ActiveSlot = 1,
Member = false Member = false,
}; Players = [],
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,
DaysLeft = 0, DaysLeft = 0,
Expired = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days < 0, Expired = false,
ExpiredTrial = false ExpiredTrial = false
}; };
@ -269,9 +216,15 @@ namespace MyMcRealms.Controllers
} }
[HttpGet("v1/{wId}/join/pc")] [HttpGet("v1/{wId}/join/pc")]
public ActionResult<Connection> Join(int wId) public async Task<ActionResult<Connection>> 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); return Ok(connection);
} }

View File

@ -0,0 +1,19 @@

namespace MyMcRealms.MyMcAPI.Responses
{
public class AllServersResponse
{
public bool Success { get; set; }
public List<Server> Servers { get; set; }
public static implicit operator Task<object>(AllServersResponse? v)
{
throw new NotImplementedException();
}
}
public class Server
{
public string Connect { get; set; } = string.Empty;
}
}

View File

@ -13,10 +13,30 @@ namespace MyMcRealms.MyMcAPI
httpClient.BaseAddress = new Uri(ApiUrl); httpClient.BaseAddress = new Uri(ApiUrl);
} }
public async void GetHello() public async Task<HelloResponse?> GetHello()
{ {
var response = await httpClient.GetFromJsonAsync<HelloResponse>("hello"); HelloResponse response = await httpClient.GetFromJsonAsync<HelloResponse>("hello");
Console.WriteLine(response.Message);
if (response == null)
{
Console.WriteLine($"error while doing GET /hello");
return null;
}
return response;
}
public async Task<AllServersResponse?> GetAllServers()
{
AllServersResponse response = await httpClient.GetFromJsonAsync<AllServersResponse>($"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;
} }
} }
} }

View File

@ -2,7 +2,6 @@ using Microsoft.EntityFrameworkCore;
using MyMcRealms.Data; using MyMcRealms.Data;
using MyMcRealms.Helpers; using MyMcRealms.Helpers;
using MyMcRealms.Middlewares; using MyMcRealms.Middlewares;
using MyMcRealms.MyMcAPI;
using Npgsql; using Npgsql;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -52,7 +51,4 @@ app.UseMiddleware<MinecraftCookieMiddleware>();
app.MapControllers(); app.MapControllers();
var mymc = new MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY"));
mymc.GetHello();
app.Run(); app.Run();

View File

@ -0,0 +1,8 @@
namespace MyMcRealms.Responses
{
public class ConnectionResponse
{
public string Address { get; set; } = string.Empty;
public bool PendingUpdate { get; set; }
}
}