Implement PlayerAttrs as intended

This commit is contained in:
xeon 2024-02-22 23:09:07 +03:00
parent aa2bab9645
commit 934ef7d74a
4 changed files with 122 additions and 46 deletions

View file

@ -1,7 +1,6 @@
using GameServer.Controllers.Attributes;
using GameServer.Models;
using GameServer.Network;
using GameServer.Network.Messages;
using GameServer.Systems.Event;
using Protocol;
@ -22,40 +21,8 @@ internal class PlayerInfoController : Controller
{
RandomSeed = 1337,
Id = player.Id,
Birthday = 0,
Attributes =
{
new PlayerAttr
{
Key = (int)PlayerAttrKey.Name,
ValueType = (int)PlayerAttrType.String,
StringValue = player.Name
},
new PlayerAttr
{
Key = (int)PlayerAttrKey.Level,
ValueType = (int)PlayerAttrType.Int32,
Int32Value = 10
},
new PlayerAttr
{
Key = (int)PlayerAttrKey.HeadPhoto,
ValueType = (int)PlayerAttrType.Int32,
Int32Value = 1402
},
new PlayerAttr
{
Key = (int)PlayerAttrKey.HeadFrame,
ValueType = (int)PlayerAttrType.Int32,
Int32Value = 80060009
},
new PlayerAttr
{
Key = (int)PlayerAttrKey.Sex,
ValueType = (int)PlayerAttrType.Int32,
Int32Value = 1
}
},
Birthday = player.BirthDay,
Attributes = { player.Attributes },
RoleShowList =
{
new RoleShowEntry
@ -78,6 +45,47 @@ internal class PlayerInfoController : Controller
await Session.Push(MessageId.BasicInfoNotify, basicInfo);
}
[NetEvent(MessageId.ChangeHeadPhotoRequest)]
public RpcResult OnChangeHeadPhotoRequest(ChangeHeadPhotoRequest request, ModelManager modelManager)
{
modelManager.Player.SetAttribute(PlayerAttrKey.HeadPhoto, request.HeadPhotoId);
return Response(MessageId.ChangeHeadPhotoResponse, new ChangeHeadPhotoResponse
{
HeadPhotoId = request.HeadPhotoId
});
}
[NetEvent(MessageId.BirthdayInitRequest)]
public RpcResult OnBirthdayInitRequest(BirthdayInitRequest request, ModelManager modelManager)
{
modelManager.Player.BirthDay = request.Birthday;
return Response(MessageId.BirthdayInitResponse, new BirthdayInitResponse());
}
[NetEvent(MessageId.ModifySignatureRequest)]
public RpcResult OnModifySignatureRequest(ModifySignatureRequest request, ModelManager modelManager)
{
modelManager.Player.SetAttribute(PlayerAttrKey.Sign, request.Signature);
return Response(MessageId.ModifySignatureResponse, new ModifySignatureResponse
{
Signature = request.Signature
});
}
[NetEvent(MessageId.ModifyNameRequest)]
public RpcResult OnModifyNameRequest(ModifyNameRequest request, ModelManager modelManager)
{
modelManager.Player.SetAttribute(PlayerAttrKey.Name, request.Name);
return Response(MessageId.ModifyNameResponse, new ModifyNameResponse
{
Name = request.Name
});
}
[NetEvent(MessageId.PlayerBasicInfoGetRequest)]
public RpcResult OnPlayerBasicInfoGetRequest()
{

View file

@ -1,39 +1,103 @@
using Protocol;
using GameServer.Settings;
using GameServer.Settings;
using Protocol;
namespace GameServer.Models;
internal class PlayerModel
{
private const int MaxPlayerLevel = 80;
public List<PlayerAttr> Attributes { get; } = [];
public int Id { get; private set; }
public string Name { get; set; }
public int Level { get; private set; }
public int BirthDay { get; set; }
public int[] Characters { get; private set; }
public Vector Position { get; private set; }
public string Name => GetStringAttribute(PlayerAttrKey.Name);
public PlayerModel()
{
Name = string.Empty;
Characters = [];
Position = new Vector();
}
public void SetAttribute(PlayerAttrKey key, int value)
{
SetAttribute(key, PlayerAttrType.Int32, value, null);
}
public void SetAttribute(PlayerAttrKey key, string value)
{
SetAttribute(key, PlayerAttrType.String, 0, value);
}
public int GetIntAttribute(PlayerAttrKey key)
{
return GetAttributeOfType(key, PlayerAttrType.Int32)?.Int32Value ?? 0;
}
public string GetStringAttribute(PlayerAttrKey key)
{
return GetAttributeOfType(key, PlayerAttrType.String)?.StringValue ?? string.Empty;
}
private void SetAttribute(PlayerAttrKey key, PlayerAttrType type, int intValue, string? stringValue)
{
PlayerAttr? attr = GetAttributeOfType(key, type);
if (attr == null)
{
attr = new PlayerAttr
{
Key = (int)key,
ValueType = (int)type,
};
Attributes.Add(attr);
}
switch (type)
{
case PlayerAttrType.Int32:
attr.Int32Value = intValue; break;
case PlayerAttrType.String:
attr.StringValue = stringValue; break;
}
}
private PlayerAttr? GetAttributeOfType(PlayerAttrKey key, PlayerAttrType type)
{
PlayerAttr? attr = Attributes.SingleOrDefault(attr => attr.Key == (int)key);
if (attr != null)
{
if (attr.ValueType != (int)type)
throw new ArgumentException($"PlayerAttr type mismatch! Key: {key}, type: {(PlayerAttrType)attr.ValueType}, argument type: {type}");
}
return attr;
}
public void LevelUp()
{
if (Level == MaxPlayerLevel) return;
Level++;
int level = GetIntAttribute(PlayerAttrKey.Level);
if (level == MaxPlayerLevel) return;
SetAttribute(PlayerAttrKey.Level, level + 1);
}
public static PlayerModel CreateDefaultPlayer(PlayerStartingValues startingValues)
{
return new PlayerModel
PlayerModel playerModel = new()
{
Id = 1337,
Name = startingValues.Name,
Level = startingValues.PlayerLevel,
Characters = startingValues.Characters,
Position = startingValues.Position.Clone()
};
playerModel.SetAttribute(PlayerAttrKey.Name, startingValues.Name);
playerModel.SetAttribute(PlayerAttrKey.Level, startingValues.PlayerLevel);
playerModel.SetAttribute(PlayerAttrKey.HeadPhoto, startingValues.HeadPhoto);
playerModel.SetAttribute(PlayerAttrKey.HeadFrame, startingValues.HeadFrame);
return playerModel;
}
}

View file

@ -5,6 +5,8 @@ internal class PlayerStartingValues
{
public required string Name { get; set; }
public required int PlayerLevel { get; set; }
public required int HeadPhoto { get; set; }
public required int HeadFrame { get; set; }
public required int[] Characters { get; set; }
public required Vector Position { get; set; }
}

View file

@ -1,7 +1,9 @@
{
"StartingValues": {
"Name": "ReversedRooms",
"Level": 8,
"PlayerLevel": 8,
"HeadPhoto": 1402,
"HeadFrame": 80060009,
"Characters": [ 1402, 1302, 1203 ],
"Position": {
"X": -35823,