Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- サンプリング周期
- samplingPeriod = 10
- -- samplingPeriod毎に期間を区切った、通ったアイテムの数を格納したDictのリスト
- samples = {}
- function table.unique (tbl)
- local check = {}
- local res = {}
- -- 整数型だけユニーク化
- for i, v in ipairs(tbl) do
- if not(check[v]) then
- check[v] = true
- res[1+#res] = v
- end
- end
- -- キータイプはそのまま残す
- for k, v in pairs (tbl) do
- -- 整数以外
- if not (type(k)=="number" and k%1==0) then
- res[k] = v
- end
- end
- return res
- end
- function array.tostring(ary):
- result = "["
- for elem in ary:
- result = result + elem
- end
- return string.sub(result, 0, string.len(result) - 1) + "]"
- function sleepConsideringDelay(func, delaySecond)
- local startTime = os.clock()
- func()
- local endTime = os.clock()
- local delta = endTime - startTime
- sleep(delaySecond - delta)
- end
- function sampling()
- currentSample = {}
- for i=1, 16 do
- itemDetail = turtle.getItemDetail(i)
- if itemDetail then
- currentItemNum = currentSample[itemDetail['name']]
- if currentItemNum == nil then
- currentItemNum = 0
- end
- currentSample[itemDetail['name']] = currentItemNum + itemDetail['count']
- end
- turtle.select(i)
- turtle.dropDown()
- end
- table.insert(samples, currentSample)
- end
- function aggregate(samples)
- aggregatedItems = {}
- -- アイテム一覧をまずは出す
- items = {}
- for sample in samples do
- for k, v in pairs(sample) do
- table.insert(items, k)
- end
- end
- items = table.unique(items)
- -- アイテム毎に数を出す
- -- Key: ItemName
- -- Value: Array[Int]
- aggregatedItem = {}
- for item in items do
- for sample in samples:
- itemNum = sample[item]
- if not itemNum == nil then
- table.insert(aggregatedItem, itemNum)
- else
- table.insert(aggregatedItem, 0)
- end
- end
- table.insert(aggregatedItems, aggregatedItem)
- end
- for k,v in pairs(aggregatedItems) do
- print(string.format("%s:\n%s", k, array.tostring(v)))
- end
- end
- function startSampling()
- while true do
- sleepConsideringDelay(sampling, samplingPeriod)
- aggregatedItems = aggregate()
- end
- end
- startSampling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement