milo: refresh on storage config change + rabbit + auto detect player name

This commit is contained in:
kepler155c
2018-11-21 22:05:04 -05:00
parent 0787cc7913
commit 7498276573
5 changed files with 85 additions and 56 deletions

View File

@@ -128,13 +128,6 @@ local page = UI.Page {
required = true, required = true,
}, },
[2] = UI.TextEntry { [2] = UI.TextEntry {
formLabel = 'User Name', formKey = 'user',
help = 'User name for bound manipulator',
shadowText = 'User name',
limit = 50,
required = true,
},
[3] = UI.TextEntry {
formLabel = 'Return Slot', formKey = 'slot', formLabel = 'Return Slot', formKey = 'slot',
help = 'Use a slot for sending to storage', help = 'Use a slot for sending to storage',
shadowText = 'Inventory slot #', shadowText = 'Inventory slot #',
@@ -142,12 +135,12 @@ local page = UI.Page {
validate = 'numeric', validate = 'numeric',
required = false, required = false,
}, },
[4] = UI.Checkbox { [3] = UI.Checkbox {
formLabel = 'Shield Slot', formKey = 'useShield', formLabel = 'Shield Slot', formKey = 'useShield',
help = 'Or, use the shield slot for sending' help = 'Or, use the shield slot for sending'
}, },
info = UI.TextArea { info = UI.TextArea {
x = 1, ex = -1, y = 7, ey = -3, x = 1, ex = -1, y = 6, ey = -4,
textColor = colors.yellow, textColor = colors.yellow,
marginLeft = 0, marginLeft = 0,
marginRight = 0, marginRight = 0,
@@ -163,6 +156,14 @@ local page = UI.Page {
items = { }, items = { },
} }
local function getPlayerName()
local neural = device.neuralInterface
if neural and neural.getName then
return neural.getName()
end
end
local function filterItems(t, filter, displayMode) local function filterItems(t, filter, displayMode)
if filter or displayMode > 0 then if filter or displayMode > 0 then
local r = { } local r = { }
@@ -194,9 +195,12 @@ function page:sendRequest(data)
if not config.server then if not config.server then
self:setStatus('Invalid configuration') self:setStatus('Invalid configuration')
Event.onTimeout(2, function() return
self:setStatus('') end
end)
local player = getPlayerName()
if not player then
self:setStatus('Missing neural or introspection')
return return
end end
@@ -207,7 +211,7 @@ function page:sendRequest(data)
self:setStatus('connecting ...') self:setStatus('connecting ...')
socket, msg = Socket.connect(config.server, 4242) socket, msg = Socket.connect(config.server, 4242)
if socket then if socket then
socket:write(config.user) socket:write(player)
local r = socket:read(2) local r = socket:read(2)
if r and not r.msg then if r and not r.msg then
self:setStatus('connected ...') self:setStatus('connected ...')
@@ -416,27 +420,32 @@ Event.addRoutine(function()
if not neural or not neural[inv] then if not neural or not neural[inv] then
_G._debug('missing Introspection module') _G._debug('missing Introspection module')
elseif config.server and (config.useShield or config.slot) then elseif config.server and (config.useShield or config.slot) then
local method = neural[inv] local s, m = pcall(function()
local item = method and method().getItemMeta(config.useShield and SHIELD_SLOT or config.slot) local method = neural[inv]
if item then local item = method and method().getItemMeta(config.useShield and SHIELD_SLOT or config.slot)
local slotNo = config.useShield and 'shield' or config.slot if item then
local response = page:sendRequest({ local slotNo = config.useShield and 'shield' or config.slot
request = 'deposit', local response = page:sendRequest({
slot = slotNo, request = 'deposit',
count = item.count, slot = slotNo,
key = table.concat({ item.name, item.damage, item.nbtHash }, ':') count = item.count,
}) key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
if response then })
local ritem = page.items[response.key] if response then
if ritem then local ritem = page.items[response.key]
ritem.count = response.current + item.count if ritem then
ritem.count = response.current + item.count
end
page.grid:draw()
page:sync()
sleepTime = math.max(sleepTime - .25, .25)
end end
page.grid:draw() else
page:sync() sleepTime = math.min(sleepTime + .25, 1.5)
sleepTime = math.max(sleepTime - .25, .25)
end end
else end)
sleepTime = math.min(sleepTime + .25, 1.5) if not s and m then
_debug(m)
end end
end end
end end

View File

@@ -301,8 +301,9 @@ function Milo:saveResources()
end end
-- Return a list of everything in the system -- Return a list of everything in the system
function Milo:listItems(forceRefresh) function Milo:listItems(forceRefresh, throttle)
return forceRefresh and self.context.storage:refresh() or self.context.storage:listItems() return forceRefresh and self.context.storage:refresh(throttle) or
self.context.storage:listItems(throttle)
end end
return Milo return Milo

View File

@@ -141,6 +141,10 @@ function Storage:onlineAdapters()
end end
end end
function Storage:setDirty()
self.dirty = true
end
function Storage:refresh(throttle) function Storage:refresh(throttle)
self.dirty = true self.dirty = true
self.lastRefresh = os.clock() self.lastRefresh = os.clock()

View File

@@ -140,6 +140,9 @@ end
function networkPage:disable() function networkPage:disable()
UI.Page.disable(self) UI.Page.disable(self)
Event.off(self.handler) Event.off(self.handler)
-- Since some storage may have been added/removed - force a full rescan
context.storage:setDirty()
end end
function networkPage:applyFilter() function networkPage:applyFilter()

View File

@@ -108,6 +108,10 @@ local listingPage = UI.Page {
}, },
}, },
notification = UI.Notification(), notification = UI.Notification(),
throttle = UI.Throttle {
textColor = colors.yellow,
borderColor = colors.gray,
},
accelerators = { accelerators = {
r = 'refresh', r = 'refresh',
[ 'control-r' ] = 'refresh', [ 'control-r' ] = 'refresh',
@@ -240,32 +244,36 @@ function listingPage:eventHandler(event)
end end
function listingPage:enable() function listingPage:enable()
self:refresh() Event.onTimeout(0, function()
self:setFocus(self.statusBar.filter) self:refresh()
self:draw()
self:sync()
self.timer = Event.onInterval(3, function() self.timer = Event.onInterval(3, function()
for _,v in pairs(self.allItems) do for _,v in pairs(self.allItems) do
local c = context.storage.cache[v.key] local c = context.storage.cache[v.key]
v.count = c and c.count or 0 v.count = c and c.count or 0
end
self.grid:draw()
self:sync()
end)
local function updateStatus()
self.statusBar.storageStatus.value =
context.storage:isOnline() and '' or 'offline'
self.statusBar.storageStatus.textColor =
context.storage:isOnline() and colors.lime or colors.red
end end
self.grid:draw()
self:sync()
end)
local function updateStatus()
self.statusBar.storageStatus.value =
context.storage:isOnline() and '' or 'offline'
self.statusBar.storageStatus.textColor =
context.storage:isOnline() and colors.lime or colors.red
end
self.handler = Event.on({ 'storage_offline', 'storage_online' }, function()
updateStatus() updateStatus()
self.statusBar.storageStatus:draw()
self:sync() self.handler = Event.on({ 'storage_offline', 'storage_online' }, function()
updateStatus()
self.statusBar.storageStatus:draw()
self:sync()
end)
end) end)
updateStatus() self:setFocus(self.statusBar.filter)
UI.Page.enable(self) UI.Page.enable(self)
end end
@@ -276,8 +284,12 @@ function listingPage:disable()
end end
function listingPage:refresh(force) function listingPage:refresh(force)
self.allItems = Milo:mergeResources(Milo:listItems(force)) local throttle = function() self.throttle:update() end
self.throttle:enable()
self.allItems = Milo:mergeResources(Milo:listItems(force, throttle))
self:applyFilter() self:applyFilter()
self.throttle:disable()
end end
function listingPage:applyFilter() function listingPage:applyFilter()