Advertisement
Bjornir90

Plugin database

Dec 7th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.33 KB | None | 0 0
  1. -- This needs the CCserver base program to work
  2.  
  3. --This is only a non-tested version, so easy-to-fix bugs may occurs
  4.  
  5. function createConfig()
  6.  if fs.exists("/serverFolder/server.config") then
  7.   return "nothing"
  8.  else
  9.  print("Thanks you for download this rednet protocol")
  10.  os.sleep(2)
  11.  shell.run("clear")
  12.  print("Enter the side of your modem")
  13.  local modemSide = io.read()
  14.  shell.run("clear")
  15.  print("Enter the id of this computer")
  16.  local idServ = io.read()
  17.  shell.run("clear")
  18.  print("You have finished the configuration of your program")
  19.  fs.makeDir("serverFolder")
  20.  fs.makeDir("usermade")
  21.  fs.makeDir("database")
  22.  local conf = io.open("/serverFolder/server.config", "w")
  23.  local data = {side = modemSide, id = idServ}
  24.  local dataS = textutils.serialize(data)
  25.  local writeIsFail = conf:write(dataS)
  26.  conf:close()
  27.   print("Config succesfully written")
  28. end
  29. end
  30.  
  31. function loadConfig()
  32.  shell.run("clear")
  33.  if fs.exists("/serverFolder/server.config") then
  34.   local conf = fs.open("/serverFolder/server.config", "r")
  35.   local dataS = conf.readAll()
  36.   conf.close()
  37.   local data = textutils.unserialize(dataS)
  38.   local side = data.side
  39.   local id = data.id
  40.   rednet.open(side)
  41.   return id
  42.  else
  43.   print("Cannot find the config file, please restart this program")
  44.   os.sleep(3)
  45.   return
  46.  end
  47. end
  48.  
  49. function server()
  50. while exit == 0  do
  51.  msgVerification = 0
  52.   id, msg = rednet.receive()
  53.   if msg == "p" then
  54.    id, msg1 = rednet.receive()
  55.    id, msg2 = rednet.receive()
  56.    shell.run(msg1, msg2)
  57.   elseif msg == "f" then
  58.    id, msgDo = rednet.receive()
  59.    id, msg1 = rednet.receive()
  60.    if msgDo == "e" then
  61.     loadstring(msg1) ()
  62.    elseif msgDo == "s" then
  63.     msgName = rednet.receive()
  64.     newFile = io.open("/usermade/"..msgName, "w")
  65.     newFile:write(msg1)
  66.     newFile:close()
  67.    end
  68.   elseif msg == "exit" then
  69.    shell.run("clear")
  70.    rednet.close("right")
  71.    break
  72.   elseif msg == "database_new" then
  73.    local id, msgDataName = rednet.receive()
  74.    local id, msgData = rednet.receive()
  75.    local data = msgData
  76.    local dataF = fs.open("/database/"..msgDataName, "w")
  77.    dataF.write(data)
  78.    dataF.close()
  79.   elseif msg == "database_append" then
  80.    local id, msgDataName = rednet.receive()
  81.    local id, msgDataAppend = rednet.receive()
  82.    dataF = fs.open("/database/"..msgDataName, "r")
  83.    dataToConc = dataF.readAll()
  84.    dataF.close()
  85.    dataAppend = textutils.unserialize(msgDataAppend)
  86.    dataToConc = textutils.unserialize(dataToConc)
  87.    local sizeTable = #dataAppend
  88.    for x=1, sizeTable do
  89.     table.insert(dataToConc, dataAppend[x])
  90.    end
  91.    dataF = fs.open("/database/"..msgDataName, "w")
  92.    dataToWrite = textutils.serialize(dataToConc)
  93.    dataF.write(dataToWrite)
  94.    dataF.close()
  95.   elseif msg == "database_read" then
  96.    id, msgDataName = rednet.receive()
  97.    dataF = fs.open("/database/"..msgDataName, "r")
  98.    dataToSend = dataF.readAll()
  99.    dataF.close()
  100.    rednet.send(id, dataToSend)
  101.   end
  102.  end
  103. end
  104. end
  105.  
  106. local msgVerification = 0
  107. local exit = 0
  108. term.clear()
  109. shell.run("id")
  110. createConfig()
  111. loadConfig()
  112. server()
  113. -- This needs the CCserver base program to work
  114.  
  115. --This is only a non-tested version, so easy-to-fix bugs may occurs
  116.  
  117. function createConfig()
  118.  if fs.exists("/serverFolder/server.config") then
  119.   return "nothing"
  120.  else
  121.  print("Thanks you for download this rednet protocol")
  122.  os.sleep(2)
  123.  shell.run("clear")
  124.  print("Enter the side of your modem")
  125.  local modemSide = io.read()
  126.  shell.run("clear")
  127.  print("Enter the id of this computer")
  128.  local idServ = io.read()
  129.  shell.run("clear")
  130.  print("You have finished the configuration of your program")
  131.  fs.makeDir("serverFolder")
  132.  fs.makeDir("usermade")
  133.  fs.makeDir("database")
  134.  local conf = io.open("/serverFolder/server.config", "w")
  135.  local data = {side = modemSide, id = idServ}
  136.  local dataS = textutils.serialize(data)
  137.  local writeIsFail = conf:write(dataS)
  138.  conf:close()
  139.   print("Config succesfully written")
  140. end
  141. end
  142.  
  143. function loadConfig()
  144.  shell.run("clear")
  145.  if fs.exists("/serverFolder/server.config") then
  146.   local conf = fs.open("/serverFolder/server.config", "r")
  147.   local dataS = conf.readAll()
  148.   conf.close()
  149.   local data = textutils.unserialize(dataS)
  150.   local side = data.side
  151.   local id = data.id
  152.   rednet.open(side)
  153.   return id
  154.  else
  155.   print("Cannot find the config file, please restart this program")
  156.   os.sleep(3)
  157.   return
  158.  end
  159. end
  160.  
  161. function server()
  162. while exit == 0  do
  163.  msgVerification = 0
  164.   id, msg = rednet.receive()
  165.   if msg == "p" then
  166.    id, msg1 = rednet.receive()
  167.    id, msg2 = rednet.receive()
  168.    shell.run(msg1, msg2)
  169.   elseif msg == "f" then
  170.    id, msgDo = rednet.receive()
  171.    id, msg1 = rednet.receive()
  172.    if msgDo == "e" then
  173.     loadstring(msg1) ()
  174.    elseif msgDo == "s" then
  175.     msgName = rednet.receive()
  176.     newFile = io.open("/usermade/"..msgName, "w")
  177.     newFile:write(msg1)
  178.     newFile:close()
  179.    end
  180.   elseif msg == "exit" then
  181.    shell.run("clear")
  182.    rednet.close("right")
  183.    break
  184.   elseif msg == "database_new" then
  185.    local id, msgDataName = rednet.receive()
  186.    local id, msgData = rednet.receive()
  187.    local data = msgData
  188.    local dataF = fs.open("/database/"..msgDataName, "w")
  189.    dataF.write(data)
  190.    dataF.close()
  191.   elseif msg == "database_append" then
  192.    local id, msgDataName = rednet.receive()
  193.    local id, msgDataAppend = rednet.receive()
  194.    dataF = fs.open("/database/"..msgDataName, "r")
  195.    dataToConc = dataF.readAll()
  196.    dataF.close()
  197.    dataAppend = textutils.unserialize(msgDataAppend)
  198.    dataToConc = textutils.unserialize(dataToConc)
  199.    local sizeTable = #dataAppend
  200.    for x=1, sizeTable do
  201.     table.insert(dataToConc, dataAppend[x])
  202.    end
  203.    dataF = fs.open("/database/"..msgDataName, "w")
  204.    dataToWrite = textutils.serialize(dataToConc)
  205.    dataF.write(dataToWrite)
  206.    dataF.close()
  207.   elseif msg == "database_read" then
  208.    id, msgDataName = rednet.receive()
  209.    dataF = fs.open("/database/"..msgDataName, "r")
  210.    dataToSend = dataF.readAll()
  211.    dataF.close()
  212.    rednet.send(id, dataToSend)
  213.   end
  214.  end
  215. end
  216. end
  217.  
  218. local msgVerification = 0
  219. local exit = 0
  220. term.clear()
  221. shell.run("id")
  222. createConfig()
  223. loadConfig()
  224. server()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement