using System.IO; using System.Data.Entity; using PSO2SERVER.Models; using PSO2SERVER.Protocol.Packets; using PSO2SERVER.Database; using System.Linq; using static PSO2SERVER.Models.Character; namespace PSO2SERVER.Protocol.Handlers { [PacketHandlerAttr(0x11, 0x05)] public class CharacterCreate : PacketHandler { #region implemented abstract members of PacketHandler public struct CharacterCreatePacket { public uint CharacterID; public uint AccountID; public uint Unk1; public uint VoiceType; public ushort Unk2; public short VoicePitch; public string Name; public uint Unk3; public LooksParam Looks; public JobParam Jobs; public string NickName; } private CharacterCreatePacket packet = new CharacterCreatePacket(); 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); packet.CharacterID = reader.ReadUInt32(); packet.AccountID = reader.ReadUInt32(); packet.Unk1 = reader.ReadUInt32();//Unk1 packet.VoiceType = reader.ReadUInt32(); // VoiceType packet.Unk2 = reader.ReadUInt16(); // 5 unknown bytes packet.VoicePitch = reader.ReadInt16(); // VoiceData packet.Name = reader.ReadFixedLengthUtf16(16); packet.Unk3 = reader.ReadUInt32(); packet.Looks = reader.ReadStruct(); packet.Jobs = reader.ReadStruct(); packet.NickName = reader.ReadFixedLengthUtf16(16); reader.BaseStream.Seek(0x56, SeekOrigin.Current); // Padding var newCharacter = new Character { CharacterID = 0, AccountID = context._account.AccountId, Unk1 = packet.Unk1, VoiceType = packet.VoiceType, Unk2 = packet.Unk2, VoicePitch = packet.VoicePitch, Name = packet.Name, Unk3 = packet.Unk3, Jobs = packet.Jobs, Looks = packet.Looks, NickName = packet.NickName, 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("New character data: {0}, {1}, {2}, {3}, {4}", newCharacter.CharacterID, newCharacter.Name, newCharacter.VoiceType, newCharacter.VoicePitch, newCharacter.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)); } #endregion } }