Skip to content

Commit

Permalink
[Game] -add: npc merchant buy/sell
Browse files Browse the repository at this point in the history
  • Loading branch information
nikes committed Feb 5, 2019
1 parent c2e6826 commit 230a970
Show file tree
Hide file tree
Showing 14 changed files with 481 additions and 65 deletions.
182 changes: 124 additions & 58 deletions AAEmu.Game/Core/Managers/UnitManagers/NpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AAEmu.Game.Core.Managers.Id;
using AAEmu.Game.Core.Managers.World;
using AAEmu.Game.Models.Game.Items;
using AAEmu.Game.Models.Game.Merchant;
using AAEmu.Game.Models.Game.NPChar;
using AAEmu.Game.Models.Game.Skills;
using AAEmu.Game.Models.Game.Skills.Templates;
Expand All @@ -17,10 +18,76 @@ public class NpcManager : Singleton<NpcManager>
private static Logger _log = LogManager.GetCurrentClassLogger();

private Dictionary<uint, NpcTemplate> _templates;
private Dictionary<uint, MerchantGoods> _goods;

public MerchantGoods GetGoods(uint id)
{
if(_goods.ContainsKey(id))
return _goods[id];
return null;
}

public Npc Create(uint objectId, uint id)
{
if (!_templates.ContainsKey(id))
return null;

var template = _templates[id];

var npc = new Npc();
npc.ObjId = objectId > 0 ? objectId : ObjectIdManager.Instance.GetNextId();
npc.TemplateId = id;
npc.Template = template;
npc.ModelId = template.ModelId;
npc.Faction = FactionManager.Instance.GetFaction(template.FactionId);
npc.Level = template.Level;

SetEquipItemTemplate(npc, template.Items.Headgear, EquipmentItemSlot.Head);
SetEquipItemTemplate(npc, template.Items.Necklace, EquipmentItemSlot.Neck);
SetEquipItemTemplate(npc, template.Items.Shirt, EquipmentItemSlot.Chest);
SetEquipItemTemplate(npc, template.Items.Belt, EquipmentItemSlot.Waist);
SetEquipItemTemplate(npc, template.Items.Pants, EquipmentItemSlot.Legs);
SetEquipItemTemplate(npc, template.Items.Gloves, EquipmentItemSlot.Hands);
SetEquipItemTemplate(npc, template.Items.Shoes, EquipmentItemSlot.Feet);
SetEquipItemTemplate(npc, template.Items.Bracelet, EquipmentItemSlot.Arms);
SetEquipItemTemplate(npc, template.Items.Back, EquipmentItemSlot.Back);
SetEquipItemTemplate(npc, template.Items.Undershirts, EquipmentItemSlot.Undershirt);
SetEquipItemTemplate(npc, template.Items.Underpants, EquipmentItemSlot.Underpants);
SetEquipItemTemplate(npc, template.Items.Mainhand, EquipmentItemSlot.Mainhand);
SetEquipItemTemplate(npc, template.Items.Offhand, EquipmentItemSlot.Offhand);
SetEquipItemTemplate(npc, template.Items.Ranged, EquipmentItemSlot.Ranged);
SetEquipItemTemplate(npc, template.Items.Musical, EquipmentItemSlot.Musical);
SetEquipItemTemplate(npc, template.Items.Cosplay, EquipmentItemSlot.Cosplay);

if (template.ModelParams != null)
{
SetEquipItemTemplate(npc, template.HairId, EquipmentItemSlot.Hair);
for (var i = 0; i < 7; i++)
SetEquipItemTemplate(npc, template.BodyItems[i], (EquipmentItemSlot) (i + 19));
}
else if (template.CharRaceId > 0)
{
for (var i = 0; i < 7; i++)
SetEquipItemTemplate(npc, template.BodyItems[i], (EquipmentItemSlot) (i + 19));
}

foreach (var bonusTemplate in template.Bonuses)
{
var bonus = new Bonus();
bonus.Template = bonusTemplate;
bonus.Value = bonusTemplate.Value; // TODO using LinearLevelBonus
npc.AddBonus(0, bonus);
}

npc.Hp = npc.MaxHp;
npc.Mp = npc.MaxMp;
return npc;
}

