[READ-ONLY] Mirror of https://github.com/thoda-dev/DU-Mining-Units-Monitoring. A mining units monitoring script jericho.dev
dual-universe dualuniverse lua mining mining-pool mining-units
0

Configure Feed

Select the types of activity you want to include in your feed.

v1.5.5 - fixed a render error when no ore is selected on a mining unit

Thomas (Jul 24, 2022, 4:36 PM +0200) ce08e77d 2e57e36e

+12 -9
+1 -1
config.json
··· 1 - {"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}}},"handlers":[{"code":"system.print(\"----------------------------------------\")\nsystem.print(\"DU-Mining-Units-Monitoring version 1.5.4\")\nsystem.print(\"----------------------------------------\")\n\nfontSize = 25 --export: font size for each line on the screen\ncalibrationSecondsRedLevel = 259200 --export: The time in seconds from last calibration above the time will be displayed in red. Default to 259200 (3 days / 72h)\ncalibrationSecondsYellowLevel = 86400 --export: The time in seconds from last calibration above the time will be displayed in yellow. Default to 86400 (1 day / 24h)\n\nlocal renderScript = [[\nlocal json = require('dkjson')\nlocal fromScript = json.decode(getInput()) or {}\npool = fromScript[1] or {}\ndata = fromScript[2] or {}\nimages = {}\nfor k,p in pairs(pool) do\n if images[k] == nil or not isImageLoaded(images[k]) then\n images[k] = loadImage(\"resources_generated/env/\" .. p[4])\n end\nend\n\nlocal rx,ry = getResolution()\n\nlocal back=createLayer()\nlocal front=createLayer()\n\nfont_size = 25\n\nlocal mini=loadFont('Play',12)\nlocal small=loadFont('Play',14)\nlocal smallBold=loadFont('Play-Bold',18)\nlocal itemName=loadFont('Play-Bold',font_size)\nlocal itemNameSmall=loadFont('Play-Bold',font_size*0.75)\nlocal itemNameXs=loadFont('Play-Bold',font_size*0.65)\n\nsetBackgroundColor( 15/255,24/255,29/255)\n\nsetDefaultStrokeColor( back,Shape_Line,0,0,0,0.5)\nsetDefaultShadow( back,Shape_Line,6,0,0,0,0.5)\n\nsetDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1)\nsetDefaultFillColor( front,Shape_Text,0,0,0,1)\nsetDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)\n\nfunction format_number(value)\n local formatted = value\n while true do\n formatted, k = string.gsub(formatted, \"^(-?%d+)(%d%d%d)\", '%1 %2')\n if (k==0) then\n break\n end\n end\n return formatted\nend\n\nfunction SecondsToClockString(a)local a=tonumber(a)if a==nil or a<=0 then return\"-\"else days=string.format(\"%2.f\",math.floor(a/(3600*24)))hours=string.format(\"%2.f\",math.floor(a/3600-days*24))mins=string.format(\"%2.f\",math.floor(a/60-hours*60-days*24*60))secs=string.format(\"%2.f\",math.floor(a-hours*3600-days*24*60*60-mins*60))str=\"\"if tonumber(days)>0 then str=str..days..\"d \"end;if tonumber(hours)>0 then str=str..hours..\"h \"end;if tonumber(mins)>0 then str=str..mins..\"m \"end;if tonumber(secs)>0 then str=str..secs..\"s\"end;return str end end\n\nfunction round(a,b)if b then return utils.round(a/b)*b end;return a>=0 and math.floor(a+0.5)or math.ceil(a-0.5)end\n\nfunction getRGBGradient(a,b,c,d,e,f,g)a=-1*math.cos(a*math.pi)/2+0.5;local h=b-a*(b-e)local i=c-a*(c-f)local j=d-a*(d-g)return h,i,j end\n\nfunction renderHeader(title)\n local h_factor = 12\n local h = 35\n addLine( back,0,h+12,rx,h+12)\n addBox(front,0,12,rx,h)\n addText(front,smallBold,title,44,35)\nend\n\nlocal storageBar = createLayer()\nsetDefaultFillColor(storageBar,Shape_Text,110/255,166/255,181/255,1)\nsetDefaultFillColor(storageBar,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor(storageBar,Shape_Line,1,1,1,1)\n\nlocal storageYellow = createLayer()\nsetDefaultFillColor(storageYellow,Shape_Text,249/255,212/255,123/255,1)\nsetDefaultFillColor(storageYellow,Shape_Box,249/255,212/255,123/255,1)\n\nlocal storageDark = createLayer()\nsetDefaultFillColor(storageDark,Shape_Text,63/255,92/255,102/255,1)\nsetDefaultFillColor(storageDark,Shape_Box,13/255,24/255,28/255,1)\n\nlocal storageRed = createLayer()\nsetDefaultFillColor(storageRed,Shape_Text,177/255,42/255,42/255,1)\nsetDefaultFillColor(storageRed,Shape_Box,177/255,42/255,42/255,1)\n\nlocal storageGreen = createLayer()\nsetDefaultFillColor(storageGreen,Shape_Text,34/255,177/255,76/255,1)\nsetDefaultFillColor(storageGreen,Shape_Box,34/255,177/255,76/255,1)\n\nlocal imagesLayer = createLayer()\n\nlocal gaugeColorLayer = createLayer()\n\nfunction renderResistanceBar(ore_id, status, time, prod_rate, calibration, optimal, efficiency, cal_time, x, y, w, h, withTitle)\n local quantity_x_pos = font_size * 6.7\n local percent_x_pos = font_size * 2\n\n local colorLayer = storageGreen\n local status_text = \"RUNNING\"\n if (status == 1) or (status == 3) or (status == 4) then\n colorLayer = storageRed\n if status == 3 then status_text = 'OUTPUT FULL'\n elseif status == 1 then status_text = 'STOPPED'\n elseif status == 4 then status_text = 'NO CONTAINER'\n end\n end\n\n local calibrationRequiredTime = ((calibration-optimal)/.625)*3600\n if 259200 - cal_time > 0 then calibrationRequiredTime = calibrationRequiredTime + 259200 - cal_time end\n if calibration < optimal then calibrationRequiredTime = 86400 - cal_time end\n\n local colorRequiredCalibrationLayer = storageBar\n if calibrationRequiredTime < 86400 then colorRequiredCalibrationLayer = storageYellow end\n if calibrationRequiredTime < 43200 then colorRequiredCalibrationLayer = storageRed end\n\n if calibrationRequiredTime < 0 then\n calibrationRequiredTime = \"REQUIRED\"\n else \n calibrationRequiredTime = SecondsToClockString(calibrationRequiredTime)\n end\n\n pcal = calibration\n if pcal > optimal then pcal = optimal end\n if optimal > 0 then\n pcal = pcal / optimal\n else\n pcal = 1\n end\n local r = 34/255\n local g = 177/255\n local b = 76/255\n if pcal < 1 then\n r,g,b = getRGBGradient(pcal,177/255,42/255,42/255,249/255,212/255,123/255)\n end\n\n local CalibrationTimeColorLayer = storageGreen\n if cal_time > 86400 then CalibrationTimeColorLayer = storageYellow end\n if cal_time > 259200 then CalibrationTimeColorLayer = storageRed end\n\n addBox(storageBar,x,y,w,h)\n\n\n if withTitle then\n addText(storageBar, small, \"ORE\", x, y-5)\n addText(storageBar, small, \"STATUS\", x+w-60, y-5)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, \"TIME & RATE\", x+(w*0.3), y-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, \"Calibration\", x+(w*0.45), y-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, \"CALIBRATION REQUIRED IN\", x+(w*0.65), y-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, \"EFFICIENCY\", x+(w*0.80), y-3)\n end\n \n addText(storageBar, itemName, pool[ore_id][3], x+15+font_size, y+h-font_size/2)\n setNextFillColor(gaugeColorLayer, r, g, b, 1)\n addBox(gaugeColorLayer,x,y+h-3,w*calibration/100,3)\n\n if isImageLoaded(images[ore_id]) then\n addImage(imagesLayer, images[ore_id], x+10, y+5, font_size, font_size)\n end\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n addText(storageBar, itemNameXs, SecondsToClockString(time), x+(w*0.3), y+(h/4)-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n if (prod_rate-round(prod_rate)) == 0 then prod_rate = round(prod_rate) end\n addText(storageBar, itemNameXs, format_number(prod_rate) .. 'L', x+(w*0.3), y+(h/4)+(h/4)+(h/4)-3)\n setNextTextAlign(CalibrationTimeColorLayer, AlignH_Center, AlignV_Middle)\n addText(CalibrationTimeColorLayer, itemNameXs, SecondsToClockString(cal_time), x+(w*0.45), y+(h/4)-3)\n setNextFillColor(gaugeColorLayer, r, g, b, 1)\n setNextTextAlign(gaugeColorLayer, AlignH_Center, AlignV_Middle)\n if (calibration-round(calibration)) == 0 then calibration = round(calibration) end\n if (optimal-round(optimal)) == 0 then optimal = round(optimal) end\n addText(gaugeColorLayer, itemNameXs, format_number(calibration) .. '% / ' ..format_number(optimal) .. '%', x+(w*0.45), y+(h/4)+(h/4)+(h/4)-3)\n setNextTextAlign(colorRequiredCalibrationLayer, AlignH_Center, AlignV_Middle)\n addText(colorRequiredCalibrationLayer, itemNameSmall, calibrationRequiredTime, x+(w*0.65), y+(h/2)-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n if (efficiency-round(efficiency)) == 0 then efficiency = round(efficiency) end\n addText(storageBar, itemNameSmall, format_number(efficiency) .. '%', x+(w*0.80), y+(h/2)-3)\n\n setNextTextAlign(colorLayer, AlignH_Right, AlignV_Middle)\n addText(colorLayer, itemName, status_text, x+w-10, y+(h/2)-3)\nend\n\nfunction renderPool(pool)\n local h_factor = 12\n local h = 35\n addLine( back,0,ry-h+h_factor,rx,ry-h+h_factor)\n addBox(front,0,ry-h-h_factor,rx,h)\n local nb_col = 1\n for k,v in pairs(pool) do nb_col = nb_col + 1 end\n local n = 1\n for k,v in pairs(pool) do\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, v[3], (rx/nb_col)*n, ry-h-h_factor)\n if isImageLoaded(images[k]) then\n addImage(storageBar, images[k], (rx/nb_col)*n-h-h, ry-(h/2)-h_factor-(h/2)+2, h-4, h-4)\n end\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n addText(storageBar, itemNameSmall, format_number(round(v[1])) .. \" / \" .. format_number(round(v[2])), (rx/nb_col)*n, ry-(h/2)-h_factor)\n n = n + 1\n end\nend\n\nrenderHeader('MINING UNITS MONITORING')\n\nstart_h = 75\n\nlocal h = font_size + font_size / 2\nfor i,mu in ipairs(data) do\n renderResistanceBar(tostring(mu[3]), mu[1], mu[2], mu[4], mu[5], mu[6], mu[7], mu[8], 44, start_h, rx-88, h, i==1)\n start_h = start_h+h+15\nend\nrenderPool(pool)\nrequestAnimationFrame(10)\n]]\n\nscreens = {}\nmining_units = {}\nfor slot_name, slot in pairs(unit) do\n if type(slot) == \"table\"\n and type(slot.export) == \"table\"\n and slot.getClass\n then\n slot.slotname = slot_name\n if slot.getClass():lower() == 'screenunit' then\n table.insert(screens,slot)\n slot.setRenderScript(renderScript)\n elseif slot.getClass():lower() == 'miningunit' then\n table.insert(mining_units,slot)\n end\n end\nend\nif #screens == 0 then\n system.print(\"No screen detected\")\nelse\n table.sort(screens, function(a,b) return a.slotname < b.slotname end)\n local plural = \"\"\n if #screens > 1 then plural = \"s\" end\n system.print(#screens .. \" screen\" .. plural .. \" connected\")\nend\nif #mining_units == 0 then\n system.print(\"No mining unit detected\")\nelse\n table.sort(mining_units, function(a,b) return a.slotname < b.slotname end)\n local plural = \"\"\n if #mining_units > 1 then plural = \"s\" end\n system.print(#mining_units .. \" mining unit\" .. plural .. \" connected\")\nend\n\nfunction round(a,b)if b then return utils.round(a/b)*b end;return a>=0 and math.floor(a+0.5)or math.ceil(a-0.5)end\n\nores = {}\n\n--Nested Coroutines by Jericho\ncoroutinesTable = {}\n--all functions here will become a coroutine\nMyCoroutines = {\n function()\n screen_data = {}\n pool = {}\n for index, mu in ipairs(mining_units) do\n local mup = mu.getOrePools()\n for _,p in ipairs(mup) do\n if ores[p.id] == nil then\n local item_data = system.getItem(p.id)\n ores[p.id] = {\n item_data.locDisplayName,\n item_data.iconPath:gsub(\"resources_generated/env/\",\"\")\n }\n end\n pool[p.id] = {p.available, p.maximum, ores[p.id][1], ores[p.id][2]}\n end\n local mu_data = {\n mu.getState(),\n round(mu.getRemainingTime()),\n mu.getActiveOre(),\n round(mu.getProductionRate()*100)/100,\n round(mu.getCalibrationRate()*10000)/100,\n round(mu.getOptimalRate()*10000)/100,\n round(mu.getEfficiency()*10000)/100,\n round(mu.getLastExtractionTime()),\n }\n table.insert(screen_data, mu_data)\n coroutine.yield(coroutinesTable[1])\n end\n for index, screen in pairs(screens) do\n local toSend = {\n pool,\n screen_data\n }\n screen.setScriptInput(json.encode(toSend))\n end\n end\n}\n\nfunction initCoroutines()\n for _,f in pairs(MyCoroutines) do\n local co = coroutine.create(f)\n table.insert(coroutinesTable, co)\n end\nend\n\ninitCoroutines()\n\nrunCoroutines = function()\n for i,co in ipairs(coroutinesTable) do\n if coroutine.status(co) == \"dead\" then\n coroutinesTable[i] = coroutine.create(MyCoroutines[i])\n end\n if coroutine.status(co) == \"suspended\" then\n assert(coroutine.resume(co))\n end\n end\nend\n\nMainCoroutine = coroutine.create(runCoroutines)\n","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"0"},{"code":"if coroutine.status(MainCoroutine) == \"dead\" then\n MainCoroutine = coroutine.create(runCoroutines)\nend\nif coroutine.status(MainCoroutine) == \"suspended\" then\n assert(coroutine.resume(MainCoroutine))\nend","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"1"}],"methods":[],"events":[]} 1 + {"events":[],"handlers":[{"code":"if coroutine.status(MainCoroutine) == \"dead\" then\n MainCoroutine = coroutine.create(runCoroutines)\nend\nif coroutine.status(MainCoroutine) == \"suspended\" then\n assert(coroutine.resume(MainCoroutine))\nend","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"1"},{"code":"local version = '1.5.5'\n\nsystem.print(\"----------------------------------------\")\nsystem.print(\"DU-Mining-Units-Monitoring version \" .. version)\nsystem.print(\"----------------------------------------\")\n\nfontSize = 25 --export: font size for each line on the screen\ncalibrationSecondsRedLevel = 259200 --export: The time in seconds from last calibration above the time will be displayed in red. Default to 259200 (3 days / 72h)\ncalibrationSecondsYellowLevel = 86400 --export: The time in seconds from last calibration above the time will be displayed in yellow. Default to 86400 (1 day / 24h)\n\nlocal renderScript = [[\nlocal json = require('dkjson')\nlocal fromScript = json.decode(getInput()) or {}\npool = fromScript[1] or {}\ndata = fromScript[2] or {}\nimages = {}\nfor k,p in pairs(pool) do\n if images[k] == nil or not isImageLoaded(images[k]) then\n images[k] = loadImage(\"resources_generated/env/\" .. p[4])\n end\nend\n\nlocal rx,ry = getResolution()\n\nlocal back=createLayer()\nlocal front=createLayer()\n\nfont_size = 25\n\nlocal mini=loadFont('Play',12)\nlocal small=loadFont('Play',14)\nlocal smallBold=loadFont('Play-Bold',18)\nlocal itemName=loadFont('Play-Bold',font_size)\nlocal itemNameSmall=loadFont('Play-Bold',font_size*0.75)\nlocal itemNameXs=loadFont('Play-Bold',font_size*0.65)\n\nsetBackgroundColor( 15/255,24/255,29/255)\n\nsetDefaultStrokeColor( back,Shape_Line,0,0,0,0.5)\nsetDefaultShadow( back,Shape_Line,6,0,0,0,0.5)\n\nsetDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1)\nsetDefaultFillColor( front,Shape_Text,0,0,0,1)\nsetDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)\n\nfunction format_number(value)\n local formatted = value\n while true do\n formatted, k = string.gsub(formatted, \"^(-?%d+)(%d%d%d)\", '%1 %2')\n if (k==0) then\n break\n end\n end\n return formatted\nend\n\nfunction SecondsToClockString(a)local a=tonumber(a)if a==nil or a<=0 then return\"-\"else days=string.format(\"%2.f\",math.floor(a/(3600*24)))hours=string.format(\"%2.f\",math.floor(a/3600-days*24))mins=string.format(\"%2.f\",math.floor(a/60-hours*60-days*24*60))secs=string.format(\"%2.f\",math.floor(a-hours*3600-days*24*60*60-mins*60))str=\"\"if tonumber(days)>0 then str=str..days..\"d \"end;if tonumber(hours)>0 then str=str..hours..\"h \"end;if tonumber(mins)>0 then str=str..mins..\"m \"end;if tonumber(secs)>0 then str=str..secs..\"s\"end;return str end end\n\nfunction round(a,b)if b then return utils.round(a/b)*b end;return a>=0 and math.floor(a+0.5)or math.ceil(a-0.5)end\n\nfunction getRGBGradient(a,b,c,d,e,f,g)a=-1*math.cos(a*math.pi)/2+0.5;local h=b-a*(b-e)local i=c-a*(c-f)local j=d-a*(d-g)return h,i,j end\n\nfunction renderHeader(title)\n local h_factor = 12\n local h = 35\n addLine( back,0,h+12,rx,h+12)\n addBox(front,0,12,rx,h)\n addText(front,smallBold,title,44,35)\nend\n\nlocal storageBar = createLayer()\nsetDefaultFillColor(storageBar,Shape_Text,110/255,166/255,181/255,1)\nsetDefaultFillColor(storageBar,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor(storageBar,Shape_Line,1,1,1,1)\n\nlocal storageYellow = createLayer()\nsetDefaultFillColor(storageYellow,Shape_Text,249/255,212/255,123/255,1)\nsetDefaultFillColor(storageYellow,Shape_Box,249/255,212/255,123/255,1)\n\nlocal storageDark = createLayer()\nsetDefaultFillColor(storageDark,Shape_Text,63/255,92/255,102/255,1)\nsetDefaultFillColor(storageDark,Shape_Box,13/255,24/255,28/255,1)\n\nlocal storageRed = createLayer()\nsetDefaultFillColor(storageRed,Shape_Text,177/255,42/255,42/255,1)\nsetDefaultFillColor(storageRed,Shape_Box,177/255,42/255,42/255,1)\n\nlocal storageGreen = createLayer()\nsetDefaultFillColor(storageGreen,Shape_Text,34/255,177/255,76/255,1)\nsetDefaultFillColor(storageGreen,Shape_Box,34/255,177/255,76/255,1)\n\nlocal imagesLayer = createLayer()\n\nlocal gaugeColorLayer = createLayer()\n\nfunction renderResistanceBar(ore_id, status, time, prod_rate, calibration, optimal, efficiency, cal_time, x, y, w, h, withTitle)\n local quantity_x_pos = font_size * 6.7\n local percent_x_pos = font_size * 2\n\n local colorLayer = storageGreen\n local status_text = \"RUNNING\"\n if (status == 1) or (status == 3) or (status == 4) then\n colorLayer = storageRed\n if status == 3 then status_text = 'OUTPUT FULL'\n elseif status == 1 then status_text = 'STOPPED'\n elseif status == 4 then status_text = 'NO CONTAINER'\n end\n end\n\n local calibrationRequiredTime = ((calibration-optimal)/.625)*3600\n if 259200 - cal_time > 0 then calibrationRequiredTime = calibrationRequiredTime + 259200 - cal_time end\n if calibration < optimal then calibrationRequiredTime = 86400 - cal_time end\n\n local colorRequiredCalibrationLayer = storageBar\n if calibrationRequiredTime < 86400 then colorRequiredCalibrationLayer = storageYellow end\n if calibrationRequiredTime < 43200 then colorRequiredCalibrationLayer = storageRed end\n\n if calibrationRequiredTime < 0 then\n calibrationRequiredTime = \"REQUIRED\"\n else \n calibrationRequiredTime = SecondsToClockString(calibrationRequiredTime)\n end\n\n pcal = calibration\n if pcal > optimal then pcal = optimal end\n if optimal > 0 then\n pcal = pcal / optimal\n else\n pcal = 1\n end\n local r = 34/255\n local g = 177/255\n local b = 76/255\n if pcal < 1 then\n r,g,b = getRGBGradient(pcal,177/255,42/255,42/255,249/255,212/255,123/255)\n end\n\n local CalibrationTimeColorLayer = storageGreen\n if cal_time > 86400 then CalibrationTimeColorLayer = storageYellow end\n if cal_time > 259200 then CalibrationTimeColorLayer = storageRed end\n\n addBox(storageBar,x,y,w,h)\n\n\n if withTitle then\n addText(storageBar, small, \"ORE\", x, y-5)\n addText(storageBar, small, \"STATUS\", x+w-60, y-5)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, \"TIME & RATE\", x+(w*0.3), y-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, \"Calibration\", x+(w*0.45), y-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, \"CALIBRATION REQUIRED IN\", x+(w*0.65), y-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, \"EFFICIENCY\", x+(w*0.80), y-3)\n end\n local ore_name = 'No Ore Selected'\n if tonumber(ore_id) > 0 then ore_name = pool[ore_id][3] end\n addText(storageBar, itemName, ore_name, x+15+font_size, y+h-font_size/2)\n setNextFillColor(gaugeColorLayer, r, g, b, 1)\n addBox(gaugeColorLayer,x,y+h-3,w*calibration/100,3)\n\n if tonumber(ore_id) > 0 and isImageLoaded(images[ore_id]) then\n addImage(imagesLayer, images[ore_id], x+10, y+5, font_size, font_size)\n end\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n addText(storageBar, itemNameXs, SecondsToClockString(time), x+(w*0.3), y+(h/4)-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n if (prod_rate-round(prod_rate)) == 0 then prod_rate = round(prod_rate) end\n addText(storageBar, itemNameXs, format_number(prod_rate) .. 'L', x+(w*0.3), y+(h/4)+(h/4)+(h/4)-3)\n setNextTextAlign(CalibrationTimeColorLayer, AlignH_Center, AlignV_Middle)\n addText(CalibrationTimeColorLayer, itemNameXs, SecondsToClockString(cal_time), x+(w*0.45), y+(h/4)-3)\n setNextFillColor(gaugeColorLayer, r, g, b, 1)\n setNextTextAlign(gaugeColorLayer, AlignH_Center, AlignV_Middle)\n if (calibration-round(calibration)) == 0 then calibration = round(calibration) end\n if (optimal-round(optimal)) == 0 then optimal = round(optimal) end\n addText(gaugeColorLayer, itemNameXs, format_number(calibration) .. '% / ' ..format_number(optimal) .. '%', x+(w*0.45), y+(h/4)+(h/4)+(h/4)-3)\n setNextTextAlign(colorRequiredCalibrationLayer, AlignH_Center, AlignV_Middle)\n addText(colorRequiredCalibrationLayer, itemNameSmall, calibrationRequiredTime, x+(w*0.65), y+(h/2)-3)\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n if (efficiency-round(efficiency)) == 0 then efficiency = round(efficiency) end\n addText(storageBar, itemNameSmall, format_number(efficiency) .. '%', x+(w*0.80), y+(h/2)-3)\n\n setNextTextAlign(colorLayer, AlignH_Right, AlignV_Middle)\n addText(colorLayer, itemName, status_text, x+w-10, y+(h/2)-3)\nend\n\nfunction renderPool(pool)\n local h_factor = 12\n local h = 35\n addLine( back,0,ry-h+h_factor,rx,ry-h+h_factor)\n addBox(front,0,ry-h-h_factor,rx,h)\n local nb_col = 1\n for k,v in pairs(pool) do nb_col = nb_col + 1 end\n local n = 1\n for k,v in pairs(pool) do\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom)\n addText(storageBar, small, v[3], (rx/nb_col)*n, ry-h-h_factor)\n if isImageLoaded(images[k]) then\n addImage(storageBar, images[k], (rx/nb_col)*n-h-h, ry-(h/2)-h_factor-(h/2)+2, h-4, h-4)\n end\n setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle)\n addText(storageBar, itemNameSmall, format_number(round(v[1])) .. \" / \" .. format_number(round(v[2])), (rx/nb_col)*n+5, ry-(h/2)-h_factor)\n n = n + 1\n end\nend\n\nrenderHeader('MINING UNITS MONITORING v]] .. version .. [[')\n\nstart_h = 75\n\nlocal h = font_size + font_size / 2\nfor i,mu in ipairs(data) do\n renderResistanceBar(tostring(mu[3]), mu[1], mu[2], mu[4], mu[5], mu[6], mu[7], mu[8], 44, start_h, rx-88, h, i==1)\n start_h = start_h+h+15\nend\nrenderPool(pool)\nrequestAnimationFrame(10)\n]]\n\nscreens = {}\nmining_units = {}\nfor slot_name, slot in pairs(unit) do\n if type(slot) == \"table\"\n and type(slot.export) == \"table\"\n and slot.getClass\n then\n slot.slotname = slot_name\n if slot.getClass():lower() == 'screenunit' then\n table.insert(screens,slot)\n slot.setRenderScript(renderScript)\n elseif slot.getClass():lower() == 'miningunit' then\n table.insert(mining_units,slot)\n end\n end\nend\nif #screens == 0 then\n system.print(\"No screen detected\")\nelse\n table.sort(screens, function(a,b) return a.slotname < b.slotname end)\n local plural = \"\"\n if #screens > 1 then plural = \"s\" end\n system.print(#screens .. \" screen\" .. plural .. \" connected\")\nend\nif #mining_units == 0 then\n system.print(\"No mining unit detected\")\nelse\n table.sort(mining_units, function(a,b) return a.slotname < b.slotname end)\n local plural = \"\"\n if #mining_units > 1 then plural = \"s\" end\n system.print(#mining_units .. \" mining unit\" .. plural .. \" connected\")\nend\n\nfunction round(a,b)if b then return utils.round(a/b)*b end;return a>=0 and math.floor(a+0.5)or math.ceil(a-0.5)end\n\nores = {}\n\n--Nested Coroutines by Jericho\ncoroutinesTable = {}\n--all functions here will become a coroutine\nMyCoroutines = {\n function()\n screen_data = {}\n pool = {}\n for index, mu in ipairs(mining_units) do\n local mup = mu.getOrePools()\n for _,p in ipairs(mup) do\n if ores[p.id] == nil then\n local item_data = system.getItem(p.id)\n ores[p.id] = {\n item_data.locDisplayName,\n item_data.iconPath:gsub(\"resources_generated/env/\",\"\")\n }\n end\n pool[p.id] = {p.available, p.maximum, ores[p.id][1], ores[p.id][2]}\n end\n local mu_data = {\n mu.getState(),\n round(mu.getRemainingTime()),\n mu.getActiveOre(),\n round(mu.getProductionRate()*100)/100,\n round(mu.getCalibrationRate()*10000)/100,\n round(mu.getOptimalRate()*10000)/100,\n round(mu.getEfficiency()*10000)/100,\n round(mu.getLastExtractionTime()),\n }\n table.insert(screen_data, mu_data)\n coroutine.yield(coroutinesTable[1])\n end\n for index, screen in pairs(screens) do\n local toSend = {\n pool,\n screen_data\n }\n screen.setScriptInput(json.encode(toSend))\n end\n end\n}\n\nfunction initCoroutines()\n for _,f in pairs(MyCoroutines) do\n local co = coroutine.create(f)\n table.insert(coroutinesTable, co)\n end\nend\n\ninitCoroutines()\n\nrunCoroutines = function()\n for i,co in ipairs(coroutinesTable) do\n if coroutine.status(co) == \"dead\" then\n coroutinesTable[i] = coroutine.create(MyCoroutines[i])\n end\n if coroutine.status(co) == \"suspended\" then\n assert(coroutine.resume(co))\n end\n end\nend\n\nMainCoroutine = coroutine.create(runCoroutines)\n","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"0"}],"methods":[],"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}}}}
+11 -8
source/unit/onStart.lua
··· 1 + local version = '1.5.5' 2 + 1 3 system.print("----------------------------------------") 2 - system.print("DU-Mining-Units-Monitoring version 1.5.4") 4 + system.print("DU-Mining-Units-Monitoring version " .. version) 3 5 system.print("----------------------------------------") 4 6 5 7 fontSize = 25 --export: font size for each line on the screen ··· 153 155 setNextTextAlign(storageBar, AlignH_Center, AlignV_Bottom) 154 156 addText(storageBar, small, "EFFICIENCY", x+(w*0.80), y-3) 155 157 end 156 - 157 - addText(storageBar, itemName, pool[ore_id][3], x+15+font_size, y+h-font_size/2) 158 + local ore_name = 'No Ore Selected' 159 + if tonumber(ore_id) > 0 then ore_name = pool[ore_id][3] end 160 + addText(storageBar, itemName, ore_name, x+15+font_size, y+h-font_size/2) 158 161 setNextFillColor(gaugeColorLayer, r, g, b, 1) 159 162 addBox(gaugeColorLayer,x,y+h-3,w*calibration/100,3) 160 163 161 - if isImageLoaded(images[ore_id]) then 164 + if tonumber(ore_id) > 0 and isImageLoaded(images[ore_id]) then 162 165 addImage(imagesLayer, images[ore_id], x+10, y+5, font_size, font_size) 163 166 end 164 167 setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle) ··· 198 201 addImage(storageBar, images[k], (rx/nb_col)*n-h-h, ry-(h/2)-h_factor-(h/2)+2, h-4, h-4) 199 202 end 200 203 setNextTextAlign(storageBar, AlignH_Center, AlignV_Middle) 201 - addText(storageBar, itemNameSmall, format_number(round(v[1])) .. " / " .. format_number(round(v[2])), (rx/nb_col)*n, ry-(h/2)-h_factor) 204 + addText(storageBar, itemNameSmall, format_number(round(v[1])) .. " / " .. format_number(round(v[2])), (rx/nb_col)*n+5, ry-(h/2)-h_factor) 202 205 n = n + 1 203 206 end 204 207 end 205 208 206 - renderHeader('MINING UNITS MONITORING') 209 + renderHeader('MINING UNITS MONITORING v]] .. version .. [[') 207 210 208 211 start_h = 75 209 212 ··· 220 223 mining_units = {} 221 224 for slot_name, slot in pairs(unit) do 222 225 if type(slot) == "table" 223 - and type(slot.export) == "table" 224 - and slot.getClass 226 + and type(slot.export) == "table" 227 + and slot.getClass 225 228 then 226 229 slot.slotname = slot_name 227 230 if slot.getClass():lower() == 'screenunit' then