-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract validator services out of InventoryController for drop/equip …
…of items
- Loading branch information
1 parent
83727d7
commit 97dfc6c
Showing
5 changed files
with
191 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace EOLib.Domain.Item | ||
{ | ||
public enum ItemDropResult | ||
{ | ||
Ok, | ||
Lore, | ||
Jail, | ||
TooFar | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using AutomaticTypeMapper; | ||
using EOLib.Domain.Character; | ||
using EOLib.Domain.Map; | ||
using EOLib.IO; | ||
using EOLib.IO.Repositories; | ||
using System; | ||
|
||
namespace EOLib.Domain.Item | ||
{ | ||
[AutoMappedType] | ||
public class ItemDropValidator : IItemDropValidator | ||
{ | ||
private readonly IEIFFileProvider _eifFileProvider; | ||
private readonly ICurrentMapStateProvider _currentMapStateProvider; | ||
|
||
public ItemDropValidator(IEIFFileProvider eifFileProvider, | ||
ICurrentMapStateProvider currentMapStateProvider) | ||
{ | ||
_eifFileProvider = eifFileProvider; | ||
_currentMapStateProvider = currentMapStateProvider; | ||
} | ||
|
||
public ItemDropResult ValidateItemDrop(ICharacter mainCharacter, IInventoryItem item, MapCoordinate dropPoint) | ||
{ | ||
if (item.ItemID <= 0) | ||
throw new ArgumentException("Item ID is invalid", nameof(item)); | ||
|
||
var itemData = _eifFileProvider.EIFFile[item.ItemID]; | ||
|
||
if (itemData.Special == ItemSpecial.Lore) | ||
return ItemDropResult.Lore; | ||
|
||
if (_currentMapStateProvider.JailMapID == _currentMapStateProvider.CurrentMapID) | ||
return ItemDropResult.Jail; | ||
|
||
var rp = mainCharacter.RenderProperties; | ||
if (Math.Max(Math.Abs(rp.MapX - dropPoint.X), Math.Abs(rp.MapY - dropPoint.Y)) > 2) | ||
return ItemDropResult.TooFar; | ||
|
||
return ItemDropResult.Ok; | ||
} | ||
} | ||
|
||
public interface IItemDropValidator | ||
{ | ||
ItemDropResult ValidateItemDrop(ICharacter mainCharacter, IInventoryItem item, MapCoordinate dropPoint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace EOLib.Domain.Item | ||
{ | ||
public enum ItemEquipResult | ||
{ | ||
Ok, | ||
AlreadyEquipped, | ||
WrongGender, | ||
StatRequirementNotMet, | ||
ClassRequirementNotMet, | ||
NotEquippable | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using AutomaticTypeMapper; | ||
using EOLib.Domain.Character; | ||
using EOLib.IO; | ||
using EOLib.IO.Extensions; | ||
using EOLib.IO.Pub; | ||
using EOLib.IO.Repositories; | ||
using System.Linq; | ||
|
||
namespace EOLib.Domain.Item | ||
{ | ||
[AutoMappedType] | ||
public class ItemEquipValidator : IItemEquipValidator | ||
{ | ||
private readonly IECFFileProvider _ecfFileProvider; | ||
private readonly IPaperdollProvider _paperdollProvider; | ||
|
||
public ItemEquipValidator(IECFFileProvider ecfFileProvider, | ||
IPaperdollProvider paperdollProvider) | ||
{ | ||
_ecfFileProvider = ecfFileProvider; | ||
_paperdollProvider = paperdollProvider; | ||
} | ||
|
||
public (ItemEquipResult, string, bool) ValidateItemEquip(ICharacter c, EIFRecord itemData) | ||
{ | ||
if (!_paperdollProvider.VisibleCharacterPaperdolls.ContainsKey(c.ID)) | ||
{ | ||
// emulate client login bug: when the paperdoll doesn't exist, show an "already equipped" message | ||
// see: https://eoserv.net/bugs/view_bug/441 | ||
return (ItemEquipResult.AlreadyEquipped, string.Empty, false); | ||
} | ||
|
||
var paperdoll = _paperdollProvider.VisibleCharacterPaperdolls[c.ID].Paperdoll; | ||
var equipLocation = itemData.GetEquipLocation(); | ||
|
||
var isAlternateEquipLocation = false; | ||
|
||
switch (itemData.Type) | ||
{ | ||
case ItemType.Armlet: | ||
case ItemType.Bracer: | ||
case ItemType.Ring: | ||
if (paperdoll[equipLocation] != 0) | ||
{ | ||
isAlternateEquipLocation = true; | ||
if (paperdoll[equipLocation + 1] != 0) | ||
return (ItemEquipResult.AlreadyEquipped, string.Empty, false); | ||
} | ||
break; | ||
case ItemType.Armor: | ||
if (c.RenderProperties.Gender != itemData.Gender) | ||
return (ItemEquipResult.WrongGender, string.Empty, false); | ||
break; | ||
} | ||
|
||
var reqs = new int[6]; | ||
var reqNames = new[] { "STR", "INT", "WIS", "AGI", "CON", "CHA" }; | ||
if ((reqs[0] = itemData.StrReq) > c.Stats[CharacterStat.Strength] || (reqs[1] = itemData.IntReq) > c.Stats[CharacterStat.Intelligence] | ||
|| (reqs[2] = itemData.WisReq) > c.Stats[CharacterStat.Wisdom] || (reqs[3] = itemData.AgiReq) > c.Stats[CharacterStat.Agility] | ||
|| (reqs[4] = itemData.ConReq) > c.Stats[CharacterStat.Constituion] || (reqs[5] = itemData.ChaReq) > c.Stats[CharacterStat.Charisma]) | ||
{ | ||
var req = reqs.Select((i, n) => new { Req = n, Ndx = i }).First(x => x.Req > 0); | ||
return (ItemEquipResult.StatRequirementNotMet, $" {reqs[req.Ndx]} {reqNames[req.Ndx]}", isAlternateEquipLocation); | ||
} | ||
|
||
|
||
if (itemData.ClassReq > 0 && itemData.ClassReq != c.ClassID) | ||
{ | ||
return (ItemEquipResult.ClassRequirementNotMet, _ecfFileProvider.ECFFile[itemData.ClassReq].Name, isAlternateEquipLocation); | ||
} | ||
|
||
if (paperdoll[equipLocation] != 0 && !isAlternateEquipLocation) | ||
{ | ||
return (ItemEquipResult.AlreadyEquipped, string.Empty, isAlternateEquipLocation); | ||
} | ||
|
||
return (ItemEquipResult.Ok, string.Empty, isAlternateEquipLocation); | ||
} | ||
} | ||
|
||
public interface IItemEquipValidator | ||
{ | ||
(ItemEquipResult Result, string Detail, bool IsAlternateEquipLocation) ValidateItemEquip(ICharacter mainCharacter, EIFRecord itemData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters