AnotherTekkituser

server pc

May 6th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. -- globsl vars
  2. local info = {}
  3. local pcinfo = "serverinfo"
  4. local commands="commands"
  5. local defstate="states"
  6. local msgs = {}
  7. local mside=nil
  8. local id=nil
  9. local channel = nil
  10. local channelcheck=nil
  11. local pastebin = nil
  12. local modem = nil
  13. local color_green=0x66FF00
  14. local color_red=0xFF0800
  15. local bridge=peripheral.wrap("left")
  16. local hud=bridge.addText(1,80,"off",color_red)
  17.  
  18. local function setstr(file,input) --writes to file from a var
  19.     local f=fs.open(file,"a")
  20.     f.writeLine(input)
  21.     f.close()
  22. end
  23.  
  24. local function mkfile(file) --creates a new file, deletes original first
  25.     shell.run("delete "..file)
  26.     local f=fs.open(file,"w")
  27.     f.close()
  28. end
  29.  
  30. function linecount(filename) --counts number of lines in a file and returns length
  31.     f = fs.open(filename, "r")
  32.     local length = 0
  33.     while true do
  34.         local line = f.readLine()
  35.         if not line then break end
  36.     end
  37.     f.close()
  38.     return length
  39. end
  40.  
  41. local function getvars(file) --// Localize the function too, define it before we use it in 'startup' function
  42.     local f = fs.open(file,"r")
  43.     for i = 1,linecount(commands) do
  44.         info[i] = f.readLine()
  45.     end
  46.     f.close()
  47. end
  48.  
  49. local function conf()
  50.     mkfile(pcinfo)
  51.     print("insert name for this server pc")
  52.     id = read()
  53.     setstr(pcinfo,id)
  54.     shell.run("label set "..id)
  55.     print("input modem side, must be valid")
  56.     mside = read()
  57.     setstr(pcinfo,mside)
  58.     print("What channel no. will this server be using?")
  59.     channel = read()
  60.     setstr(pcinfo,channel)
  61.     print("please input pastebin command that will be sent to new client pcs")
  62.     pastebin=read()
  63.     setstr(pcinfo,pastebin)
  64.     mkfile("config")
  65.     print("config complete, rebooting in...")
  66.     for i = 3,0,-1 do
  67.         if i == 0 then
  68.             shell.run("reboot")
  69.         end
  70.         print(tostring(i).."...")
  71.         sleep(1)
  72.     end
  73. end
  74.  
  75. local function startup ()-- catches info from output
  76.     getvars(pcinfo) -- sets all the variables needed from config
  77.     id=info[1]
  78.     mside=info[2]
  79.     channel=tonumber(info[3])
  80.     channelcheck= info[3]
  81.     pastebin=info[4]
  82.     getvars(commands) --gets all the commands for the server to display on Terminal Glasses
  83.     for i = 1,linecount(commands) do
  84.         msgs[i] = info[i]
  85.     end
  86. end
  87.  
  88. if not fs.exists("config") then
  89.     conf()
  90. else
  91.     startup()
  92. end
  93.  
  94. function new_command(cmd)
  95.     if not fs.exists(commands) then
  96.         mkfile(commands)
  97.     else
  98.         setstr(commands,cmd)
  99.     end
  100. end
  101.  
  102. modem=peripheral.wrap(mside)
  103. modem.open(channel)--opens a channel on the predefined channel
  104. print("server:"..id..":ACTIVE")
  105. print("channel assigned:"..channelcheck)
  106. while true do
  107.     local event= {os.pullEvent()}
  108.     if event[5] == channelcheck then
  109.         modem.transmit(channel,channel,channelcheck)
  110.         print(chsnnelcheck)
  111.     end
  112.     if event[5] ~= "" then
  113.         modem.transmit(channel,channel,message)
  114.     end
  115.     if event[5] == "New Command" then
  116.         os.startTimer(1)
  117.         local msgr = {os.pullEvent()}
  118.         if msgr[5]~="" then
  119.             setstr(commands,msgr[5])
  120.             modem.transmit(channel,channel,"done")
  121.         elseif msgr[1]=="timer" then
  122.             error("Oops, Command failed to register")
  123.         end
  124.     end
  125. end
Advertisement
Add Comment
Please, Sign In to add comment