public void Load()
{
_templates = new Dictionary<uint, NpcTemplate>();
_goods = new Dictionary<uint, MerchantGoods>();

using (var connection = SQLite.CreateConnection())
{
Expand Down Expand Up @@ -314,74 +381,73 @@ public void Load()
continue;
var npc = _templates[npcId];
var template = new BonusTemplate();
template.Attribute = (UnitAttribute) reader.GetByte("unit_attribute_id");
template.ModifierType = (UnitModifierType) reader.GetByte("unit_modifier_type_id");
template.Attribute = (UnitAttribute)reader.GetByte("unit_attribute_id");
template.ModifierType = (UnitModifierType)reader.GetByte("unit_modifier_type_id");
template.Value = reader.GetInt32("value");
template.LinearLevelBonus = reader.GetInt32("linear_level_bonus");
npc.Bonuses.Add(template);
}
}
}

_log.Info("Loaded {0} npc templates", _templates.Count);
}
}

public Npc Create(uint objectId, uint id)
{
if (!_templates.ContainsKey(id))
return null;

var template = _templates[id];

var npc = new Npc();
npc.ObjId = objectId > 0 ? objectId : ObjectIdManager.Instance.GetNextId();
npc.TemplateId = id;
npc.Template = template;
npc.ModelId = template.ModelId;
npc.Faction = FactionManager.Instance.GetFaction(template.FactionId);
npc.Level = template.Level;
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM npc_initial_buffs";
command.Prepare();
using (var reader = new SQLiteWrapperReader(command.ExecuteReader()))
{
while (reader.Read())
{
var id = reader.GetUInt32("npc_id");
var buffId = reader.GetUInt32("buff_id");
if (!_templates.ContainsKey(id))
continue;
var template = _templates[id];
template.Buffs.Add(buffId);
}
}
}

SetEquipItemTemplate(npc, template.Items.Headgear, EquipmentItemSlot.Head);
SetEquipItemTemplate(npc, template.Items.Necklace, EquipmentItemSlot.Neck);
SetEquipItemTemplate(npc, template.Items.Shirt, EquipmentItemSlot.Chest);
SetEquipItemTemplate(npc, template.Items.Belt, EquipmentItemSlot.Waist);
SetEquipItemTemplate(npc, template.Items.Pants, EquipmentItemSlot.Legs);
SetEquipItemTemplate(npc, template.Items.Gloves, EquipmentItemSlot.Hands);
SetEquipItemTemplate(npc, template.Items.Shoes, EquipmentItemSlot.Feet);
SetEquipItemTemplate(npc, template.Items.Bracelet, EquipmentItemSlot.Arms);
SetEquipItemTemplate(npc, template.Items.Back, EquipmentItemSlot.Back);
SetEquipItemTemplate(npc, template.Items.Undershirts, EquipmentItemSlot.Undershirt);
SetEquipItemTemplate(npc, template.Items.Underpants, EquipmentItemSlot.Underpants);
SetEquipItemTemplate(npc, template.Items.Mainhand, EquipmentItemSlot.Mainhand);
SetEquipItemTemplate(npc, template.Items.Offhand, EquipmentItemSlot.Offhand);
SetEquipItemTemplate(npc, template.Items.Ranged, EquipmentItemSlot.Ranged);
SetEquipItemTemplate(npc, template.Items.Musical, EquipmentItemSlot.Musical);
SetEquipItemTemplate(npc, template.Items.Cosplay, EquipmentItemSlot.Cosplay);
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM merchants";
command.Prepare();
using (var reader = new SQLiteWrapperReader(command.ExecuteReader()))
{
while (reader.Read())
{
var id = reader.GetUInt32("npc_id");
if (!_templates.ContainsKey(id))
continue;
var template = _templates[id];
template.MerchantPackId = reader.GetUInt32("merchant_pack_id");
}
}
}

