milo: improve list perf
This commit is contained in:
@@ -3,32 +3,23 @@ _G.requireInjector()
|
||||
local Event = require('event')
|
||||
local UI = require('ui')
|
||||
|
||||
local colors = _G.colors
|
||||
local peripheral = _G.peripheral
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
local turtle = _G.turtle
|
||||
|
||||
local speaker = peripheral.find('speaker') or
|
||||
error('Speaker must be attached')
|
||||
if not turtle then
|
||||
error('This program can only be run on a turtle')
|
||||
end
|
||||
|
||||
local radio = device.drive or error('No drive attached')
|
||||
if radio.side ~= 'top' and radio.side ~= 'bottom' then
|
||||
error('Disk drive must be above or below turtle')
|
||||
end
|
||||
|
||||
UI:configure('Music', ...)
|
||||
|
||||
UI.Button.defaults.backgroundFocusColor = colors.gray
|
||||
|
||||
local songs = {
|
||||
{ record = 'record.11', title = '11' },
|
||||
{ record = 'record.13', title = '13' },
|
||||
{ record = 'record.block', title = 'Block' },
|
||||
{ record = 'record.cat', title = 'Cat' },
|
||||
{ record = 'record.chirp', title = 'Chirp' },
|
||||
{ record = 'record.far', title = 'Faf' },
|
||||
{ record = 'record.mall', title = 'Mall' },
|
||||
{ record = 'record.mellohi', title = 'Mellohi' },
|
||||
{ record = 'record.stal', title = 'Stal' },
|
||||
{ record = 'record.strad', title = 'Strad' },
|
||||
{ record = 'record.wait', title = 'Wait' },
|
||||
{ record = 'record.ward', title = 'Ward' },
|
||||
}
|
||||
local songNo = 1
|
||||
|
||||
local page = UI.Page({
|
||||
volume = 15,
|
||||
stationName = UI.Text({
|
||||
@@ -177,18 +168,41 @@ function page:setVolume(volume)
|
||||
end
|
||||
|
||||
function page:seek()
|
||||
songNo = songNo + 1
|
||||
if songNo > #songs then
|
||||
songNo = 1
|
||||
end
|
||||
|
||||
local actions = {
|
||||
top = {
|
||||
suck = turtle.suckUp,
|
||||
drop = turtle.dropUp,
|
||||
},
|
||||
bottom = {
|
||||
suck = turtle.suckDown,
|
||||
drop = turtle.dropDown,
|
||||
},
|
||||
}
|
||||
|
||||
local slot = turtle.selectOpenSlot()
|
||||
actions[radio.side].suck()
|
||||
repeat
|
||||
slot = slot + 1
|
||||
if (slot > 16) then
|
||||
slot = 1
|
||||
end
|
||||
until turtle.getItemCount(slot) >= 1
|
||||
turtle.select(slot)
|
||||
actions[radio.side].drop()
|
||||
self:updateStationName()
|
||||
end
|
||||
|
||||
function page:play(onOff)
|
||||
self.playing = onOff
|
||||
if self.playing then
|
||||
|
||||
if not radio.hasAudio() then
|
||||
self:seek()
|
||||
end
|
||||
|
||||
self:updateStationName()
|
||||
speaker.playSound(songs[songNo])
|
||||
radio.playAudio()
|
||||
|
||||
Event.addNamedTimer('songTimer', 180, false, function()
|
||||
if self.playing then
|
||||
@@ -199,7 +213,7 @@ function page:play(onOff)
|
||||
end)
|
||||
|
||||
else
|
||||
--radio.stopAudio()
|
||||
radio.stopAudio()
|
||||
end
|
||||
end
|
||||
|
||||
@@ -209,7 +223,7 @@ function page.stationName:draw()
|
||||
end
|
||||
|
||||
function page:updateStationName()
|
||||
local title = songs[songNo].title
|
||||
local title = radio.getAudioTitle()
|
||||
|
||||
if title then
|
||||
self.stationName.value = title
|
||||
@@ -234,7 +248,9 @@ page:setVolume(page.volume, true)
|
||||
|
||||
UI:setPage(page)
|
||||
|
||||
turtle.setStatus('Jamming')
|
||||
UI:pullEvents()
|
||||
turtle.setStatus('idle')
|
||||
page:play(false)
|
||||
|
||||
UI.term:reset()
|
||||
|
||||
@@ -244,6 +244,7 @@ function page:sendRequest(data, statusMsg)
|
||||
return
|
||||
end
|
||||
|
||||
local success
|
||||
sync(self, function()
|
||||
local msg
|
||||
for _ = 1, 2 do
|
||||
@@ -271,7 +272,8 @@ function page:sendRequest(data, statusMsg)
|
||||
end)
|
||||
end
|
||||
if socket:write(data) then
|
||||
return true
|
||||
success = true
|
||||
return
|
||||
end
|
||||
socket:close()
|
||||
socket = nil
|
||||
@@ -279,6 +281,8 @@ function page:sendRequest(data, statusMsg)
|
||||
end
|
||||
self:setStatus(msg or 'Failed to connect')
|
||||
end)
|
||||
|
||||
return success
|
||||
end
|
||||
|
||||
function page.grid:getRowTextColor(row, selected)
|
||||
@@ -505,8 +509,13 @@ function page:applyFilter()
|
||||
end
|
||||
|
||||
Event.addRoutine(function()
|
||||
local sleepTime = 1.5
|
||||
local lastTransfer
|
||||
while true do
|
||||
local sleepTime = 1.5
|
||||
if lastTransfer and os.clock() - lastTransfer < 3 then
|
||||
sleepTime = .25
|
||||
end
|
||||
|
||||
os.sleep(socket and sleepTime or 5)
|
||||
if config.deposit then
|
||||
local neural = device.neuralInterface
|
||||
@@ -523,10 +532,8 @@ Event.addRoutine(function()
|
||||
slot = config.useShield and 'shield' or config.slot,
|
||||
count = item.count,
|
||||
}) then
|
||||
sleepTime = math.max(sleepTime - .25, .25)
|
||||
lastTransfer = os.clock()
|
||||
end
|
||||
else
|
||||
sleepTime = math.min(sleepTime + .25, 1.5)
|
||||
end
|
||||
end)
|
||||
if not s and m then
|
||||
|
||||
57
milo/apis/miniAdapter.lua
Normal file
57
milo/apis/miniAdapter.lua
Normal file
@@ -0,0 +1,57 @@
|
||||
local class = require('class')
|
||||
local itemDB = require('itemDB')
|
||||
local Util = require('util')
|
||||
|
||||
local device = _G.device
|
||||
|
||||
local Adapter = class()
|
||||
|
||||
function Adapter:init(args)
|
||||
if args.side then
|
||||
local inventory = device[args.side]
|
||||
if inventory then
|
||||
Util.merge(self, inventory)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Adapter:listItems(throttle)
|
||||
local cache = { }
|
||||
throttle = throttle or Util.throttle()
|
||||
|
||||
for k,v in pairs(self.list()) do
|
||||
if v.count > 0 then
|
||||
local key = table.concat({ v.name, v.damage, v.nbtHash }, ':')
|
||||
|
||||
local entry = cache[key]
|
||||
if not entry then
|
||||
local cached = itemDB:get(v)
|
||||
if cached then
|
||||
cached = Util.shallowCopy(cached)
|
||||
else
|
||||
cached = self.getItemMeta(k)
|
||||
if cached then
|
||||
cached = Util.shallowCopy(itemDB:add(cached))
|
||||
end
|
||||
end
|
||||
if cached then
|
||||
entry = cached
|
||||
entry.count = 0
|
||||
cache[key] = entry
|
||||
else
|
||||
_G._debug('Adapter: failed to get item details')
|
||||
end
|
||||
end
|
||||
|
||||
if entry then
|
||||
entry.count = entry.count + v.count
|
||||
end
|
||||
throttle()
|
||||
end
|
||||
end
|
||||
itemDB:flush()
|
||||
|
||||
self.cache = cache
|
||||
end
|
||||
|
||||
return Adapter
|
||||
@@ -1,6 +1,6 @@
|
||||
local Adapter = require('miniAdapter')
|
||||
local class = require('class')
|
||||
local Event = require('event')
|
||||
local Adapter = require('inventoryAdapter')
|
||||
local itemDB = require('itemDB')
|
||||
local Util = require('util')
|
||||
|
||||
@@ -61,7 +61,7 @@ function Storage:initStorage()
|
||||
if v.adapter then
|
||||
v.adapter.online = not not device[k]
|
||||
elseif device[k] and device[k].list and device[k].size and device[k].pullItems then
|
||||
v.adapter = Adapter.wrap({ side = k })
|
||||
v.adapter = Adapter({ side = k })
|
||||
v.adapter.online = true
|
||||
v.adapter.dirty = true
|
||||
elseif device[k] then
|
||||
|
||||
@@ -75,7 +75,7 @@ local function client(socket)
|
||||
if not data then
|
||||
break
|
||||
end
|
||||
--_G._debug(data)
|
||||
_G._debug(data)
|
||||
socket.co = coroutine.running()
|
||||
|
||||
if data.request == 'scan' then -- full scan of all inventories
|
||||
|
||||
Reference in New Issue
Block a user