Last active
December 21, 2015 11:19
-
-
Save Tyderion/6298100 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("right") | |
slaves = {} | |
rednet.broadcast("newmaster") | |
function dbjson(thing) | |
print(json.encodePretty(thing)) | |
end | |
function compileResourceList() | |
local categories = {} | |
for _, slave in pairs(slaves) do | |
local categoryName = slave.category | |
local subCategoryName = slave.subcategory | |
local name = slave.name | |
local cat = categories[categoryName] | |
if ( categories[categoryName]== nil) then | |
categories[categoryName] = {} | |
end | |
subcat = categories[categoryName][subCategoryName] | |
if (subcat == nil) then | |
categories[categoryName][subCategoryName] = {} | |
end | |
local length = #categories[categoryName][subCategoryName] | |
categories[categoryName][subCategoryName][length+1] = slave.name | |
end | |
return categories | |
end | |
while true do | |
event, id, text = os.pullEvent() | |
if event == "rednet_message" then | |
if (string.match(text, "^registerSlave:%a+")) then | |
local material = string.sub(text, 15) | |
local category = string.match(material, "(%a+):") | |
local subcategory = string.match(material, ":(%a+):") | |
local name = string.match(material, ":(%a+)$") | |
slaves[material] = { | |
["id"] = tonumber(id), | |
["category"] = category, | |
["subcategory"] = subcategory, | |
["name"] = name | |
} | |
rednet.send(slaves[material].id, "registered") | |
print("registering " .. id .. " as " .. material) | |
end | |
if (string.match(text, "^getRess:%a+")) then | |
local materialname_amount = string.sub(text, 8) | |
-- ress_type, amount = string.match(text, "^getRess:(%a+):(%d+)") | |
print(materialname_amount) | |
local category = string.match(materialname_amount, "(%a+):") | |
local subcategory = string.match(materialname_amount, category .. ":(%a+):") | |
local name = string.match(materialname_amount, ":(%a+)-") | |
local num = string.match(materialname_amount, "-(%d+)$") | |
local material = category ..":".. subcategory ..":".. name | |
print("type = " .. material) | |
print("amount = " .. num) | |
rec = slaves[material] | |
if rec == nil then | |
print("we do not have " .. material) | |
rednet.send(id, material .. " is unavailable.") | |
else | |
print("sending " .. num .. " of " .. material) | |
rednet.send(id, "Ordered ".. num .. "x " .. material) | |
rednet.send(rec.id, material .. ":" .. num) | |
end | |
end | |
if (string.match(text, "^master$")) then | |
print("got a client") | |
rednet.send(id, "yes") | |
end | |
if (string.match(text, "^getResourceList$")) then | |
print("compile resource list") | |
local tlist = compileResourceList() | |
print(json.encodePretty(tlist)) | |
rednet.send(id, json.encode(tlist)) | |
-- result = "Available Resources: \n" | |
-- for key,value in pairs(slaves) do | |
-- result = result .. key .."\n" | |
-- end | |
-- rednet.send(id, result) | |
end | |
if (string.match(text, "^unregister:(%a+)")) then | |
restype = string.match(text, "^unregister:(%a+)") | |
slaves[restype] = nil | |
end | |
if (string.match(text, "^update$")) then | |
shell.run("update") | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment