Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.execute("clear"); --Clears the screen before displaying anything
- --Basic Variables and Do Dads
- local component = require("component");
- local serialization = require("serialization");
- local event = require("event");
- local thread = require("thread");
- local filesystem = require("filesystem");
- local redstoneList = component.list("redstone");
- local network = component.modem
- local runServer = false;
- local farmWidth = 4;
- local farmHeight = 5;
- local addressFile = "/server/addresses.dat";
- local dataFile = "/server/farmData.dat";
- local dataFileBACK = "/server/farmData_backup.dat";
- local farmGrid = {};
- local addresses = {};
- local ver = "0.1.0";
- local serverPort = 63;
- local runSetup = true;
- local threads = {};
- --Basic Functions
- function waitUntilEnter() --Waits For User Input
- local userInput = io.read();
- return;
- end
- function saveTable(input, fileIn) --Saves Table
- file = io.open(fileIn, "w");
- local ser = serialization.serialize(input);
- file:write(ser);
- file:close();
- file:flush();
- end
- function loadTable(file) --Loads Table
- local fileIn = io.open(file, "r");
- local serInput = fileIn:read();
- fileIn:close();
- fileIn:flush();
- return serialization.unserialize(serInput);
- end
- --External Functions
- function setRedstoneSignal(x, y, signal) --Sets Redstone Signal
- --print("Command Recognized");
- redAddress = addresses[tonumber(x)][tonumber(y)];
- --print(redAddress);
- local redstoneIO = component.proxy(redAddress);
- for i = 0, 5, 1 do
- redstoneIO.setOutput(i, signal);
- end
- return true;
- end
- function getRedstoneAddress() --Checks Which Redstone IO has a red Signal
- for it, comp in pairs(redstoneList) do
- compont = component.proxy(it);
- for z = 0, 5, 1 do
- if compont.getInput(z) > 0 then
- return it;
- end
- end
- end
- return false;
- end
- function shutdown()
- print("Shutting Down Server!");
- network.close(serverPort);
- print("Closed port " .. serverPort);
- print("Creating Backup Files Just in case.");
- if filesystem.exists(dataFileBACK) then filesystem.remove(dataFileBACK) end
- os.execute("cp " .. dataFile .. " " .. dataFileBACK);
- print("Saving Current Configuration...");
- os.execute("del " .. dataFile);
- saveTable(farmGrid, dataFile);
- print("Auto Running All Farms");
- for i=1, farmWidth, 1 do
- for t=1, farmHeight, 1 do
- setRedstoneSignal(i, t, 0);
- end
- end
- print("All Farms Enabled!");
- os.execute("shutdown");
- end
- function setup() --Setup Initial Variables : First Time
- local farmX = {};
- local addressX = {};
- print("Starting Cailibration Process");
- print("Please enable lever on the corresponding Redstone IO Ports, and Press Enter After");
- for x = 1, farmWidth, 1 do --Each X Coordinate
- local farmY = {};
- local addressY = {};
- for y = 1, farmHeight, 1 do
- local loop = true;
- local farm = {};
- farm["crop"] = "N/A";
- farm["status"] = true;
- while loop == true do
- print("Please Turn Redstone Signal On, and Press enter");
- print("[" .. x .. "," .. y .. "]");
- waitUntilEnter();
- addressY[y] = getRedstoneAddress();
- --Set Farm Grid Variables
- if addressY[y] == false then
- print("ERROR. No Redstone Signal Found! Going Back.");
- else
- loop = false;
- end
- end --End While
- print("Redstone Address Set To " .. addressY[y]);
- print("--------------------------------------------------------");
- farmY[y] = farm;
- end --End Y For
- addressX[x] = addressY;
- farmX[x] = farmY;
- end --End X For
- filesystem.makeDirectory("/server/");
- saveTable(addressX, addressFile);
- saveTable(farmX, dataFile);
- runServer = false;
- end --End Function
- function init()
- print("Starting Server...");
- print("Loading addresses");
- addresses = loadTable(addressFile);
- if addresses ~= nil then print("Addresses Loaded Successfully");
- else print("Error Loading addresses, Stopping Server"); runServer = false; return; end
- print("Loading Farm Grid Data");
- farmGrid = loadTable(dataFile);
- if farmGrid ~= nil then print("Farm Grid Loaded Successfully");
- else print("Error Loading Farm Grid, Stopping Server"); runServer = false; return; end
- print("Opening Port " .. serverPort);
- portStat = network.open(serverPort);
- if portStat == false then
- print("Error Opening Port " .. serverPort .. ". Stopping Server");
- runServer = false;
- return;
- else
- print("Successfully Opened " .. serverPort .. "!");
- end
- print("Setting Farming Stations");
- for i = 1, farmWidth, 1 do
- for t=1,farmHeight,1 do
- local currentFarm = farmGrid[i][t];
- if currentFarm["status"] == true then
- setRedstoneSignal(i, t, 0);
- else
- setRedstoneSignal(i, t, 15);
- end
- end
- end
- print("Server Successfully Initiated!");
- event.listen("interrupted", shutdown);
- print("------------------------------------------");
- end
- --Start Server
- print("-------------------------");
- print("-Farm Server-");
- print("Version: " .. ver);
- print("-------------------------");
- print("Checking to see if Essential Directorys/Files Exist");
- if filesystem.exists("/server/") == true then --Checking To See if Setup is needed
- print("Directory Exists");
- if filesystem.exists(addressFile) == true then
- if filesystem.exists(dataFile) == true then
- runServer = true;
- runSetup = false;
- print("Everything Seems In Order, Starting Server");
- else
- print("Data File Non Existent, Initiating Setup");
- end
- else
- print("Address File Non Existent, Initiating Setup");
- end
- else
- print("Directory Non Existent, Initiating Setup");
- end
- print("-------------------------");
- if runSetup == true then setup(); end
- init();
- function funnelMessage(from, port, message)
- print("[" .. from .. "] " .. message);
- local input = serialization.unserialize(message);
- local cmd = input[1];
- if cmd == "shutdown" then shutdown(); network.send(from, port, "Shutting Down Server"); --end of shutdown
- elseif cmd == "whois" then if input[2] == "farmController" then network.send(from, port, network.address) end --end of whois
- elseif cmd == "disableFarm" then farmGrid[tonumber(input[2])][tonumber(input[3])]["status"] = false; setRedstoneSignal(tonumber(input[2]), tonumber(input[3]), 15); network.send(from, port, "Disabled")
- elseif cmd == "enableFarm" then farmGrid[tonumber(input[2])][tonumber(input[3])]["status"] = true; setRedstoneSignal(tonumber(input[2]), tonumber(input[3]), 0); network.send(from, port, "Enabled")
- elseif cmd == "setCrop" then farmGrid[tonumber(input[2])][tonumber(input[3])]["crop"] = input[4];
- else print("Command Not Found");
- end
- print(farmGrid[1][1]["crop"]);
- print(farmGrid[1][1]["status"]);
- end
- print("-------------------------")
- if runServer == true then
- while runServer == true do
- print("Now Listening For Signals");
- local _, _, from, port, _, message = event.pull("modem_message")
- local thr = thread.create(funnelMessage, from, port, message);
- thr:detach();
- end
- end
- shutdown();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement