Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------
- -- Клиентская программа для шахтерских лазеров из кросоварпа.
- -- Ставится на компьютер возле лазера
- -- и подключается сетевыми кабелями к серверу,
- -- стоящему в кабине. На тот компьютер
- -- нужно поставить серверную программу.
- -- И то, и то нужно поставить в autostart.
- --
- -- (c) Efrim
- -- skype: gods_pee
- -- 05.10.2013
- --------------------------------------------------
- NETWORK_SIDE = "back" -- Сторона, к которой подключен модем
- LASER_SIDE = "left" -- Сторона, к которой подключен лазер
- DATA_FILE = "miner.dat" -- Файл для сохранения данных
- TEMP_FILE = "miner.temp" -- Временное хранилище
- TIMEOUTS_MAX = 3 -- Максимальное количество таймаутов в сетевом общении
- TIMEOUT = 1 -- Время таймаута
- REFRESH_PERIOD = 5 -- Период обновления экрана
- --------------------------------------------------
- ownID = os.getComputerID()
- mLayerOffset = 30
- initialized = 0
- miner = peripheral.wrap(LASER_SIDE)
- --------------------------------------------------
- -- Графическая часть
- --------------------------------------------------
- function DisplayStats()
- local state, energy, currentLayer, mined, total = miner.getMinerState()
- term.clear()
- term.setCursorPos(1,1)
- print("State: " .. state)
- print("Energy: " .. energy .. " Eu")
- print("Mined " .. mined .. "/" .. total .." at layer " .. currentLayer)
- print("Offset: " .. mLayerOffset)
- print("Initialized: " .. initialized)
- end
- --------------------------------------------------
- -- Инициализация
- --------------------------------------------------
- function Initialize()
- if not rednet.isOpen(NETWORK_SIDE) then
- rednet.open(NETWORK_SIDE)
- end
- serverID = -1
- term.clear()
- term.setCursorPos(1,1)
- print("Initializing...")
- while true do
- local receivedID, receivedMessage, _ = rednet.receive()
- print("Received message.")
- if receivedMessage == "Server is here" then
- serverID = receivedID
- rednet.send(serverID, "Miner " .. ownID .. " is here")
- local receivedID, receivedMessage, _ = rednet.receive(TIMEOUT)
- if receivedID == serverID and receivedMessage == "Gotcha" then
- initialized = 1
- return 0
- end
- end
- end
- return 1
- end
- --------------------------------------------------
- -- Общение с сервером после инициализации
- --------------------------------------------------
- function SendStatus()
- -- local state = {miner.getMinerState()}
- -- table.insert(state, mLayerOffset)
- rednet.send(serverID, textutils.serialize({miner.getMinerState()}))
- return 0
- end
- function ProcessCommand(command)
- local _, _, text, number = string.find(command, "([%a%d]+)% ?([%a%d]*)")
- if text == "start" or text == "startall" then
- miner.setStartLayerOffset(mLayerOffset)
- miner.startMining()
- elseif text == "stop" or text == "stopall" then
- miner.stop()
- elseif text == "quarry" or text == "quarryall" then
- miner.setStartLayerOffset(mLayerOffset)
- miner.startQuarry()
- elseif text == "reinit" then
- initialized = 0
- Initialize()
- elseif text == "offset" and number ~= "" then
- mLayerOffset = tonumber(number)
- miner.setStartLayerOffset(mLayerOffset)
- elseif text == "restart" then
- os.reboot()
- elseif text == "update" and number ~= "" then
- fs.delete("autostart")
- os.run({}, "pastebin", "get " .. number .. " autostart")
- os.reboot()
- elseif text == "statusrequest" then
- SendStatus()
- end
- return command
- end
- --------------------------------------------------
- -- Сохранение и загрузка
- --------------------------------------------------
- function SaveDataToFile()
- local file = fs.open(TEMP_FILE, "w")
- file.writeLine(serverID .. " " .. mLayerOffset .. " " .. initialized)
- file.close()
- fs.delete(DATA_FILE)
- fs.move(TEMP_FILE, DATA_FILE)
- return 0
- end
- function LoadDataFromFile()
- local file = fs.open(DATA_FILE, "r")
- local line = file.readAll()
- _, _, param1, param2, param3 = string.find(line, "(%d+) (%d+) (%d)")
- file.close()
- serverID = tonumber(param1)
- mLayerOffset = tonumber(param2)
- initialized = tonumber(param3)
- return 0
- end
- --------------------------------------------------
- -- Главный луп
- --------------------------------------------------
- function main()
- if not rednet.isOpen(NETWORK_SIDE) then
- print("Opening networking...")
- rednet.open(NETWORK_SIDE)
- end
- if fs.exists(DATA_FILE) then
- print("Loading previous data...")
- LoadDataFromFile()
- end
- if not initialized then
- print("Initializing...")
- Initialize()
- end
- while true do
- DisplayStats()
- local timeout = os.startTimer(REFRESH_PERIOD)
- while true do
- local event, id, text = os.pullEvent()
- if event == "rednet_message" then
- ProcessCommand(text)
- DisplayStats()
- SaveDataToFile()
- timeout = os.startTimer(REFRESH_PERIOD)
- elseif event == "timer" and id == timeout then
- break
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement