Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement active spells panel #182

Merged
merged 9 commits into from
Apr 13, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

namespace EOLib.Domain.Character
{
//todo: maybe this should go into its own namespace? Domain.Character is pretty monolithic
[AutoMappedType]
public class StatTrainingActions : IStatTrainingActions
public class TrainingActions : ITrainingActions
{
private readonly IPacketSendService _packetSendService;

public StatTrainingActions(IPacketSendService packetSendService)
public TrainingActions(IPacketSendService packetSendService)
{
_packetSendService = packetSendService;
}
Expand All @@ -29,6 +28,16 @@ public void LevelUpStat(CharacterStat whichStat)
_packetSendService.SendPacket(packet);
}

public void LevelUpSkill(int spellId)
{
var packet = new PacketBuilder(PacketFamily.StatSkill, PacketAction.Add)
.AddChar((byte)TrainType.Skill)
.AddShort((short)spellId)
.Build();

_packetSendService.SendPacket(packet);
}

private static bool InvalidStat(CharacterStat whichStat)
{
switch (whichStat)
Expand Down Expand Up @@ -59,8 +68,10 @@ private static short GetStatIndex(CharacterStat whichStat)
}
}

public interface IStatTrainingActions
public interface ITrainingActions
{
void LevelUpStat(CharacterStat whichStat);

void LevelUpSkill(int spellId);
}
}
47 changes: 0 additions & 47 deletions EOLib/Net/API/StatSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,20 @@ internal StatResetData(OldPacket pkt)
}

public delegate void SpellLearnErrorEvent(SkillMasterReply reply, short classID);
public delegate void SpellLearnSuccessEvent(short spellID, int goldRemaining);
public delegate void SpellForgetEvent(short spellID);
public delegate void SpellTrainEvent(short skillPtsRemaining, short spellID, short spellLevel);

partial class PacketAPI
{
public event Action<SkillmasterData> OnSkillmasterOpen;
public event SpellLearnErrorEvent OnSpellLearnError;
public event SpellLearnSuccessEvent OnSpellLearnSuccess;
public event SpellForgetEvent OnSpellForget;
public event SpellTrainEvent OnSpellTrain;
public event Action<StatResetData> OnCharacterStatsReset;

private void _createStatSkillMembers()
{
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.StatSkill, PacketAction.Open), _handleStatSkillOpen, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.StatSkill, PacketAction.Reply), _handleStatSkillReply, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.StatSkill, PacketAction.Take), _handleStatSkillTake, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.StatSkill, PacketAction.Remove), _handleStatSkillRemove, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.StatSkill, PacketAction.Accept), _handleStatSkillAccept, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.StatSkill, PacketAction.Junk), _handleStatSkillJunk, true);
}

Expand Down Expand Up @@ -192,35 +186,13 @@ public bool ForgetSpell(short spellID)
return m_client.SendPacket(pkt);
}

public bool LevelUpStat(short statID)
{
return _trainStatShared(statID, TrainType.Stat);
}

public bool LevelUpSpell(short spellID)
{
return _trainStatShared(spellID, TrainType.Skill);
}

public bool ResetCharacterStatSkill()
{
OldPacket pkt = new OldPacket(PacketFamily.StatSkill, PacketAction.Junk);
pkt.AddInt(1234); //shop ID, ignored by eoserv - eomain may require this to be correct
return !m_client.ConnectedAndInitialized || !Initialized || m_client.SendPacket(pkt);
}

private bool _trainStatShared(short id, TrainType type)
{
if (!m_client.ConnectedAndInitialized || !Initialized)
return false;

OldPacket pkt = new OldPacket(PacketFamily.StatSkill, PacketAction.Add);
pkt.AddChar((byte)type);
pkt.AddShort(id);

return m_client.SendPacket(pkt);
}

//handlers

private void _handleStatSkillOpen(OldPacket pkt)
Expand All @@ -238,15 +210,6 @@ private void _handleStatSkillReply(OldPacket pkt)
OnSpellLearnError((SkillMasterReply)pkt.GetShort(), pkt.GetShort());
}

