Merge pull request #12 from gordominossi/feature/integration-machines

Feature/integration machines
This commit is contained in:
gordominossi 2021-01-18 17:39:21 -03:00 committed by GitHub
commit 4139d3b215
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 167 additions and 81 deletions

View File

@ -1,16 +1,16 @@
local Parser = {}
local parser = {}
function Parser.parseProgress(progressString)
function parser.parseProgress(progressString)
local current = string.sub(progressString, string.find(progressString, "%ba§"))
current = tonumber((string.gsub(string.gsub(current, "a", ""), "§", "")))
local maximum = string.sub(progressString, string.find(progressString, "%be§"))
local maximum = string.sub(progressString, string.find(progressString, "%be§", (string.find(progressString, "/"))))
maximum = tonumber((string.gsub(string.gsub(maximum, "e", ""), "§", "")))
return {current = current, maximum = maximum}
end
function Parser.parseStoredEnergy(storedEnergyString)
function parser.parseStoredEnergy(storedEnergyString)
local noCommaString = string.gsub(storedEnergyString, ",", "")
local current = string.sub(noCommaString, string.find(noCommaString, "%ba§"))
@ -22,40 +22,40 @@ function Parser.parseStoredEnergy(storedEnergyString)
return {current = current, maximum = maximum}
end
function Parser.parseAverageInput(averageInputString)
function parser.parseAverageInput(averageInputString)
local noCommaString = string.gsub(averageInputString, ",", "")
return tonumber((string.sub(noCommaString, string.find(noCommaString, "%d+"))))
end
function Parser.parseAverageOutput(averageOutputString)
function parser.parseAverageOutput(averageOutputString)
local noCommaString = string.gsub(averageOutputString, ",", "")
return tonumber((string.sub(noCommaString, string.find(noCommaString, "%d+"))))
end
function Parser.parseProblems(problemsString)
function parser.parseProblems(problemsString)
local problems = string.sub(problemsString, string.find(problemsString, "c%d"))
return tonumber((string.gsub(problems, "c", "")))
end
function Parser.parseEfficiency(efficiencyString)
function parser.parseEfficiency(efficiencyString)
local noParagraphMarkString = string.gsub(efficiencyString, "§r", "")
local efficiency = string.sub(noParagraphMarkString, string.find(noParagraphMarkString, "%d+%.*%d*%s%%"))
return tonumber((string.gsub(efficiency, "%s%%", "")))
end
function Parser.parseName(nameString)
function parser.parseName(nameString)
return string.gsub(string.gsub(nameString, "§9", ""), "§r", "")
end
function Parser.parseWorkArea(worAreaString)
function parser.parseWorkArea(worAreaString)
local size = string.sub(worAreaString, string.find(worAreaString, "§a%d+x%d+§r"))
return string.gsub(string.gsub(size, "§a", ""), "§r", "")
end
function Parser.parseProbablyUses(probablyUsesString)
function parser.parseProbablyUses(probablyUsesString)
local noCommaString = string.gsub(probablyUsesString, ",", "")
local estimate = string.sub(noCommaString, string.find(noCommaString, "%bc§"))
return tonumber((string.gsub(string.gsub(estimate, "c", ""), "§", "")))
end
return Parser
return parser

View File

@ -95,15 +95,7 @@ local function drawRebootButton()
Widget.drawBaseWidget(x, y, width, height, "Restart")
end
local function clickNavigationButton(self)
if not self.active then
return
end
if self.title == "" then
elements.machineWidgets.active.index = elements.machineWidgets.active.index - 1
else
elements.machineWidgets.active.index = elements.machineWidgets.active.index + 1
end
local function refreshActiveMachineWidgets()
for i = 1, 9 do
elements.machineWidgets.active[i] = elements.machineWidgets[9 * (elements.machineWidgets.active.index - 1) + i]
end
@ -117,12 +109,29 @@ local function clickNavigationButton(self)
elements[14] = elements.machineWidgets.active[7]
elements[15] = elements.machineWidgets.active[8]
elements[16] = elements.machineWidgets.active[9]
Widget.clear()
end
local function clickNavigationButton(self)
if not self.active then
return
end
if self.title == "" then
elements.machineWidgets.active.index = elements.machineWidgets.active.index - 1
else
elements.machineWidgets.active.index = elements.machineWidgets.active.index + 1
end
refreshActiveMachineWidgets()
end
function page.create(element)
drawTitle(element.title)
elements.machineWidgets.active = {}
elements.machineWidgets.active.index = 1
refreshActiveMachineWidgets()
local panelIndex = 1
for _, pg in ipairs(pages) do
if pg ~= element then
@ -136,12 +145,11 @@ function page.create(element)
panelIndex = panelIndex + 1
end
end
elements.machineWidgets.active = {}
elements.machineWidgets.active.index = 1
for i = 1, 9 do
elements.machineWidgets.active[i] = elements.machineWidgets[9 * (elements.machineWidgets.active.index - 1) + i]
end
elements[1] = elements.panelSections[1]
elements[5] = elements.panelSections[2]
elements[9] = elements.panelSections[3]
elements[13] = elements.panelSections[4]
elements[17] = elements.panelSections[5]
elements.navigationButtons[1] = {
title = "",
@ -161,6 +169,8 @@ function page.create(element)
onClick = clickNavigationButton,
draw = drawNavigationButton
}
elements[20] = elements.navigationButtons[1]
elements[20.5] = elements.navigationButtons[2]
elements.rebootButton = {
onClick = function()
@ -168,30 +178,10 @@ function page.create(element)
end
}
drawRebootButton()
elements[4.5] = elements.rebootButton
elements[6] = elements.machineWidgets.active[1]
elements[7] = elements.machineWidgets.active[2]
elements[8] = elements.machineWidgets.active[3]
elements[10] = elements.machineWidgets.active[4]
elements[11] = elements.machineWidgets.active[5]
elements[12] = elements.machineWidgets.active[6]
elements[14] = elements.machineWidgets.active[7]
elements[15] = elements.machineWidgets.active[8]
elements[16] = elements.machineWidgets.active[9]
elements[18] = elements.powerWidget
elements[19] = elements.powerWidget
elements[1] = elements.panelSections[1]
elements[5] = elements.panelSections[2]
elements[9] = elements.panelSections[3]
elements[13] = elements.panelSections[4]
elements[17] = elements.panelSections[5]
elements[20] = elements.navigationButtons[1]
elements[20.5] = elements.navigationButtons[2]
end
function page.fake()
@ -200,8 +190,10 @@ function page.fake()
page.create(pages.overview)
end
function page.setup(energyBufferAddress)
elements.machineWidgets = Widget.fakeWidgets()
function page.setup(energyBufferAddress, multiblockAddresses)
for name, address in pairs(multiblockAddresses) do
table.insert(elements.machineWidgets, Widget.createMachineWidget(address, name))
end
elements.powerWidget = Widget.createPowerWidget(energyBufferAddress)
page.create(pages.overview)
end
@ -211,7 +203,7 @@ function page.update()
machineWidget:update()
end
for index, activeMachineWidget in ipairs(elements.machineWidgets.active) do
activeMachineWidget.draw(activeMachineWidget, index)
activeMachineWidget:draw(index)
end
elements.powerWidget:update()

View File

@ -15,8 +15,9 @@ local states = {
}
local types = {
power = "power",
machine = "machine"
POWER = "POWER",
MULTIBLOCK = "MULTIBLOCK",
MACHINE = "MACHINE"
}
local function drawProgress(x, y, width, height, progress, maxProgress, color)
@ -70,7 +71,7 @@ function widget.drawBaseWidget(x, y, width, height, title)
end
local function draw(self, index)
if self.type == types.power then
if self.type == types.POWER then
index = 10
end
local scale = self.scale or 1
@ -198,7 +199,7 @@ function fake.machineWidget.create()
state = state,
progress = 0,
maxProgress = state ~= states[3] and state ~= states[4] and math.random(500) or 0,
type = "machine",
type = types.MACHINE,
update = fake.machineWidget.update,
onClick = fake.machineWidget.onClick,
getMiddleString = fake.machineWidget.getMiddleString,
@ -206,6 +207,34 @@ function fake.machineWidget.create()
}
end
function widget.createMachineWidget(address, name)
local getMultiblockStatus = require("domain.multiblock.get-multiblock-status-usecase")
local function update(self)
for key, value in pairs(getMultiblockStatus(address, self.name)) do
self[key] = value
end
end
local toggleMultiblockWork = require("domain.multiblock.toggle-multiblock-work")
local function onClick(self)
toggleMultiblockWork(address, self.name)
end
local machineWidget = {
name = name,
type = types.MULTIBLOCK,
update = update,
onClick = onClick,
getMiddleString = function()
end,
draw = draw
}
machineWidget:update()
return machineWidget
end
fake.powerWidget = {}
function fake.powerWidget:update()
@ -231,7 +260,7 @@ function fake.powerWidget.create()
maxProgress = 16000000,
dProgress = 1,
scale = 2,
type = types.power,
type = types.POWER,
update = fake.powerWidget.update,
onClick = fake.powerWidget.onClick,
getMiddleString = fake.powerWidget.getMiddleString,
@ -269,7 +298,7 @@ function widget.createPowerWidget(address)
local powerWidget = {
name = "Power",
scale = 2,
type = types.power,
type = types.POWER,
update = update,
onClick = function()
end,

View File

@ -1,27 +1,40 @@
-- Import section
local mock = require("data.mock.mock-energy-provider")
EnergyProvider = require("data.datasource.energy-provider")
MultiBlock = require("data.datasource.multi-block")
Inherits = require("utils.inherits")
--
local machine = Inherits(EnergyProvider)
local machines = {}
machine.types = {
energy = "energy",
multiblock = "multiblock",
singleblock = "singleblock"
}
machine.states = {
ON = {name = "ON", color = Colors.workingColor},
FULL = {name = "FULL", color = Colors.workingColor},
IDLE = {name = "IDLE", color = Colors.idleColor},
FILLING = {name = "FILLING", color = Colors.idleColor},
OFF = {name = "OFF", color = Colors.offColor},
DRAINING = {name = "DRAINING", color = Colors.offColor},
BROKEN = {name = "BROKEN", color = Colors.errorColor},
EMPTY = {name = "EMPTY", color = Colors.errorColor}
}
function machine.getMachine(address, name, type)
if machines[address] then
return machines[address]
else
local mach = {}
if type == machine.types.energy then
print(machine.name)
mach = EnergyProvider:new(address, name)
elseif type == machine.types.multiblock then
mach = MultiBlock:new(address, name)
end
mach = machine:new(address, name)
machines[address] = mach
return mach
end

View File

@ -14,6 +14,16 @@ local MultiBlock =
}
)
function Parser.parseProgress(progressString)
local current = string.sub(progressString, string.find(progressString, "%ba§"))
current = tonumber((string.gsub(string.gsub(current, "a", ""), "§", "")))
local maximum = string.sub(progressString, string.find(progressString, "%be§", (string.find(progressString, "/"))))
maximum = tonumber((string.gsub(string.gsub(maximum, "e", ""), "§", "")))
return {current = current, maximum = maximum}
end
function MultiBlock:getNumberOfProblems()
local sensorInformation = self:getSensorInformation()
return Parser.parseProblems(sensorInformation[5])

View File

@ -7,20 +7,36 @@ local MockMultiBlock =
Inherits(
MockSingleBlock,
{
name = "MockMultiBlock"
name = "MockMultiBlock",
isBroken = false
}
)
function MockMultiBlock.getSensorInformation()
MockMultiBlock.workProgress = MockMultiBlock.workProgress + 1
if MockMultiBlock.workProgress > MockMultiBlock.workMaxProgress then
MockMultiBlock.workProgress = 0
end
if MockMultiBlock.workAllowed and not MockMultiBlock.isBroken and math.random(1000) > 999 then
MockMultiBlock.workMaxProgress = math.random(500)
end
MockMultiBlock.isBroken = MockMultiBlock.isBroken or math.random(100000) > 99999
return {
"Progress: §a2§r s / §e5§r s",
"Progress: §a" .. MockMultiBlock.workProgress .. "§r s / §e" .. MockMultiBlock.workMaxProgress .. "§r s",
"Stored Energy: §a1000§r EU / §e1000§r EU",
"Probably uses: §c4§r EU/t",
"Max Energy Income: §e128§r EU/t(x2A) Tier: §eMV§r",
"Problems: §c0§r Efficiency: §e100.0§r %",
"Problems: §c" .. (MockMultiBlock.isBroken and 1 or 0) .. "§r Efficiency: §e100.0§r %",
"Pollution reduced to: §a0§r %",
n = 6
}
end
function MockMultiBlock.setWorkAllowed(allow)
if MockMultiBlock.isBroken then
MockMultiBlock.isBroken = false
end
MockMultiBlock.workAllowed = allow
end
return MockMultiBlock

