Advertisement
Agent_Silence

Server Template.

Jan 16th, 2015 (edited)
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.29 KB | None | 0 0
  1. -- TorakTu - Template Server [ 8.26.2013.1531 ]
  2. -- ComputerCraft
  3.  
  4. os.pullEvent = os.pullEventRaw
  5.  
  6. local mainName = "Template Server"
  7. local em = "NONE"
  8. local sides = {"left","right","top","bottom","back"}
  9. local mSide = { }
  10. local mtotal = 0
  11. local isModem = 0
  12. local off = 0
  13. local mon = ""
  14. local advancedornot = false
  15. local currPath = shell.resolve(".")
  16. local timer
  17. local theSwitch = 0
  18. local theTime = 0
  19. local menu_options = 0
  20. local trans = 0
  21. local mScaleSize = 0.5
  22. local setMonScale = 0
  23. local setlock = 0
  24. local initLock = false
  25. local tr = 0
  26. local serverIdent = ""
  27. local complexity = 5 -- This is where you define
  28.                      -- how complex your serverIdent
  29.                      -- should be.
  30.                      -- Higher # = more complex
  31.  
  32. if currPath == "disk" then currPath = "/disk/" end
  33.  
  34. local settings             = { }
  35. settings.isonline          = "offline"
  36.  
  37. for _,v in pairs(sides) do
  38.    if peripheral.getType(v) == "modem" then
  39.       mtotal = mtotal + 1
  40.       mSide[mtotal] = v
  41.       isModem = 1
  42.    end
  43.    if peripheral.getType(v) == "monitor" then
  44.       em = v
  45.    end
  46. end
  47.  
  48. if isModem == 0 then
  49.    term.clear()
  50.    term.setCursorPos(1,1)
  51.    print(" ")
  52.    print(" WARNING ! No modem attached !")
  53.    print(" ")
  54.    print(" PROGRAM TERMINATED !")
  55.    print(" ")
  56.    return
  57. end
  58.  
  59. local modem = peripheral.wrap(mSide[1])
  60.  
  61. if em ~= "NONE" or em == "bottom" or em == "top" or em == "left" or em == "right" or em == "back" then
  62.    mon = peripheral.wrap(em)
  63.    mon.setTextScale(0.5)
  64. else
  65.    mon = "NONE"
  66. end
  67.  
  68. if term.isColor() then
  69.    advancedornot = true
  70. end
  71.  
  72. function mtprint(theString,x,y,fg,bg)
  73.    if mon ~= "NONE" then
  74.       if advancedornot then
  75.          if f ~= 0 then
  76.             mon.setTextColor(fg)
  77.             term.setTextColor(fg)
  78.          end
  79.          if b ~= 0 then
  80.             mon.setBackgroundColor(bg)
  81.             term.setBackgroundColor(bg)
  82.          end
  83.       end
  84.          mon.setCursorPos(x,y)
  85.          mon.write(theString)
  86.          term.setCursorPos(x,y)
  87.          term.write(theString)
  88.    else
  89.       if advancedornot then
  90.          if f ~= 0 then
  91.             term.setTextColor(fg)
  92.          end
  93.          if b ~= 0 then
  94.             term.setBackgroundColor(bg)
  95.          end
  96.       end
  97.       term.setCursorPos(x,y)
  98.       term.write(theString)
  99.    end
  100. end
  101.  
  102. function clearMe()
  103.    if mon ~= "NONE" then
  104.       if advancedornot then
  105.          term.setBackgroundColor(colors.black)
  106.          term.setTextColor(colors.white)
  107.          mon.setBackgroundColor(colors.black)
  108.          mon.setTextColor(colors.white)
  109.       end
  110.       mon.clear()
  111.       mon.setCursorPos(1,1)
  112.       term.clear()
  113.       term.setCursorPos(1,1)
  114.    else
  115.       if advancedornot then
  116.          term.setTextColor(colors.white)
  117.          term.setBackgroundColor(colors.black)
  118.       end
  119.       term.clear()
  120.       term.setCursorPos(1,1)
  121.    end
  122. end
  123.  
  124. function pscreen()
  125.    if settings.isonline == "online" then
  126.       mtprint(settings.isonline.." ",26,3,colors.green,colors.black)
  127.    else
  128.       mtprint(settings.isonline.." ",26,3,colors.red,colors.black)
  129.    end
  130.    mtprint(tostring(mSide[1]).." ",26,4,colors.cyan,colors.black)
  131.    mtprint(serverIdent.."                ",26,5,colors.cyan,colors.black)
  132. end
  133.  
  134. function saveFile( file, data )
  135.    local f = fs.open( file, "w" )
  136.    f.write( data )
  137.    f.close()
  138. end
  139.  
  140. function readFile( file )
  141.    if not fs.exists( file ) then return end
  142.    local f = fs.open( file, "r" )
  143.    local loadInfo = f.readAll()
  144.    f.close()
  145.    return loadInfo
  146. end
  147.  
  148. function emodem()
  149.    if off == 0 then
  150.       settings.isonline = "offline"
  151.       modem.close(1)
  152.    end
  153.    if off == 1 then
  154.       settings.isonline = "online"
  155.       modem.open(1)
  156.       modem.transmit(1,1,serverIdent)
  157.    end
  158. end
  159.  
  160. function mDraw()
  161.    mtprint(" ---------- ",3,7,colors.black,colors.brown)
  162.    mtprint("| Q - QUIT |",3,8,colors.black,colors.brown)
  163.    mtprint(" ---------- ",3,9,colors.black,colors.brown)
  164.    mtprint(" ---------- ",16,7,colors.black,colors.brown)
  165.    mtprint("|E - ENABLE|",16,8,colors.black,colors.brown)
  166.    mtprint(" ---------- ",16,9,colors.black,colors.brown)
  167.    mtprint("R - Reset Server Identity",3,11,colors.red,colors.black)
  168. end
  169.  
  170. function transmissions()
  171.           if trans == 0 then
  172.                  tr = 0
  173.                  mtprint("0",35,7,colors.gray,colors.gray)
  174.                  mtprint("0",35,9,colors.gray,colors.gray)
  175.           end
  176.           if trans == 1 then
  177.                  tr = 1
  178.                  timer = os.startTimer(0.1)
  179.                  mtprint("1",35,7,colors.green,colors.green)
  180.           end
  181.           if trans == 2 then
  182.                  tr = 1
  183.                  timer = os.startTimer(0.1)
  184.                  mtprint("1",35,9,colors.green,colors.green)
  185.           end
  186. end
  187.  
  188. function mainScreen()
  189.    mtprint(mainName,10,1,colors.red,colors.black)
  190.    mtprint("Server Status      : ",5,3,colors.white,colors.black)
  191.    mtprint("Modem Side         : ",5,4,colors.white,colors.black)
  192.    mtprint("Server Identity    : ",5,5,colors.white,colors.black)
  193.    mtprint("Tx : ",30,7,colors.white,colors.black)
  194.    mtprint("Rx : ",30,9,colors.white,colors.black)
  195. end
  196.  
  197. function makeHexString()
  198.      hex = ""
  199.      hexit = ""
  200.      for i = 1, complexity do
  201.         a = math.random(0, 15)
  202.         if a == 10 then hex = "A" end
  203.         if a == 11 then hex = "B" end
  204.         if a == 12 then hex = "C" end
  205.         if a == 13 then hex = "D" end
  206.         if a == 14 then hex = "E" end
  207.         if a == 15 then hex = "F" end
  208.         if a <= 9 then
  209.            hexit = hexit..tostring(a)
  210.         else
  211.            hexit = hexit..hex
  212.         end
  213.      end
  214.      serverIdent = hexit
  215. end
  216.  
  217. function isRandom()
  218.    if not fs.exists("Ident") then
  219.       makeHexString()
  220.       saveFile("Ident", serverIdent)
  221.    else
  222.       serverIdent = readFile("Ident")
  223.    end
  224. end
  225.  
  226. isRandom()
  227. clearMe()
  228. mainScreen()
  229. emodem()
  230. pscreen()
  231. mDraw()
  232. transmissions()
  233.  
  234. while true do
  235.  
  236.   if not initLock then setlock = setlock + 1 end
  237.   if not fs.exists(currPath.."server.lck") and setlock >= 25 then
  238.      saveFile("server.lck", "locked")
  239.      initLock = true
  240.   else
  241.      if not initLock and setlock >= 25 then
  242.         print("The Main TorNet server has a")
  243.         print("running instance. Shut down")
  244.         print("the other server first.")
  245.         return
  246.      end
  247.   end
  248.  
  249.   if tr >= 1 then
  250.          tr = tr - 1
  251.   else
  252.          if trans > 0 then
  253.                  tr = 0
  254.                  trans = 0
  255.                  transmissions()
  256.           end
  257.    end
  258.  
  259.    if theTime == 1 then
  260.       timer = os.startTimer(0.1)
  261.       if theSwitch >= 1 then theSwitch = theSwitch - 1 end
  262.       if theSwitch <= 0 then                                                                                                                                    
  263.          theSwitch = 0
  264.          theTime = 0
  265.       end
  266.    end
  267.    if off > 2 then off = 0 end
  268.    if theTime == 0 and theSwitch >= 1 then theSwitch = theSwitch - 1 end
  269.    if theSwitch <= 0 then
  270.       theSwitch = 0
  271.       theTime = 0
  272.    end
  273.    
  274.    local evt, arg, xPos, yPos, message = os.pullEvent()
  275.  
  276.    if evt == "timer" then
  277.           if tr > 0 then timer = os.startTimer(0.1) end
  278.    end
  279.  
  280.    if evt == "modem_message" then
  281.       if settings.isonline == "online" then
  282.          trans = 2
  283.          transmissions()
  284.          if message == serverIdent then
  285.             trans = 1
  286.             transmissions()
  287.             modem.transmit(1,1,serverIdent.."isalreadyonline")
  288.          end
  289.          if message == serverIdent.."isalreadyonline" then
  290.             off = 0
  291.             emodem()
  292.             pscreen()
  293.             clearMe()
  294.             print(" ")
  295.             print("This server is")
  296.             print("already running.")
  297.             print(" ")
  298.             return
  299.          end
  300.       end
  301.    end
  302.    if evt == "char" then
  303.       local c = string.lower(arg)
  304.       if c == "q" then
  305.          if off == 0 then
  306.             break
  307.          else
  308.          end
  309.       end
  310.       if c == "e" then
  311.          if theSwitch == 0 then
  312.             theTime = 1
  313.             theSwitch = 1
  314.             off = off + 1
  315.             if off >= 2 then off = 0 end
  316.             emodem()
  317.             pscreen()
  318.          end
  319.       end
  320.       if c == "r" then
  321.          if off == 0 then
  322.             if fs.exists("Ident") then
  323.                fs.delete("Ident")
  324.                isRandom()
  325.                pscreen()
  326.             else
  327.                isRandom()
  328.                pscreen()
  329.             end
  330.          end
  331.       end
  332.    end
  333.    if evt == "mouse_click" or evt == "monitor_touch" then
  334.       menu_options = 0
  335.       setMonScale = 0
  336.       if evt == "monitor_touch" then setMonScale = 1 end
  337.       if xPos >= 3 + setMonScale and xPos <= 14 + setMonScale and yPos >= 7 and yPos <= 9 then
  338.          menu_options = 1
  339.       end
  340.       if xPos >= 16 + (setMonScale / 2) and xPos <= 27 + setMonScale and yPos >= 7 and yPos <= 9 + (setMonScale / 2) then
  341.          menu_options = 2
  342.       end
  343.       if menu_options == 1 and off == 0 then
  344.          break
  345.       else
  346.       end
  347.       if menu_options == 2 then
  348.          off = off + 1
  349.          if off >= 2 then off = 0 end
  350.          emodem()
  351.          pscreen()
  352.       end
  353.    end
  354. end
  355.  
  356. if fs.exists(currPath.."server.lck") then fs.delete(currPath.."server.lck") end
  357.  
  358. clearMe()
  359. print(" ")
  360. print("Program Terminated")
  361. print(" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement