92 lines
5.3 KiB
Lua
92 lines
5.3 KiB
Lua
-- Game-independent selection/ownership logic. This file never reads or writes game memory.
|
|
-- The future transport must capture the complete engine receipt before acknowledging a request.
|
|
local function copy(handles)
|
|
assert(type(handles)=='table'and #handles<=32,'饰品效果记录不可读')
|
|
local count=0;for k in pairs(handles)do assert(math.type(k)=='integer'and k>=1 and k<=#handles,'饰品效果记录不连续');count=count+1 end;assert(count==#handles,'饰品效果记录不连续')
|
|
local out={};for i,v in ipairs(handles)do assert(math.type(v)=='integer','饰品效果句柄无效');out[i]=v end
|
|
return out
|
|
end
|
|
local function same(a,b)
|
|
if type(a)~='table'or type(b)~='table'or #a~=#b then return false end
|
|
for i=1,#a do if a[i]~=b[i]then return false end end;return true
|
|
end
|
|
local function keys(t)local out={};for k in pairs(t)do out[#out+1]=k end;table.sort(out);return out end
|
|
function CreateAccessorySelectionSession(transport)
|
|
assert(type(transport.snapshot)=='function'and type(transport.send)=='function','饰品接线未就绪')
|
|
local s={desired={},observedOwned={},inventoryDeleted=0,leases={},pending=nil,stopping=false}
|
|
local function snapshot()
|
|
local p=transport.snapshot()
|
|
assert(type(p)=='table'and type(p.key)=='string'and p.key~=''and type(p.owned)=='table'and type(p.equipped)=='table'and type(p.effects)=='table','饰品上下文不可读')
|
|
return p
|
|
end
|
|
local function request(p,id,operation,expected)
|
|
local add=operation=='add'
|
|
local command={key=p.key,id=id,operation=operation,add=add,expected=expected and copy(expected)or nil}
|
|
s.pending=command -- Own the in-flight intent before transport can publish or throw.
|
|
local function done(kind,handles,why)
|
|
if s.pending~=command then return end
|
|
if kind=='obtained'and operation=='obtain'then
|
|
-- Remember the confirmed acquisition before another snapshot can observe a user deletion.
|
|
if s.desired[id]then s.observedOwned[id]=command.key end
|
|
-- Acquisition changes inventory, but does not create a removable effect lease.
|
|
elseif kind=='added'and add then
|
|
local ok,receipt=pcall(copy,handles)
|
|
if not ok then s.lastError=tostring(receipt);return end -- Keep request until a usable receipt arrives.
|
|
s.leases[id]={key=command.key,handles=receipt}
|
|
elseif (not add and(kind=='removed'or kind=='native'or kind=='absent'))or(add and(kind=='native'or kind=='present'))then
|
|
if not add then s.leases[id]=nil end
|
|
elseif kind=='failed'then
|
|
s.pending=nil;s.lastError=why or '饰品效果请求失败,恢复信息已保留';return
|
|
else s.lastError='饰品请求结果未知,保留在途状态';return end
|
|
s.pending=nil;s.lastError=nil
|
|
end
|
|
local ok,e=pcall(transport.send,command,done)
|
|
if not ok and s.pending==command then s.lastError=tostring(e)..';请求结果未确认,不能丢弃在途状态'end
|
|
end
|
|
s.select=function(ids)
|
|
assert(not s.stopping,'请重新开启饰品多选后再选择');assert(type(ids)=='table','请选择饰品')
|
|
local p=snapshot();local desired={}
|
|
for _,id in ipairs(ids)do
|
|
assert(type(id)=='string'and(p.owned[id]or p.available and p.available[id]),'饰品不在当前完整目录:'..tostring(id));desired[id]=true
|
|
end
|
|
s.desired=desired;s.observedOwned={};s.inventoryDeleted=0
|
|
for id in pairs(desired)do if p.owned[id]then s.observedOwned[id]=p.key end end
|
|
if not s.pending then s.lastError=nil end
|
|
end
|
|
s.tick=function()
|
|
if s.pending or s.lastError then return end
|
|
local ok,p=pcall(snapshot);if not ok then s.lastError=tostring(p);return end
|
|
-- A selected item that disappears after being owned was deleted externally.
|
|
-- Withdraw that intent before cleanup; acquisition is an explicit Apply action,
|
|
-- not a perpetual inventory replenisher. A new context is not proof of deletion.
|
|
for _,id in ipairs(keys(s.desired))do
|
|
if p.owned[id]then s.observedOwned[id]=p.key
|
|
elseif s.observedOwned[id]==p.key then
|
|
s.desired[id]=nil;s.observedOwned[id]=nil;s.inventoryDeleted=s.inventoryDeleted+1
|
|
end
|
|
end
|
|
-- The adapter must prove a previous context ended before retiring its receipts.
|
|
-- Changing a pointer or world key is never sufficient permission to write there.
|
|
for _,id in ipairs(keys(s.leases))do
|
|
local lease=s.leases[id]
|
|
if lease.key~=p.key then s.lastError='玩家或世界已变化,暂停并保留原效果恢复记录';return end
|
|
if p.equipped[id]or not p.effects[id]then s.leases[id]=nil
|
|
elseif not same(p.effects[id],lease.handles)then s.lastError='饰品效果已被外部更改,暂停以免误移除:'..id;return
|
|
elseif not s.desired[id]or not p.owned[id]then request(p,id,'remove',lease.handles);return end
|
|
end
|
|
if s.stopping then return end
|
|
for _,id in ipairs(keys(s.desired))do
|
|
if not p.owned[id]then
|
|
if p.available and p.available[id]then request(p,id,'obtain');return end
|
|
s.lastError='所选饰品已不在当前背包:'..id;return
|
|
end
|
|
if not p.equipped[id]and not p.effects[id]and not s.leases[id]then request(p,id,'add');return end
|
|
end
|
|
end
|
|
s.stop=function()s.stopping=true;s.desired={}end
|
|
s.retry=function()assert(not s.pending,'饰品请求仍在执行,暂不能重试');s.lastError=nil end
|
|
s.ownedCount=function()local n=0;for _ in pairs(s.leases)do n=n+1 end;return n end
|
|
s.restored=function()return s.stopping and not s.pending and next(s.leases)==nil end
|
|
return s
|
|
end
|