Additional tools for Admin portal, and huge NameSpace Revamp
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Server.Redis;
|
||||
|
||||
namespace Server.Services.Admin.Pages.Broadcast;
|
||||
|
||||
public class IndexModel : AdminPageModel
|
||||
{
|
||||
private readonly IRedisManager _redis;
|
||||
private readonly ILogger<IndexModel> _logger;
|
||||
|
||||
public IndexModel(IRedisManager redis, ILogger<IndexModel> logger)
|
||||
{
|
||||
_redis = redis;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public int ConnectedPlayers { get; set; }
|
||||
public int ActiveDungeons { get; set; }
|
||||
|
||||
[TempData]
|
||||
public string? Message { get; set; }
|
||||
[TempData]
|
||||
public bool IsError { get; set; }
|
||||
|
||||
public async Task OnGetAsync()
|
||||
{
|
||||
await LoadStats();
|
||||
}
|
||||
|
||||
private async Task LoadStats()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Try to get connected player count from Redis
|
||||
var playerCount = await _redis.Db.StringGetAsync("server:player_count");
|
||||
if (playerCount.HasValue && int.TryParse(playerCount, out var count))
|
||||
ConnectedPlayers = count;
|
||||
|
||||
var dungeonCount = await _redis.Db.StringGetAsync("server:dungeon_count");
|
||||
if (dungeonCount.HasValue && int.TryParse(dungeonCount, out var dCount))
|
||||
ActiveDungeons = dCount;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to load server stats from Redis");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostQuickMessageAsync(string type, string message)
|
||||
{
|
||||
return await SendBroadcast(type, message, 10);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostCustomMessageAsync(string type, string message, int duration = 10)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
{
|
||||
Message = "Message cannot be empty.";
|
||||
IsError = true;
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
return await SendBroadcast(type, message, duration);
|
||||
}
|
||||
|
||||
private async Task<IActionResult> SendBroadcast(string type, string message, int duration)
|
||||
{
|
||||
try
|
||||
{
|
||||
var payload = System.Text.Json.JsonSerializer.Serialize(new
|
||||
{
|
||||
type = type,
|
||||
message = message,
|
||||
duration = duration,
|
||||
timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||
});
|
||||
|
||||
// Publish to Redis channel that game servers listen to
|
||||
await _redis.Bus.PublishAsync("server:broadcast", payload);
|
||||
|
||||
_logger.LogInformation("Broadcast sent: {Type} - {Message}", type, message);
|
||||
Message = $"Broadcast sent to all connected players.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to send broadcast");
|
||||
Message = $"Failed to send broadcast: {ex.Message}";
|
||||
IsError = true;
|
||||
}
|
||||
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostRefreshAssetsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var payload = System.Text.Json.JsonSerializer.Serialize(new
|
||||
{
|
||||
command = "refresh_assets",
|
||||
timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||
});
|
||||
|
||||
await _redis.Bus.PublishAsync("server:command", payload);
|
||||
Message = "Asset refresh command sent.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to send asset refresh command");
|
||||
Message = $"Failed: {ex.Message}";
|
||||
IsError = true;
|
||||
}
|
||||
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostForceReconnectAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var payload = System.Text.Json.JsonSerializer.Serialize(new
|
||||
{
|
||||
command = "force_reconnect",
|
||||
timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||
});
|
||||
|
||||
await _redis.Bus.PublishAsync("server:command", payload);
|
||||
Message = "Force reconnect command sent. All clients will reconnect.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to send force reconnect command");
|
||||
Message = $"Failed: {ex.Message}";
|
||||
IsError = true;
|
||||
}
|
||||
|
||||
return RedirectToPage();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user