Last active
December 21, 2015 11:19
-
-
Save Tyderion/6298113 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rednet.open("top") | |
config = { | |
} | |
registered = false | |
master = 0 | |
pulseLengthInSec ={ | |
["stack"] = 1.65, | |
["single-redstone"] = 6 | |
} | |
local currentText = "" | |
configured = 0 | |
function extractConfig(conf, side) | |
local name = string.match(conf, "^(%a+:%a+:%a+)") | |
local rtype = string.match(conf, ":(%a%a)$") | |
if rtype == "sn" then | |
rtype = "single-redstone" | |
else | |
rtype = "stack" | |
end | |
config[side] = { | |
["resource"] = name, | |
["type"] = rtype, | |
} | |
end | |
function loadConfig() | |
if fs.exists("slave.cfg") then | |
file = fs.open("slave.cfg", "r") | |
while true do | |
local line = file.readLine() | |
if line == nil then | |
break | |
end | |
local side= string.match(line, "^(%a+):") | |
local conf = string.sub(line, #side+2) | |
extractConfig(conf, side) | |
end | |
file.close() | |
print(json.encodePretty(config)) | |
end | |
end | |
loadConfig() | |
function establishMaster() | |
while master == 0 do | |
print("Trying to reach master") | |
rednet.broadcast("master") | |
id, msg = rednet.receive(10) | |
if msg == "yes" then | |
master = id | |
sleep(1) | |
register() | |
end | |
end | |
end | |
function register() | |
for name,config in pairs(config) do | |
rednet.send(master, "registerSlave:".. config.resource) | |
while true do | |
id, text = rednet.receive(10) | |
if (string.match(text, "^registered$") and id == master) then | |
print("registered " .. name .. " as " .. config.resource) | |
break | |
end | |
end | |
end | |
run() | |
end | |
function checkNil(arg) | |
if arg == nil then | |
return "nil" | |
else | |
return arg | |
end | |
end | |
function checkAndSendRess(text) | |
for name,config in pairs(config) do | |
if (id == master and string.match(text, "^" .. config.resource .. ":%d+$")) then | |
amount = checkNil(string.match(text, ":(%d+)$")) | |
local amountstr = "x stacks " | |
if config.type == "single-redstone" then | |
amountstr = "x " | |
end | |
local answer = "sending " .. amount .. amountstr .. " of " .. config.resource | |
print( answer) | |
rednet.send(master, answer) | |
for i=1,tonumber(amount) do | |
giveStack(name) | |
sleep(0.2) | |
end | |
end | |
end | |
end | |
function run() | |
while true do | |
id, text = rednet.receive() | |
if text ~= nil then | |
if (string.match(text, "^newmaster$")) then | |
break | |
end | |
checkAndSendRess(text) | |
end | |
end | |
register() | |
end | |
function giveStack(direction) | |
conf = config[direction] | |
redstone.setOutput(direction, true) | |
sleep(pulseLengthInSec[conf.type]) | |
redstone.setOutput(direction, false) | |
-- if conf.type == "single-redstone" then | |
-- print("waiting for 6s") | |
-- sleep(6) | |
-- end | |
end | |
establishMaster() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment