new builder
This commit is contained in:
@@ -62,7 +62,7 @@ public sealed partial class DungeonHandler
|
||||
// Get active dungeons in player's bracket
|
||||
var availableDungeons = _dungeons.Values
|
||||
.Where(d => d.Simulation.Phase == DungeonPhase.Active &&
|
||||
!d.BossKilled)
|
||||
!d.AllBossesDefeated)
|
||||
.Select(d => new
|
||||
{
|
||||
id = d.Id.ToString(),
|
||||
@@ -75,6 +75,8 @@ public sealed partial class DungeonHandler
|
||||
playerCount = d.Simulation.Players.Count,
|
||||
maxPlayers = 20,
|
||||
isJoinWindowOpen = d.IsJoinWindowOpen,
|
||||
totalBosses = d.TotalBossCount,
|
||||
defeatedBosses = d.BossesDefeatedCount,
|
||||
createdAt = d.CreatedAt,
|
||||
secondsRemaining = d.IsJoinWindowOpen ? (int)(d.JoinWindowEnd - DateTime.UtcNow).TotalSeconds : 0
|
||||
})
|
||||
@@ -220,6 +222,7 @@ public sealed partial class DungeonHandler
|
||||
CreatorName = session.Character.Name,
|
||||
LayoutV2 = layoutV2 // Store V2 layout for visual tile data
|
||||
};
|
||||
instance.InitializeBossState(simulation.Mobs.Values.Count(m => m.Type == MobType.Boss));
|
||||
|
||||
_dungeons[dungeonId] = instance;
|
||||
_log.LogInformation("Player {Name} (Level {Level}) created dungeon {Id} (tier {Tier}, bracket {Min}-{Max})",
|
||||
@@ -268,7 +271,7 @@ public sealed partial class DungeonHandler
|
||||
}
|
||||
|
||||
// Check if boss killed
|
||||
if (instance.BossKilled)
|
||||
if (instance.AllBossesDefeated)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.DUNGEON_ENTER_RESULT, new { ok = false, error = "dungeon_cleared" }, ct);
|
||||
return;
|
||||
@@ -540,6 +543,7 @@ public sealed partial class DungeonHandler
|
||||
IsPersisted = false,
|
||||
LastPlayerActivity = DateTime.UtcNow
|
||||
};
|
||||
instance.InitializeBossState(simulation.Mobs.Values.Count(m => m.Type == MobType.Boss), persisted.BossKilled);
|
||||
|
||||
_log.LogInformation("Restored dungeon {Id} from persistence with fresh mobs", persisted.Id);
|
||||
return instance;
|
||||
@@ -557,6 +561,15 @@ public sealed partial class DungeonHandler
|
||||
/// </summary>
|
||||
private void RespawnAllMobs(DungeonInstance instance)
|
||||
{
|
||||
var defeatedBossMobIds = !instance.BossesRespawnOnReload
|
||||
? instance.GetDefeatedBossMobIdsSnapshot()
|
||||
: new HashSet<int>();
|
||||
|
||||
if (instance.BossesRespawnOnReload)
|
||||
{
|
||||
instance.ResetBossProgress();
|
||||
}
|
||||
|
||||
// Check if any existing mobs have carried player loot - don't respawn those
|
||||
var mobsWithLoot = instance.Simulation.Mobs.Values
|
||||
.Where(m => m.HasCarriedLoot)
|
||||
@@ -587,6 +600,15 @@ public sealed partial class DungeonHandler
|
||||
else
|
||||
{
|
||||
// Use the fresh mob
|
||||
if (!instance.BossesRespawnOnReload &&
|
||||
mob.Value.Type == MobType.Boss &&
|
||||
defeatedBossMobIds.Contains(mob.Key))
|
||||
{
|
||||
mob.Value.IsAlive = false;
|
||||
mob.Value.Health = 0;
|
||||
mob.Value.State = MobState.Dead;
|
||||
}
|
||||
|
||||
instance.Simulation.Mobs.TryAdd(mob.Key, mob.Value);
|
||||
}
|
||||
}
|
||||
@@ -612,8 +634,8 @@ public sealed partial class DungeonHandler
|
||||
}
|
||||
}
|
||||
|
||||
_log.LogDebug("Respawned {MobCount} mobs in dungeon {Id} (preserved {PreservedCount} with loot)",
|
||||
instance.Simulation.Mobs.Count, instance.Id, mobsWithLoot.Count);
|
||||
_log.LogDebug("Respawned {MobCount} mobs in dungeon {Id} (preserved {PreservedCount} with loot, bossesRespawn={BossesRespawn})",
|
||||
instance.Simulation.Mobs.Count, instance.Id, mobsWithLoot.Count, instance.BossesRespawnOnReload);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -621,7 +643,7 @@ public sealed partial class DungeonHandler
|
||||
/// </summary>
|
||||
private async Task PersistDungeonAsync(DungeonInstance instance, CancellationToken ct)
|
||||
{
|
||||
if (instance.IsPersisted || instance.BossKilled) return;
|
||||
if (instance.IsPersisted || instance.AllBossesDefeated) return;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -653,7 +675,7 @@ public sealed partial class DungeonHandler
|
||||
// Update existing record
|
||||
existing.LastActivity = instance.LastPlayerActivity;
|
||||
existing.PersistedAt = DateTime.UtcNow;
|
||||
existing.BossKilled = instance.BossKilled;
|
||||
existing.BossKilled = instance.AllBossesDefeated;
|
||||
existing.MobStateJson = System.Text.Json.JsonSerializer.Serialize(mobStates);
|
||||
existing.ChestStateJson = System.Text.Json.JsonSerializer.Serialize(chestStates);
|
||||
}
|
||||
@@ -672,7 +694,7 @@ public sealed partial class DungeonHandler
|
||||
CreatedAt = instance.CreatedAt,
|
||||
LastActivity = instance.LastPlayerActivity,
|
||||
PersistedAt = DateTime.UtcNow,
|
||||
BossKilled = instance.BossKilled,
|
||||
BossKilled = instance.AllBossesDefeated,
|
||||
MobStateJson = System.Text.Json.JsonSerializer.Serialize(mobStates),
|
||||
ChestStateJson = System.Text.Json.JsonSerializer.Serialize(chestStates)
|
||||
};
|
||||
|
||||
@@ -611,6 +611,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
if (dungeon.Tier == tier &&
|
||||
dungeon.LevelBracketMin == bracketMin && // Must match bracket
|
||||
dungeon.Simulation.Phase == DungeonPhase.Active &&
|
||||
!dungeon.AllBossesDefeated &&
|
||||
dungeon.Simulation.Players.Count < maxPlayersPerDungeon &&
|
||||
dungeon.IsJoinWindowOpen) // Use join window instead of hardcoded time
|
||||
{
|
||||
@@ -695,6 +696,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
CreatorName = session.Character.Name,
|
||||
LayoutV2 = layoutV2 // Store V2 layout for visual tile data
|
||||
};
|
||||
instance.InitializeBossState(simulation.Mobs.Values.Count(m => m.Type == MobType.Boss));
|
||||
|
||||
_dungeons[dungeonId] = instance;
|
||||
|
||||
@@ -1414,8 +1416,14 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
{
|
||||
if (killedMob.Type == MobType.Boss)
|
||||
{
|
||||
instance.BossKilled = true;
|
||||
_log.LogInformation("Boss killed in dungeon {Id}! Dungeon is now cleared.", dungeonId);
|
||||
if (instance.RegisterBossDefeat(death.EntityId))
|
||||
{
|
||||
_log.LogInformation(
|
||||
"Boss defeated in dungeon {Id}: {Defeated}/{Total} bosses cleared.",
|
||||
dungeonId,
|
||||
instance.BossesDefeatedCount,
|
||||
instance.TotalBossCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1564,14 +1572,14 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
{
|
||||
var emptyTime = DateTime.UtcNow - instance.LastPlayerActivity;
|
||||
|
||||
if (!instance.BossKilled && !instance.IsPersisted && emptyTime.TotalMinutes >= 5)
|
||||
if (!instance.AllBossesDefeated && !instance.IsPersisted && emptyTime.TotalMinutes >= 5)
|
||||
{
|
||||
_ = PersistDungeonAsync(instance, CancellationToken.None);
|
||||
_dungeons.TryRemove(dungeonId, out _);
|
||||
_ = TrackDungeonDestroyedAsync(dungeonId);
|
||||
_log.LogInformation("Dungeon {Id} persisted after 10 minutes empty", dungeonId);
|
||||
}
|
||||
else if (instance.BossKilled && emptyTime.TotalMinutes >= 5)
|
||||
else if (instance.AllBossesDefeated && emptyTime.TotalMinutes >= 5)
|
||||
{
|
||||
_dungeons.TryRemove(dungeonId, out _);
|
||||
_ = TrackDungeonDestroyedAsync(dungeonId);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Concurrent;
|
||||
using DataServer.Data.Entities;
|
||||
using DataServer.Data.Enums;
|
||||
|
||||
namespace GameServer.Dungeon;
|
||||
|
||||
@@ -68,7 +69,7 @@ public partial class DungeonHandler
|
||||
if (dungeon.IsPersisted)
|
||||
return (false, "Dungeon already archived.");
|
||||
|
||||
if (dungeon.BossKilled)
|
||||
if (dungeon.AllBossesDefeated)
|
||||
return (false, "Boss was killed; dungeon is marked for cleanup, not archive.");
|
||||
|
||||
await PersistDungeonAsync(dungeon, ct);
|
||||
@@ -93,7 +94,7 @@ public partial class DungeonHandler
|
||||
TotalPlayers = dungeons.Sum(d => d.Simulation.Players.Count),
|
||||
TotalMobsAlive = dungeons.Sum(d => d.Simulation.Mobs.Values.Count(m => m.IsAlive)),
|
||||
CompletedCount = dungeons.Count(d => d.Simulation.Phase == DungeonPhase.Cleared),
|
||||
BossesKilled = dungeons.Count(d => d.BossKilled)
|
||||
BossesKilled = dungeons.Count(d => d.AllBossesDefeated)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -168,6 +169,10 @@ public partial class DungeonHandler
|
||||
dungeon.Simulation.InitializeMobAbilities(mob, mobDef, stats);
|
||||
|
||||
dungeon.Simulation.Mobs[newMobId] = mob;
|
||||
if (mob.Type == MobType.Boss)
|
||||
{
|
||||
dungeon.RegisterBossSpawn(newMobId);
|
||||
}
|
||||
return mob;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,9 +30,13 @@ public class DungeonInstance
|
||||
public DateTime LastPlayerActivity { get; set; } = DateTime.UtcNow;
|
||||
public bool IsPersisted { get; set; } = false;
|
||||
public bool BossKilled { get; set; } = false;
|
||||
public int TotalBossCount { get; set; } = 1;
|
||||
public int BossesDefeatedCount { get; set; } = 0;
|
||||
public bool BossesRespawnOnReload { get; set; } = false;
|
||||
|
||||
// Admin editing
|
||||
public bool HasPendingTileUpdates { get; set; } = false;
|
||||
private HashSet<int> _defeatedBossMobIds { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Check if a player level is within the bracket range
|
||||
@@ -58,4 +62,56 @@ public class DungeonInstance
|
||||
/// Get the bracket number (1, 2, 3, etc.)
|
||||
/// </summary>
|
||||
public int BracketNumber => (LevelBracketMin - 1) / 10 + 1;
|
||||
|
||||
public bool AllBossesDefeated => TotalBossCount > 0 && BossesDefeatedCount >= TotalBossCount;
|
||||
|
||||
public void InitializeBossState(int totalBossCount, bool allBossesDefeated = false)
|
||||
{
|
||||
TotalBossCount = Math.Max(1, totalBossCount);
|
||||
BossesDefeatedCount = allBossesDefeated ? TotalBossCount : 0;
|
||||
_defeatedBossMobIds.Clear();
|
||||
BossKilled = AllBossesDefeated;
|
||||
}
|
||||
|
||||
public void RegisterBossSpawn(int mobId)
|
||||
{
|
||||
if (_defeatedBossMobIds.Contains(mobId))
|
||||
return;
|
||||
|
||||
TotalBossCount = Math.Max(1, TotalBossCount + 1);
|
||||
BossKilled = AllBossesDefeated;
|
||||
}
|
||||
|
||||
public bool RegisterBossDefeat(int mobId)
|
||||
{
|
||||
if (_defeatedBossMobIds.Contains(mobId))
|
||||
return false;
|
||||
|
||||
_defeatedBossMobIds.Add(mobId);
|
||||
BossesDefeatedCount = Math.Min(TotalBossCount, BossesDefeatedCount + 1);
|
||||
BossKilled = AllBossesDefeated;
|
||||
return true;
|
||||
}
|
||||
|
||||
public HashSet<int> GetDefeatedBossMobIdsSnapshot()
|
||||
{
|
||||
return new HashSet<int>(_defeatedBossMobIds);
|
||||
}
|
||||
|
||||
public void SetDefeatedBossMobIds(IEnumerable<int> mobIds)
|
||||
{
|
||||
_defeatedBossMobIds.Clear();
|
||||
foreach (var mobId in mobIds)
|
||||
_defeatedBossMobIds.Add(mobId);
|
||||
|
||||
BossesDefeatedCount = Math.Min(TotalBossCount, _defeatedBossMobIds.Count);
|
||||
BossKilled = AllBossesDefeated;
|
||||
}
|
||||
|
||||
public void ResetBossProgress()
|
||||
{
|
||||
BossesDefeatedCount = 0;
|
||||
_defeatedBossMobIds.Clear();
|
||||
BossKilled = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user