milo cleanup

This commit is contained in:
kepler155c
2018-11-11 01:04:24 -05:00
parent bc9028f0c7
commit a00a66f4e6
6 changed files with 136 additions and 158 deletions

View File

@@ -1,6 +1,7 @@
local class = require('class')
local Event = require('event')
local InventoryAdapter = require('inventoryAdapter')
local itemDB = require('itemDB')
local Peripheral = require('peripheral')
local Util = require('util')
@@ -13,11 +14,8 @@ function Storage:init(args)
local defaults = {
remoteDefaults = { },
dirty = true,
listCount = 0,
activity = { },
storageOnline = true,
hits = 0,
misses = 0,
lastRefresh = os.clock(),
}
Util.merge(self, defaults)
@@ -27,12 +25,11 @@ listCount = 0,
self.localName = modem.getNameLocal()
Event.on({ 'device_attach', 'device_detach' }, function(e, dev)
_debug('%s: %s', e, tostring(dev))
_G._debug('%s: %s', e, tostring(dev))
self:initStorage()
end)
Event.onInterval(15, function()
self:showStorage()
_debug('STORAGE: cache: %d/%d', self.hits, self.misses)
end)
end
@@ -45,19 +42,11 @@ function Storage:showStorage()
end
end
if #t > 0 then
_debug('Adapter:')
_G._debug('Adapter:')
for _, k in pairs(t) do
_debug(' offline: ' .. k)
_G._debug(' offline: ' .. k)
end
_debug('')
end
end
function Storage:setOnline(online)
if online ~= self.storageOnline then
self.storageOnline = online
os.queueEvent(self.storageOnline and 'storage_online' or 'storage_offline', online)
_debug('Storage: %s', self.storageOnline and 'online' or 'offline')
_G._debug('')
end
end
@@ -68,7 +57,7 @@ end
function Storage:initStorage()
local online = true
_debug('Initializing storage')
_G._debug('Initializing storage')
for k,v in pairs(self.remoteDefaults) do
if v.adapter then
v.adapter.online = not not device[k]
@@ -82,7 +71,11 @@ function Storage:initStorage()
end
end
self:setOnline(online)
if online ~= self.storageOnline then
self.storageOnline = online
os.queueEvent(self.storageOnline and 'storage_online' or 'storage_offline', online)
_G._debug('Storage: %s', self.storageOnline and 'online' or 'offline')
end
end
function Storage:filterActive(mtype, filter)
@@ -138,29 +131,33 @@ end
function Storage:refresh(throttle)
self.dirty = true
self.lastRefresh = os.clock()
_debug('STORAGE: Forcing full refresh')
_G._debug('STORAGE: Forcing full refresh')
for _, adapter in self:onlineAdapters() do
adapter.dirty = true
end
return self:listItems(throttle)
end
local function Timer()
local ct = os.clock()
return function()
return os.clock() - ct
end
end
-- provide a consolidated list of items
function Storage:listItems(throttle)
if not self.dirty then
return self.items
return self.cache
end
self.listCount = self.listCount + 1
--_debug(self.listCount)
local ct = os.clock()
-- TODO: is there any reason now to maintain 2 lists
local cache = { }
local items = { }
throttle = throttle or Util.throttle()
local timer = Timer()
for _, adapter in self:onlineAdapters() do
if adapter.dirty then
--_debug('STORAGE: refresh: ' .. adapter.name)
adapter:listItems(throttle)
adapter.dirty = false
end
@@ -172,8 +169,6 @@ local ct = os.clock()
entry.count = v.count
entry.key = key
cache[key] = entry
items[key] = entry
-- table.insert(items, entry)
else
entry.count = entry.count + v.count
end
@@ -181,76 +176,80 @@ local ct = os.clock()
throttle()
end
end
_debug('STORAGE: refresh in ' .. (os.clock() - ct))
_G._debug('STORAGE: refresh in ' .. timer())
self.dirty = false
self.cache = cache
self.items = items
return items
return cache
end
function Storage:updateCache(adapter, key, count)
local entry = adapter.cache[key]
if not entry then
if count < 0 then
adapter.dirty = true
self.dirty = true
else
entry = Util.shallowCopy(itemDB:get(key))
entry.count = count
entry.key = key
adapter.cache[key] = entry
end
else
entry.count = entry.count + count
if entry.count <= 0 then
adapter.cache[key] = nil
end
end
end
function Storage:export(target, slot, count, item)
return self:provide(item, count, slot, target)
end
function Storage:provide(item, qty, slot, direction)
local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
for _, adapter in self:onlineAdapters() do
if adapter.cache and adapter.cache[key] then
local amount = adapter:provide(item, qty, slot, direction or self.localName)
if amount > 0 then
self.hits = self.hits + 1
local function provide(adapter)
local amount = adapter:provide(item, count, slot, target or self.localName)
if amount > 0 then
-- _debug('EXT: %s(%d): %s -> %s%s',
-- item.name, amount, adapter.name, direction or self.localName,
-- slot and string.format('[%d]', slot) or '')
self.dirty = true
adapter.dirty = true
end
qty = qty - amount
total = total + amount
if qty <= 0 then
self:updateCache(adapter, key, -amount)
self:updateCache(self, key, -amount)
end
count = count - amount
total = total + amount
end
-- request from adapters with this item
for _, adapter in self:onlineAdapters() do
if adapter.cache and adapter.cache[key] then
provide(adapter)
if count <= 0 then
return total
end
end
end
_debug('STORAGE: MISS: %s - %d', key, qty)
self.misses = self.misses + 1
_G._debug('STORAGE: MISS: %s - %d', key, count)
-- not found - scan all others
for _, adapter in self:onlineAdapters() do
local amount = adapter:provide(item, qty, slot, direction or self.localName)
if amount > 0 then
--_debug('EXT: %s(%d): %s -> %s%s',
-- item.name, amount, adapter.name, direction or self.localName,
-- slot and string.format('[%d]', slot) or '')
self.dirty = true
adapter.dirty = true
end
qty = qty - amount
total = total + amount
if qty <= 0 then
break
if not adapter.cache or not adapter.cache[key] then
provide(adapter)
if count <= 0 then
_G._debug('STORAGE: FOUND: %s - %d', key, count)
break
end
end
end
return total
end
function Storage:trash(source, slot, count)
local trashcan = Util.find(self.remoteDefaults, 'mtype', 'trashcan')
if trashcan and trashcan.adapter and trashcan.adapter.online then
--_debug('TRA: %s[%d] (%d)', source or self.localName, slot, count or 64)
-- return trashcan.adapter.pullItems(source or self.localName, slot, count)
end
return 0
end
function Storage:import(source, slot, count, item)
local total = 0
local key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
if not self.cache then
self:listItems()
@@ -259,21 +258,16 @@ function Storage:import(source, slot, count, item)
local function insert(adapter)
local amount = adapter:insert(slot, count, nil, source or self.localName)
if amount > 0 then
_G._debug('INS: %s(%d): %s[%d] -> %s',
item.name, amount,
source or self.localName, slot, adapter.name)
self.dirty = true
adapter.dirty = true
local entry = self.activity[key] or 0
self.activity[key] = entry + amount
--[[
local cached = adapter.cache[key]
if cached then
cached.count = cached.count + amount
else
end
]]
self:updateCache(adapter, key, amount)
self:updateCache(self, key, amount)
-- record that we have imported this item into storage during this cycle
self.activity[key] = (self.activity[key] or 0) + amount
end
count = count - amount
total = total + amount
@@ -281,8 +275,7 @@ _G._debug('INS: %s(%d): %s[%d] -> %s',
-- find a chest locked with this item
for remote in self:onlineAdapters() do
-- TODO: proper checking using ignore dmg/nbt
if remote.lock == key or remote.lock == item.name then
if remote.lock == key then
insert(remote.adapter)
if count > 0 then -- TODO: only if void flag set
total = total + self:trash(source, slot, count)
@@ -291,10 +284,11 @@ _G._debug('INS: %s(%d): %s[%d] -> %s',
end
end
if self.cache[key] then -- is this item in some chest
-- is this item in some chest
if self.cache[key] then
for _, adapter in self:onlineAdapters() do
if count <= 0 then
break
return total
end
if adapter.cache and adapter.cache[key] and not adapter.lock then
insert(adapter)
@@ -315,4 +309,16 @@ _G._debug('INS: %s(%d): %s[%d] -> %s',
return total
end
-- When importing items into a locked chest, trash any remaining items if full
function Storage:trash(source, slot, count)
local trashcan = Util.find(self.remoteDefaults, 'mtype', 'trashcan')
if trashcan and trashcan.adapter and trashcan.adapter.online then
_G._debug('TRA: %s[%d] (%d)', source or self.localName, slot, count or 64)
return trashcan.adapter.pullItems(source or self.localName, slot, count)
end
return 0
end
return Storage