2024-09-10 00:31:40 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Data.Entity;
|
2024-09-10 01:13:20 +08:00
|
|
|
|
using PSO2SERVER.Models;
|
|
|
|
|
using PSO2SERVER.Packets.PSOPackets;
|
|
|
|
|
using PSO2SERVER.Database;
|
2024-09-12 15:06:07 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System;
|
2024-09-17 11:29:41 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
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
|
|
|
|
{
|
|
|
|
|
[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.User == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-09-19 11:48:56 +08:00
|
|
|
|
|
|
|
|
|
PacketWriter w = new PacketWriter();
|
|
|
|
|
|
2024-09-10 00:31:40 +08:00
|
|
|
|
var reader = new PacketReader(data, position, size);
|
2024-09-19 11:48:56 +08:00
|
|
|
|
var info = string.Format("[<--] 接收到的数据 (hex): ");
|
|
|
|
|
Logger.WriteHex(info, data);
|
2024-09-17 11:29:41 +08:00
|
|
|
|
var setting = reader.ReadStruct<Character.CharParam>();
|
2024-09-12 15:06:07 +08:00
|
|
|
|
var name = reader.ReadFixedLengthUtf16(16);//玩家名称 宽字符
|
2024-09-10 00:31:40 +08:00
|
|
|
|
var looks = reader.ReadStruct<Character.LooksParam>();
|
2024-09-19 11:48:56 +08:00
|
|
|
|
var unk3 = reader.ReadUInt32();
|
2024-09-10 00:31:40 +08:00
|
|
|
|
var jobs = reader.ReadStruct<Character.JobParam>();
|
2024-09-19 11:48:56 +08:00
|
|
|
|
w.WriteStruct(jobs);
|
|
|
|
|
Logger.WriteHex(info, w.ToArray());
|
2024-09-10 00:31:40 +08:00
|
|
|
|
|
2024-09-12 17:49:10 +08:00
|
|
|
|
//Logger.WriteInternal("[CHR] {0} 创建了名为 {1} 的新角色.", context.User.Username, name);
|
2024-09-10 00:31:40 +08:00
|
|
|
|
var newCharacter = new Character
|
|
|
|
|
{
|
2024-09-19 11:48:56 +08:00
|
|
|
|
unk1 = setting.unk1,
|
|
|
|
|
voice_type = setting.voice_type,
|
|
|
|
|
unk2 = setting.unk2,
|
|
|
|
|
voice_pitch = setting.voice_pitch,
|
2024-09-10 00:31:40 +08:00
|
|
|
|
Name = name,
|
|
|
|
|
Looks = looks,
|
2024-09-19 11:48:56 +08:00
|
|
|
|
unk3 = unk3,
|
|
|
|
|
Jobs = jobs,
|
2024-09-10 00:31:40 +08:00
|
|
|
|
Player = context.User
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Add to database
|
2024-09-10 01:13:20 +08:00
|
|
|
|
using (var db = new ServerEf())
|
2024-09-12 15:06:07 +08:00
|
|
|
|
{
|
|
|
|
|
// Check if any characters exist for this player
|
|
|
|
|
var existingCharacters = db.Characters.Where(c => c.Player.PlayerId == context.User.PlayerId).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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 22:47:58 +08:00
|
|
|
|
newCharacter.player_id = context.User.PlayerId;
|
|
|
|
|
|
2024-09-12 17:49:10 +08:00
|
|
|
|
//Logger.Write("newCharacter.CharacterId {0} {1}", newCharacter.CharacterId, context.User.PlayerId);
|
2024-09-12 15:06:07 +08:00
|
|
|
|
|
2024-09-10 00:31:40 +08:00
|
|
|
|
db.Characters.Add(newCharacter);
|
|
|
|
|
db.Entry(newCharacter.Player).State = EntityState.Modified;
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assign character to player
|
|
|
|
|
context.Character = newCharacter;
|
|
|
|
|
|
|
|
|
|
// Set Player ID
|
2024-09-17 11:29:41 +08:00
|
|
|
|
context.SendPacket(new CharacterCreateResponsePacket(CharacterCreateResponsePacket.CharacterCreationStatus.Success, (uint)context.User.PlayerId));
|
2024-09-10 00:31:40 +08:00
|
|
|
|
|
|
|
|
|
// Spawn
|
|
|
|
|
context.SendPacket(new NoPayloadPacket(0x11, 0x3E));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|