//success learning a skill
private void _handleStatSkillTake(OldPacket pkt)
{
//short - spell id
//int - character gold remaining
if (OnSpellLearnSuccess != null)
OnSpellLearnSuccess(pkt.GetShort(), pkt.GetInt());
}

//forgetting a skill
private void _handleStatSkillRemove(OldPacket pkt)
{
Expand All @@ -255,16 +218,6 @@ private void _handleStatSkillRemove(OldPacket pkt)
OnSpellForget(pkt.GetShort());
}

//skill point added to spell
private void _handleStatSkillAccept(OldPacket pkt)
{
//short - character skill pts remaining
//short - stat ID (spell ID)
//short - spell level
if (OnSpellTrain != null)
OnSpellTrain(pkt.GetShort(), pkt.GetShort(), pkt.GetShort());
}

//reset character
private void _handleStatSkillJunk(OldPacket pkt)
{
Expand Down
45 changes: 45 additions & 0 deletions EOLib/PacketHandlers/Skill/StatskillTake.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Net;
using EOLib.Net.Handlers;
using System.Linq;

namespace EOLib.PacketHandlers.Skill
{
/// <summary>
/// Sent when learning a skill, either via $learn command or from skillmaster
/// </summary>
[AutoMappedType]
public class StatskillTake : InGameOnlyPacketHandler
{
private readonly ICharacterInventoryRepository _characterInventoryRepository;

public override PacketFamily Family => PacketFamily.StatSkill;

public override PacketAction Action => PacketAction.Take;

public StatskillTake(IPlayerInfoProvider playerInfoProvider,
ICharacterInventoryRepository characterInventoryRepository)
: base(playerInfoProvider)
{
_characterInventoryRepository = characterInventoryRepository;
}

public override bool HandlePacket(IPacket packet)
{
var spellId = packet.ReadShort();
var characterGold = packet.ReadInt();

if (!_characterInventoryRepository.SpellInventory.Any(x => x.ID == spellId))
{
_characterInventoryRepository.SpellInventory.Add(new InventorySpell(spellId, 0));
}

_characterInventoryRepository.ItemInventory.RemoveWhere(x => x.ItemID == 1);
_characterInventoryRepository.ItemInventory.Add(new InventoryItem(1, characterGold));

return true;
}
}
}
43 changes: 43 additions & 0 deletions EOLib/PacketHandlers/SpellTrainingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Net;
using EOLib.Net.Handlers;

namespace EOLib.PacketHandlers
{
[AutoMappedType]
public class SpellTrainingHandler : InGameOnlyPacketHandler
{
private readonly ICharacterRepository _characterRepository;
private readonly ICharacterInventoryRepository _characterInventoryRepository;

public override PacketFamily Family => PacketFamily.StatSkill;

public override PacketAction Action => PacketAction.Accept;

public SpellTrainingHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICharacterInventoryRepository characterInventoryRepository)
: base(playerInfoProvider)
{
_characterRepository = characterRepository;
_characterInventoryRepository = characterInventoryRepository;
}

public override bool HandlePacket(IPacket packet)
{
var skillPoints = packet.ReadShort();
var spellId = packet.ReadShort();
var spellLevel = packet.ReadShort();

_characterInventoryRepository.SpellInventory.RemoveWhere(x => x.ID == spellId);
_characterInventoryRepository.SpellInventory.Add(new InventorySpell(spellId, spellLevel));

var stats = _characterRepository.MainCharacter.Stats.WithNewStat(CharacterStat.SkillPoints, skillPoints);
_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithStats(stats);

return true;
}
}
}
1 change: 1 addition & 0 deletions EOLib/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static class Constants
public const string IgnoreListFile = "config/ignore.ini";

public const string InventoryFile = "config/inventory.ini";
public const string SpellsFile = "config/spells.ini";

