Skip to content

Commit

Permalink
feat: 装等计算
Browse files Browse the repository at this point in the history
  • Loading branch information
DengSir committed Sep 4, 2024
1 parent 7321c90 commit 4f9ce82
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 77 deletions.
82 changes: 5 additions & 77 deletions Core/Inspect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local ipairs, pairs, time = ipairs, pairs, time
local tostring = tostring
local tinsert, tconcat = table.insert, table.concat
local select = select
local max = math.max

local CanInspect = CanInspect
local CheckInteractDistance = CheckInteractDistance
Expand Down Expand Up @@ -56,6 +57,9 @@ function Inspect:OnInitialize()
self.waitingItems = {}
self.userCache = ns.Addon.db.global.userCache
self.stepTimer = {}
self.itemLevelCalculator = ns.ItemLevelCalculator:New(function(slot)
return self:GetItemLink(slot)
end)

self.db = setmetatable({}, {
__index = function(_, k)
Expand Down Expand Up @@ -185,84 +189,8 @@ function Inspect:IsItemEquipped(itemId)
end
end

local function GetSlotItemLevel(slot)
local itemId = Inspect:GetItemLink(slot)
if not itemId then
return 0
end

local itemLevel, _, _, _, _, itemEquipLoc, _, _, _, subClassId = select(4, GetItemInfo(itemId))
if itemLevel then
return itemLevel, itemEquipLoc, subClassId
end
end

local function GetHandItemLevel()
local itemLevel, itemEquipLoc = GetSlotItemLevel(INVSLOT_MAINHAND)
if itemEquipLoc == 'INVTYPE_2HWEAPON' then
return itemLevel * 2
else
local itemLevelOff = GetSlotItemLevel(INVSLOT_OFFHAND)
return itemLevel + itemLevelOff
end
end

local DOUBLE_ITEM_LEVEL_RANGED = {
[Enum.ItemWeaponSubclass.Guns] = true,
[Enum.ItemWeaponSubclass.Bows] = true,
[Enum.ItemWeaponSubclass.Crossbow] = true,
}

local function GetRangedItemLevel()
local itemLevel, itemEquipLoc, subClassId = GetSlotItemLevel(INVSLOT_RANGED)
if itemEquipLoc == 'INVTYPE_THROWN' then
return itemLevel
elseif itemEquipLoc == 'INVTYPE_RANGED' or itemEquipLoc == 'INVTYPE_RANGEDRIGHT' then
if DOUBLE_ITEM_LEVEL_RANGED[subClassId] then
return itemLevel * 2
else
return itemLevel
end
else
return 0
end
end

local function GetWeaponItemLevel()
local itemLevelHand = GetHandItemLevel()
local itemLevelRanged = GetRangedItemLevel()
return max(itemLevelHand, itemLevelRanged)
end

local SLOTS = {
[INVSLOT_HEAD] = GetSlotItemLevel,
[INVSLOT_NECK] = GetSlotItemLevel,
[INVSLOT_SHOULDER] = GetSlotItemLevel,
[INVSLOT_CHEST] = GetSlotItemLevel,
[INVSLOT_WAIST] = GetSlotItemLevel,
[INVSLOT_LEGS] = GetSlotItemLevel,
[INVSLOT_FEET] = GetSlotItemLevel,
[INVSLOT_WRIST] = GetSlotItemLevel,
[INVSLOT_HAND] = GetSlotItemLevel,
[INVSLOT_FINGER1] = GetSlotItemLevel,
[INVSLOT_FINGER2] = GetSlotItemLevel,
[INVSLOT_TRINKET1] = GetSlotItemLevel,
[INVSLOT_TRINKET2] = GetSlotItemLevel,
[INVSLOT_BACK] = GetSlotItemLevel,
[INVSLOT_MAINHAND] = GetWeaponItemLevel,
}

function Inspect:GetItemLevel()
local total = 0

for slot, f in pairs(SLOTS) do
local itemLevel = f(slot)
if not itemLevel then
return
end
total = total + itemLevel
end
return total / 16
return self.itemLevelCalculator:GetItemLevel()
end

-- @build>2@
Expand Down
128 changes: 128 additions & 0 deletions Core/ItemLevelCalculator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
-- ItemLevel.lua
-- @Author : Dencer ([email protected])
-- @Link : https://dengsir.github.io
-- @Date : 9/4/2024, 11:42:34 AM
--
---@class ns
local ns = select(2, ...)

local ipairs = ipairs
local select = select
local max, min = math.max, math.min

local ItemLevelCalculator = ns.Addon:NewClass('ItemLevelCalculator')
ns.ItemLevelCalculator = ItemLevelCalculator

local SLOTS = {
INVSLOT_HEAD, INVSLOT_NECK, INVSLOT_SHOULDER, INVSLOT_CHEST, INVSLOT_WAIST, INVSLOT_LEGS, INVSLOT_FEET,
INVSLOT_WRIST, INVSLOT_HAND, INVSLOT_FINGER1, INVSLOT_FINGER2, INVSLOT_TRINKET1, INVSLOT_TRINKET2, INVSLOT_BACK,
}
local DOUBLE_ITEM_LEVEL_RANGED = {
[Enum.ItemWeaponSubclass.Guns] = true,
[Enum.ItemWeaponSubclass.Bows] = true,
[Enum.ItemWeaponSubclass.Crossbow] = true,
}

function ItemLevelCalculator:Constructor(fn)
self.fn = fn
end

function ItemLevelCalculator:GetItemLevel()
local level = 0
for _, slot in ipairs(SLOTS) do
local itemLevel = self:GetSlotInfo(slot)
if not itemLevel then
return
end
level = level + itemLevel
end

do
local itemLevel = self:GetWeaponItemLevel()
if not itemLevel then
return
end
level = level + itemLevel
end

return level / 16
end

function ItemLevelCalculator:GetSlotInfo(slot)
local item = self.fn(slot)
if not item then
return 0
end

local itemLevel, _, _, _, _, itemEquipLoc, _, _, _, subClassId = select(4, GetItemInfo(item))
if itemLevel then
return itemLevel, itemEquipLoc, subClassId
end
end

local function IsSingle(itemEquipLoc)
return not itemEquipLoc or itemEquipLoc == 'INVTYPE_WEAPON' or itemEquipLoc == 'INVTYPE_HOLDABLE' or itemEquipLoc ==
'INVTYPE_WEAPONOFFHAND'
end

function ItemLevelCalculator:GetWeaponItemLevel()
local itemLevelMain, itemEquipLocMain, subClassIdMain = self:GetSlotInfo(INVSLOT_MAINHAND)
local itemLevelOff, itemEquipLocOff, subClassIdOff = self:GetSlotInfo(INVSLOT_OFFHAND)
local itemLevelRanged, itemEquipLocRanged, subClassIdRanged = self:GetSlotInfo(INVSLOT_RANGED)

if itemEquipLocOff == 'INVTYPE_HOLDABLE' and subClassIdRanged == Enum.ItemWeaponSubclass.Wand then
return itemLevelOff + max(itemLevelMain, itemLevelRanged)
end

if subClassIdRanged == Enum.ItemWeaponSubclass.Thrown and IsSingle(itemEquipLocMain) and IsSingle(itemEquipLocOff) then
return itemLevelMain + itemLevelOff + itemLevelRanged - min(itemLevelMain, itemLevelOff, itemLevelRanged)
end

return self:GetWeaponItemLevel2()
end

function ItemLevelCalculator:GetWeaponItemLevel2()
local itemLevelHand = self:GetHandItemLevel()
local itemLevelRanged = self:GetRangedItemLevel()
return max(itemLevelHand, itemLevelRanged)
end

function ItemLevelCalculator:GetHandItemLevel()
local itemLevel, itemEquipLoc = self:GetSlotInfo(INVSLOT_MAINHAND)
local itemLevelOff, itemEquipLocOff = self:GetSlotInfo(INVSLOT_OFFHAND)

if itemEquipLoc == 'INVTYPE_2HWEAPON' or itemEquipLocOff == 'INVTYPE_2HWEAPON' then
if itemEquipLoc == itemEquipLocOff then
return max(itemLevel, itemLevelOff) * 2
elseif itemEquipLoc == 'INVTYPE_2HWEAPON' then
if itemLevel > itemLevelOff then
return itemLevel * 2
else
return itemLevel + itemLevelOff
end
elseif itemEquipLocOff == 'INVTYPE_2HWEAPON' then
if itemLevelOff > itemLevel then
return itemLevelOff * 2
else
return itemLevel + itemLevelOff
end
end
else
return itemLevel + itemLevelOff
end
end

function ItemLevelCalculator:GetRangedItemLevel()
local itemLevel, itemEquipLoc, subClassId = self:GetSlotInfo(INVSLOT_RANGED)
if itemEquipLoc == 'INVTYPE_THROWN' then
return itemLevel
elseif itemEquipLoc == 'INVTYPE_RANGED' or itemEquipLoc == 'INVTYPE_RANGEDRIGHT' then
if DOUBLE_ITEM_LEVEL_RANGED[subClassId] then
return itemLevel * 2
else
return itemLevel
end
else
return 0
end
end
1 change: 1 addition & 0 deletions Load.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Script file="Data\Wrath\Glyph.lua" build="Wrath" />
<Script file="Data\Wrath\ItemEnchant.lua" build="Wrath" />

<Script file="Core\ItemLevelCalculator.lua" />
<Script file="Core\Encoder.lua" />
<Script file="Core\Inspect.lua" />
<Script file="Core\Hook.lua" />
Expand Down

0 comments on commit 4f9ce82

Please sign in to comment.