Files
systemrevivalserver/Services/Admin/Pages/Broadcast/Index.cshtml.cs
T

140 lines
4.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Server.Redis;
namespace GameServices.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
{
// Use correct Redis keys (stats:players and stats:dungeons)
var playerCount = await _redis.Db.StringGetAsync("stats:players");
if (playerCount.HasValue && int.TryParse(playerCount, out var count))
ConnectedPlayers = count;
var dungeonCount = await _redis.Db.StringGetAsync("stats:dungeons");
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();
}
}