From 91d434f93d27b99c62c15bb25bbf29713ccdf409 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sat, 12 Jan 2019 10:06:48 -0500 Subject: [PATCH] milo shop updates --- milo/Milo.lua | 10 ++++++++-- milo/plugins/item/shopTab.lua | 4 ++-- milo/plugins/shopConfig.lua | 10 ++++++++-- milo/plugins/shopView.lua | 32 +++++++++++++++++++++++++++---- swshop/autorun/startup.lua | 1 - swshop/swshop.lua | 36 ++++++++++++++++++----------------- swshop/w.lua | 5 ++++- 7 files changed, 69 insertions(+), 29 deletions(-) delete mode 100644 swshop/autorun/startup.lua diff --git a/milo/Milo.lua b/milo/Milo.lua index da34037..8751596 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -5,6 +5,7 @@ local Storage = require('storage') local UI = require('ui') local Util = require('util') +local colors = _G.colors local device = _G.device local fs = _G.fs local multishell = _ENV.multishell @@ -172,6 +173,11 @@ os.queueEvent( context.storage:isOnline() and 'storage_online' or 'storage_offline', context.storage:isOnline()) ---os.queueEvent('milo_cycle') - UI:pullEvents() + +for _, node in pairs(context.storage.nodes) do + if node.category == 'display' and node.adapter and node.adapter.clear then + node.adapter.setBackgroundColor(colors.black) + node.adapter.clear() + end +end diff --git a/milo/plugins/item/shopTab.lua b/milo/plugins/item/shopTab.lua index 25db2b6..fde7bd8 100644 --- a/milo/plugins/item/shopTab.lua +++ b/milo/plugins/item/shopTab.lua @@ -53,14 +53,14 @@ function storeTab:eventHandler(event) self.form:setValues({ }) config[self.item.key] = nil Config.update('shop', config) - os.queueEvent('store_refresh') + os.queueEvent('shop_refresh') self.form:draw() elseif event.type == 'update' then if self.form:save() then config[self.item.key] = self.form.values Config.update('shop', config) - os.queueEvent('store_refresh') + os.queueEvent('shop_refresh') self:emit({ type = 'success_message', message = 'Updated' }) end diff --git a/milo/plugins/shopConfig.lua b/milo/plugins/shopConfig.lua index 355777c..06b23f9 100644 --- a/milo/plugins/shopConfig.lua +++ b/milo/plugins/shopConfig.lua @@ -2,6 +2,7 @@ local UI = require('ui') local colors = _G.colors local device = _G.device +local os = _G.os --[[ Configuration Page ]]-- local wizardPage = UI.Window { @@ -9,7 +10,7 @@ local wizardPage = UI.Window { index = 2, backgroundColor = colors.cyan, form = UI.Form { - x = 2, ex = -2, y = 2, ey = -4, + x = 2, ex = -2, y = 2, ey = -2, manualControls = true, [1] = UI.TextEntry { formLabel = 'Domain', formKey = 'domain', @@ -32,6 +33,11 @@ local wizardPage = UI.Window { shadowText = "xxxx's shop", required = false, }, + warning = UI.Text { + x = 2, y = -1, + textColor = colors.yellow, + value = 'swshop Package must be installed', + }, [4] = UI.Chooser { width = 9, formLabel = 'Font Size', formKey = 'textScale', @@ -54,7 +60,7 @@ function wizardPage:validate() end function wizardPage:saveNode(node) --- queue event ?? + os.queueEvent('shop_restart', node) end function wizardPage:isValidType(node) diff --git a/milo/plugins/shopView.lua b/milo/plugins/shopView.lua index a9ca230..18a5c88 100644 --- a/milo/plugins/shopView.lua +++ b/milo/plugins/shopView.lua @@ -7,9 +7,32 @@ local UI = require('ui') local Util = require('util') local colors = _G.colors +local multishell = _ENV.multishell local os = _G.os +local shell = _ENV.shell local config = Config.load('shop') +local shopTab + +local function startShop(node) + if shopTab then + multishell.terminate(shopTab) + end + shopTab = shell.openTab('/packages/swshop/swshop.lua', node.domain, node.password) +end + +-- node has been reconfigured +Event.on('shop_restart', function(_, node) + startShop(node) +end) + +-- milo is being terminated +Event.on('terminate', function() + if shopTab then + multishell.terminate(shopTab) + shopTab = nil + end +end) --[[ Display ]]-- local function createPage(node) @@ -163,14 +186,15 @@ end local pages = { } -- called when an item to sell has been changed -Event.on('store_refresh', function() +Event.on('shop_refresh', function() config = Config.load('shop') end) -Event.on('store_provide', function(_, item, quantity, uid) +-- called from the shop when an item has been purchased +Event.on('shop_provide', function(_, item, quantity, uid) Milo:queueRequest({ }, function() local count = Milo:eject(itemDB:splitKey(item), quantity) - os.queueEvent('store_provided', uid, count) + os.queueEvent('shop_provided', uid, count) Sound.play('entity.villager.yes') end) end) @@ -184,8 +208,8 @@ local StoreTask = { function StoreTask:cycle(context) for node in context.storage:filterActive('shop') do if not pages[node.name] then + startShop(node) pages[node.name] = createPage(node) - os.queueEvent('open_store', node.domain, node.password) end -- update the display pages[node.name]:update() diff --git a/swshop/autorun/startup.lua b/swshop/autorun/startup.lua deleted file mode 100644 index 45a9359..0000000 --- a/swshop/autorun/startup.lua +++ /dev/null @@ -1 +0,0 @@ -shell.openTab('swshop') \ No newline at end of file diff --git a/swshop/swshop.lua b/swshop/swshop.lua index d05faa0..495e463 100644 --- a/swshop/swshop.lua +++ b/swshop/swshop.lua @@ -1,3 +1,7 @@ +local fs = _G.fs +local os = _G.os +local shell = _ENV.shell + local programDir = fs.getDir(shell.getRunningProgram()) os.loadAPI(programDir .. '/'.. 'json') @@ -7,9 +11,7 @@ local k = require("k") local jua = require("jua") local await = jua.await -local fs = _G.fs local json = _G.json -local os = _G.os local rs = _G.rs local textutils = _G.textutils @@ -19,10 +21,15 @@ r.init(jua) w.init(jua) k.init(jua, json, w, r) -local domain -local password -local privatekey -local address +local function Syntax() + error('Syntax: swshop [domain] [password]') +end + +local args = { ... } +local domain = args[1] or Syntax() +local password = args[2] or Syntax() +local privatekey = k.toKristWalletFormat(password) +local address = k.makev2address(privatekey) jua.on("terminate", function() rs.setOutput('top', false) @@ -78,7 +85,7 @@ local function handleTransaction(transaction) local count = math.floor(value / price) local uid = math.random() print(string.format('requesting %d of %s', count, itemId)) - os.queueEvent('store_provide', itemId, count, uid) + os.queueEvent('shop_provide', itemId, count, uid) local timerId = os.startTimer(5) while true do local e, p1, p2 = os.pullEvent() @@ -87,7 +94,7 @@ local function handleTransaction(transaction) refundTransaction(value, "error=Timed out attempting to provide items") break - elseif e == 'store_provided' and p1 == uid then + elseif e == 'shop_provided' and p1 == uid then local extra = value - (price * p2) if extra > 0 then print('extra: ' .. extra) @@ -98,28 +105,23 @@ local function handleTransaction(transaction) end end -jua.on('open_store', function(_, _domain, _password) - domain = _domain - password = _password - - rs.setOutput('top', true) +local function connect() print('opening store for: ' .. domain) - privatekey = k.toKristWalletFormat(password) - address = k.makev2address(privatekey) - local success, ws = await(k.connect, privatekey) assert(success, "Failed to get websocket URL") print("Connected to websocket.") + rs.setOutput('top', true) success = await(ws.subscribe, "ownTransactions", function(data) local transaction = data.transaction handleTransaction(transaction) end) assert(success, "Failed to subscribe to event") -end) +end jua.go(function() print("Ready") + connect() end) diff --git a/swshop/w.lua b/swshop/w.lua index 7158792..084133d 100644 --- a/swshop/w.lua +++ b/swshop/w.lua @@ -31,7 +31,10 @@ end local function findID(url) local found = gfind(url, idPatt) - return tonumber(found[#found]:sub(found[#found]:find("%d+"))) + local id = tonumber(found[#found]:sub(found[#found]:find("%d+"))) + _debug('id: ') + _debug(id) + return id end local function newID()