Redis fix
This commit is contained in:
@@ -9,6 +9,7 @@ using DataServer.Data.Enums;
|
||||
using Server.Networking;
|
||||
using Server.Networking.Handling;
|
||||
using Server.Networking.Messages;
|
||||
using Server.Redis;
|
||||
using Server.Services;
|
||||
using Server.Services.Auth;
|
||||
|
||||
@@ -25,6 +26,10 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
private readonly IMessageSerializer _ser;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
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)
|
||||
private readonly ConcurrentDictionary<Guid, DungeonPlayerSession> _playerSessions = new();
|
||||
@@ -53,7 +58,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
IServiceScopeFactory scopeFactory,
|
||||
LootService lootService,
|
||||
CraftingHandler craftingHandler,
|
||||
IServerBroadcastService broadcastService)
|
||||
IServerBroadcastService broadcastService,
|
||||
IRedisManager redis)
|
||||
{
|
||||
_log = log;
|
||||
_sessions = sessions;
|
||||
@@ -62,6 +68,12 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
_lootService = lootService;
|
||||
_craftingHandler = craftingHandler;
|
||||
_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[]
|
||||
@@ -109,12 +121,99 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
|
||||
await LoadMobDefinitionsAsync();
|
||||
await _lootService.InitializeAsync();
|
||||
|
||||
// Subscribe to Redis broadcast channel for multi-pod support
|
||||
await SubscribeToBroadcastChannelAsync();
|
||||
|
||||
// Start tick loop (20 ticks per second)
|
||||
_tickTimer = new Timer(TickAll, null, 0, 50);
|
||||
|
||||
_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()
|
||||
@@ -339,8 +438,122 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
};
|
||||
|
||||
_playerSessions[connection.Id] = session;
|
||||
|
||||
// Track in Redis for global player count
|
||||
await TrackPlayerConnectedAsync(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)
|
||||
{
|
||||
@@ -454,6 +667,10 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
};
|
||||
|
||||
_dungeons[dungeonId] = instance;
|
||||
|
||||
// Track in Redis
|
||||
_ = TrackDungeonCreatedAsync(instance);
|
||||
|
||||
_log.LogInformation("Player {Name} (Level {Level}) created new dungeon {Id} (tier {Tier}, bracket {Min}-{Max})",
|
||||
session.Character.Name, playerLevel, dungeonId, tier, bracketMin, bracketMax);
|
||||
}
|
||||
@@ -849,12 +1066,14 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
{
|
||||
_ = PersistDungeonAsync(instance, CancellationToken.None);
|
||||
_dungeons.TryRemove(dungeonId, out _);
|
||||
_ = TrackDungeonDestroyedAsync(dungeonId);
|
||||
_log.LogInformation("Dungeon {Id} persisted after 10 minutes empty", dungeonId);
|
||||
}
|
||||
// If boss killed and empty for 5+ minutes, just remove (cleared)
|
||||
else if (instance.BossKilled && emptyTime.TotalMinutes >= 5)
|
||||
{
|
||||
_dungeons.TryRemove(dungeonId, out _);
|
||||
_ = TrackDungeonDestroyedAsync(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();
|
||||
if (broadcasts.Count == 0) return;
|
||||
|
||||
_log.LogInformation("Processing {Count} pending broadcasts, {PlayerCount} players online",
|
||||
broadcasts.Count, _playerSessions.Count);
|
||||
|
||||
foreach (var broadcast in broadcasts)
|
||||
{
|
||||
// Send to all connected player sessions
|
||||
foreach (var session in _playerSessions.Values)
|
||||
{
|
||||
_log.LogDebug("Sending broadcast to player session {ConnectionId}", session.ConnectionId);
|
||||
_ = SendAsync(session.Connection, DungeonOpCodes.SERVER_BROADCAST, new
|
||||
{
|
||||
message = broadcast.Message,
|
||||
@@ -2841,6 +3064,9 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
{
|
||||
if (_playerSessions.TryRemove(connectionId, out var session))
|
||||
{
|
||||
// Track disconnection in Redis
|
||||
_ = TrackPlayerDisconnectedAsync(session);
|
||||
|
||||
if (session.CurrentDungeonId.HasValue)
|
||||
{
|
||||
_ = RemoveFromDungeonAsync(session, false, CancellationToken.None);
|
||||
|
||||
Reference in New Issue
Block a user