1
1
mirror of https://github.com/CyberL1/MyMcRealms.git synced 2024-12-24 05:18:23 -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

@ -22,18 +22,20 @@ namespace MyMcRealms.Controllers
var world = (await _api.GetAllServers()).Servers[wId];
if (world == null) return NotFound("World not found");
var api = new MyMcAPI.Wrapper(world.OwnersToken);
var whitelist = await api.GetWhitelist();
// Get player 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}");
List<PlayerResponse> whitelistedPlayers = [];
foreach (var player in world.Whitelist)
foreach (var player in whitelist.Result)
{
PlayerResponse whitelistedPlayer = new()
{
@ -67,7 +69,7 @@ namespace MyMcRealms.Controllers
OwnerUUID = "blank",
Name = "blank",
Motd = world.Motd,
State = world.WhitelistEnable ? "CLOSED" : "OPEN",
State = whitelist.Enabled ? "CLOSED" : "OPEN",
WorldType = "NORMAL",
MaxPlayers = 10,
MinigameId = null,
@ -93,14 +95,16 @@ namespace MyMcRealms.Controllers
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
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}");
return Ok(true);

View File

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

View File

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