PSO2SERVER/Server/Packets/Handlers/11-ClientHandler/11-04-CharacterSelected.cs

77 lines
2.5 KiB
C#
Raw Normal View History

2024-09-10 01:13:20 +08:00
using PSO2SERVER.Database;
using PSO2SERVER.Packets.PSOPackets;
using PSO2SERVER.Party;
2024-09-20 16:10:43 +08:00
using System;
2024-09-12 15:06:07 +08:00
using System.Linq;
2024-09-10 00:31:40 +08:00
2024-09-10 01:13:20 +08:00
namespace PSO2SERVER.Packets.Handlers
2024-09-10 00:31:40 +08:00
{
2024-09-16 15:30:00 +08:00
[PacketHandlerAttr(0x11, 0x04)]
2024-09-19 11:48:56 +08:00
public class CharacterSelected : PacketHandler
2024-09-10 00:31:40 +08:00
{
2024-09-19 11:48:56 +08:00
public struct CharacterSelectedPacket
{
/// <summary>
/// Selected character ID.
/// </summary>
public uint CharId;
public uint Unk1;
public uint Unk2;
}
2024-09-10 00:31:40 +08:00
#region implemented abstract members of PacketHandler
public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
{
2024-09-20 21:58:37 +08:00
if (context._account == null)
2024-09-20 11:48:27 +08:00
return;
2024-09-10 00:31:40 +08:00
var reader = new PacketReader(data, position, size);
2024-09-19 11:48:56 +08:00
var pkt = reader.ReadStruct<CharacterSelectedPacket>();
2024-09-10 00:31:40 +08:00
2024-09-20 16:10:43 +08:00
var charId = pkt.CharId;
2024-09-20 21:58:37 +08:00
Logger.Write("id {0}", charId);
2024-09-12 15:06:07 +08:00
2024-09-10 00:31:40 +08:00
if (context.Character == null) // On character create, this is already set.
{
2024-09-10 01:13:20 +08:00
using (var db = new ServerEf())
2024-09-10 00:31:40 +08:00
{
2024-09-20 16:10:43 +08:00
try
2024-09-12 15:06:07 +08:00
{
2024-09-20 21:58:37 +08:00
var character = db.Characters.FirstOrDefault(c => c.Player_ID == charId);
2024-09-20 16:10:43 +08:00
2024-09-20 21:58:37 +08:00
if (character == null || character.Account == null || character.Account.AccountId != context._account.AccountId)
2024-09-20 16:10:43 +08:00
{
Logger.WriteError("数据库中未找到 {0} 角色ID {1} ({2})"
2024-09-20 21:58:37 +08:00
, context._account.Username
2024-09-20 16:10:43 +08:00
, charId
2024-09-20 21:58:37 +08:00
, context._account.AccountId
2024-09-12 15:06:07 +08:00
);
2024-09-20 16:10:43 +08:00
context.Socket.Close();
return;
}
context.Character = character;
}
catch (Exception ex)
{
Logger.WriteError("查询角色时发生异常: {0}", ex.Message);
2024-09-12 15:06:07 +08:00
context.Socket.Close();
}
2024-09-10 00:31:40 +08:00
}
}
// 将客户端加入空余的队伍中
2024-09-10 00:31:40 +08:00
PartyManager.Instance.CreateNewParty(context);
// 告诉客户端切换到加载界面
context.SendPacket(new LoadingScreenTransitionPacket());
2024-09-10 00:31:40 +08:00
// TODO Set area, Set character, possibly more. See PolarisLegacy for more.
}
#endregion
}
}