Handler
This commit is contained in:
+217
-144
@@ -23,21 +23,21 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
private readonly ISessionRegistry _sessions;
|
private readonly ISessionRegistry _sessions;
|
||||||
private readonly IMessageSerializer _ser;
|
private readonly IMessageSerializer _ser;
|
||||||
private readonly IServiceScopeFactory _scopeFactory;
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
// Active dungeon instances
|
// Active dungeon instances
|
||||||
private readonly ConcurrentDictionary<Guid, DungeonInstance> _dungeons = new();
|
private readonly ConcurrentDictionary<Guid, DungeonInstance> _dungeons = new();
|
||||||
|
|
||||||
// Mob definitions cache
|
// Mob definitions cache
|
||||||
private Dictionary<int, DungeonMobDefinition> _mobDefinitions = new();
|
private Dictionary<int, DungeonMobDefinition> _mobDefinitions = new();
|
||||||
private bool _initialized = false;
|
private bool _initialized = false;
|
||||||
|
|
||||||
// Tick management
|
// Tick management
|
||||||
private Timer? _tickTimer;
|
private Timer? _tickTimer;
|
||||||
private long _currentTick = 0;
|
private long _currentTick = 0;
|
||||||
|
|
||||||
public DungeonHandler(
|
public DungeonHandler(
|
||||||
ILogger<DungeonHandler> log,
|
ILogger<DungeonHandler> log,
|
||||||
ISessionRegistry sessions,
|
ISessionRegistry sessions,
|
||||||
@@ -49,7 +49,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
_ser = ser;
|
_ser = ser;
|
||||||
_scopeFactory = scopeFactory;
|
_scopeFactory = scopeFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyCollection<int> SupportedOpCodes => new[]
|
public IReadOnlyCollection<int> SupportedOpCodes => new[]
|
||||||
{
|
{
|
||||||
DungeonOpCodes.HUB_STATE,
|
DungeonOpCodes.HUB_STATE,
|
||||||
@@ -63,29 +63,29 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
DungeonOpCodes.ESCAPE_START,
|
DungeonOpCodes.ESCAPE_START,
|
||||||
DungeonOpCodes.ESCAPE_CANCEL
|
DungeonOpCodes.ESCAPE_CANCEL
|
||||||
};
|
};
|
||||||
|
|
||||||
public async Task InitializeAsync()
|
public async Task InitializeAsync()
|
||||||
{
|
{
|
||||||
if (_initialized) return;
|
if (_initialized) return;
|
||||||
|
|
||||||
await LoadMobDefinitionsAsync();
|
await LoadMobDefinitionsAsync();
|
||||||
|
|
||||||
// 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 with {MobCount} mob definitions", _mobDefinitions.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadMobDefinitionsAsync()
|
private async Task LoadMobDefinitionsAsync()
|
||||||
{
|
{
|
||||||
using var scope = _scopeFactory.CreateScope();
|
using var scope = _scopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||||
|
|
||||||
var mobs = await db.Set<DungeonMobDefinition>()
|
var mobs = await db.Set<DungeonMobDefinition>()
|
||||||
.Where(m => m.IsEnabled)
|
.Where(m => m.IsEnabled)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
if (mobs.Count == 0)
|
if (mobs.Count == 0)
|
||||||
{
|
{
|
||||||
// Seed default mobs
|
// Seed default mobs
|
||||||
@@ -95,10 +95,10 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
mobs = defaults;
|
mobs = defaults;
|
||||||
_log.LogInformation("Seeded {Count} default mob definitions", defaults.Count);
|
_log.LogInformation("Seeded {Count} default mob definitions", defaults.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
_mobDefinitions = mobs.ToDictionary(m => m.Id);
|
_mobDefinitions = mobs.ToDictionary(m => m.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task HandleAsync(int opCode, IConnection connection, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
public async Task HandleAsync(int opCode, IConnection connection, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
||||||
{
|
{
|
||||||
// Ensure initialized
|
// Ensure initialized
|
||||||
@@ -106,14 +106,14 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
{
|
{
|
||||||
await InitializeAsync();
|
await InitializeAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
// All dungeon operations require authentication
|
// All dungeon operations require authentication
|
||||||
if (!_sessions.TryGet(connection.Id, out var accountId, out _))
|
if (!_sessions.TryGet(connection.Id, out var accountId, out _))
|
||||||
{
|
{
|
||||||
await SendErrorAsync(connection, "auth_required", ct);
|
await SendErrorAsync(connection, "auth_required", ct);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get or create player session
|
// Get or create player session
|
||||||
var session = await GetOrCreateSessionAsync(connection, accountId, ct);
|
var session = await GetOrCreateSessionAsync(connection, accountId, ct);
|
||||||
if (session == null)
|
if (session == null)
|
||||||
@@ -121,7 +121,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
await SendErrorAsync(connection, "no_character", ct);
|
await SendErrorAsync(connection, "no_character", ct);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
switch (opCode)
|
switch (opCode)
|
||||||
@@ -129,39 +129,39 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
case DungeonOpCodes.HUB_STATE:
|
case DungeonOpCodes.HUB_STATE:
|
||||||
await HandleHubStateRequest(connection, session, ct);
|
await HandleHubStateRequest(connection, session, ct);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.STAT_ALLOCATE:
|
case DungeonOpCodes.STAT_ALLOCATE:
|
||||||
await HandleStatAllocate(connection, session, payload, ct);
|
await HandleStatAllocate(connection, session, payload, ct);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.DUNGEON_ENTER:
|
case DungeonOpCodes.DUNGEON_ENTER:
|
||||||
await HandleDungeonEnter(connection, session, payload, ct);
|
await HandleDungeonEnter(connection, session, payload, ct);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.DUNGEON_LEAVE:
|
case DungeonOpCodes.DUNGEON_LEAVE:
|
||||||
await HandleDungeonLeave(connection, session, ct);
|
await HandleDungeonLeave(connection, session, ct);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.DUNGEON_INPUT_MOVE:
|
case DungeonOpCodes.DUNGEON_INPUT_MOVE:
|
||||||
HandleInputMove(session, payload);
|
HandleInputMove(session, payload);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.DUNGEON_INPUT_ATTACK:
|
case DungeonOpCodes.DUNGEON_INPUT_ATTACK:
|
||||||
HandleInputAttack(session, payload);
|
HandleInputAttack(session, payload);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.DUNGEON_INPUT_PICKUP:
|
case DungeonOpCodes.DUNGEON_INPUT_PICKUP:
|
||||||
HandleInputPickup(session, payload);
|
HandleInputPickup(session, payload);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.DUNGEON_INPUT_CHEST:
|
case DungeonOpCodes.DUNGEON_INPUT_CHEST:
|
||||||
HandleInputChest(session, payload);
|
HandleInputChest(session, payload);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.ESCAPE_START:
|
case DungeonOpCodes.ESCAPE_START:
|
||||||
HandleEscapeStart(session);
|
HandleEscapeStart(session);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DungeonOpCodes.ESCAPE_CANCEL:
|
case DungeonOpCodes.ESCAPE_CANCEL:
|
||||||
HandleEscapeCancel(session);
|
HandleEscapeCancel(session);
|
||||||
break;
|
break;
|
||||||
@@ -173,21 +173,21 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
await SendErrorAsync(connection, "internal_error", ct);
|
await SendErrorAsync(connection, "internal_error", ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<DungeonPlayerSession?> GetOrCreateSessionAsync(IConnection connection, ulong accountId, CancellationToken ct)
|
private async Task<DungeonPlayerSession?> GetOrCreateSessionAsync(IConnection connection, ulong accountId, CancellationToken ct)
|
||||||
{
|
{
|
||||||
if (_playerSessions.TryGetValue(connection.Id, out var existing))
|
if (_playerSessions.TryGetValue(connection.Id, out var existing))
|
||||||
{
|
{
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load character from database
|
// Load character from database
|
||||||
using var scope = _scopeFactory.CreateScope();
|
using var scope = _scopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||||
|
|
||||||
var character = await db.Set<DungeonCharacter>()
|
var character = await db.Set<DungeonCharacter>()
|
||||||
.FirstOrDefaultAsync(c => c.AccountId == accountId && !c.IsDeleted, ct);
|
.FirstOrDefaultAsync(c => c.AccountId == accountId && !c.IsDeleted, ct);
|
||||||
|
|
||||||
if (character == null)
|
if (character == null)
|
||||||
{
|
{
|
||||||
// Auto-create a character for this account
|
// Auto-create a character for this account
|
||||||
@@ -203,12 +203,12 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
Endurance = 1,
|
Endurance = 1,
|
||||||
Dexterity = 1
|
Dexterity = 1
|
||||||
};
|
};
|
||||||
|
|
||||||
db.Set<DungeonCharacter>().Add(character);
|
db.Set<DungeonCharacter>().Add(character);
|
||||||
await db.SaveChangesAsync(ct);
|
await db.SaveChangesAsync(ct);
|
||||||
_log.LogInformation("Created new dungeon character for account {AccountId}", accountId);
|
_log.LogInformation("Created new dungeon character for account {AccountId}", accountId);
|
||||||
}
|
}
|
||||||
|
|
||||||
var session = new DungeonPlayerSession
|
var session = new DungeonPlayerSession
|
||||||
{
|
{
|
||||||
ConnectionId = connection.Id,
|
ConnectionId = connection.Id,
|
||||||
@@ -218,25 +218,25 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
Character = character,
|
Character = character,
|
||||||
State = PlayerState.InHub
|
State = PlayerState.InHub
|
||||||
};
|
};
|
||||||
|
|
||||||
_playerSessions[connection.Id] = session;
|
_playerSessions[connection.Id] = session;
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
await SendAsync(connection, DungeonOpCodes.HUB_STATE, hubState, ct);
|
await SendAsync(connection, DungeonOpCodes.HUB_STATE, hubState, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleStatAllocate(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
private async Task HandleStatAllocate(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
||||||
{
|
{
|
||||||
var request = _ser.Deserialize<StatAllocateRequest>(payload);
|
var request = _ser.Deserialize<StatAllocateRequest>(payload);
|
||||||
if (string.IsNullOrEmpty(request.Stat)) return;
|
if (string.IsNullOrEmpty(request.Stat)) return;
|
||||||
|
|
||||||
var character = session.Character;
|
var character = session.Character;
|
||||||
if (character.UnspentStatPoints <= 0) return;
|
if (character.UnspentStatPoints <= 0) return;
|
||||||
|
|
||||||
switch (request.Stat.ToLower())
|
switch (request.Stat.ToLower())
|
||||||
{
|
{
|
||||||
case "strength": character.Strength++; break;
|
case "strength": character.Strength++; break;
|
||||||
@@ -246,9 +246,9 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
case "dexterity": character.Dexterity++; break;
|
case "dexterity": character.Dexterity++; break;
|
||||||
default: return;
|
default: return;
|
||||||
}
|
}
|
||||||
|
|
||||||
character.UnspentStatPoints--;
|
character.UnspentStatPoints--;
|
||||||
|
|
||||||
// Save to database
|
// Save to database
|
||||||
using var scope = _scopeFactory.CreateScope();
|
using var scope = _scopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||||
@@ -260,10 +260,10 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
db.Entry(character).Property(c => c.Dexterity).IsModified = true;
|
db.Entry(character).Property(c => c.Dexterity).IsModified = true;
|
||||||
db.Entry(character).Property(c => c.UnspentStatPoints).IsModified = true;
|
db.Entry(character).Property(c => c.UnspentStatPoints).IsModified = true;
|
||||||
await db.SaveChangesAsync(ct);
|
await db.SaveChangesAsync(ct);
|
||||||
|
|
||||||
await SendAsync(connection, DungeonOpCodes.HUB_STATE, GetHubState(character), ct);
|
await SendAsync(connection, DungeonOpCodes.HUB_STATE, GetHubState(character), ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleDungeonEnter(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
private async Task HandleDungeonEnter(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
||||||
{
|
{
|
||||||
if (session.State == PlayerState.InDungeon)
|
if (session.State == PlayerState.InDungeon)
|
||||||
@@ -271,36 +271,63 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
await SendAsync(connection, DungeonOpCodes.DUNGEON_ENTER_RESULT, new { ok = false, error = "already_in_dungeon" }, ct);
|
await SendAsync(connection, DungeonOpCodes.DUNGEON_ENTER_RESULT, new { ok = false, error = "already_in_dungeon" }, ct);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var request = _ser.Deserialize<DungeonEnterRequest>(payload);
|
var request = _ser.Deserialize<DungeonEnterRequest>(payload);
|
||||||
int tier = Math.Clamp(request.Tier, 1, 10);
|
int tier = Math.Clamp(request.Tier, 1, 10);
|
||||||
|
|
||||||
// Create new dungeon instance
|
const int maxPlayersPerDungeon = 4;
|
||||||
var dungeonId = Guid.NewGuid();
|
|
||||||
int seed = Random.Shared.Next();
|
// Try to find an existing dungeon of the same tier that isn't full
|
||||||
|
DungeonInstance? instance = null;
|
||||||
var layout = DungeonGenerator.Generate(seed, tier);
|
Guid dungeonId = Guid.Empty;
|
||||||
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions);
|
|
||||||
|
foreach (var (id, dungeon) in _dungeons)
|
||||||
var instance = new DungeonInstance
|
|
||||||
{
|
{
|
||||||
Id = dungeonId,
|
if (dungeon.Tier == tier &&
|
||||||
Tier = tier,
|
dungeon.Simulation.Phase == DungeonPhase.Active &&
|
||||||
Seed = seed,
|
dungeon.Simulation.Players.Count < maxPlayersPerDungeon &&
|
||||||
Simulation = simulation,
|
(DateTime.UtcNow - dungeon.CreatedAt).TotalSeconds < 60) // Only join dungeons < 60s old
|
||||||
CreatedAt = DateTime.UtcNow
|
{
|
||||||
};
|
instance = dungeon;
|
||||||
|
dungeonId = id;
|
||||||
_dungeons[dungeonId] = instance;
|
_log.LogInformation("Player {Name} joining existing dungeon {Id} (tier {Tier}, {Players}/{Max} players)",
|
||||||
|
session.Character.Name, id, tier, dungeon.Simulation.Players.Count, maxPlayersPerDungeon);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new dungeon if none found
|
||||||
|
if (instance == null)
|
||||||
|
{
|
||||||
|
dungeonId = Guid.NewGuid();
|
||||||
|
int seed = Random.Shared.Next();
|
||||||
|
|
||||||
|
var layout = DungeonGenerator.Generate(seed, tier);
|
||||||
|
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions);
|
||||||
|
|
||||||
|
instance = new DungeonInstance
|
||||||
|
{
|
||||||
|
Id = dungeonId,
|
||||||
|
Tier = tier,
|
||||||
|
Seed = seed,
|
||||||
|
Simulation = simulation,
|
||||||
|
CreatedAt = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
|
||||||
|
_dungeons[dungeonId] = instance;
|
||||||
|
_log.LogInformation("Player {Name} created new dungeon {Id} (tier {Tier})", session.Character.Name, dungeonId, tier);
|
||||||
|
}
|
||||||
|
|
||||||
|
var layout2 = instance.Simulation.Layout;
|
||||||
|
|
||||||
// Add player to dungeon
|
// Add player to dungeon
|
||||||
var spawnPos = layout.Rooms[0]; // Entrance room
|
var spawnPos = layout2.Rooms[0]; // Entrance room
|
||||||
var player = new PlayerInstance
|
var player = new PlayerInstance
|
||||||
{
|
{
|
||||||
CharacterId = session.CharacterId,
|
CharacterId = session.CharacterId,
|
||||||
CharacterName = session.Character.Name,
|
CharacterName = session.Character.Name,
|
||||||
X = spawnPos.CenterX * 32,
|
X = spawnPos.CenterX * 32 + Random.Shared.Next(-20, 20), // Slight offset to avoid stacking
|
||||||
Y = spawnPos.CenterY * 32,
|
Y = spawnPos.CenterY * 32 + Random.Shared.Next(-20, 20),
|
||||||
Health = session.Character.CurrentHealth,
|
Health = session.Character.CurrentHealth,
|
||||||
MaxHealth = DungeonCharacterStats.GetMaxHealth(session.Character),
|
MaxHealth = DungeonCharacterStats.GetMaxHealth(session.Character),
|
||||||
Mana = session.Character.CurrentMana,
|
Mana = session.Character.CurrentMana,
|
||||||
@@ -312,27 +339,32 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
Strength = session.Character.Strength,
|
Strength = session.Character.Strength,
|
||||||
Intelligence = session.Character.Intelligence
|
Intelligence = session.Character.Intelligence
|
||||||
};
|
};
|
||||||
|
|
||||||
simulation.Players[session.CharacterId] = player;
|
instance.Simulation.Players[session.CharacterId] = player;
|
||||||
|
|
||||||
// Update session state
|
// Update session state
|
||||||
session.State = PlayerState.InDungeon;
|
session.State = PlayerState.InDungeon;
|
||||||
session.CurrentDungeonId = dungeonId;
|
session.CurrentDungeonId = dungeonId;
|
||||||
session.PlayerInstance = player;
|
session.PlayerInstance = player;
|
||||||
|
|
||||||
_log.LogInformation("Player {Name} entered dungeon tier {Tier}", session.Character.Name, tier);
|
|
||||||
|
|
||||||
await SendAsync(connection, DungeonOpCodes.DUNGEON_ENTER_RESULT, new
|
await SendAsync(connection, DungeonOpCodes.DUNGEON_ENTER_RESULT, new
|
||||||
{
|
{
|
||||||
ok = true,
|
ok = true,
|
||||||
dungeonId = dungeonId.ToString(),
|
dungeonId = dungeonId.ToString(),
|
||||||
tier,
|
tier = instance.Tier,
|
||||||
seed,
|
seed = instance.Seed,
|
||||||
width = layout.Width,
|
width = layout2.Width,
|
||||||
height = layout.Height,
|
height = layout2.Height,
|
||||||
tiles = layout.SerializeTiles(),
|
tiles = layout2.SerializeTiles(),
|
||||||
spawn = new { x = player.X, y = player.Y },
|
spawn = new { x = player.X, y = player.Y },
|
||||||
mobs = simulation.Mobs.Values.Select(m => new
|
players = instance.Simulation.Players.Values.Select(p => new
|
||||||
|
{
|
||||||
|
id = p.CharacterId,
|
||||||
|
name = p.CharacterName,
|
||||||
|
x = p.X,
|
||||||
|
y = p.Y
|
||||||
|
}),
|
||||||
|
mobs = instance.Simulation.Mobs.Values.Select(m => new
|
||||||
{
|
{
|
||||||
id = m.Id,
|
id = m.Id,
|
||||||
name = m.Name,
|
name = m.Name,
|
||||||
@@ -341,7 +373,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
health = m.Health,
|
health = m.Health,
|
||||||
maxHealth = m.MaxHealth
|
maxHealth = m.MaxHealth
|
||||||
}),
|
}),
|
||||||
chests = simulation.Chests.Values.Select(c => new
|
chests = instance.Simulation.Chests.Values.Select(c => new
|
||||||
{
|
{
|
||||||
id = c.Id,
|
id = c.Id,
|
||||||
x = c.X,
|
x = c.X,
|
||||||
@@ -351,58 +383,98 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
})
|
})
|
||||||
}, ct);
|
}, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleDungeonLeave(IConnection connection, DungeonPlayerSession session, CancellationToken ct)
|
private async Task HandleDungeonLeave(IConnection connection, DungeonPlayerSession session, CancellationToken ct)
|
||||||
{
|
{
|
||||||
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue)
|
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await RemoveFromDungeonAsync(session, false, ct);
|
await RemoveFromDungeonAsync(session, false, ct);
|
||||||
|
|
||||||
await SendAsync(connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), ct);
|
await SendAsync(connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleInputMove(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
private void HandleInputMove(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
||||||
{
|
{
|
||||||
if (session.PlayerInstance == null) return;
|
if (session.PlayerInstance == null) return;
|
||||||
|
|
||||||
var input = _ser.Deserialize<MoveInput>(payload);
|
var input = _ser.Deserialize<MoveInput>(payload);
|
||||||
session.PlayerInstance.InputX = input.X;
|
session.PlayerInstance.InputX = input.Dx;
|
||||||
session.PlayerInstance.InputY = input.Y;
|
session.PlayerInstance.InputY = input.Dy;
|
||||||
|
session.PlayerInstance.Rotation = input.AimAngle;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleInputAttack(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
private void HandleInputAttack(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
||||||
{
|
{
|
||||||
if (session.PlayerInstance == null) return;
|
if (session.PlayerInstance == null) return;
|
||||||
|
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
||||||
|
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
||||||
|
|
||||||
var input = _ser.Deserialize<AttackInput>(payload);
|
var input = _ser.Deserialize<AttackInput>(payload);
|
||||||
session.PlayerInstance.AttackTargetId = input.TargetId;
|
var player = session.PlayerInstance;
|
||||||
session.PlayerInstance.WantsToAttack = true;
|
|
||||||
|
// Find nearest mob in attack direction (within attack range and angle tolerance)
|
||||||
|
const float attackRange = 60f;
|
||||||
|
const float angleToleranceRad = 0.8f; // ~45 degrees
|
||||||
|
|
||||||
|
MobInstance? bestTarget = null;
|
||||||
|
float bestDist = float.MaxValue;
|
||||||
|
|
||||||
|
foreach (var mob in dungeon.Simulation.Mobs.Values.Where(m => m.IsAlive))
|
||||||
|
{
|
||||||
|
float dx = mob.X - player.X;
|
||||||
|
float dy = mob.Y - player.Y;
|
||||||
|
float dist = MathF.Sqrt(dx * dx + dy * dy);
|
||||||
|
|
||||||
|
if (dist > attackRange) continue;
|
||||||
|
|
||||||
|
float angleToMob = MathF.Atan2(dy, dx);
|
||||||
|
float angleDiff = MathF.Abs(NormalizeAngle(angleToMob - input.AimAngle));
|
||||||
|
|
||||||
|
if (angleDiff <= angleToleranceRad && dist < bestDist)
|
||||||
|
{
|
||||||
|
bestDist = dist;
|
||||||
|
bestTarget = mob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bestTarget != null)
|
||||||
|
{
|
||||||
|
player.AttackTargetId = (ulong)bestTarget.Id;
|
||||||
|
player.WantsToAttack = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static float NormalizeAngle(float angle)
|
||||||
|
{
|
||||||
|
while (angle > MathF.PI) angle -= 2 * MathF.PI;
|
||||||
|
while (angle < -MathF.PI) angle += 2 * MathF.PI;
|
||||||
|
return angle;
|
||||||
|
}
|
||||||
|
|
||||||
private void HandleInputPickup(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
private void HandleInputPickup(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
||||||
{
|
{
|
||||||
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
||||||
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
||||||
|
|
||||||
var input = _ser.Deserialize<PickupInput>(payload);
|
var input = _ser.Deserialize<PickupInput>(payload);
|
||||||
|
|
||||||
if (dungeon.Simulation.DroppedItems.TryRemove((int)input.ItemId, out var item))
|
if (dungeon.Simulation.DroppedItems.TryRemove((int)input.ItemId, out var item))
|
||||||
{
|
{
|
||||||
// TODO: Add to player inventory
|
// TODO: Add to player inventory
|
||||||
_log.LogDebug("Player {Name} picked up item {ItemId}", session.Character.Name, item.ItemDefinitionId);
|
_log.LogDebug("Player {Name} picked up item {ItemId}", session.Character.Name, item.ItemDefinitionId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleInputChest(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
private void HandleInputChest(DungeonPlayerSession session, ReadOnlyMemory<byte> payload)
|
||||||
{
|
{
|
||||||
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
||||||
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
||||||
|
|
||||||
var input = _ser.Deserialize<ChestInput>(payload);
|
var input = _ser.Deserialize<ChestInput>(payload);
|
||||||
|
|
||||||
if (dungeon.Simulation.Chests.TryGetValue((int)input.ChestId, out var chest) && !chest.IsOpened)
|
if (dungeon.Simulation.Chests.TryGetValue((int)input.ChestId, out var chest) && !chest.IsOpened)
|
||||||
{
|
{
|
||||||
chest.IsOpened = true;
|
chest.IsOpened = true;
|
||||||
@@ -410,50 +482,50 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
_log.LogDebug("Player {Name} opened chest {ChestId}", session.Character.Name, chest.Id);
|
_log.LogDebug("Player {Name} opened chest {ChestId}", session.Character.Name, chest.Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleEscapeStart(DungeonPlayerSession session)
|
private void HandleEscapeStart(DungeonPlayerSession session)
|
||||||
{
|
{
|
||||||
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return;
|
||||||
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return;
|
||||||
|
|
||||||
// Check if at exit
|
// Check if at exit
|
||||||
var layout = dungeon.Simulation.Layout;
|
var layout = dungeon.Simulation.Layout;
|
||||||
if (layout.ExitPosition == null) return;
|
if (layout.ExitPosition == null) return;
|
||||||
|
|
||||||
var player = session.PlayerInstance;
|
var player = session.PlayerInstance;
|
||||||
if (player == null) return;
|
if (player == null) return;
|
||||||
|
|
||||||
float exitX = layout.ExitPosition.Value.X * 32 + 16;
|
float exitX = layout.ExitPosition.Value.X * 32 + 16;
|
||||||
float exitY = layout.ExitPosition.Value.Y * 32 + 16;
|
float exitY = layout.ExitPosition.Value.Y * 32 + 16;
|
||||||
float dist = MathF.Sqrt((player.X - exitX) * (player.X - exitX) + (player.Y - exitY) * (player.Y - exitY));
|
float dist = MathF.Sqrt((player.X - exitX) * (player.X - exitX) + (player.Y - exitY) * (player.Y - exitY));
|
||||||
|
|
||||||
if (dist < 48) // Within range
|
if (dist < 48) // Within range
|
||||||
{
|
{
|
||||||
session.IsEscaping = true;
|
session.IsEscaping = true;
|
||||||
session.EscapeStartTick = _currentTick;
|
session.EscapeStartTick = _currentTick;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleEscapeCancel(DungeonPlayerSession session)
|
private void HandleEscapeCancel(DungeonPlayerSession session)
|
||||||
{
|
{
|
||||||
session.IsEscaping = false;
|
session.IsEscaping = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void TickAll(object? state)
|
private void TickAll(object? state)
|
||||||
{
|
{
|
||||||
_currentTick++;
|
_currentTick++;
|
||||||
|
|
||||||
foreach (var (dungeonId, instance) in _dungeons)
|
foreach (var (dungeonId, instance) in _dungeons)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = instance.Simulation.Tick(_currentTick);
|
var result = instance.Simulation.Tick(_currentTick);
|
||||||
|
|
||||||
// Send state to all players in this dungeon
|
// Send state to all players in this dungeon
|
||||||
var playersInDungeon = _playerSessions.Values
|
var playersInDungeon = _playerSessions.Values
|
||||||
.Where(s => s.CurrentDungeonId == dungeonId && s.State == PlayerState.InDungeon)
|
.Where(s => s.CurrentDungeonId == dungeonId && s.State == PlayerState.InDungeon)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
foreach (var session in playersInDungeon)
|
foreach (var session in playersInDungeon)
|
||||||
{
|
{
|
||||||
// Check escape completion (5 seconds = 100 ticks)
|
// Check escape completion (5 seconds = 100 ticks)
|
||||||
@@ -462,17 +534,17 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
_ = CompleteEscapeAsync(session);
|
_ = CompleteEscapeAsync(session);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if player died
|
// Check if player died
|
||||||
if (session.PlayerInstance != null && !session.PlayerInstance.IsAlive)
|
if (session.PlayerInstance != null && !session.PlayerInstance.IsAlive)
|
||||||
{
|
{
|
||||||
_ = HandlePlayerDeathAsync(session);
|
_ = HandlePlayerDeathAsync(session);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = SendDungeonStateAsync(session, result);
|
_ = SendDungeonStateAsync(session, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup empty dungeons
|
// Cleanup empty dungeons
|
||||||
if (playersInDungeon.Count == 0 && (DateTime.UtcNow - instance.CreatedAt).TotalMinutes > 5)
|
if (playersInDungeon.Count == 0 && (DateTime.UtcNow - instance.CreatedAt).TotalMinutes > 5)
|
||||||
{
|
{
|
||||||
@@ -485,21 +557,21 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendDungeonStateAsync(DungeonPlayerSession session, TickResult? tickResult)
|
private async Task SendDungeonStateAsync(DungeonPlayerSession session, TickResult? tickResult)
|
||||||
{
|
{
|
||||||
var dungeon = _dungeons.GetValueOrDefault(session.CurrentDungeonId!.Value);
|
var dungeon = _dungeons.GetValueOrDefault(session.CurrentDungeonId!.Value);
|
||||||
if (dungeon == null) return;
|
if (dungeon == null) return;
|
||||||
|
|
||||||
var sim = dungeon.Simulation;
|
var sim = dungeon.Simulation;
|
||||||
|
|
||||||
var state = new
|
var state = new
|
||||||
{
|
{
|
||||||
tick = _currentTick,
|
tick = _currentTick,
|
||||||
phase = sim.Phase.ToString().ToLower(),
|
phase = sim.Phase.ToString().ToLower(),
|
||||||
|
|
||||||
//tiles = sim.Layout.Tiles,
|
// NOTE: tiles are NOT sent every tick - they're static and sent once on DUNGEON_ENTER_RESULT
|
||||||
|
|
||||||
players = sim.Players.Values.Select(p => new
|
players = sim.Players.Values.Select(p => new
|
||||||
{
|
{
|
||||||
id = p.CharacterId,
|
id = p.CharacterId,
|
||||||
@@ -516,7 +588,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
isAlive = p.IsAlive,
|
isAlive = p.IsAlive,
|
||||||
isInCombat = p.IsInCombat
|
isInCombat = p.IsInCombat
|
||||||
}),
|
}),
|
||||||
|
|
||||||
mobs = sim.Mobs.Values.Where(m => m.IsAlive).Select(m => new
|
mobs = sim.Mobs.Values.Where(m => m.IsAlive).Select(m => new
|
||||||
{
|
{
|
||||||
id = m.Id,
|
id = m.Id,
|
||||||
@@ -532,7 +604,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
spriteHeight = m.SpriteHeight,
|
spriteHeight = m.SpriteHeight,
|
||||||
tintColor = m.TintColor
|
tintColor = m.TintColor
|
||||||
}),
|
}),
|
||||||
|
|
||||||
chests = sim.Chests.Values.Select(c => new
|
chests = sim.Chests.Values.Select(c => new
|
||||||
{
|
{
|
||||||
id = c.Id,
|
id = c.Id,
|
||||||
@@ -540,7 +612,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
y = c.Y,
|
y = c.Y,
|
||||||
opened = c.IsOpened
|
opened = c.IsOpened
|
||||||
}),
|
}),
|
||||||
|
|
||||||
droppedItems = sim.DroppedItems.Values.Select(i => new
|
droppedItems = sim.DroppedItems.Values.Select(i => new
|
||||||
{
|
{
|
||||||
id = i.Id,
|
id = i.Id,
|
||||||
@@ -549,7 +621,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
x = i.X,
|
x = i.X,
|
||||||
y = i.Y
|
y = i.Y
|
||||||
}),
|
}),
|
||||||
|
|
||||||
events = tickResult == null ? null : new
|
events = tickResult == null ? null : new
|
||||||
{
|
{
|
||||||
damage = tickResult.DamageEvents.Select(e => new
|
damage = tickResult.DamageEvents.Select(e => new
|
||||||
@@ -571,74 +643,74 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
xp = e.XpReward
|
xp = e.XpReward
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
exitPosition = sim.Layout.ExitPosition != null ? new
|
exitPosition = sim.Layout.ExitPosition != null ? new
|
||||||
{
|
{
|
||||||
x = sim.Layout.ExitPosition.Value.X * 32 + 16,
|
x = sim.Layout.ExitPosition.Value.X * 32 + 16,
|
||||||
y = sim.Layout.ExitPosition.Value.Y * 32 + 16
|
y = sim.Layout.ExitPosition.Value.Y * 32 + 16
|
||||||
} : null
|
} : null
|
||||||
};
|
};
|
||||||
|
|
||||||
await SendAsync(session.Connection, DungeonOpCodes.DUNGEON_STATE, state, CancellationToken.None);
|
await SendAsync(session.Connection, DungeonOpCodes.DUNGEON_STATE, state, CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CompleteEscapeAsync(DungeonPlayerSession session)
|
private async Task CompleteEscapeAsync(DungeonPlayerSession session)
|
||||||
{
|
{
|
||||||
session.IsEscaping = false;
|
session.IsEscaping = false;
|
||||||
|
|
||||||
await SendAsync(session.Connection, DungeonOpCodes.ESCAPE_SUCCESS, new { }, CancellationToken.None);
|
await SendAsync(session.Connection, DungeonOpCodes.ESCAPE_SUCCESS, new { }, CancellationToken.None);
|
||||||
await RemoveFromDungeonAsync(session, true, CancellationToken.None);
|
await RemoveFromDungeonAsync(session, true, CancellationToken.None);
|
||||||
await SendAsync(session.Connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), CancellationToken.None);
|
await SendAsync(session.Connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandlePlayerDeathAsync(DungeonPlayerSession session)
|
private async Task HandlePlayerDeathAsync(DungeonPlayerSession session)
|
||||||
{
|
{
|
||||||
await RemoveFromDungeonAsync(session, false, CancellationToken.None);
|
await RemoveFromDungeonAsync(session, false, CancellationToken.None);
|
||||||
|
|
||||||
// Reset health on death
|
// Reset health on death
|
||||||
session.Character.CurrentHealth = DungeonCharacterStats.GetMaxHealth(session.Character);
|
session.Character.CurrentHealth = DungeonCharacterStats.GetMaxHealth(session.Character);
|
||||||
session.Character.CurrentMana = DungeonCharacterStats.GetMaxMana(session.Character);
|
session.Character.CurrentMana = DungeonCharacterStats.GetMaxMana(session.Character);
|
||||||
session.Character.TotalDeaths++;
|
session.Character.TotalDeaths++;
|
||||||
|
|
||||||
using var scope = _scopeFactory.CreateScope();
|
using var scope = _scopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||||
db.Attach(session.Character);
|
db.Attach(session.Character);
|
||||||
db.Entry(session.Character).State = EntityState.Modified;
|
db.Entry(session.Character).State = EntityState.Modified;
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
await SendAsync(session.Connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), CancellationToken.None);
|
await SendAsync(session.Connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RemoveFromDungeonAsync(DungeonPlayerSession session, bool extracted, CancellationToken ct)
|
private async Task RemoveFromDungeonAsync(DungeonPlayerSession session, bool extracted, CancellationToken ct)
|
||||||
{
|
{
|
||||||
if (!session.CurrentDungeonId.HasValue) return;
|
if (!session.CurrentDungeonId.HasValue) return;
|
||||||
|
|
||||||
if (_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon))
|
if (_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon))
|
||||||
{
|
{
|
||||||
dungeon.Simulation.Players.TryRemove(session.CharacterId, out var player);
|
dungeon.Simulation.Players.TryRemove(session.CharacterId, out var player);
|
||||||
|
|
||||||
if (player != null)
|
if (player != null)
|
||||||
{
|
{
|
||||||
await SaveCharacterStateAsync(session.Character, player, extracted, ct);
|
await SaveCharacterStateAsync(session.Character, player, extracted, ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
session.State = PlayerState.InHub;
|
session.State = PlayerState.InHub;
|
||||||
session.CurrentDungeonId = null;
|
session.CurrentDungeonId = null;
|
||||||
session.PlayerInstance = null;
|
session.PlayerInstance = null;
|
||||||
session.IsEscaping = false;
|
session.IsEscaping = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SaveCharacterStateAsync(DungeonCharacter character, PlayerInstance player, bool extracted, CancellationToken ct)
|
private async Task SaveCharacterStateAsync(DungeonCharacter character, PlayerInstance player, bool extracted, CancellationToken ct)
|
||||||
{
|
{
|
||||||
using var scope = _scopeFactory.CreateScope();
|
using var scope = _scopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||||
|
|
||||||
// Add XP earned
|
// Add XP earned
|
||||||
character.Experience += player.XpGained;
|
character.Experience += player.XpGained;
|
||||||
character.TotalKills += player.MobsKilled;
|
character.TotalKills += player.MobsKilled;
|
||||||
character.PlayersKilled += player.PlayersKilled;
|
character.PlayersKilled += player.PlayersKilled;
|
||||||
|
|
||||||
if (extracted)
|
if (extracted)
|
||||||
{
|
{
|
||||||
character.DungeonsCompleted++;
|
character.DungeonsCompleted++;
|
||||||
@@ -651,24 +723,24 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
character.CurrentHealth = DungeonCharacterStats.GetMaxHealth(character);
|
character.CurrentHealth = DungeonCharacterStats.GetMaxHealth(character);
|
||||||
character.CurrentMana = DungeonCharacterStats.GetMaxMana(character);
|
character.CurrentMana = DungeonCharacterStats.GetMaxMana(character);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for level up
|
// Check for level up
|
||||||
while (character.Experience >= LevelSystem.GetTotalXpForLevel(character.Level + 1))
|
while (character.Experience >= LevelSystem.GetTotalXpForLevel(character.Level + 1))
|
||||||
{
|
{
|
||||||
character.Level++;
|
character.Level++;
|
||||||
character.UnspentStatPoints += 5;
|
character.UnspentStatPoints += 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
character.LastPlayedAt = DateTime.UtcNow;
|
character.LastPlayedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
db.Attach(character);
|
db.Attach(character);
|
||||||
db.Entry(character).State = EntityState.Modified;
|
db.Entry(character).State = EntityState.Modified;
|
||||||
await db.SaveChangesAsync(ct);
|
await db.SaveChangesAsync(ct);
|
||||||
|
|
||||||
_log.LogInformation("Saved character {Name}: Level {Level}, XP {Xp}, Extracted={Extracted}",
|
_log.LogInformation("Saved character {Name}: Level {Level}, XP {Xp}, Extracted={Extracted}",
|
||||||
character.Name, character.Level, character.Experience, extracted);
|
character.Name, character.Level, character.Experience, extracted);
|
||||||
}
|
}
|
||||||
|
|
||||||
private object GetHubState(DungeonCharacter character)
|
private object GetHubState(DungeonCharacter character)
|
||||||
{
|
{
|
||||||
return new
|
return new
|
||||||
@@ -692,7 +764,7 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendAsync(IConnection connection, int opCode, object payload, CancellationToken ct)
|
private async Task SendAsync(IConnection connection, int opCode, object payload, CancellationToken ct)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -707,12 +779,12 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
_log.LogWarning(ex, "Failed to send message to connection {Id}", connection.Id);
|
_log.LogWarning(ex, "Failed to send message to connection {Id}", connection.Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendErrorAsync(IConnection connection, string error, CancellationToken ct)
|
private async Task SendErrorAsync(IConnection connection, string error, CancellationToken ct)
|
||||||
{
|
{
|
||||||
await SendAsync(connection, OpCodes.ERROR, new { error }, ct);
|
await SendAsync(connection, OpCodes.ERROR, new { error }, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called when a connection is closed - cleanup session
|
/// Called when a connection is closed - cleanup session
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -743,13 +815,14 @@ public class DungeonEnterRequest
|
|||||||
|
|
||||||
public class MoveInput
|
public class MoveInput
|
||||||
{
|
{
|
||||||
public float X { get; set; }
|
public float Dx { get; set; }
|
||||||
public float Y { get; set; }
|
public float Dy { get; set; }
|
||||||
|
public float AimAngle { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AttackInput
|
public class AttackInput
|
||||||
{
|
{
|
||||||
public ulong TargetId { get; set; }
|
public float AimAngle { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PickupInput
|
public class PickupInput
|
||||||
|
|||||||
Reference in New Issue
Block a user