//Should be easily customizable between different clients (based on graphics)
//not a config option because this shouldn't be exposed at the user level
Expand Down
15 changes: 11 additions & 4 deletions EndlessClient/Controllers/TrainingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ namespace EndlessClient.Controllers
[MappedType(BaseType = typeof(ITrainingController))]
public class TrainingController : ITrainingController
{
private readonly IStatTrainingActions _statTrainingActions;
private readonly ITrainingActions _trainingActions;

public TrainingController(IStatTrainingActions statTrainingActions)
public TrainingController(ITrainingActions trainingActions)
{
_statTrainingActions = statTrainingActions;
_trainingActions = trainingActions;
}

public void AddStatPoint(CharacterStat whichStat)
{
if (InvalidStat(whichStat))
return;

_statTrainingActions.LevelUpStat(whichStat);
_trainingActions.LevelUpStat(whichStat);
}

public void AddSkillPoint(int spellId)
{
_trainingActions.LevelUpSkill(spellId);
}

private static bool InvalidStat(CharacterStat whichStat)
Expand All @@ -39,5 +44,7 @@ private static bool InvalidStat(CharacterStat whichStat)
public interface ITrainingController
{
void AddStatPoint(CharacterStat whichStat);

void AddSkillPoint(int spellId);
}
}
2 changes: 1 addition & 1 deletion EndlessClient/Dialogs/Old/TradeDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void CompleteTrade(short p1, List<InventoryItem> p1items, short p2, List<
// weightDelta += OldWorld.Instance.EIF[item.ItemID].Weight * item.Amount;
//}
m_main.Weight += (byte)weightDelta;
((EOGame)Game).Hud.RefreshStats();
//((EOGame)Game).Hud.RefreshStats();

Close(null, XNADialogResult.NO_BUTTON_PRESSED);
EOMessageBox.Show(DialogResourceID.TRADE_SUCCESS, EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader);
Expand Down
17 changes: 0 additions & 17 deletions EndlessClient/HUD/Controls/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class HUD : DrawableGameComponent

private readonly OldChatRenderer chatRenderer;
private readonly OldEOPartyPanel m_party;
private OldActiveSpells activeSpells;

private ChatTextBox chatTextBox;

Expand Down Expand Up @@ -80,9 +79,6 @@ public override void Initialize()
//the draw orders are adjusted for child items in the constructor.
//calling SetParent will break this.

//activeSpells = new OldActiveSpells(pnlActiveSpells, m_packetAPI);
activeSpells.Initialize();

SessionStartTime = DateTime.Now;

base.Initialize();
Expand Down Expand Up @@ -118,26 +114,13 @@ public void SetStatusLabel(EOResourceID type, string detail)
//SetStatusLabelText(string.Format("[ {0} ] {1}", typeText, detail));
}

public void RefreshStats()
{
if (activeSpells != null)
activeSpells.RefreshTotalSkillPoints();
}

public void SetPartyData(List<PartyMember> party) { m_party.SetData(party); }
public void AddPartyMember(PartyMember member) { m_party.AddMember(member); }
public void RemovePartyMember(short memberID) { m_party.RemoveMember(memberID); }
public void CloseParty() { m_party.CloseParty(); }
public bool MainPlayerIsInParty() { return m_party.PlayerIsMember((short)OldWorld.Instance.MainPlayer.ActiveCharacter.ID); }
public bool PlayerIsPartyMember(short playerID) { return m_party.PlayerIsMember(playerID); }

public void AddNewSpellToActiveSpellsByID(int spellID) { activeSpells.AddNewSpellToNextOpenSlot(spellID); }
public ESFRecord GetSpellFromIndex(int index) { return activeSpells.GetSpellRecordBySlot(index); }
public void SetSelectedSpell(int index) { activeSpells.SetSelectedSpellBySlot(index); }
public void RemoveSpellFromActiveSpellsByID(int spellID) { activeSpells.RemoveSpellByID(spellID); }
public void UpdateActiveSpellLevelByID(short spellID, short spellLevel) { activeSpells.UpdateSpellLevelByID(spellID, spellLevel); }
public void RemoveAllSpells() { activeSpells.RemoveAllSpells(); }

#endregion

protected override void Dispose(bool disposing)
Expand Down
Loading