PSO2SERVER/Server/Packets/Handlers/11-ClientHandler/11-9B-CharacterNewNameRequest.cs

61 lines
2.2 KiB
C#
Raw Normal View History

2024-11-24 23:40:50 +08:00
using System;
using PSO2SERVER.Database;
using System.Data.Entity;
using PSO2SERVER.Models;
using PSO2SERVER.Packets.PSOPackets;
using static PSO2SERVER.Packets.PSOPackets.CharacterNewNamePacket;
using System.Linq;
namespace PSO2SERVER.Packets.Handlers
{
[PacketHandlerAttr(0x11, 0x9B)]
2024-11-24 23:52:52 +08:00
class CharacterNewNameRequest : PacketHandler
2024-11-24 23:40:50 +08:00
{
2024-11-24 23:52:52 +08:00
public struct CharacterNewNameRequestPacket
{
2024-11-25 23:33:41 +08:00
public uint CharacterID;
2024-11-24 23:52:52 +08:00
public string Name;
}
2024-11-24 23:40:50 +08:00
public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
{
var info = string.Format("[<--] 接收到的数据 (hex): {0} 字节", data.Length);
Logger.WriteHex(info, data);
if (context._account == null)
return;
2024-11-24 23:52:52 +08:00
// 创建一个结构体实例
var packet = new CharacterNewNameRequestPacket();
// 读取数据并填充到结构体中
2024-11-24 23:40:50 +08:00
var reader = new PacketReader(data, position, size);
2024-11-25 23:33:41 +08:00
packet.CharacterID = reader.ReadUInt32();
2024-11-24 23:52:52 +08:00
packet.Name = reader.ReadFixedLengthUtf16(16); // 读取16字节长度的名字
2024-11-24 23:40:50 +08:00
NewNameStatus isdone = NewNameStatus.Failure;
// 修改数据库字段
using (var db = new ServerEf())
{
// 先检测角色是否存在
2024-11-25 23:33:41 +08:00
var existingCharacters = db.Characters.Where(c => c.CharacterID == packet.CharacterID && c.AccountID == context._account.AccountId).ToList();
2024-11-24 23:40:50 +08:00
if (existingCharacters.Count > 0)
{
// 更新角色名字字段
var character = existingCharacters.FirstOrDefault();
if (character != null)
{
// 确保名字不为空并且是16字节长度
2024-11-24 23:52:52 +08:00
character.Name = packet.Name.PadRight(16, '\0'); // 确保名字是16字节
2024-11-24 23:40:50 +08:00
db.SaveChanges(); // 保存修改
isdone = NewNameStatus.Success;
}
}
}
2024-11-25 23:33:41 +08:00
context.SendPacket(new CharacterNewNamePacket(isdone, packet.CharacterID, packet.Name));
2024-11-24 23:40:50 +08:00
}
}
}