From 17912f6054bfb47f39ede9622a2af80c58a0caaa Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Fri, 11 Jan 2019 14:59:33 -0500 Subject: [PATCH] milo shop updates --- .../item/{storeTab.lua => shopTab.lua} | 6 ++ .../{storeConfig.lua => shopConfig.lua} | 17 ++-- milo/plugins/{storeView.lua => shopView.lua} | 81 +++++++++++++------ swshop/autorun/startup.lua | 1 + swshop/{shop.lua => swshop.lua} | 26 +++--- 5 files changed, 91 insertions(+), 40 deletions(-) rename milo/plugins/item/{storeTab.lua => shopTab.lua} (90%) rename milo/plugins/{storeConfig.lua => shopConfig.lua} (80%) rename milo/plugins/{storeView.lua => shopView.lua} (60%) create mode 100644 swshop/autorun/startup.lua rename swshop/{shop.lua => swshop.lua} (86%) diff --git a/milo/plugins/item/storeTab.lua b/milo/plugins/item/shopTab.lua similarity index 90% rename from milo/plugins/item/storeTab.lua rename to milo/plugins/item/shopTab.lua index 2262570..25db2b6 100644 --- a/milo/plugins/item/storeTab.lua +++ b/milo/plugins/item/shopTab.lua @@ -25,6 +25,11 @@ local storeTab = UI.Window { required = true, validate = 'numeric', }, + [3] = UI.TextEntry { + limit = 64, + formLabel = 'Extra Info', formKey = 'info', + help = 'Additional info to display for item', + }, clearButton = UI.Button { x = 2, y = -2, event = 'clear', @@ -58,6 +63,7 @@ function storeTab:eventHandler(event) os.queueEvent('store_refresh') self:emit({ type = 'success_message', message = 'Updated' }) end + else return end diff --git a/milo/plugins/storeConfig.lua b/milo/plugins/shopConfig.lua similarity index 80% rename from milo/plugins/storeConfig.lua rename to milo/plugins/shopConfig.lua index 8d27e60..355777c 100644 --- a/milo/plugins/storeConfig.lua +++ b/milo/plugins/shopConfig.lua @@ -13,9 +13,9 @@ local wizardPage = UI.Window { manualControls = true, [1] = UI.TextEntry { formLabel = 'Domain', formKey = 'domain', - help = 'Krist wallet address', + help = 'Krist wallet domain (minus .kst)', limit = 64, - shadowText = 'example.kst', + shadowText = 'example', required = true, }, [2] = UI.TextEntry { @@ -25,7 +25,14 @@ local wizardPage = UI.Window { required = true, help = 'Krist wallet password', }, - [3] = UI.Chooser { + [3] = UI.TextEntry { + formLabel = 'Header', formKey = 'header', + help = 'Text to show in header', + limit = 64, + shadowText = "xxxx's shop", + required = false, + }, + [4] = UI.Chooser { width = 9, formLabel = 'Font Size', formKey = 'textScale', nochoice = 'Small', @@ -54,14 +61,14 @@ function wizardPage:isValidType(node) local m = device[node.name] return m and m.type == 'monitor' and { name = 'Store Front', - value = 'store', + value = 'shop', category = 'display', help = 'Add a store front display' } end function wizardPage:isValidFor(node) - return node.mtype == 'store' + return node.mtype == 'shop' end UI:getPage('nodeWizard').wizard:add({ storeFront = wizardPage }) diff --git a/milo/plugins/storeView.lua b/milo/plugins/shopView.lua similarity index 60% rename from milo/plugins/storeView.lua rename to milo/plugins/shopView.lua index 4e4959f..225fdcf 100644 --- a/milo/plugins/storeView.lua +++ b/milo/plugins/shopView.lua @@ -24,55 +24,92 @@ local function createPage(node) local page = UI.Page { parent = monitor, + header = UI.Window { + backgroundColor = colors.blue, + ey = 3, + }, grid = UI.Grid { - ey = -6, + y = 4, ey = -7, + headerHeight = 3, + headerBackgroundColor = colors.gray, + backgroundSelectedColor = colors.black, + unfocusedBackgroundSelectedColor = colors.gray, columns = { - { heading = 'Qty', key = 'count', width = 5 }, - { heading = 'Price', key = 'price', width = 5 }, + { heading = 'Stock', key = 'count', width = 6, justify = 'right' }, + { heading = ' Price', key = 'price', width = 9, justify = 'right' }, { heading = 'Name', key = 'displayName' }, - { heading = 'Address', key = 'address', width = 20 }, + { heading = 'Address', key = 'address', width = 16 }, }, sortColumn = 'displayName', }, - buttons = UI.Window { - y = -5, height = 5, + footer = UI.Window { + y = -6, backgroundColor = colors.gray, prevButton = UI.Button { - x = 2, y = 2, height = 3, width = 5, + x = 2, y = 3, height = 3, width = 5, event = 'previous', backgroundColor = colors.lightGray, - text = ' < ' + text = ' \017 ', }, nextButton = UI.Button { - x = -6, y = 2, height = 3, width = 5, + x = -6, y = 3, height = 3, width = 5, event = 'next', backgroundColor = colors.lightGray, - text = ' > ' + text = ' \016 ', }, + info = UI.Window { + x = 9, ex = -9, + textColor = colors.white, + } }, timestamp = os.clock(), } + function page.header:draw() + self:clear() + if node.header then + self:centeredWrite(2, node.header, nil, colors.white) + end + end + + function page.footer.info:draw() + self:clear() + local selected = page.grid:getSelected() + + if selected then + if selected.info then + self:centeredWrite(2, selected.info) + end + + self:centeredWrite(4, 'To purchase:') + self:centeredWrite(5, string.format('/pay %s@%s.kst ', selected.name, node.domain)) + end + end + function page.grid:getRowTextColor(row, selected) if row.count == 0 then - return colors.gray + return colors.lightGray end return UI.Grid:getRowTextColor(row, selected) end function page.grid:getDisplayValues(row) row = Util.shallowCopy(row) - row.count = Util.toBytes(row.count) - row.address = row.name .. '@' .. node.domain + row.count = Util.toBytes(row.count) .. ' x' + row.price = row.price .. ' kst ' + row.address = row.name return row end function page:eventHandler(event) if event.type == 'next' then - self.grid:nextPage() + self.grid:emit({ type = 'scroll_down' }) elseif event.type == 'previous' then - self.grid:previousPage() + self.grid:emit({ type = 'scroll_up' }) + + elseif event.type == 'grid_focus_row' then + self.footer:draw() else return UI.Page.eventHandler(self, event) @@ -90,19 +127,13 @@ local function createPage(node) self.grid.values = { } for k,v in pairs(config) do local item = list[k] - if item then + if item and item.count > 0 then table.insert(self.grid.values, { displayName = item.displayName, count = item.count, name = v.name, price = v.price, - }) - else - table.insert(self.grid.values, { - displayName = itemDB:getName(k), - count = 0, - name = v.name, - price = v.price, + info = v.info, }) end end @@ -128,7 +159,7 @@ end) Event.on('store_provide', function(_, item, quantity, uid) Milo:queueRequest({ }, function() - local count = Milo:eject(item, quantity) + local count = Milo:eject(itemDB:splitKey(item), quantity) os.queueEvent('store_provided', uid, count) end) end) @@ -140,7 +171,7 @@ local StoreTask = { } function StoreTask:cycle(context) - for node in context.storage:filterActive('store') do + for node in context.storage:filterActive('shop') do if not pages[node.name] then pages[node.name] = createPage(node) os.queueEvent('open_store', node.domain, node.password) diff --git a/swshop/autorun/startup.lua b/swshop/autorun/startup.lua new file mode 100644 index 0000000..45a9359 --- /dev/null +++ b/swshop/autorun/startup.lua @@ -0,0 +1 @@ +shell.openTab('swshop') \ No newline at end of file diff --git a/swshop/shop.lua b/swshop/swshop.lua similarity index 86% rename from swshop/shop.lua rename to swshop/swshop.lua index 8a54519..99389cf 100644 --- a/swshop/shop.lua +++ b/swshop/swshop.lua @@ -1,17 +1,20 @@ local programDir = fs.getDir(shell.getRunningProgram()) os.loadAPI(programDir .. '/'.. 'json') -local w = require("w") -local r = require("r") -local k = require("k") +local w = require("w") +local r = require("r") +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 await = jua.await +local fs = _G.fs +local json = _G.json +local os = _G.os +local rs = _G.rs local textutils = _G.textutils +rs.setOutput('top', false) + r.init(jua) w.init(jua) k.init(jua, json, w, r) @@ -22,6 +25,7 @@ local privatekey local address jua.on("terminate", function() + rs.setOutput('top', false) jua.stop() _G.printError("Terminated") end) @@ -32,9 +36,9 @@ local function getItemDetails(item) local t = f.readAll() f.close() t = textutils.unserialize(t) - for k, v in pairs(t) do + for key, v in pairs(t) do if v.name == item then - return k, v.price + return key, v.price end end end @@ -73,6 +77,7 @@ local function handleTransaction(transaction) local count = math.floor(value / price) local uid = math.random() + print('requesting %d of %s', count, itemId) os.queueEvent('store_provide', itemId, count, uid) local timerId = os.startTimer(5) while true do @@ -97,7 +102,8 @@ jua.on('open_store', function(_, _domain, _password) domain = _domain password = _password -print('opening store for: ' .. domain) + rs.setOutput('top', true) + print('opening store for: ' .. domain) privatekey = k.toKristWalletFormat(password) address = k.makev2address(privatekey)