Skip to content

Commit

Permalink
Packet handling for training a spell
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Apr 13, 2022
1 parent d951cb9 commit 40bcd01
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 46 deletions.
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);
}
}
34 changes: 0 additions & 34 deletions EOLib/Net/API/StatSkill.cs
Expand Up @@ -144,7 +144,6 @@ partial class PacketAPI
public event SpellLearnErrorEvent OnSpellLearnError;
public event SpellLearnSuccessEvent OnSpellLearnSuccess;
public event SpellForgetEvent OnSpellForget;
public event SpellTrainEvent OnSpellTrain;
public event Action<StatResetData> OnCharacterStatsReset;

private void _createStatSkillMembers()
Expand All @@ -153,7 +152,6 @@ private void _createStatSkillMembers()
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 +190,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 Down Expand Up @@ -255,16 +231,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
43 changes: 43 additions & 0 deletions EOLib/PacketHandlers/SpellTrainingHandler.cs
@@ -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;
}
}
}
15 changes: 11 additions & 4 deletions EndlessClient/Controllers/TrainingController.cs
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
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
5 changes: 2 additions & 3 deletions EndlessClient/HUD/Panels/ActiveSpellsPanel.cs
Expand Up @@ -321,9 +321,8 @@ private void LevelUp_Click(object sender, EventArgs args)
}
else
{
var selectedSpell = _childItems.SingleOrNone(x => x.IsSelected);
// todo: implement in training controller
//_trainingController.LevelUpSpell(selectedSpell.SpellData.ID);
_childItems.SingleOrNone(x => x.IsSelected)
.MatchSome(x => _trainingController.AddSkillPoint(x.SpellData.ID));
}
}

Expand Down

0 comments on commit 40bcd01

Please sign in to comment.