102 lines
5.0 KiB
Lua
102 lines
5.0 KiB
Lua
-- Octo's TW_SHOP wire data. IDs, prices and availability belong to the server.
|
|
TurtleDonationShop = TurtleDonationShop or {}
|
|
local Shop = TurtleDonationShop
|
|
Shop.prefix = "TW_SHOP"
|
|
|
|
local function split(text, separator)
|
|
local result, start = {}, 1
|
|
while true do
|
|
local at = string.find(text, separator, start, true)
|
|
if not at then result[#result + 1] = string.sub(text, start); return result end
|
|
result[#result + 1] = string.sub(text, start, at - 1)
|
|
start = at + #separator
|
|
end
|
|
end
|
|
local function number(text, integer, minimum)
|
|
local value = tonumber(text)
|
|
if not value or value ~= value or value == math.huge or value == -math.huge then return nil end
|
|
if integer and value % 1 ~= 0 then return nil end
|
|
if minimum and value < minimum then return nil end
|
|
if math.abs(value) > 2147483647 then return nil end
|
|
return value
|
|
end
|
|
function Shop.NewState()
|
|
return { categories = {}, order = {}, entries = {}, complete = {}, balance = nil }
|
|
end
|
|
function Shop.Parse(message)
|
|
if type(message) ~= "string" or #message > 8192 or string.find(message, "\0", 1, true) then return nil end
|
|
local balance = string.match(message, "^Balance:(%d+)$")
|
|
if balance then
|
|
balance = number(balance, true, 0)
|
|
return balance and { kind = "balance", value = balance } or nil
|
|
end
|
|
local categories = string.match(message, "^Categories:(.*)$")
|
|
if categories then
|
|
local rows, seen = {}, {}
|
|
for _, row in ipairs(split(categories, ";")) do
|
|
if row ~= "" then
|
|
local fields = split(row, "=")
|
|
if #fields ~= 4 then return nil end
|
|
local id, parent = number(fields[1], true, 1), number(fields[2], true, 0)
|
|
if not id or parent ~= 0 or fields[3] == "" or not string.match(fields[4], "^[%w_%-]+$") or seen[id] then return nil end
|
|
rows[#rows + 1] = { id = id, parent = parent, name = fields[3], icon = fields[4] }
|
|
seen[id] = true
|
|
if #rows > 64 then return nil end
|
|
end
|
|
end
|
|
return { kind = "categories", rows = rows }
|
|
end
|
|
local cat, boundary = string.match(message, "^Entries:(%d+)=(%a+)$")
|
|
if cat and (boundary == "start" or boundary == "end") then
|
|
cat = number(cat, true, 1)
|
|
return cat and { kind = boundary, category = cat } or nil
|
|
end
|
|
local payload = string.match(message, "^Entries:(.*)$")
|
|
if payload then
|
|
-- Empty description (field5) is intentional. Never use a token iterator
|
|
-- that drops it: all following item/model/position fields would shift.
|
|
local f = split(payload, "=")
|
|
-- The newer server appends two reserved fields after holiday
|
|
-- (empty colors and unrestricted gender). Older rows end at holiday.
|
|
if #f ~= 13 and not (#f == 15 and f[14] == "" and f[15] == "0") then return nil end
|
|
local entry = { name = f[3], description = f[5] }
|
|
local fields = { [1] = "category", [2] = "subcategory", [4] = "price", [6] = "id", [7] = "modelID", [8] = "itemID", [9] = "x", [10] = "y", [11] = "z", [12] = "rotation", [13] = "holiday" }
|
|
for column, name in pairs(fields) do
|
|
local integer = column < 9 or column == 13
|
|
entry[name] = number(f[column], integer, integer and 0 or nil)
|
|
if entry[name] == nil then return nil end
|
|
end
|
|
if entry.name == "" or entry.category < 1 or entry.subcategory ~= 0 or entry.id < 1 or entry.price <= 0 then return nil end
|
|
return { kind = "entry", value = entry, category = entry.category }
|
|
end
|
|
local result = string.match(message, "^BuyResult:([%w]+)$")
|
|
if result then return { kind = "result", value = result } end
|
|
end
|
|
function Shop.Apply(state, event)
|
|
if not event then return false end
|
|
if event.kind == "balance" then state.balance = event.value
|
|
elseif event.kind == "categories" then
|
|
state.categories, state.order, state.entries, state.complete = {}, {}, {}, {}
|
|
for _, category in ipairs(event.rows) do
|
|
state.categories[category.id] = category
|
|
state.order[#state.order + 1] = category.id
|
|
end
|
|
elseif event.kind == "start" then
|
|
if not state.categories[event.category] then return false end
|
|
state.entries[event.category], state.complete[event.category] = {}, false
|
|
elseif event.kind == "entry" then
|
|
local entries = state.entries[event.category]
|
|
if not entries or state.complete[event.category] then return false end
|
|
entries[event.value.id] = event.value
|
|
elseif event.kind == "end" then
|
|
if not state.entries[event.category] then return false end
|
|
state.complete[event.category] = true
|
|
elseif event.kind == "result" then state.lastResult = event.value
|
|
else return false end
|
|
return true
|
|
end
|
|
function Shop.Send(payload)
|
|
-- Qualified native self-service envelope. Gateway preserves Source GUILD.
|
|
return C_ChatInfo.SendAddonMessage(Shop.prefix, payload, "WHISPER", UnitName("player"))
|
|
end
|