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

55 lines
1.9 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)]
class CharacterNewNameRequestPacket : PacketHandler
{
public uint player_id { get; set; }
public string Name { get; set; }
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;
var reader = new PacketReader(data, position, size);
player_id = reader.ReadUInt32();
Name = reader.ReadFixedLengthUtf16(16);
NewNameStatus isdone = NewNameStatus.Failure;
// 修改数据库字段
using (var db = new ServerEf())
{
// 先检测角色是否存在
var existingCharacters = db.Characters.Where(c => c.Account.AccountId == context._account.AccountId).ToList();
if (existingCharacters.Count > 0)
{
// 更新角色名字字段
var character = existingCharacters.FirstOrDefault();
if (character != null)
{
// 确保名字不为空并且是16字节长度
character.Name = Name; // 用0填充至16字节
db.SaveChanges(); // 保存修改
isdone = NewNameStatus.Success;
}
}
}
context.SendPacket(new CharacterNewNamePacket(isdone, player_id, Name));
}
}
}