if (template.ModelParams != null)
{
SetEquipItemTemplate(npc, template.HairId, EquipmentItemSlot.Hair);
for (var i = 0; i < 7; i++)
SetEquipItemTemplate(npc, template.BodyItems[i], (EquipmentItemSlot) (i + 19));
}
else if (template.CharRaceId > 0)
{
for (var i = 0; i < 7; i++)
SetEquipItemTemplate(npc, template.BodyItems[i], (EquipmentItemSlot) (i + 19));
}
_log.Info("Loaded {0} npc templates", _templates.Count);
_log.Info("Loading merchant packs...");
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM merchant_goods";
command.Prepare();
using (var reader = new SQLiteWrapperReader(command.ExecuteReader()))
{
while (reader.Read())
{
var id = reader.GetUInt32("merchant_pack_id");
var template = new MerchantItem();
template.Id = reader.GetUInt32("item_id");
template.Grade = reader.GetByte("grade_id");
if (!_goods.ContainsKey(id))
_goods.Add(id, new MerchantGoods(id));
_goods[id].Items.Add(template.Id, template);
}
}
}

foreach (var bonusTemplate in template.Bonuses)
{
var bonus = new Bonus();
bonus.Template = bonusTemplate;
bonus.Value = bonusTemplate.Value; // TODO using LinearLevelBonus
npc.AddBonus(0, bonus);
_log.Info("Loaded {0} merchant packs", _goods.Count);
}

npc.Hp = npc.MaxHp;
npc.Mp = npc.MaxMp;
return npc;
}

private void SetEquipItemTemplate(Npc npc, uint templateId, EquipmentItemSlot slot, byte grade = 0)
Expand All @@ -397,4 +463,4 @@ private void SetEquipItemTemplate(Npc npc, uint templateId, EquipmentItemSlot sl
npc.Equip[(int) slot] = item;
}
}
}
}
6 changes: 6 additions & 0 deletions AAEmu.Game/Core/Managers/World/WorldManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ public Unit GetUnit(uint objId)
return ret;
}

public Npc GetNpc(uint oId)
{
_npcs.TryGetValue(oId, out var ret);
return ret;
}

public Character GetCharacter(string name)
{
foreach (var player in _characters.Values)
Expand Down
4 changes: 4 additions & 0 deletions AAEmu.Game/Core/Network/Game/GameNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ private GameNetwork()
RegisterPacket(0x093, 1, typeof(CSLearnBuffPacket));
RegisterPacket(0x094, 1, typeof(CSResetSkillsPacket));
RegisterPacket(0x096, 1, typeof(CSSwapAbilityPacket));
RegisterPacket(0x0aa, 1, typeof(CSBuyItemsPacket));
RegisterPacket(0x0ab, 1, typeof(CSBuyCoinItemPacket));
RegisterPacket(0x0ac, 1, typeof(CSSellItemsPacket));
RegisterPacket(0x0ad, 1, typeof(CSListSoldItemPacket));
RegisterPacket(0x0b2, 1, typeof(CSUpdateActionSlotPacket));
RegisterPacket(0x0d1, 1, typeof(CSStartQuestContextPacket));
RegisterPacket(0x0d2, 1, typeof(CSCompleteQuestContextPacket));
Expand Down
20 changes: 20 additions & 0 deletions AAEmu.Game/Core/Packets/C2G/CSBuyCoinItemPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AAEmu.Commons.Network;
using AAEmu.Game.Core.Network.Game;

namespace AAEmu.Game.Core.Packets.C2G
{
public class CSBuyCoinItemPacket : GamePacket
{
public CSBuyCoinItemPacket() : base(0x0ab, 1)
{
}

public override void Read(PacketStream stream)
{
var objId = stream.ReadBc();
var id = stream.ReadUInt32();

_log.Debug("BuyCoinItem, objId: {0}, id: {1}", objId, id);
}
}
}
Loading

0 comments on commit 230a970

Please sign in to comment.