milo shop updates
This commit is contained in:
184
milo/plugins/shopView.lua
Normal file
184
milo/plugins/shopView.lua
Normal file
@@ -0,0 +1,184 @@
|
||||
local Config = require('config')
|
||||
local Event = require('event')
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local os = _G.os
|
||||
|
||||
local config = Config.load('shop')
|
||||
|
||||
--[[ Display ]]--
|
||||
local function createPage(node)
|
||||
local monitor = UI.Device {
|
||||
device = node.adapter,
|
||||
textScale = node.textScale or .5,
|
||||
}
|
||||
|
||||
function monitor:resize()
|
||||
self.textScale = node.textScale or .5
|
||||
UI.Device.resize(self)
|
||||
end
|
||||
|
||||
local page = UI.Page {
|
||||
parent = monitor,
|
||||
header = UI.Window {
|
||||
backgroundColor = colors.blue,
|
||||
ey = 3,
|
||||
},
|
||||
grid = UI.Grid {
|
||||
y = 4, ey = -7,
|
||||
headerHeight = 3,
|
||||
headerBackgroundColor = colors.gray,
|
||||
backgroundSelectedColor = colors.black,
|
||||
unfocusedBackgroundSelectedColor = colors.gray,
|
||||
columns = {
|
||||
{ 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 = 16 },
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
},
|
||||
footer = UI.Window {
|
||||
y = -6,
|
||||
backgroundColor = colors.gray,
|
||||
prevButton = UI.Button {
|
||||
x = 2, y = 3, height = 3, width = 5,
|
||||
event = 'previous',
|
||||
backgroundColor = colors.lightGray,
|
||||
text = ' \017 ',
|
||||
},
|
||||
nextButton = UI.Button {
|
||||
x = -6, y = 3, height = 3, width = 5,
|
||||
event = 'next',
|
||||
backgroundColor = colors.lightGray,
|
||||
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 <amount>', selected.name, node.domain))
|
||||
end
|
||||
end
|
||||
|
||||
function page.grid:getRowTextColor(row, selected)
|
||||
if row.count == 0 then
|
||||
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) .. ' x'
|
||||
row.price = row.price .. ' kst '
|
||||
row.address = row.name
|
||||
return row
|
||||
end
|
||||
|
||||
function page:eventHandler(event)
|
||||
if event.type == 'next' then
|
||||
self.grid:emit({ type = 'scroll_down' })
|
||||
|
||||
elseif event.type == 'previous' then
|
||||
self.grid:emit({ type = 'scroll_up' })
|
||||
|
||||
elseif event.type == 'grid_focus_row' then
|
||||
self.footer:draw()
|
||||
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
end
|
||||
|
||||
Event.onTimeout(.1, function()
|
||||
self:setFocus(self.grid)
|
||||
self:sync()
|
||||
end)
|
||||
return true
|
||||
end
|
||||
|
||||
function page:refresh()
|
||||
local list = Milo:listItems()
|
||||
self.grid.values = { }
|
||||
for k,v in pairs(config) do
|
||||
local item = list[k]
|
||||
if item and item.count > 0 then
|
||||
table.insert(self.grid.values, {
|
||||
displayName = item.displayName,
|
||||
count = item.count,
|
||||
name = v.name,
|
||||
price = v.price,
|
||||
info = v.info,
|
||||
})
|
||||
end
|
||||
end
|
||||
self.grid:update()
|
||||
self.grid:draw()
|
||||
end
|
||||
|
||||
function page:update()
|
||||
page:refresh()
|
||||
page:sync()
|
||||
end
|
||||
|
||||
UI:setPage(page)
|
||||
return page
|
||||
end
|
||||
|
||||
local pages = { }
|
||||
|
||||
-- called when an item to sell has been changed
|
||||
Event.on('store_refresh', function()
|
||||
config = Config.load('shop')
|
||||
end)
|
||||
|
||||
Event.on('store_provide', function(_, item, quantity, uid)
|
||||
Milo:queueRequest({ }, function()
|
||||
local count = Milo:eject(itemDB:splitKey(item), quantity)
|
||||
os.queueEvent('store_provided', uid, count)
|
||||
end)
|
||||
end)
|
||||
|
||||
--[[ Task ]]--
|
||||
local StoreTask = {
|
||||
name = 'store',
|
||||
priority = 30,
|
||||
}
|
||||
|
||||
function StoreTask:cycle(context)
|
||||
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)
|
||||
end
|
||||
-- update the display
|
||||
pages[node.name]:update()
|
||||
end
|
||||
end
|
||||
|
||||
Milo:registerTask(StoreTask)
|
||||
Reference in New Issue
Block a user