t8rt0t

fern-host.lua

May 19th, 2024 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.62 KB | None | 0 0
  1. local fern_data = require("fern-data")
  2.  
  3. peripheral.find("modem",rednet.open)
  4. if not rednet.isOpen() then error("Must have a connected rednet to host") end
  5. --Using rednet host so scanners can find this computer
  6. rednet.host("FERN-HOST",""..math.random(999999)) --They force you to use a hostname :(
  7.  
  8.  
  9. local function ss_receive()
  10.     --@2m80_
  11.     term.setTextColor(colors.yellow)
  12.     local sender,message,protocol = rednet.receive()
  13.     print("receive",sender,message,protocol)
  14.    
  15.     return sender, message, protocol --Just like rednet?
  16. end
  17.  
  18. local function ss_send(target,message,protocol)
  19.     --@2m80_
  20.     term.setTextColor(colors.orange)
  21.     print("send",target,message,protocol)
  22.     rednet.send(target,message,protocol) --temporary
  23.     return success --Should include?
  24. end
  25.  
  26. local function canDiskFitRegion(disk,rx,rz)
  27.     local path = "disk"
  28.     if disk >= 2 then
  29.         path = "disk" .. disk
  30.     end
  31.     if not fs.isDir(path) then error("No disk space") end
  32.     --case where region file is in disk
  33.     if fs.exists(path .. "/region/".. rx .. "_" .. rz .. ".dat") then
  34.         if fs.getFreeSpace(path) < 100 then
  35.             return false,path .. "/region/".. rx .. "_" .. rz .. ".dat"
  36.         end
  37.         return true,path .. "/region/".. rx .. "_" .. rz .. ".dat"
  38.     else
  39.         if fs.getFreeSpace(path) < 17000 then
  40.             return false
  41.         end
  42.         return true,path .. "/region/".. rx .. "_" .. rz .. ".dat"
  43.     end
  44. end    
  45.  
  46. --Gets the path to the region file for the chunkX,chunkZ
  47. local function getRegionPath(chunkX,chunkZ)
  48.     local rX,rZ = math.floor(chunkX/128),math.floor(chunkZ/128) --The region coordinates of the data
  49.     local index = 1
  50.     local canFit = false
  51.     local pathToCopy,path
  52.     while not canFit do
  53.         canFit,path = canDiskFitRegion()
  54.         if not canFit and path then pathToCopy = path end
  55.     end
  56.     if pathToCopy then
  57.         fs.copy(pathToCopy,path)
  58.     end
  59.     --@2m80
  60.     --path would look like "disk0/regions/rX_rZ.dat"?
  61.     --It would ensure there is room for the region to be written as well
  62.     --If there is no room for additional data in the disk, it moves the existing region file to a new disk and returns that new path
  63.     return path
  64. end
  65.  
  66.  
  67. local function handle_messages(sender,message,protocol)
  68.     if protocol == "get_region" then
  69.         ss_send(sender,fern_data.getRegionAssignment(sender),"region_assignment")
  70.     --Ideal "data" protocol message:
  71.         --{chunk={cX,cZ},biome="forest"}
  72.     elseif protocol == "data" then
  73.         --Invalid message type error
  74.         if type(message) ~= "table" or type(message.chunk) ~= "table" or not tonumber(chunk[1]) or not tonumber(chunk[2]) then ss_send(sender,{err="invalid_message",protocol=protocol,message=message},"error") end
  75.         local chunk = message.chunk
  76.         local region = {math.floor(tonumber(chunk[1])/128),math.floor(tonumber(chunk[2])/128)}
  77.         --If the turtle does not have permission to edit that region then error
  78.         if tonumber(fern_data.getRegionAssignment(region)) ~= sender then ss_send(sender,{err="invalid_region",protocol=protocol,message=message},"error") end
  79.         --If no biome was sent or a non-string biome was sent then error
  80.         if type(message.biome) ~= "string" then ss_send(sender,{err="invalid_biome",protocol=protocol,message=message},"error") end
  81.         local path = getRegionPath(tonumber(chunk[1]),tonumber(chunk[2]))
  82.         fern_data.writeDat(path,chunk[1]%128,chunk[2]%128,message.biome)
  83.         ss_send(sender,message.biome,"data_success")
  84.     elseif protocol == "synchronize" then
  85.         --TODO: fill out
  86.         ss_send(sender,"cod","synchronize_response")
  87.     elseif protocol == "pong" then
  88.         --TODO
  89.     end
  90. end
  91.  
  92. if fs.exists("latest.log") then
  93.     local file = fs.open("latest.log","r")
  94.     local time = file.readLine()
  95.     file.close()
  96.     if fs.exists("logs/" .. time .. ".log") then fs.delete("logs/" .. time .. ".log") end
  97.     pcall(function() fs.move("latest.log","logs/" .. time .. ".log") end)
  98.     if fs.exists("latest.log") then fs.delete("latest.log") end
  99. end
  100. local log_file = fs.open("latest.log","w+")
  101. log_file.writeLine(os.date("%y-%m-%d_%H:%M"))
  102. log_file.flush()
  103.    
  104. local function log(msg)
  105.     log_file.writeLine(msg)
  106.     log_file.flush()
  107. end
  108.  
  109. shell.openTab("sh")
  110.  
  111. local function main()
  112.     while true do
  113.         local sender,message,protocol = ss_receive()
  114.         local result = {pcall(function() handle_messages(sender,message,protocol) end)}
  115.         if not result[1] then
  116.             for i=2,#result do
  117.                 log("Error" .. tostring(result[i]))
  118.             end
  119.         end
  120.     end
  121. end
  122. main()
Advertisement
Add Comment
Please, Sign In to add comment