388 lines
17 KiB
Lua
388 lines
17 KiB
Lua
-- Source-mode data adapter for the original Store integration. No frames,
|
|
-- protected globals, network calls, retail currency or inferred ownership.
|
|
-- The caller injects the existing TW_SHOP parser and qualified presentation.
|
|
local Provider = {}
|
|
Provider.__index = Provider
|
|
|
|
-- Presentation-only IDs. Source category IDs are strictly positive; these
|
|
-- negative keys stay in the original Store's Lua-local tree/selection state.
|
|
local fashionSlots = {
|
|
{key="all",id=-7000}, {key="head",id=-7001}, {key="shoulder",id=-7002},
|
|
{key="back",id=-7003}, {key="chest",id=-7004}, {key="shirt",id=-7005},
|
|
{key="tabard",id=-7006}, {key="wrist",id=-7007}, {key="hands",id=-7008},
|
|
{key="waist",id=-7009}, {key="legs",id=-7010}, {key="feet",id=-7011},
|
|
{key="mainhand",id=-7012}, {key="offhand",id=-7013}, {key="ranged",id=-7014},
|
|
{key="other",id=-7099},
|
|
}
|
|
-- Forever store layout (owner 2026-11-23): the Forever top categories, with the Octo
|
|
-- categories Forever has no top level for shown as their subcategories. Mapping is by
|
|
-- the Source category name; an unknown future category stays a top-level group.
|
|
local foreverTops = {
|
|
{key="mounts", id=-8001, names={mounts=true}},
|
|
{key="pets", id=-8002, names={companions=true, pets=true}},
|
|
{key="transmog", id=-8003, names={fashion=true, illusions=true}},
|
|
{key="services", id=-8004, names={skins=true, glyphs=true, gameplay=true, misc=true}},
|
|
}
|
|
local foreverByGroup = {}
|
|
for _,top in ipairs(foreverTops) do foreverByGroup[top.id]=top end
|
|
local fashionByGroup = {}
|
|
for _,slot in ipairs(fashionSlots) do fashionByGroup[slot.id]=slot end
|
|
-- Reviewed Item/ItemSparse InventoryType namespace. Robe and chest share the
|
|
-- original chest slot; items without a visual equipment slot remain Other.
|
|
local inventorySlots = {[1]="head",[3]="shoulder",[16]="back",[5]="chest",[20]="chest",
|
|
[4]="shirt",[19]="tabard",[9]="wrist",[10]="hands",[6]="waist",[7]="legs",[8]="feet",
|
|
[13]="mainhand",[17]="mainhand",[21]="mainhand",[14]="offhand",[22]="offhand",[23]="offhand",
|
|
[15]="ranged",[25]="ranged",[26]="ranged"}
|
|
|
|
-- Frozen active batch8c Source skin masks; the input receipt pins all40 rows.
|
|
-- This is appearance compatibility, never a new purchase restriction.
|
|
local sourceSkinRaceMasks = {
|
|
[50105]=1,
|
|
[50106]=1,
|
|
[50204]=4,
|
|
[50205]=4,
|
|
[50206]=4,
|
|
[50207]=2,
|
|
[50208]=2,
|
|
[50209]=2,
|
|
[50212]=64,
|
|
[50220]=2,
|
|
[50221]=2,
|
|
[50223]=2,
|
|
[50224]=2,
|
|
[50250]=4,
|
|
[50251]=4,
|
|
[50252]=4,
|
|
[50290]=1,
|
|
[50291]=1,
|
|
[50292]=1,
|
|
[51010]=128,
|
|
[51011]=128,
|
|
[51920]=16,
|
|
[51921]=16,
|
|
[61104]=8,
|
|
[61105]=8,
|
|
[61106]=8,
|
|
[81206]=512,
|
|
[81208]=128,
|
|
[81209]=512,
|
|
[81210]=1,
|
|
[81228]=32,
|
|
[81229]=4,
|
|
[81230]=64,
|
|
[81255]=2,
|
|
[81256]=32,
|
|
[83090]=1,
|
|
[83091]=1,
|
|
[83092]=8,
|
|
[83099]=8,
|
|
[83100]=8,
|
|
}
|
|
|
|
function Provider.SourceRaceFromNativeRace(nativeRaceID)
|
|
if type(nativeRaceID) ~= "number" or nativeRaceID % 1 ~= 0
|
|
or nativeRaceID < 1 or nativeRaceID > 10 then return nil end
|
|
-- Accepted self/peer writers preserve the Source numeric Race field.
|
|
-- Source Goblin9 and HighElf10 are not translated through English names.
|
|
return nativeRaceID
|
|
end
|
|
|
|
function Provider.GetSourceSkinRaceEligibility(itemID, nativeRaceID)
|
|
local mask = sourceSkinRaceMasks[itemID]
|
|
if not mask then return nil, "no-qualified-source-race-mask" end
|
|
local race = Provider.SourceRaceFromNativeRace(nativeRaceID)
|
|
if not race then return nil, "source-race-unavailable" end
|
|
local eligible = math.floor(mask / (2 ^ (race - 1))) % 2 == 1
|
|
return eligible, eligible and "source-race-mask-compatible" or "source-race-mask-incompatible"
|
|
end
|
|
|
|
local function copy(value)
|
|
if type(value) ~= "table" then return value end
|
|
local result = {}
|
|
for k, v in pairs(value) do result[k] = copy(v) end
|
|
return result
|
|
end
|
|
|
|
function Provider.New(shop, options)
|
|
assert(type(shop) == "table" and shop.Parse and shop.Apply and shop.NewState)
|
|
assert(options and options.sourceMode == true, "explicit Source mode required")
|
|
return setmetatable({shop=shop, options=options, state=shop.NewState(),
|
|
revision=0, pending=nil, result=nil, fashionInventory={}}, Provider)
|
|
end
|
|
|
|
-- Call only on a new authoritative world/session, not to retry a timed-out buy.
|
|
function Provider:ResetSession()
|
|
self.state = self.shop.NewState()
|
|
self.revision = self.revision + 1
|
|
self.pending, self.result = nil, nil
|
|
self.fashionInventory = {}
|
|
end
|
|
|
|
function Provider:ApplyMessage(message)
|
|
local event = self.shop.Parse(message)
|
|
if not event then return false, "invalid-message" end
|
|
if event.kind == "result" then
|
|
-- BuyResult contains no ID. Never apply a previous unsolicited result
|
|
-- to the selected card, invent ownership, or clear a new selection.
|
|
if not self.pending then return false, "unsolicited-result" end
|
|
self.result = {entryID=self.pending.entryID, value=event.value}
|
|
self.pending = nil
|
|
-- Await a fresh authoritative balance before another purchase.
|
|
self.state.balance = nil
|
|
return true, "result"
|
|
end
|
|
if not self.shop.Apply(self.state, event) then return false, "invalid-sequence" end
|
|
if event.kind=="categories" or (event.kind=="start" and event.category==7) then self.fashionInventory={} end
|
|
if event.kind ~= "balance" then self.revision = self.revision + 1 end
|
|
return true, event.kind
|
|
end
|
|
|
|
function Provider:GetBalanceTokens() return self.state.balance end
|
|
function Provider:GetSourceProductGroups()
|
|
local groups = {}
|
|
for i,id in ipairs(self.state.order) do
|
|
groups[i] = {groupID=id, parentGroupID=self.state.categories[id].parent}
|
|
end
|
|
return groups
|
|
end
|
|
function Provider:FashionSlot(entry,refresh)
|
|
local selector=entry.itemID>0 and entry.itemID or entry.id
|
|
local cached=self.fashionInventory[entry.id]
|
|
if not refresh and cached and cached.selector==selector then return cached.slot end
|
|
local inventory=self.options.inventoryType and self.options.inventoryType(selector)
|
|
if type(inventory)~="number" or inventory%1~=0 or inventory<0 or inventory>255 then inventory=nil end
|
|
local slot=inventorySlots[inventory] or "other"
|
|
self.fashionInventory[entry.id]={selector=selector,inventory=inventory,slot=slot}
|
|
return slot
|
|
end
|
|
function Provider:RefreshFashionSlots(itemID)
|
|
if not self.options.fashionSlotGroups or not self.state.complete[7] then return false end
|
|
local changed=false
|
|
for _,entry in pairs(self.state.entries[7] or {}) do
|
|
if entry.id==itemID or entry.itemID==itemID then
|
|
local previous=self:FashionSlot(entry)
|
|
if self:FashionSlot(entry,true)~=previous then changed=true end
|
|
end
|
|
end
|
|
return changed
|
|
end
|
|
function Provider:GetSourceCategoryForGroup(groupID)
|
|
if self.options.foreverLayout then
|
|
if foreverByGroup[groupID] then
|
|
if #self:ForeverSources(foreverByGroup[groupID])>0 then return groupID end
|
|
return nil
|
|
end
|
|
local source=(self.options.fashionSlotGroups and fashionByGroup[groupID]) and 7 or groupID
|
|
local top=self:ForeverTopOf(source)
|
|
if top and not self:ForeverHasChildren(top,self:ForeverSources(top)) then return top.id end
|
|
if top and source==7 and not fashionByGroup[groupID] then return top.id end
|
|
if fashionByGroup[groupID] then return top and top.id or 7 end
|
|
if self.state.categories[groupID] then return groupID end
|
|
return nil
|
|
end
|
|
if self.options.fashionSlotGroups and fashionByGroup[groupID] then return 7 end
|
|
if self.state.categories[groupID] then return groupID end
|
|
end
|
|
function Provider:GetFashionGroups()
|
|
local result={}
|
|
if not self.options.fashionSlotGroups or not self.state.categories[7] or not self.state.complete[7] then return result end
|
|
local present={}
|
|
for _,entry in pairs(self.state.entries[7] or {}) do present[self:FashionSlot(entry)]=true end
|
|
if not next(present) then return result end
|
|
for _,slot in ipairs(fashionSlots) do
|
|
if slot.key=="all" or present[slot.key] then result[#result+1]=slot end
|
|
end
|
|
return result
|
|
end
|
|
-- Source category IDs under one Forever top, in Source order.
|
|
function Provider:ForeverSources(top)
|
|
local result={}
|
|
for _,id in ipairs(self.state.order) do
|
|
local category=self.state.categories[id]
|
|
if category and top.names[string.lower(category.name or "")] then result[#result+1]=id end
|
|
end
|
|
return result
|
|
end
|
|
function Provider:ForeverTopOf(sourceID)
|
|
local category=self.state.categories[sourceID]
|
|
if not category then return nil end
|
|
local name=string.lower(category.name or "")
|
|
for _,top in ipairs(foreverTops) do if top.names[name] then return top end end
|
|
end
|
|
-- A Forever top with one plain Source category shows it directly, without a child row.
|
|
function Provider:ForeverHasChildren(top,sources)
|
|
return #sources>1 or (sources[1]==7 and self.options.fashionSlotGroups)
|
|
end
|
|
function Provider:GetForeverGroups()
|
|
local groups={}
|
|
for _,top in ipairs(foreverTops) do
|
|
local sources=self:ForeverSources(top)
|
|
if #sources>0 then
|
|
groups[#groups+1]={groupID=top.id,parentGroupID=0}
|
|
if self:ForeverHasChildren(top,sources) then
|
|
for _,id in ipairs(sources) do
|
|
if id==7 and self.options.fashionSlotGroups then
|
|
for _,slot in ipairs(self:GetFashionGroups()) do
|
|
if slot.key~="all" then groups[#groups+1]={groupID=slot.id,parentGroupID=top.id} end
|
|
end
|
|
else
|
|
groups[#groups+1]={groupID=id,parentGroupID=top.id}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
for _,id in ipairs(self.state.order) do
|
|
if not self:ForeverTopOf(id) then groups[#groups+1]={groupID=id,parentGroupID=self.state.categories[id].parent} end
|
|
end
|
|
return groups
|
|
end
|
|
function Provider:GetProductGroups()
|
|
if self.options.foreverLayout then return self:GetForeverGroups() end
|
|
local groups=self:GetSourceProductGroups()
|
|
for _,slot in ipairs(self:GetFashionGroups()) do groups[#groups+1]={groupID=slot.id,parentGroupID=7} end
|
|
return groups
|
|
end
|
|
function Provider:GetProductGroupInfo(id)
|
|
local top=self.options.foreverLayout and foreverByGroup[id]
|
|
if top then
|
|
if #self:ForeverSources(top)==0 then return nil end
|
|
return {groupName=(self.options.foreverLabels or {})[top.key] or top.key,
|
|
texture=(self.options.foreverTextures or {})[top.key] or (self.options.categoryTexture and self.options.categoryTexture({name=top.key})),
|
|
flags=0,sourceDisplayOnly=true}
|
|
end
|
|
if self.options.fashionSlotGroups and fashionByGroup[id] then
|
|
for _,slot in ipairs(self:GetFashionGroups()) do
|
|
if slot.id==id then
|
|
return {groupName=(self.options.fashionSlotLabels or {})[slot.key] or slot.key,
|
|
texture=self.options.categoryTexture and self.options.categoryTexture(copy(self.state.categories[7])),
|
|
flags=0,sourceCategoryID=7,sourceDisplayOnly=true}
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
local category = self.state.categories[id]
|
|
if not category then return nil end
|
|
local texture = self.options.categoryTexture and self.options.categoryTexture(copy(category))
|
|
return {groupName=category.name, texture=texture, flags=0, sourceCategoryID=id}
|
|
end
|
|
|
|
function Provider:FindEntry(id)
|
|
local found
|
|
for category, entries in pairs(self.state.entries) do
|
|
if self.state.complete[category] and entries[id] then
|
|
if found then return nil end -- conflicting IDs are not merged
|
|
found = entries[id]
|
|
end
|
|
end
|
|
return found
|
|
end
|
|
|
|
function Provider:GetEntryInfo(id)
|
|
local entry = self:FindEntry(id)
|
|
if not entry then return nil end
|
|
local presentation = self.options.presentation and self.options.presentation(copy(entry)) or {}
|
|
local eligible, reason
|
|
if self.options.eligibility then
|
|
eligible, reason = self.options.eligibility(copy(entry))
|
|
else
|
|
local raceID = self.options.nativeRaceID
|
|
if type(raceID) == "function" then raceID = raceID() end
|
|
eligible, reason = Provider.GetSourceSkinRaceEligibility(entry.id, raceID)
|
|
end
|
|
if eligible ~= true and eligible ~= false then eligible = nil end
|
|
local cards = copy(presentation.cards or {})
|
|
-- Presentation IDs must be supplied through a qualified native join;
|
|
-- Source modelID/itemID are never cast into native display/appearance IDs.
|
|
local shared = {
|
|
name=presentation.name or entry.name, description=entry.description,
|
|
texture=presentation.texture, flags=0, cards=cards, deliverables={},
|
|
modelSceneID=presentation.modelSceneID, cardType=self.options.cardType,
|
|
productDecorator=self.options.productDecorator,
|
|
-- Race masks affect presentation only. Source also permits buying
|
|
-- incompatible items; never invent a client-side purchase restriction.
|
|
buyableHere=true, sourcePurchaseEligible=true,
|
|
sourceRaceEligible=eligible,
|
|
sourceEligibilityReason=reason, sourcePriceTokens=entry.price,
|
|
sourceRevision=self.revision, sourceItemID=entry.id,
|
|
sourcePreviewItemID=entry.itemID,
|
|
sourceModelID=entry.modelID, sourceCategoryID=entry.category,
|
|
}
|
|
-- No PurchaseEligibility.Owned/PartiallyOwned, dollars, cents or retail
|
|
-- deliverable are fabricated. Source-mode price/ownership branches use
|
|
-- the explicit fields above and real Source result below.
|
|
return {entryID=entry.id, productID=entry.id, sharedData=shared,
|
|
displayID=presentation.displayID}
|
|
end
|
|
|
|
function Provider:GetProductInfo(id) return self:GetEntryInfo(id) end
|
|
|
|
function Provider:GetProducts(category)
|
|
local top=self.options.foreverLayout and foreverByGroup[category]
|
|
if top then
|
|
local ids={}
|
|
for _,source in ipairs(self:ForeverSources(top)) do
|
|
for _,id in ipairs(self:GetProducts(source)) do ids[#ids+1]=id end
|
|
end
|
|
return ids
|
|
end
|
|
local rows = {}
|
|
local slot=self.options.fashionSlotGroups and fashionByGroup[category]
|
|
-- data lookup: a Fashion slot reads Fashion (7); a Source ID reads itself
|
|
local sourceCategory=slot and 7 or (self.state.categories[category] and category)
|
|
if not sourceCategory or not self.state.complete[sourceCategory] then return rows end
|
|
for id,entry in pairs(self.state.entries[sourceCategory] or {}) do
|
|
local info = self:GetEntryInfo(id)
|
|
if info and (not slot or slot.key=="all" or self:FashionSlot(entry)==slot.key) then rows[#rows+1] = info end
|
|
end
|
|
table.sort(rows, function(a,b)
|
|
local ae,be = a.sharedData.sourceRaceEligible == true,b.sharedData.sourceRaceEligible == true
|
|
if ae ~= be then return ae end
|
|
if a.sharedData.name ~= b.sharedData.name then return a.sharedData.name < b.sharedData.name end
|
|
return a.entryID < b.entryID
|
|
end)
|
|
local ids = {}
|
|
for i, row in ipairs(rows) do ids[i] = row.entryID end
|
|
return ids
|
|
end
|
|
|
|
function Provider:CanPurchase(selection)
|
|
if self.pending then return false, "purchase-pending" end
|
|
if type(selection) ~= "table" or type(selection.sharedData) ~= "table" then return false, "invalid-selection" end
|
|
local current = self:GetEntryInfo(selection.entryID)
|
|
if not current or current.sharedData.sourceRevision ~= selection.sharedData.sourceRevision
|
|
or current.sharedData.sourcePriceTokens ~= selection.sharedData.sourcePriceTokens then
|
|
return false, "stale-selection"
|
|
end
|
|
if current.entryID > 999999 then return false, "unsupported-item-id" end
|
|
if not current.sharedData.buyableHere then return false, "source-restriction" end
|
|
if self.state.balance == nil then return false, "balance-unavailable" end
|
|
if self.state.balance < current.sharedData.sourcePriceTokens then return false, "insufficient-tokens" end
|
|
return true
|
|
end
|
|
|
|
-- Caller performs one trusted TW_SHOP send of this exact payload. No automatic
|
|
-- retry or pending cancellation: the wire response has no correlation ID.
|
|
function Provider:BeginPurchase(selection)
|
|
local valid, reason = self:CanPurchase(selection)
|
|
if not valid then return nil, reason end
|
|
self.pending = {entryID=selection.entryID}
|
|
self.result = nil
|
|
return "Buy:" .. selection.entryID
|
|
end
|
|
|
|
function Provider:GetPendingPurchase() return copy(self.pending) end
|
|
function Provider:TakePurchaseResult()
|
|
local result = self.result
|
|
self.result = nil
|
|
return copy(result)
|
|
end
|
|
|
|
function Provider:GetRefundCapability()
|
|
-- Existing TW_SHOP parser has no refund operation. Real item refund is a
|
|
-- separate native inventory transaction; catalogue balance is not proof.
|
|
return {available=false, reason="no-qualified-TW_SHOP-refund-operation"}
|
|
end
|
|
|
|
return Provider
|