rework plugin management
This commit is contained in:
@@ -35,25 +35,7 @@ function craftTask:craft(recipe, item)
|
||||
item.ingredients[recipe.result].total = item.count
|
||||
item.ingredients[recipe.result].crafted = item.crafted
|
||||
|
||||
--[[
|
||||
_G._p2 = item
|
||||
if not item.history then
|
||||
item.history = { }
|
||||
end
|
||||
local t = Util.shallowCopy(item)
|
||||
t.history = { input = { }, output = { } }
|
||||
for k,v in pairs(item.ingredients) do
|
||||
t.history.input[k] = Util.shallowCopy(v)
|
||||
end
|
||||
table.insert(item.history, t)
|
||||
]]
|
||||
Craft.craftRecipe(recipe, item.requested - item.crafted, context.storage, item)
|
||||
|
||||
--[[
|
||||
for k,v in pairs(item.ingredients) do
|
||||
t.history.output[k] = Util.shallowCopy(v)
|
||||
end
|
||||
]]
|
||||
end
|
||||
|
||||
function craftTask:cycle()
|
||||
|
||||
@@ -1,28 +1,8 @@
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local fs = _G.fs
|
||||
local shell = _ENV.shell
|
||||
|
||||
local function loadDirectory(dir)
|
||||
local tabs = { }
|
||||
for _, file in pairs(fs.list(dir)) do
|
||||
if not fs.isDir(fs.combine(dir, file)) then
|
||||
local s, m = Util.run(_ENV, fs.combine(dir, file))
|
||||
if not s and m then
|
||||
_G.printError('Error loading: ' .. file)
|
||||
error(m or 'Unknown error')
|
||||
end
|
||||
table.insert(tabs, m)
|
||||
end
|
||||
end
|
||||
return tabs
|
||||
end
|
||||
|
||||
local programDir = fs.getDir(shell.getRunningProgram())
|
||||
local tabs = loadDirectory(fs.combine(programDir, 'plugins/item'))
|
||||
|
||||
table.sort(tabs, function(a, b) return a.index < b.index end)
|
||||
local context = Milo:getContext()
|
||||
|
||||
local page = UI.Page {
|
||||
titleBar = UI.TitleBar {
|
||||
@@ -34,12 +14,21 @@ local page = UI.Page {
|
||||
}
|
||||
|
||||
function page:enable(item)
|
||||
for _, v in pairs(tabs) do
|
||||
if not self.tabs then
|
||||
table.sort(context.plugins.itemTab, function(a, b) return a.index < b.index end)
|
||||
local t = Util.shallowCopy(context.plugins.itemTab)
|
||||
t.y = 2
|
||||
t.ey = -2
|
||||
|
||||
self:add({ tabs = UI.Tabs(t) })
|
||||
end
|
||||
|
||||
for _, v in pairs(context.plugins.itemTab) do
|
||||
if v.UIElement then
|
||||
v:setItem(item)
|
||||
end
|
||||
end
|
||||
self.tabs:selectTab(tabs[1])
|
||||
self.tabs:selectTab(context.plugins.itemTab[1])
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
@@ -69,10 +58,4 @@ function page:eventHandler(event)
|
||||
return true
|
||||
end
|
||||
|
||||
local t = Util.shallowCopy(tabs)
|
||||
t.y = 2
|
||||
t.ey = -2
|
||||
|
||||
page:add({ tabs = UI.Tabs(t) })
|
||||
|
||||
UI:addPage('item', page)
|
||||
|
||||
@@ -43,4 +43,4 @@ function infoTab:draw()
|
||||
UI.Window.draw(self)
|
||||
end
|
||||
|
||||
return infoTab
|
||||
return { itemTab = infoTab }
|
||||
|
||||
@@ -61,4 +61,4 @@ function machinesTab:eventHandler(event)
|
||||
end
|
||||
end
|
||||
|
||||
return machinesTab
|
||||
return { itemTab = machinesTab }
|
||||
|
||||
@@ -93,4 +93,4 @@ function manageTab:eventHandler(event)
|
||||
return true
|
||||
end
|
||||
|
||||
return manageTab
|
||||
return { itemTab = manageTab }
|
||||
|
||||
@@ -87,4 +87,4 @@ function recipeTab:eventHandler(event)
|
||||
end
|
||||
end
|
||||
|
||||
return recipeTab
|
||||
return { itemTab = recipeTab }
|
||||
|
||||
@@ -52,4 +52,4 @@ function resetTab:eventHandler(event)
|
||||
end
|
||||
end
|
||||
|
||||
return resetTab
|
||||
return { itemTab = resetTab }
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
local Config = require('config')
|
||||
local UI = require('ui')
|
||||
|
||||
local os = _G.os
|
||||
|
||||
local config = Config.load('shop')
|
||||
|
||||
local storeTab = UI.Window {
|
||||
tabTitle = 'Store',
|
||||
index = 6,
|
||||
form = UI.Form {
|
||||
x = 2, ex = -2, y = 2, ey = -2,
|
||||
manualControls = true,
|
||||
[1] = UI.TextEntry {
|
||||
formLabel = 'Name', formKey = 'name',
|
||||
help = 'Unique name used when paying for an item',
|
||||
required = true,
|
||||
width = 12,
|
||||
limit = 64,
|
||||
},
|
||||
[2] = UI.TextEntry {
|
||||
width = 6,
|
||||
formLabel = 'Price', formKey = 'price',
|
||||
help = 'Per item cost',
|
||||
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',
|
||||
text = 'Remove',
|
||||
},
|
||||
updateButton = UI.Button {
|
||||
x = -12, y = -2,
|
||||
event = 'update',
|
||||
text = 'Update',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
function storeTab:setItem(item)
|
||||
self.item = item
|
||||
self.form:setValues(config[item.key] or { })
|
||||
end
|
||||
|
||||
function storeTab:eventHandler(event)
|
||||
if event.type == 'clear' then
|
||||
self.form:setValues({ })
|
||||
config[self.item.key] = nil
|
||||
Config.update('shop', config)
|
||||
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('shop_refresh')
|
||||
self:emit({ type = 'success_message', message = 'Updated' })
|
||||
end
|
||||
|
||||
else
|
||||
return
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return storeTab
|
||||
@@ -1,80 +0,0 @@
|
||||
local UI = require('ui')
|
||||
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
local os = _G.os
|
||||
|
||||
--[[ Configuration Page ]]--
|
||||
local wizardPage = UI.Window {
|
||||
title = 'Store Front',
|
||||
index = 2,
|
||||
backgroundColor = colors.cyan,
|
||||
form = UI.Form {
|
||||
x = 2, ex = -2, y = 1, ey = -2,
|
||||
manualControls = true,
|
||||
[1] = UI.TextEntry {
|
||||
formLabel = 'Domain', formKey = 'domain',
|
||||
help = 'Krist wallet domain (minus .kst)',
|
||||
limit = 64,
|
||||
shadowText = 'example',
|
||||
required = true,
|
||||
},
|
||||
[2] = UI.TextEntry {
|
||||
formLabel = 'Password', formKey = 'password',
|
||||
shadowText = 'password',
|
||||
limit = 64,
|
||||
required = true,
|
||||
help = 'Krist wallet password',
|
||||
},
|
||||
[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',
|
||||
choices = {
|
||||
{ name = 'Small', value = .5 },
|
||||
{ name = 'Large', value = 1 },
|
||||
},
|
||||
help = 'Adjust text scaling',
|
||||
},
|
||||
warning = UI.Text {
|
||||
x = 2, y = -1,
|
||||
textColor = colors.orange,
|
||||
value = 'Package swshop must be installed',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
function wizardPage:setNode(node)
|
||||
self.form:setValues(node)
|
||||
end
|
||||
|
||||
function wizardPage:validate()
|
||||
return self.form:save()
|
||||
end
|
||||
|
||||
function wizardPage:saveNode(node)
|
||||
os.queueEvent('shop_restart', node)
|
||||
end
|
||||
|
||||
function wizardPage:isValidType(node)
|
||||
local m = device[node.name]
|
||||
return m and m.type == 'monitor' and {
|
||||
name = 'Store Front',
|
||||
value = 'shop',
|
||||
category = 'display',
|
||||
help = 'Add a store front display'
|
||||
}
|
||||
end
|
||||
|
||||
function wizardPage:isValidFor(node)
|
||||
return node.mtype == 'shop'
|
||||
end
|
||||
|
||||
UI:getPage('nodeWizard').wizard:add({ storeFront = wizardPage })
|
||||
@@ -1,219 +0,0 @@
|
||||
local Config = require('config')
|
||||
local Event = require('event')
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
local Sound = require('sound')
|
||||
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)
|
||||
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 = 'Name', key = 'displayName' },
|
||||
{ heading = ' Price', key = 'price', width = 9, justify = 'right' },
|
||||
{ heading = 'Address', key = 'address', width = 12 },
|
||||
},
|
||||
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
|
||||
self:write(self.width - 15, 3, 'powered by Milo', nil, colors.lightGray)
|
||||
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 selected then
|
||||
return colors.yellow
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
function page.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.count = Util.toBytes(row.count) .. ' '
|
||||
row.price = string.format('%s kst ', row.price)
|
||||
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
|
||||
|
||||
local chars = { '\183', '\7', '\186', '\7' }
|
||||
Event.onInterval(1, function()
|
||||
local ch = chars[math.floor(os.clock() % #chars) + 1]
|
||||
page.header:write(2, 2, ch)
|
||||
page.header:write(page.header.width - 1, 2, ch)
|
||||
page:sync()
|
||||
end)
|
||||
|
||||
UI:setPage(page)
|
||||
return page
|
||||
end
|
||||
|
||||
local pages = { }
|
||||
|
||||
-- called when an item to sell has been changed
|
||||
Event.on('shop_refresh', function()
|
||||
config = Config.load('shop')
|
||||
end)
|
||||
|
||||
-- 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('shop_provided', uid, count)
|
||||
Sound.play('entity.villager.yes')
|
||||
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
|
||||
startShop(node)
|
||||
pages[node.name] = createPage(node)
|
||||
end
|
||||
-- update the display
|
||||
pages[node.name]:update()
|
||||
end
|
||||
end
|
||||
|
||||
Milo:registerTask(StoreTask)
|
||||
Reference in New Issue
Block a user