修正角色数据创建和选择

This commit is contained in:
Longfeng Qin 2024-09-12 15:06:07 +08:00
parent 934298497c
commit 30b4cab4af
4 changed files with 36 additions and 3 deletions

View File

@ -141,6 +141,8 @@ namespace PSO2SERVER.Models
// Probably more info than this
[Key]
public int Id { get; set; }
public int CharacterId { get; set; }
public virtual Player Player { get; set; }

View File

@ -3,6 +3,8 @@ using System.Data.Entity;
using PSO2SERVER.Models;
using PSO2SERVER.Packets.PSOPackets;
using PSO2SERVER.Database;
using System.Linq;
using System;
namespace PSO2SERVER.Packets.Handlers
{
@ -17,12 +19,14 @@ namespace PSO2SERVER.Packets.Handlers
return;
var reader = new PacketReader(data, position, size);
var info = string.Format("[<--] 接收到的数据 (hex): ");
Logger.WriteHex(info, data);
reader.ReadBytes(12); // 12 unknown bytes
reader.ReadByte(); // VoiceType
reader.ReadBytes(5); // 5 unknown bytes
reader.ReadUInt16(); // VoiceData
var name = reader.ReadFixedLengthUtf16(16);
var name = reader.ReadFixedLengthUtf16(16);//玩家名称 宽字符
reader.BaseStream.Seek(0x4, SeekOrigin.Current); // Padding
var looks = reader.ReadStruct<Character.LooksParam>();
@ -39,7 +43,22 @@ namespace PSO2SERVER.Packets.Handlers
// Add to database
using (var db = new ServerEf())
{
{
// 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;
}
Logger.Write("newCharacter.CharacterId {0} {1}", newCharacter.CharacterId, context.User.PlayerId);
db.Characters.Add(newCharacter);
db.Entry(newCharacter.Player).State = EntityState.Modified;
db.SaveChanges();

View File

@ -1,6 +1,7 @@
using PSO2SERVER.Database;
using PSO2SERVER.Packets.PSOPackets;
using PSO2SERVER.Party;
using System.Linq;
namespace PSO2SERVER.Packets.Handlers
{
@ -14,6 +15,8 @@ namespace PSO2SERVER.Packets.Handlers
var reader = new PacketReader(data, position, size);
var charId = reader.ReadUInt32();
Logger.Write("id {0}", charId);
if (context.User == null)
return;
@ -21,10 +24,18 @@ namespace PSO2SERVER.Packets.Handlers
{
using (var db = new ServerEf())
{
var character = db.Characters.Find((int)charId);
var character = db.Characters.Where(c => c.CharacterId == charId).First();
if (character == null || character.Player.PlayerId != context.User.PlayerId)
{
Logger.WriteError("数据库中未找到 {0} 角色ID {1} ({2})"
, context.User.Username
, charId
, context.User.PlayerId
);
context.Socket.Close();
return;
}
context.Character = character;
}

View File

@ -179,6 +179,7 @@ namespace PSO2SERVER
Server = new Server();
await InitializeConfigurationAsync();
await InitializeDatabaseAsync();
await InitializeQueryServers(QueryMode.AuthList, ServerShipProt, ServerShipProtNums); // Assuming this is synchronous