View File

@ -1,22 +1,38 @@
-- Import section
Alarm = require("api.sound.alarm")
Machine = require("data.datasource.machine")
--
local function exec(multiblocks)
local statuses = {}
for _, multiblock in ipairs(multiblocks) do
local problems = multiblock:getNumberOfProblems()
if problems > 0 then
Alarm()
end
local function exec(address, name)
local multiblock = Machine.getMachine(address, name, Machine.types.multiblock)
local status = {}
local problems = multiblock:getNumberOfProblems()
statuses[multiblock.name] = {
problems = problems,
probablyUses = multiblock:getEnergyUsage(),
efficiencyPercentage = multiblock:getEfficiencyPercentage()
}
local state = {}
if multiblock:isWorkAllowed() then
if multiblock:hasWork() then
state = Machine.states.ON
else
state = Machine.states.IDLE
end
else
state = Machine.states.OFF
end
return statuses
if problems > 0 then
state = Machine.states.BROKEN
end
local totalProgress = multiblock:getProgress()
status = {
progress = totalProgress.current,
maxProgress = totalProgress.maximum,
problems = problems,
probablyUses = multiblock:getEnergyUsage(),
efficiencyPercentage = multiblock:getEfficiencyPercentage(),
state = state
}
return status
end
return exec

View File

@ -0,0 +1,12 @@
-- Import section
Machine = require("data.datasource.machine")
Alarm = require("api.sound.alarm")
--
local function exec(address, name)
local multiblock = Machine.getMachine(address, name, Machine.types.multiblock)
local workAllowed = multiblock:isWorkAllowed()
multiblock:setWorkAllowed(not workAllowed)
end
return exec

View File

@ -7,7 +7,7 @@ Page = require("api.gui.page")
EnergyProvider = require("data.datasource.energy-provider")
-- local cleanroomAddresses = require("config.addresses.cleanroom")
-- local multiBlockAddresses = require("config.addresses.multi-blocks")
local multiBlockAddresses = require("config.addresses.multi-blocks")
local energyBufferAddresses = require("config.addresses.energy-buffers")
-- local protectCleanroomRecipes = require("domain.cleanroom.protect-recipes-usecase")
@ -62,7 +62,7 @@ end
require("api.sound.zelda-secret")()
--]]
Page.setup(energyBufferAddresses.batteryBuffer1)
Page.setup(energyBufferAddresses.batteryBuffer1, multiBlockAddresses)
while true do
Page.update()

View File

@ -12,9 +12,6 @@ shell.execute("wget -fq " .. tarBin)
local InfOS = "https://github.com/gordominossi/InfOS/releases/download/v0.2.1/InfOS.tar"
shell.setWorkingDirectory("/home")
if not shell.resolve("/home/lib") then
shell.execute("mkdir lib")
end
if not shell.resolve("/home/InfOS") then
shell.execute("mkdir InfOS")
end
@ -28,7 +25,8 @@ shell.execute("rm -f InfOS.tar")
shell.setWorkingDirectory("/home/")
shell.execute("rm -rf lib")
shell.execute("cp -r InfOS/Libraries lib")
shell.execute("mkdir lib")
shell.execute("cp -r InfOS/Libraries/* lib")
shell.execute("rm -f .shrc")
shell.execute("cp InfOS/.shrc .shrc")
shell.execute("rm -f setup.lua")