1
0
mirror of https://github.com/CyberL1/MyMcRealms.git synced 2024-11-21 21:48:21 -05:00

feat: show whitelisted players in invites menu

This commit is contained in:
CyberL1 2024-05-13 21:24:19 +02:00
parent fbc1e64d18
commit 9115402ff1
7 changed files with 174 additions and 17 deletions

View File

@ -0,0 +1,67 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Responses;
using MyMcRealms.Attributes;
using MyMcRealms.MyMcAPI;
namespace Minecraft_Realms_Emulator.Controllers
{
[Route("[controller]")]
[ApiController]
[RequireMinecraftCookie]
public class OpsController : ControllerBase
{
[HttpPost("{wId}/{uuid}")]
public async Task<ActionResult<OpsResponse>> OpPlayer(int wId, string uuid)
{
var api = new MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY"));
var world = (await api.GetAllServers()).Servers[wId];
var ops = world.Ops;
var player = world.Whitelist.Find(p => p.Uuid.Replace("-", "") == uuid);
List<string> opNames = [];
foreach (var op in ops)
{
opNames.Add(op.Name);
}
api.ExecuteCommand($"op {player.Name}");
opNames.Add(player.Name);
var opsResponse = new OpsResponse
{
Ops = opNames
};
return Ok(opsResponse);
}
[HttpDelete("{wId}/{uuid}")]
public async Task<ActionResult<OpsResponse>> DeopPlayerAsync(int wId, string uuid)
{
var api = new MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY"));
var world = (await api.GetAllServers()).Servers[wId];
var ops = world.Ops;
var player = world.Whitelist.Find(p => p.Uuid.Replace("-", "") == uuid);
List<string> opNames = [];
foreach (var op in ops)
{
opNames.Add(op.Name);
}
api.ExecuteCommand($"deop {player.Name}");
opNames.Remove(player.Name);
var opsResponse = new OpsResponse
{
Ops = opNames
};
return Ok(opsResponse);
}
}
}

View File

@ -74,32 +74,79 @@ namespace MyMcRealms.Controllers
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<ActionResult<WorldResponse>> GetWorldById(int id) public async Task<ActionResult<WorldResponse>> GetWorldById(int id)
{ {
return NotFound("World not found"); var worlds = await new MyMcAPI.MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY")).GetAllServers();
var world = worlds.Servers[id];
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<Player> whitelistedPlayers = [];
foreach (var player in world.Whitelist)
{
Player whitelistedPlayer = new()
{
Name = player.Name,
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",
};
whitelistedPlayers.Add(whitelistedPlayer);
} }
// [HttpPut("{id}/open")] WorldResponse response = new()
// public async Task<ActionResult<bool>> Open(int id) {
// { Id = id,
// var world = worlds.Find(w => w.Id == id); Owner = worldOwnerName,
OwnerUUID = worldOwnerUuid,
Name = worldName,
Motd = world.Motd,
State = "OPEN",
WorldType = "NORMAL",
MaxPlayers = 10,
MinigameId = null,
MinigameName = null,
MinigameImage = null,
ActiveSlot = 1,
Member = false,
Players = whitelistedPlayers,
DaysLeft = 7,
Expired = false,
ExpiredTrial = false,
ActiveVersion = world.GameVersion
};
// if (world == null) return NotFound("World not found"); return Ok(response);
}
[HttpPut("{id}/open")]
public async Task<ActionResult<bool>> Open(int id)
{
var worlds = await new MyMcAPI.MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY")).GetAllServers();
var world = worlds.Servers[id];
if (world == null) return NotFound("World not found");
// Turn off whitelist // Turn off whitelist
// return Ok(true); return Ok(true);
// } }
// [HttpPut("{id}/close")] [HttpPut("{id}/close")]
// public async Task<ActionResult<bool>> Close(int id) public async Task<ActionResult<bool>> Close(int id)
// { {
// var world = worlds.FirstOrDefault(w => w.Id == id); var worlds = await new MyMcAPI.MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY")).GetAllServers();
var world = worlds.Servers[id];
// if (world == null) return NotFound("World not found"); if (world == null) return NotFound("World not found");
// Turn on whitelist // Turn on whitelist
// return Ok(true); return Ok(true);
// } }
[HttpGet("v1/{wId}/join/pc")] [HttpGet("v1/{wId}/join/pc")]
public async Task<ActionResult<ConnectionResponse>> Join(int wId) public async Task<ActionResult<ConnectionResponse>> Join(int wId)

View File

@ -15,6 +15,7 @@
public bool Online { get; set; } public bool Online { get; set; }
public List<Op> Ops { get; set; } = null!; public List<Op> Ops { get; set; } = null!;
public List<Ban> Banlist { get; set; } = null!; public List<Ban> Banlist { get; set; } = null!;
public List<Whitelist> Whitelist { get; set; } = null!;
public string OwnersToken { get; set; } = string.Empty; public string OwnersToken { get; set; } = string.Empty;
} }

View File

@ -0,0 +1,9 @@

namespace MyMcRealms.MyMcAPI.Responses
{
public class ConsoleResponse
{
public bool Success { get; set; }
public string Message { get; set; } = null!;
}
}

View File

@ -25,5 +25,21 @@ namespace MyMcRealms.MyMcAPI
return response; return response;
} }
public HttpResponseMessage? ExecuteCommand(string command)
{
string json = $"{{ \"command\": \"{command}\" }}";
StringContent content = new(json, System.Text.Encoding.UTF8, "application/json");
var response = httpClient.PostAsync("console", content).Result;
if (!response.IsSuccessStatusCode)
{
Console.WriteLine("error while doing POST /console");
return null;
}
return response;
}
} }
} }

View File

@ -0,0 +1,7 @@
namespace Minecraft_Realms_Emulator.Responses
{
public class OpsResponse
{
public List<string> Ops { get; set; } = null!;
}
}

View File

@ -12,7 +12,7 @@ namespace MyMcRealms.Entities
public string? Motd { get; set; } public string? Motd { get; set; }
public string State { get; set; } = "OPEN"; public string State { get; set; } = "OPEN";
public string WorldType { get; set; } = "NORMAL"; public string WorldType { get; set; } = "NORMAL";
// public List<Player> Players { get; set; } = []; public List<Player> Players { get; set; } = [];
public int MaxPlayers { get; set; } = 10; public int MaxPlayers { get; set; } = 10;
public string? MinigameName { get; set; } public string? MinigameName { get; set; }
public int? MinigameId { get; set; } public int? MinigameId { get; set; }
@ -27,4 +27,14 @@ namespace MyMcRealms.Entities
public string Compatibility { get; set; } = string.Empty; public string Compatibility { get; set; } = string.Empty;
public string ActiveVersion { get; set; } = string.Empty; public string ActiveVersion { get; set; } = string.Empty;
} }
public class Player
{
public string Name { get; set; } = string.Empty;
public string Uuid { get; set; } = string.Empty;
public bool Operator { get; set; }
public bool Accepted { get; set; }
public bool Online { get; set; }
public string Permission { get; set; } = "MEMBER";
}
} }