1
0
mirror of https://github.com/CyberL1/MyMcRealms.git synced 2024-11-24 15:08:22 -05:00

feat: configute page should use cache

This commit is contained in:
CyberL1 2024-05-19 11:39:41 +02:00
parent 55986afcf5
commit 58d5674ec7
5 changed files with 56 additions and 18 deletions

View File

@ -23,17 +23,19 @@ namespace MyMcRealms.Controllers
if (world == null) return NotFound("World not found"); if (world == null) return NotFound("World not found");
var api = new MyMcAPI.Wrapper(world.OwnersToken);
var whitelist = await api.GetWhitelist();
// Get player name // Get player name
var playerInfo = await new HttpClient().GetFromJsonAsync<MinecraftPlayerResponse>($"https://api.mojang.com/users/profiles/minecraft/{body.Name}"); var playerInfo = await new HttpClient().GetFromJsonAsync<MinecraftPlayerResponse>($"https://api.mojang.com/users/profiles/minecraft/{body.Name}");
if (world.Whitelist.Any(p => p.Name == body.Name)) return BadRequest("Player already whitelisted"); if (whitelist.Result.Any(p => p.Name == body.Name)) return BadRequest("Player already whitelisted");
var api = new MyMcAPI.Wrapper(world.OwnersToken);
api.ExecuteCommand($"whitelist add {body.Name}"); api.ExecuteCommand($"whitelist add {body.Name}");
List<PlayerResponse> whitelistedPlayers = []; List<PlayerResponse> whitelistedPlayers = [];
foreach (var player in world.Whitelist) foreach (var player in whitelist.Result)
{ {
PlayerResponse whitelistedPlayer = new() PlayerResponse whitelistedPlayer = new()
{ {
@ -67,7 +69,7 @@ namespace MyMcRealms.Controllers
OwnerUUID = "blank", OwnerUUID = "blank",
Name = "blank", Name = "blank",
Motd = world.Motd, Motd = world.Motd,
State = world.WhitelistEnable ? "CLOSED" : "OPEN", State = whitelist.Enabled ? "CLOSED" : "OPEN",
WorldType = "NORMAL", WorldType = "NORMAL",
MaxPlayers = 10, MaxPlayers = 10,
MinigameId = null, MinigameId = null,
@ -93,14 +95,16 @@ namespace MyMcRealms.Controllers
if (world == null) return NotFound("World not found"); if (world == null) return NotFound("World not found");
var player = world.Whitelist.Find(p => p.Uuid.Replace("-", "") == uuid); var api = new MyMcAPI.Wrapper(world.OwnersToken);
var whitelist = await api.GetWhitelist();
var player = whitelist.Result.Find(p => p.Uuid.Replace("-", "") == uuid);
// Get player name // Get player name
var playerInfo = await new HttpClient().GetFromJsonAsync<MinecraftPlayerResponse>($"https://sessionserver.mojang.com/session/minecraft/profile/{uuid}"); var playerInfo = await new HttpClient().GetFromJsonAsync<MinecraftPlayerResponse>($"https://sessionserver.mojang.com/session/minecraft/profile/{uuid}");
if (!world.Whitelist.Any(p => p.Uuid.Replace("-", "") == uuid)) return BadRequest("Player not whitelisted"); if (!whitelist.Result.Any(p => p.Uuid.Replace("-", "") == uuid)) return BadRequest("Player not whitelisted");
var api = new MyMcAPI.Wrapper(world.OwnersToken);
api.ExecuteCommand($"whitelist remove {player.Name}"); api.ExecuteCommand($"whitelist remove {player.Name}");
return Ok(true); return Ok(true);

View File

@ -14,10 +14,12 @@ namespace MyMcRealms.Controllers
{ {
var _api = new MyMcAPI.Wrapper(Environment.GetEnvironmentVariable("MYMC_API_KEY")); var _api = new MyMcAPI.Wrapper(Environment.GetEnvironmentVariable("MYMC_API_KEY"));
var world = (await _api.GetAllServers()).Servers[wId]; var world = (await _api.GetAllServers()).Servers[wId];
var api = new MyMcAPI.Wrapper(world.OwnersToken);
var ops = world.Ops; var api = new MyMcAPI.Wrapper(world.OwnersToken);
var player = world.Whitelist.Find(p => p.Uuid.Replace("-", "") == uuid); var whitelist = await api.GetWhitelist();
var ops = whitelist.Ops;
var player = whitelist.Result.Find(p => p.Uuid.Replace("-", "") == uuid);
List<string> opNames = []; List<string> opNames = [];
@ -42,10 +44,12 @@ namespace MyMcRealms.Controllers
{ {
var _api = new MyMcAPI.Wrapper(Environment.GetEnvironmentVariable("MYMC_API_KEY")); var _api = new MyMcAPI.Wrapper(Environment.GetEnvironmentVariable("MYMC_API_KEY"));
var world = (await _api.GetAllServers()).Servers[wId]; var world = (await _api.GetAllServers()).Servers[wId];
var api = new MyMcAPI.Wrapper(world.OwnersToken);
var ops = world.Ops; var api = new MyMcAPI.Wrapper(world.OwnersToken);
var player = world.Whitelist.Find(p => p.Uuid.Replace("-", "") == uuid); var whitelist = await api.GetWhitelist();
var ops = whitelist.Ops;
var player = whitelist.Result.Find(p => p.Uuid.Replace("-", "") == uuid);
List<string> opNames = []; List<string> opNames = [];

View File

@ -83,15 +83,18 @@ namespace MyMcRealms.Controllers
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<ActionResult<WorldResponse>> GetWorldById(int id) public async Task<ActionResult<WorldResponse>> GetWorldById(int id)
{ {
var worlds = await new MyMcAPI.Wrapper(Environment.GetEnvironmentVariable("MYMC_API_KEY")).GetAllServers(); var _api = new MyMcAPI.Wrapper(Environment.GetEnvironmentVariable("MYMC_API_KEY"));
var world = worlds.Servers[id];
var world = (await _api.GetAllServers()).Servers[id];
var api = new MyMcAPI.Wrapper(world.OwnersToken);
var whitelist = await api.GetWhitelist();
string worldOwnerName = world.Ops.ToArray().Length == 0 ? "Owner" : world.Ops[0].Name; string worldOwnerName = world.Ops.ToArray().Length == 0 ? "Owner" : world.Ops[0].Name;
string worldOwnerUuid = world.Ops.ToArray().Length == 0 ? "069a79f444e94726a5befca90e38aaf5" : world.Ops[0].Uuid; string worldOwnerUuid = world.Ops.ToArray().Length == 0 ? "069a79f444e94726a5befca90e38aaf5" : world.Ops[0].Uuid;
string worldName = world.Ops.ToArray().Length == 0 ? world.ServerName : $"{world.Ops[0].Name}'s server"; string worldName = world.Ops.ToArray().Length == 0 ? world.ServerName : $"{world.Ops[0].Name}'s server";
List<PlayerResponse> whitelistedPlayers = []; List<PlayerResponse> whitelistedPlayers = [];
foreach (var player in world.Whitelist) foreach (var player in whitelist.Result)
{ {
PlayerResponse whitelistedPlayer = new() PlayerResponse whitelistedPlayer = new()
{ {
@ -99,8 +102,8 @@ namespace MyMcRealms.Controllers
Uuid = player.Uuid, Uuid = player.Uuid,
Accepted = true, Accepted = true,
Online = false, Online = false,
Operator = world.Ops.Find(p => p.Name == player.Name) != null, Operator = whitelist.Ops.Find(p => p.Name == player.Name) != null,
Permission = world.Ops.Find(p => p.Name == player.Name) != null ? "OPERATOR" : "MEMBER", Permission = whitelist.Ops.Find(p => p.Name == player.Name) != null ? "OPERATOR" : "MEMBER",
}; };
whitelistedPlayers.Add(whitelistedPlayer); whitelistedPlayers.Add(whitelistedPlayer);
@ -113,7 +116,7 @@ namespace MyMcRealms.Controllers
OwnerUUID = worldOwnerUuid, OwnerUUID = worldOwnerUuid,
Name = worldName, Name = worldName,
Motd = world.Motd, Motd = world.Motd,
State = world.WhitelistEnable ? "CLOSED" : "OPEN", State = whitelist.Enabled ? "CLOSED" : "OPEN",
WorldType = "NORMAL", WorldType = "NORMAL",
MaxPlayers = 10, MaxPlayers = 10,
MinigameId = null, MinigameId = null,

View File

@ -0,0 +1,11 @@
namespace MyMcRealms.MyMcAPI.Responses
{
public class WhitelistReponse
{
public bool Success { get; set; }
public string Message { get; set; } = null!;
public List<Whitelist> Result { get; set; } = null!;
public bool Enabled { get; set; }
public List<Op> Ops { get; set; } = null!;
}
}

View File

@ -44,5 +44,21 @@ namespace MyMcRealms.MyMcAPI
return response; return response;
} }
public async Task<WhitelistReponse?> GetWhitelist()
{
try
{
WhitelistReponse? response = await httpClient.GetFromJsonAsync<WhitelistReponse>($"whitelist");
return response;
}
catch (Exception e)
{
Console.WriteLine("error while doing GET /whitelist");
Console.WriteLine(e.Message);
return null;
}
}
} }
} }