Redis fix
This commit is contained in:
@@ -9,6 +9,7 @@ using DataServer.Data.Enums;
|
|||||||
using Server.Networking;
|
using Server.Networking;
|
||||||
using Server.Networking.Handling;
|
using Server.Networking.Handling;
|
||||||
using Server.Networking.Messages;
|
using Server.Networking.Messages;
|
||||||
|
using Server.Redis;
|
||||||
using Server.Services;
|
using Server.Services;
|
||||||
using Server.Services.Auth;
|
using Server.Services.Auth;
|
||||||
|
|
||||||
@@ -25,6 +26,10 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
private readonly IMessageSerializer _ser;
|
private readonly IMessageSerializer _ser;
|
||||||
private readonly IServiceScopeFactory _scopeFactory;
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
private readonly IServerBroadcastService _broadcastService;
|
private readonly IServerBroadcastService _broadcastService;
|
||||||
|
private readonly IRedisManager _redis;
|
||||||
|
|
||||||
|
// Pod identifier for multi-instance tracking
|
||||||
|
private readonly string _podId;
|
||||||
|
|
||||||
// Player state tracking (keyed by connection ID)
|
// Player state tracking (keyed by connection ID)
|
||||||
private readonly ConcurrentDictionary<Guid, DungeonPlayerSession> _playerSessions = new();
|
private readonly ConcurrentDictionary<Guid, DungeonPlayerSession> _playerSessions = new();
|
||||||
@@ -53,7 +58,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
IServiceScopeFactory scopeFactory,
|
IServiceScopeFactory scopeFactory,
|
||||||
LootService lootService,
|
LootService lootService,
|
||||||
CraftingHandler craftingHandler,
|
CraftingHandler craftingHandler,
|
||||||
IServerBroadcastService broadcastService)
|
IServerBroadcastService broadcastService,
|
||||||
|
IRedisManager redis)
|
||||||
{
|
{
|
||||||
_log = log;
|
_log = log;
|
||||||
_sessions = sessions;
|
_sessions = sessions;
|
||||||
@@ -62,6 +68,12 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
_lootService = lootService;
|
_lootService = lootService;
|
||||||
_craftingHandler = craftingHandler;
|
_craftingHandler = craftingHandler;
|
||||||
_broadcastService = broadcastService;
|
_broadcastService = broadcastService;
|
||||||
|
_redis = redis;
|
||||||
|
|
||||||
|
// Generate unique pod ID (use hostname in K8s, or random for local dev)
|
||||||
|
_podId = Environment.GetEnvironmentVariable("HOSTNAME")
|
||||||
|
?? Environment.GetEnvironmentVariable("POD_NAME")
|
||||||
|
?? $"local-{Guid.NewGuid().ToString()[..8]}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyCollection<int> SupportedOpCodes => new[]
|
public IReadOnlyCollection<int> SupportedOpCodes => new[]
|
||||||
@@ -110,11 +122,98 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
await LoadMobDefinitionsAsync();
|
await LoadMobDefinitionsAsync();
|
||||||
await _lootService.InitializeAsync();
|
await _lootService.InitializeAsync();
|
||||||
|
|
||||||
|
// Subscribe to Redis broadcast channel for multi-pod support
|
||||||
|
await SubscribeToBroadcastChannelAsync();
|
||||||
|
|
||||||
// Start tick loop (20 ticks per second)
|
// Start tick loop (20 ticks per second)
|
||||||
_tickTimer = new Timer(TickAll, null, 0, 50);
|
_tickTimer = new Timer(TickAll, null, 0, 50);
|
||||||
|
|
||||||
_initialized = true;
|
_initialized = true;
|
||||||
_log.LogInformation("DungeonHandler initialized with {MobCount} mob definitions", _mobDefinitions.Count);
|
_log.LogInformation("DungeonHandler initialized on pod {PodId} with {MobCount} mob definitions", _podId, _mobDefinitions.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SubscribeToBroadcastChannelAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Subscribe to server:broadcast channel
|
||||||
|
await _redis.Bus.SubscribeAsync("server:broadcast", (channel, message) =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (message.IsNullOrEmpty) return;
|
||||||
|
|
||||||
|
var payload = JsonSerializer.Deserialize<BroadcastPayload>(message.ToString());
|
||||||
|
if (payload == null) return;
|
||||||
|
|
||||||
|
_log.LogInformation("Received broadcast from Redis: [{Type}] {Message}", payload.Type, payload.Message);
|
||||||
|
|
||||||
|
// Send to all local player sessions
|
||||||
|
foreach (var session in _playerSessions.Values)
|
||||||
|
{
|
||||||
|
_ = SendAsync(session.Connection, DungeonOpCodes.SERVER_BROADCAST, new
|
||||||
|
{
|
||||||
|
message = payload.Message,
|
||||||
|
type = payload.Type
|
||||||
|
}, CancellationToken.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogError(ex, "Error processing broadcast message");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Subscribe to server:command channel for asset refresh, force reconnect, etc.
|
||||||
|
await _redis.Bus.SubscribeAsync("server:command", (channel, message) =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (message.IsNullOrEmpty) return;
|
||||||
|
|
||||||
|
var payload = JsonSerializer.Deserialize<CommandPayload>(message.ToString());
|
||||||
|
if (payload == null) return;
|
||||||
|
|
||||||
|
_log.LogInformation("Received command from Redis: {Command}", payload.Command);
|
||||||
|
|
||||||
|
switch (payload.Command)
|
||||||
|
{
|
||||||
|
case "refresh_assets":
|
||||||
|
// Queue asset refresh for all local players
|
||||||
|
_broadcastService.QueueAssetRefresh();
|
||||||
|
break;
|
||||||
|
case "force_reconnect":
|
||||||
|
// Could implement force disconnect here
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogError(ex, "Error processing command message");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_log.LogInformation("Subscribed to Redis broadcast channels");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogError(ex, "Failed to subscribe to Redis broadcast channels");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Payload classes for Redis pub/sub
|
||||||
|
private class BroadcastPayload
|
||||||
|
{
|
||||||
|
public string Type { get; set; } = "system";
|
||||||
|
public string Message { get; set; } = "";
|
||||||
|
public int Duration { get; set; } = 10;
|
||||||
|
public long Timestamp { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class CommandPayload
|
||||||
|
{
|
||||||
|
public string Command { get; set; } = "";
|
||||||
|
public long Timestamp { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadMobDefinitionsAsync()
|
private async Task LoadMobDefinitionsAsync()
|
||||||
@@ -339,9 +438,123 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
};
|
};
|
||||||
|
|
||||||
_playerSessions[connection.Id] = session;
|
_playerSessions[connection.Id] = session;
|
||||||
|
|
||||||
|
// Track in Redis for global player count
|
||||||
|
await TrackPlayerConnectedAsync(session);
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Track player connection in Redis for global stats
|
||||||
|
/// </summary>
|
||||||
|
private async Task TrackPlayerConnectedAsync(DungeonPlayerSession session)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Increment global player count
|
||||||
|
await _redis.Db.StringIncrementAsync("stats:players");
|
||||||
|
|
||||||
|
// Store player session info for this pod
|
||||||
|
await _redis.SetJsonAsync($"player:{session.CharacterId}", new
|
||||||
|
{
|
||||||
|
accountId = session.AccountId,
|
||||||
|
characterId = session.CharacterId,
|
||||||
|
characterName = session.Character.Name,
|
||||||
|
podId = _podId,
|
||||||
|
state = session.State.ToString(),
|
||||||
|
connectedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
|
}, TimeSpan.FromHours(24));
|
||||||
|
|
||||||
|
_log.LogDebug("Player {CharacterId} connected on pod {PodId}", session.CharacterId, _podId);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogWarning(ex, "Failed to track player connection in Redis");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove player tracking from Redis
|
||||||
|
/// </summary>
|
||||||
|
private async Task TrackPlayerDisconnectedAsync(DungeonPlayerSession session)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Decrement global player count (don't go below 0)
|
||||||
|
var count = await _redis.Db.StringDecrementAsync("stats:players");
|
||||||
|
if (count < 0)
|
||||||
|
{
|
||||||
|
await _redis.Db.StringSetAsync("stats:players", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove player session info
|
||||||
|
await _redis.Db.KeyDeleteAsync($"player:{session.CharacterId}");
|
||||||
|
|
||||||
|
_log.LogDebug("Player {CharacterId} disconnected from pod {PodId}", session.CharacterId, _podId);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogWarning(ex, "Failed to track player disconnection in Redis");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Track dungeon creation in Redis
|
||||||
|
/// </summary>
|
||||||
|
private async Task TrackDungeonCreatedAsync(DungeonInstance dungeon)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Increment global dungeon count
|
||||||
|
await _redis.Db.StringIncrementAsync("stats:dungeons");
|
||||||
|
|
||||||
|
// Store dungeon info
|
||||||
|
await _redis.SetJsonAsync($"dungeon:{dungeon.Id}", new
|
||||||
|
{
|
||||||
|
id = dungeon.Id.ToString(),
|
||||||
|
tier = dungeon.Tier,
|
||||||
|
podId = _podId,
|
||||||
|
creatorName = dungeon.CreatorName,
|
||||||
|
creatorLevel = dungeon.CreatorLevel,
|
||||||
|
levelBracketMin = dungeon.LevelBracketMin,
|
||||||
|
levelBracketMax = dungeon.LevelBracketMax,
|
||||||
|
createdAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
|
}, TimeSpan.FromHours(2)); // Dungeons auto-expire from Redis after 2 hours
|
||||||
|
|
||||||
|
_log.LogDebug("Dungeon {DungeonId} created on pod {PodId}", dungeon.Id, _podId);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogWarning(ex, "Failed to track dungeon creation in Redis");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Track dungeon destruction in Redis
|
||||||
|
/// </summary>
|
||||||
|
private async Task TrackDungeonDestroyedAsync(Guid dungeonId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Decrement global dungeon count (don't go below 0)
|
||||||
|
var count = await _redis.Db.StringDecrementAsync("stats:dungeons");
|
||||||
|
if (count < 0)
|
||||||
|
{
|
||||||
|
await _redis.Db.StringSetAsync("stats:dungeons", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove dungeon info
|
||||||
|
await _redis.Db.KeyDeleteAsync($"dungeon:{dungeonId}");
|
||||||
|
|
||||||
|
_log.LogDebug("Dungeon {DungeonId} destroyed on pod {PodId}", dungeonId, _podId);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogWarning(ex, "Failed to track dungeon destruction in Redis");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task HandleHubStateRequest(IConnection connection, DungeonPlayerSession session, CancellationToken ct)
|
private async Task HandleHubStateRequest(IConnection connection, DungeonPlayerSession session, CancellationToken ct)
|
||||||
{
|
{
|
||||||
var hubState = GetHubState(session.Character);
|
var hubState = GetHubState(session.Character);
|
||||||
@@ -454,6 +667,10 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
};
|
};
|
||||||
|
|
||||||
_dungeons[dungeonId] = instance;
|
_dungeons[dungeonId] = instance;
|
||||||
|
|
||||||
|
// Track in Redis
|
||||||
|
_ = TrackDungeonCreatedAsync(instance);
|
||||||
|
|
||||||
_log.LogInformation("Player {Name} (Level {Level}) created new dungeon {Id} (tier {Tier}, bracket {Min}-{Max})",
|
_log.LogInformation("Player {Name} (Level {Level}) created new dungeon {Id} (tier {Tier}, bracket {Min}-{Max})",
|
||||||
session.Character.Name, playerLevel, dungeonId, tier, bracketMin, bracketMax);
|
session.Character.Name, playerLevel, dungeonId, tier, bracketMin, bracketMax);
|
||||||
}
|
}
|
||||||
@@ -849,12 +1066,14 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
{
|
{
|
||||||
_ = PersistDungeonAsync(instance, CancellationToken.None);
|
_ = PersistDungeonAsync(instance, CancellationToken.None);
|
||||||
_dungeons.TryRemove(dungeonId, out _);
|
_dungeons.TryRemove(dungeonId, out _);
|
||||||
|
_ = TrackDungeonDestroyedAsync(dungeonId);
|
||||||
_log.LogInformation("Dungeon {Id} persisted after 10 minutes empty", dungeonId);
|
_log.LogInformation("Dungeon {Id} persisted after 10 minutes empty", dungeonId);
|
||||||
}
|
}
|
||||||
// If boss killed and empty for 5+ minutes, just remove (cleared)
|
// If boss killed and empty for 5+ minutes, just remove (cleared)
|
||||||
else if (instance.BossKilled && emptyTime.TotalMinutes >= 5)
|
else if (instance.BossKilled && emptyTime.TotalMinutes >= 5)
|
||||||
{
|
{
|
||||||
_dungeons.TryRemove(dungeonId, out _);
|
_dungeons.TryRemove(dungeonId, out _);
|
||||||
|
_ = TrackDungeonDestroyedAsync(dungeonId);
|
||||||
_log.LogInformation("Cleared dungeon {Id} removed after 5 minutes empty", dungeonId);
|
_log.LogInformation("Cleared dungeon {Id} removed after 5 minutes empty", dungeonId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -874,11 +1093,15 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
var broadcasts = _broadcastService.GetPendingBroadcasts();
|
var broadcasts = _broadcastService.GetPendingBroadcasts();
|
||||||
if (broadcasts.Count == 0) return;
|
if (broadcasts.Count == 0) return;
|
||||||
|
|
||||||
|
_log.LogInformation("Processing {Count} pending broadcasts, {PlayerCount} players online",
|
||||||
|
broadcasts.Count, _playerSessions.Count);
|
||||||
|
|
||||||
foreach (var broadcast in broadcasts)
|
foreach (var broadcast in broadcasts)
|
||||||
{
|
{
|
||||||
// Send to all connected player sessions
|
// Send to all connected player sessions
|
||||||
foreach (var session in _playerSessions.Values)
|
foreach (var session in _playerSessions.Values)
|
||||||
{
|
{
|
||||||
|
_log.LogDebug("Sending broadcast to player session {ConnectionId}", session.ConnectionId);
|
||||||
_ = SendAsync(session.Connection, DungeonOpCodes.SERVER_BROADCAST, new
|
_ = SendAsync(session.Connection, DungeonOpCodes.SERVER_BROADCAST, new
|
||||||
{
|
{
|
||||||
message = broadcast.Message,
|
message = broadcast.Message,
|
||||||
@@ -2841,6 +3064,9 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
{
|
{
|
||||||
if (_playerSessions.TryRemove(connectionId, out var session))
|
if (_playerSessions.TryRemove(connectionId, out var session))
|
||||||
{
|
{
|
||||||
|
// Track disconnection in Redis
|
||||||
|
_ = TrackPlayerDisconnectedAsync(session);
|
||||||
|
|
||||||
if (session.CurrentDungeonId.HasValue)
|
if (session.CurrentDungeonId.HasValue)
|
||||||
{
|
{
|
||||||
_ = RemoveFromDungeonAsync(session, false, CancellationToken.None);
|
_ = RemoveFromDungeonAsync(session, false, CancellationToken.None);
|
||||||
|
|||||||
@@ -21,10 +21,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
<!-- Stats -->
|
<!-- Stats -->
|
||||||
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
|
<div class="grid grid-cols-2 md:grid-cols-5 gap-4">
|
||||||
<div class="card p-4">
|
<div class="card p-4">
|
||||||
<p class="text-2xl font-bold text-green-400">@Model.ActiveConnections</p>
|
<p class="text-2xl font-bold text-green-400">@Model.ActiveConnections</p>
|
||||||
<p class="text-sm text-gray-400">Active Connections</p>
|
<p class="text-sm text-gray-400">Online Players</p>
|
||||||
|
</div>
|
||||||
|
<div class="card p-4">
|
||||||
|
<p class="text-2xl font-bold text-purple-400">@Model.ActiveDungeons</p>
|
||||||
|
<p class="text-sm text-gray-400">Active Dungeons</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="card p-4">
|
<div class="card p-4">
|
||||||
<p class="text-2xl font-bold text-white">@Model.TotalAccounts</p>
|
<p class="text-2xl font-bold text-white">@Model.TotalAccounts</p>
|
||||||
|
|||||||
@@ -1,37 +1,57 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using DataServer.Data;
|
using DataServer.Data;
|
||||||
using DataServer.Data.Entities;
|
using Server.Redis;
|
||||||
using Server.Services;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace Server.Services.Admin.Pages.Server;
|
namespace Server.Services.Admin.Pages.Server;
|
||||||
|
|
||||||
public class IndexModel : AdminPageModel
|
public class IndexModel : AdminPageModel
|
||||||
{
|
{
|
||||||
private readonly GameDbContext _db;
|
private readonly GameDbContext _db;
|
||||||
private readonly IServerBroadcastService _broadcast;
|
private readonly IRedisManager _redis;
|
||||||
|
private readonly ILogger<IndexModel> _logger;
|
||||||
private static readonly List<BroadcastRecord> _recentBroadcasts = new();
|
private static readonly List<BroadcastRecord> _recentBroadcasts = new();
|
||||||
private static readonly object _lock = new();
|
private static readonly object _lock = new();
|
||||||
|
|
||||||
public IndexModel(GameDbContext db, IServerBroadcastService broadcast)
|
public IndexModel(GameDbContext db, IRedisManager redis, ILogger<IndexModel> logger)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_broadcast = broadcast;
|
_redis = redis;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int ActiveConnections { get; set; }
|
public int ActiveConnections { get; set; }
|
||||||
|
public int ActiveDungeons { get; set; }
|
||||||
public int TotalAccounts { get; set; }
|
public int TotalAccounts { get; set; }
|
||||||
public int TotalCharacters { get; set; }
|
public int TotalCharacters { get; set; }
|
||||||
public int TrackedAssets { get; set; }
|
public int TrackedAssets { get; set; }
|
||||||
|
|
||||||
public List<BroadcastRecord> RecentBroadcasts { get; set; } = new();
|
public List<BroadcastRecord> RecentBroadcasts { get; set; } = new();
|
||||||
|
|
||||||
|
[TempData]
|
||||||
public string? Message { get; set; }
|
public string? Message { get; set; }
|
||||||
|
[TempData]
|
||||||
public bool IsError { get; set; }
|
public bool IsError { get; set; }
|
||||||
|
|
||||||
public async Task OnGetAsync()
|
public async Task OnGetAsync()
|
||||||
{
|
{
|
||||||
ActiveConnections = _broadcast.GetActiveConnectionCount();
|
// Get stats from Redis
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var playerCount = await _redis.Db.StringGetAsync("stats:players");
|
||||||
|
if (playerCount.HasValue && int.TryParse(playerCount, out var pc))
|
||||||
|
ActiveConnections = pc;
|
||||||
|
|
||||||
|
var dungeonCount = await _redis.Db.StringGetAsync("stats:dungeons");
|
||||||
|
if (dungeonCount.HasValue && int.TryParse(dungeonCount, out var dc))
|
||||||
|
ActiveDungeons = dc;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Failed to get stats from Redis");
|
||||||
|
}
|
||||||
|
|
||||||
TotalAccounts = await _db.Accounts.CountAsync();
|
TotalAccounts = await _db.Accounts.CountAsync();
|
||||||
TotalCharacters = await _db.DungeonCharacters.CountAsync(c => !c.IsDeleted);
|
TotalCharacters = await _db.DungeonCharacters.CountAsync(c => !c.IsDeleted);
|
||||||
TrackedAssets = await _db.ClientAssets.CountAsync();
|
TrackedAssets = await _db.ClientAssets.CountAsync();
|
||||||
@@ -48,66 +68,137 @@ public class IndexModel : AdminPageModel
|
|||||||
{
|
{
|
||||||
Message = "Message cannot be empty.";
|
Message = "Message cannot be empty.";
|
||||||
IsError = true;
|
IsError = true;
|
||||||
await OnGetAsync();
|
return RedirectToPage();
|
||||||
return Page();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_broadcast.QueueBroadcast(message, messageType);
|
try
|
||||||
|
{
|
||||||
|
// Publish to Redis for all pods to pick up
|
||||||
|
var payload = JsonSerializer.Serialize(new
|
||||||
|
{
|
||||||
|
type = messageType ?? "system",
|
||||||
|
message = message,
|
||||||
|
duration = 10,
|
||||||
|
timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
|
});
|
||||||
|
|
||||||
|
await _redis.Bus.PublishAsync("server:broadcast", payload);
|
||||||
|
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
_recentBroadcasts.Add(new BroadcastRecord(message, messageType, DateTime.UtcNow));
|
_recentBroadcasts.Add(new BroadcastRecord(message, messageType ?? "system", DateTime.UtcNow));
|
||||||
// Keep only last 50
|
|
||||||
while (_recentBroadcasts.Count > 50)
|
while (_recentBroadcasts.Count > 50)
|
||||||
_recentBroadcasts.RemoveAt(0);
|
_recentBroadcasts.RemoveAt(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Message = $"Broadcast queued ({messageType}): {message}";
|
Message = $"Broadcast sent ({messageType}): {message}";
|
||||||
await OnGetAsync();
|
_logger.LogInformation("Admin broadcast sent: [{Type}] {Message}", messageType, message);
|
||||||
return Page();
|
}
|
||||||
|
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()
|
public async Task<IActionResult> OnPostRefreshAssetsAsync()
|
||||||
{
|
{
|
||||||
_broadcast.QueueAssetRefresh();
|
try
|
||||||
_broadcast.QueueBroadcast("Asset update available. Your client will refresh automatically.", "info");
|
{
|
||||||
|
var commandPayload = JsonSerializer.Serialize(new
|
||||||
|
{
|
||||||
|
command = "refresh_assets",
|
||||||
|
timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
|
});
|
||||||
|
await _redis.Bus.PublishAsync("server:command", commandPayload);
|
||||||
|
|
||||||
|
var broadcastPayload = JsonSerializer.Serialize(new
|
||||||
|
{
|
||||||
|
type = "info",
|
||||||
|
message = "Asset update available. Your client will refresh automatically.",
|
||||||
|
duration = 10,
|
||||||
|
timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
|
});
|
||||||
|
await _redis.Bus.PublishAsync("server:broadcast", broadcastPayload);
|
||||||
|
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
_recentBroadcasts.Add(new BroadcastRecord("Asset refresh signal sent", "system", DateTime.UtcNow));
|
_recentBroadcasts.Add(new BroadcastRecord("Asset refresh signal sent", "system", DateTime.UtcNow));
|
||||||
}
|
}
|
||||||
|
|
||||||
Message = "Asset refresh signal queued.";
|
Message = "Asset refresh signal sent to all pods.";
|
||||||
await OnGetAsync();
|
}
|
||||||
return Page();
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Failed to send asset refresh");
|
||||||
|
Message = $"Failed: {ex.Message}";
|
||||||
|
IsError = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RedirectToPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> OnPostMaintenanceWarningAsync()
|
public async Task<IActionResult> OnPostMaintenanceWarningAsync()
|
||||||
{
|
{
|
||||||
_broadcast.QueueBroadcast("⚠️ Server maintenance in 5 minutes. Please finish your current activities.", "announcement");
|
try
|
||||||
|
{
|
||||||
|
var payload = JsonSerializer.Serialize(new
|
||||||
|
{
|
||||||
|
type = "announcement",
|
||||||
|
message = "⚠️ Server maintenance in 5 minutes. Please finish your current activities.",
|
||||||
|
duration = 30,
|
||||||
|
timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
|
});
|
||||||
|
await _redis.Bus.PublishAsync("server:broadcast", payload);
|
||||||
|
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
_recentBroadcasts.Add(new BroadcastRecord("Maintenance warning (5 min)", "announcement", DateTime.UtcNow));
|
_recentBroadcasts.Add(new BroadcastRecord("Maintenance warning (5 min)", "announcement", DateTime.UtcNow));
|
||||||
}
|
}
|
||||||
|
|
||||||
Message = "Maintenance warning sent.";
|
Message = "Maintenance warning sent to all pods.";
|
||||||
await OnGetAsync();
|
}
|
||||||
return Page();
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Failed to send maintenance warning");
|
||||||
|
Message = $"Failed: {ex.Message}";
|
||||||
|
IsError = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RedirectToPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> OnPostServerRestartAsync()
|
public async Task<IActionResult> OnPostServerRestartAsync()
|
||||||
{
|
{
|
||||||
_broadcast.QueueBroadcast("🔄 Server restart imminent. Please save your progress and return to hub.", "alert");
|
try
|
||||||
|
{
|
||||||
|
var payload = JsonSerializer.Serialize(new
|
||||||
|
{
|
||||||
|
type = "alert",
|
||||||
|
message = "🔄 Server restart imminent. Please save your progress and return to hub.",
|
||||||
|
duration = 30,
|
||||||
|
timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
||||||
|
});
|
||||||
|
await _redis.Bus.PublishAsync("server:broadcast", payload);
|
||||||
|
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
_recentBroadcasts.Add(new BroadcastRecord("Server restart warning", "alert", DateTime.UtcNow));
|
_recentBroadcasts.Add(new BroadcastRecord("Server restart warning", "alert", DateTime.UtcNow));
|
||||||
}
|
}
|
||||||
|
|
||||||
Message = "Server restart warning sent.";
|
Message = "Server restart warning sent to all pods.";
|
||||||
await OnGetAsync();
|
}
|
||||||
return Page();
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Failed to send restart warning");
|
||||||
|
Message = $"Failed: {ex.Message}";
|
||||||
|
IsError = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RedirectToPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public record BroadcastRecord(string Message, string Type, DateTime SentAt);
|
public record BroadcastRecord(string Message, string Type, DateTime SentAt);
|
||||||
|
|||||||
Reference in New Issue
Block a user