Working on the world.
This commit is contained in:
@@ -288,27 +288,140 @@ public sealed partial class WorldHandler
|
||||
return;
|
||||
}
|
||||
|
||||
// Award gold based on mob tier
|
||||
int goldDrop = Math.Max(1, mob.Stats.XpReward / 2);
|
||||
// Use LootService to generate loot from configured loot tables
|
||||
var lootResult = _lootService.GenerateMobLoot(mob.DefinitionId, 1, 1.0f);
|
||||
|
||||
// Fallback gold if loot table has no gold: use XP-based formula
|
||||
long goldDrop = lootResult.Gold > 0 ? lootResult.Gold : Math.Max(1, mob.Stats.XpReward / 2);
|
||||
|
||||
var character = await db.DungeonCharacters.FirstOrDefaultAsync(c => c.Id == s.CharacterId, ct);
|
||||
if (character != null)
|
||||
{
|
||||
character.Gold += goldDrop;
|
||||
|
||||
// Add item drops to character inventory
|
||||
foreach (var drop in lootResult.Items)
|
||||
{
|
||||
await AddWorldItemToInventoryAsync(db, s.CharacterId, drop.ItemDefinitionId, drop.Quantity, ct);
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
// Mark mob as looted (it will be cleaned up eventually)
|
||||
mob.LootEmptiedAtTick = zoneSim.CurrentTick;
|
||||
|
||||
var lootItems = lootResult.Items.Select(d => new
|
||||
{
|
||||
itemDefinitionId = d.ItemDefinitionId,
|
||||
itemName = d.Item?.Name ?? $"Item {d.ItemDefinitionId}",
|
||||
quantity = d.Quantity
|
||||
}).ToArray();
|
||||
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_MOB_LOOT_RESULT, new
|
||||
{
|
||||
success = true,
|
||||
mobId = mob.Id,
|
||||
gold = goldDrop,
|
||||
items = Array.Empty<object>() // TODO: Loot table integration in future
|
||||
items = lootItems
|
||||
}, ct);
|
||||
}
|
||||
|
||||
private async Task HandleWorldRespawnAsync(IConnection conn, WorldSessionState s, CancellationToken ct)
|
||||
{
|
||||
var zoneKey = WorldChunkCache.ZoneKey(s.RegionId, s.ZoneX, s.ZoneY, s.ZoneZ);
|
||||
if (!_activeZones.TryGetValue(zoneKey, out var zoneSim)) return;
|
||||
|
||||
if (!zoneSim.Players.TryGetValue(s.CharacterId, out var playerCombat))
|
||||
return;
|
||||
|
||||
// Must actually be dead to respawn
|
||||
if (playerCombat.IsAlive)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_RESPAWN_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "not_dead"
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Respawn at zone center (tile center of chunk 0,0)
|
||||
var region = await _cache.GetRegionAsync(s.RegionId, ct);
|
||||
if (region == null) return;
|
||||
|
||||
float spawnX = WorldMovementService.WorldPxX(region, s.ZoneX, 0, region.ChunkWidthTiles / 2);
|
||||
float spawnY = WorldMovementService.WorldPxY(region, s.ZoneY, 0, region.ChunkHeightTiles / 2);
|
||||
|
||||
// Reset combat state
|
||||
playerCombat.Health = playerCombat.MaxHealth;
|
||||
playerCombat.IsAlive = true;
|
||||
playerCombat.IsInCombat = false;
|
||||
playerCombat.IsInvulnerable = true;
|
||||
playerCombat.InvulnerabilityTimer = RespawnInvulnerabilitySeconds;
|
||||
playerCombat.StatusEffects.Clear();
|
||||
|
||||
// Move player to spawn position
|
||||
s.X = spawnX;
|
||||
s.Y = spawnY;
|
||||
s.VelX = 0;
|
||||
s.VelY = 0;
|
||||
s.IsMoving = false;
|
||||
|
||||
// Update chunk position
|
||||
var (pzX, pzY, pcX, pcY) = WorldMovementService.ToChunkPos(region, spawnX, spawnY);
|
||||
s.ChunkX = pcX;
|
||||
s.ChunkY = pcY;
|
||||
|
||||
// Increment death counter
|
||||
_ = IncrementDeathCounterAsync(s.CharacterId);
|
||||
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_RESPAWN_RESULT, new
|
||||
{
|
||||
success = true,
|
||||
x = spawnX,
|
||||
y = spawnY,
|
||||
health = playerCombat.Health,
|
||||
maxHealth = playerCombat.MaxHealth,
|
||||
invulnerableSeconds = RespawnInvulnerabilitySeconds
|
||||
}, ct);
|
||||
|
||||
// Broadcast to other players
|
||||
foreach (var player in zoneSim.Players.Values)
|
||||
{
|
||||
if (player.CharacterId == s.CharacterId) continue;
|
||||
await _broadcast.SendAsync(player.Session.Connection, OpCodes.WORLD_COMBAT_STATE, new
|
||||
{
|
||||
events = new[] { new
|
||||
{
|
||||
type = "PlayerRespawn",
|
||||
targetPlayerId = s.CharacterId,
|
||||
x = spawnX,
|
||||
y = spawnY
|
||||
}}
|
||||
}, ct);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task IncrementDeathCounterAsync(ulong characterId)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
var character = await db.DungeonCharacters.FirstOrDefaultAsync(c => c.Id == characterId);
|
||||
if (character != null)
|
||||
{
|
||||
character.TotalDeaths++;
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogWarning(ex, "Failed to increment death counter for character {CharacterId}", characterId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AwardXpAsync(ulong characterId, int xpAmount)
|
||||
{
|
||||
if (xpAmount <= 0) return;
|
||||
|
||||
Reference in New Issue
Block a user