MudkipTheEpic

RedIntercept Server

May 24th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. local getNumber, getCpuNum, getModem, getMonitor, updateStatus, writeToLog
  2.  
  3. function getModem(isWireless)
  4. for k,v in pairs(rs.getSides()) do
  5. if peripheral.getType(v)=="modem" and peripheral.call(v,"isWireless")==isWireless then return v end
  6. end
  7. return false
  8. end
  9.  
  10. function getMonitor()
  11. for k,v in pairs(rs.getSides()) do
  12. if peripheral.getType(v)=="monitor" then return v end
  13. end
  14. return false
  15. end
  16. local sWire=getModem(false)
  17. local sMon=getMonitor()
  18. if not sWire or not sMon then error("Needs wired modem and monitor.") end
  19. term.clear()
  20. term.setCursorPos(1,1)
  21. local pWire=peripheral.wrap(sWire)
  22. local pMon=peripheral.wrap(sMon)
  23. function callRemote(call)
  24. local function cake()
  25. callRemote("shutdown")
  26. callRemote("turnOn")
  27. end
  28. if call=="reboot" then return cake() end
  29. for k,v in pairs(pWire.getNamesRemote()) do
  30. if pWire.getTypeRemote(v)=="computer" then
  31. local numz=((getNumber()~=0 and getNumber()+1) or 0)
  32. pWire.callRemote(v,call)
  33. if call~="shutdown" then
  34. sleep(0.2)
  35. pWire.transmit(1234,1234,numz)
  36. sleep(0.05)
  37. end
  38. end
  39. end
  40. end
  41. local function centerPrint(text, ny)
  42. if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
  43. else
  44. local x, y = term.getCursorPos()
  45. local w, h = term.getSize()
  46. term.setCursorPos(w/2 - text:len()/2 + (#text % 2 == 0 and 1 or 0), ny or y)
  47. print(text)
  48. end
  49. end
  50. function updateStatus(stat,line)
  51. line=line or 10
  52. term.setCursorPos(1,line)
  53. term.clearLine()
  54. centerPrint(stat,line)
  55. end
  56. function getNumber()
  57. return (getCpuNum()>0 and (getCpuNum()*128)-1) or 0
  58. end
  59. function getCpuNum()
  60. local i=0
  61. for k,v in pairs(peripheral.getNames()) do
  62. if peripheral.getType(v)=="computer" and peripheral.call(v,"getID") then
  63. i=i+1
  64. end
  65. end
  66. return i
  67. end
  68.  
  69. function writeToLog(...)
  70. local args={...}
  71. local string=table.concat(args)
  72. local f=fs.open("log","a")
  73. f.writeLine(string)
  74. f.close()
  75. end
  76.  
  77. pMon.clear()
  78. pMon.setCursorPos(1,1)
  79. pWire.closeAll()
  80. pWire.open(1337)
  81. pWire.closeAll()
  82. pWire.open(9001)
  83. pWire.open(1337)
  84. pWire.open(1301)
  85. callRemote("reboot")
  86. local num=getNumber()
  87. if num<0 then num=0 end
  88. updateStatus("RedIntercept Server CPU",1)
  89. updateStatus("S for shutdown",2)
  90. updateStatus("R for reboot",3)
  91. updateStatus("T for turn on",4)
  92. updateStatus("E to exit",5)
  93. local isAlive=true
  94. while true do
  95. num=getNumber()
  96. updateStatus("Live Computers: "..getCpuNum(),6)
  97. updateStatus("Channel Range: 0 - "..num,7)
  98. local e={os.pullEventRaw()}
  99. if e[1]=="char" then
  100. if e[2]=="s" then
  101. updateStatus("Shutting down computers.")
  102. updateStatus("Shutting down",9)
  103. callRemote("shutdown")
  104. updateStatus("Shut down",9)
  105. isAlive=false
  106. num=getNumber()
  107. elseif e[2]=="r" then
  108. updateStatus("Rebooting computers.")
  109. updateStatus("Rebooting",9)
  110. callRemote("reboot")
  111. updateStatus("Online",9)
  112. num=getNumber()
  113. isAlive=true
  114. elseif e[2]=="t" then
  115. updateStatus("Booting computers.")
  116. updateStatus("Booting",9)
  117. callRemote("turnOn")
  118. updateStatus("Online",9)
  119. isAlive=true
  120. num=getNumber()
  121. elseif e[2]=="e" then
  122. term.clear()
  123. term.setCursorPos(1,1)
  124. centerPrint("Thanks for using RedIntercept",1)
  125. centerPrint("By Mudkip",2)
  126. return
  127. end
  128. elseif e[1]=="modem_message" then
  129. if e[3]==9001 then
  130. local tMessage=textutils.unserialize(e[5])
  131. term.redirect(pMon)
  132. print("Channel: "..tMessage[1])
  133. writeToLog("Channel: "..tMessage[1])
  134. print("ReplyChannel: "..tMessage[2])
  135. writeToLog("ReplyChannel: "..tMessage[2])
  136. print("Message:")
  137. writeToLog("Message:")
  138. print(tMessage[3])
  139. writeToLog(tMessage[3])
  140. print("Distance: "..tostring(tMessage[4]))
  141. writeToLog("Distance: "..tostring(tMessage[4]))
  142. print("--End of Transmission--")
  143. writeToLog("-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-")
  144. term.restore()
  145. elseif e[3]==1301 then
  146. pWire.transmit(1234,1234,((getNumber()~=0 and getNumber()-127) or 0))
  147. end
  148. end
  149. end
Advertisement
Add Comment
Please, Sign In to add comment