PSO2SERVER/Server/Protocol/Handlers/11-ClientHandler/11-05-CharacterCreate.cs
2024-11-29 10:01:28 +08:00

90 lines
3.3 KiB
C#

using System.IO;
using System.Data.Entity;
using PSO2SERVER.Models;
using PSO2SERVER.Protocol.Packets;
using PSO2SERVER.Database;
using System.Linq;
namespace PSO2SERVER.Protocol.Handlers
{
[PacketHandlerAttr(0x11, 0x05)]
public class CharacterCreate : PacketHandler
{
#region implemented abstract members of PacketHandler
public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
{
if (context._account == null)
return;
var reader = new PacketReader(data, position, size);
var info = string.Format("[<--] 接收到的数据 (hex): {0} 字节", data.Length);
Logger.WriteHex(info, data);
reader.ReadUInt32();//CharacterID 未预设的空值
reader.ReadUInt32();//AccountID 未预设的空值
var unk1 = reader.ReadUInt32();//Unk1
var VoiceType = reader.ReadUInt32(); // VoiceType
var unk2 = reader.ReadUInt16(); // 5 unknown bytes
var VoicePitch = reader.ReadInt16(); // VoiceData
var name = reader.ReadFixedLengthUtf16(16);
reader.BaseStream.Seek(0x4, SeekOrigin.Current); // Padding
var looks = reader.ReadStruct<Character.LooksParam>();
var jobs = reader.ReadStruct<Character.JobParam>();
Logger.WriteStructBinary(info, jobs);
Logger.WriteInternal("[CHR] {0} 创建了名为 {1} 的新角色.", context._account.Username, name);
var newCharacter = new Character
{
CharacterID = 0,
AccountID = context._account.AccountId,
Unk1 = unk1,
VoiceType = VoiceType,
Unk2 = unk2,
VoicePitch = VoicePitch,
Name = name,
Jobs = jobs,
Looks = looks,
Account = context._account
};
// Add to database
using (var db = new ServerEf())
{
// Check if any characters exist for this player
var existingCharacters = db.Characters.Where(c => c.Account.AccountId == context._account.AccountId).ToList();
if (existingCharacters.Count > 0)
{
// Increment ID if characters already exist
newCharacter.CharacterID = existingCharacters.Max(c => c.CharacterID) + 1;
}
else
{
// Start with ID 1 if no characters exist
newCharacter.CharacterID = 1;
}
//Logger.Write("newCharacter.CharacterId {0} {1}", newCharacter.CharacterId, context._account.AccountId);
db.Characters.Add(newCharacter);
db.Entry(newCharacter.Account).State = EntityState.Modified;
db.SaveChanges();
}
// Assign character to player
context.Character = newCharacter;
// Set Account ID
context.SendPacket(new CharacterCreateResponsePacket(CharacterCreateResponsePacket.CharacterCreationStatus.Success, (uint)context._account.AccountId));
// Spawn
context.SendPacket(new NoPayloadPacket(0x11, 0x3E));
}
#endregion
}
}