rokkwarr

ServerMaster

Sep 28th, 2012 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. --[[
  2. Scroll Program: Master
  3.  
  4. By: Negeator
  5.  
  6. Instructions:
  7. Don't touch this file! (unless you know what you are doing)
  8. Define all of the slave computer ids in the 'scrollNodes' file.
  9. Put an id on each line, this will be the order they go in.
  10. Define all of the lines of text in the 'scrollFile' file.
  11. Put the speed (higher the number, slower it scrolls, NO DECIMAL VALUES) on one line, and the text on the other.
  12. In case you can't count, there should be two lines in the text file for every line it displays.
  13.  
  14. To Run, run all of the slaves first, then run the master program. If you run the master before one of the slaves, you'll have to stop the master and re-run it.
  15. ]]--
  16. sleep(1) --Make sure slave computers boot first (for logging into a server with computers set to automatically run the program)
  17. local mon
  18. local timeMax
  19. --Arrays Initialized
  20. local nodes = { } --Slave Computer ID's
  21. local text = { } --Current Text for each line of each monitor
  22. local heights = { } --Heights of each monitor
  23. local serverText = { } --Current server text (Feeds one character of this at a time into the Master monitor, and then removes that character. When the string is empty, it is replenished by being reset using storeServerText)
  24. local storeServerText = { } --Stored Server Text (this does not change)
  25. local scrollSpeeds = { } --Time it takes to scroll (1 = fastest, only whole numbers for now)
  26. --[[ Functions ]]--
  27. function main()
  28. --Initialization
  29. getNodeData() --Load Nodes (slave computer id's) from a file
  30. getMessageData() --Load Message (text and speeds for each line) from a file
  31. startModem() --Start Modem
  32. startMon() --Start Monitor
  33. textInit() --Initialize the Text arrays
  34. getServerText() --Initialize serverText values
  35. timeMax = getLongestTime() --Get longest scroll time
  36. local currTime = 0 --Initialize the current time variable
  37. os.startTimer(.01) --Start the timer (currTime will increment ever .01 seconds)
  38. print("Working!")
  39. while true do
  40. drawText()
  41. event = os.pullEvent()
  42. if event == "timer" then
  43. for i = 1, #scrollSpeeds do
  44. if (currTime % scrollSpeeds[i]) == 0 then
  45. updateCharLine(i)
  46. updateText()
  47. end
  48. end
  49.  
  50. currTime = currTime + 1
  51. if currTime > timeMax then
  52. currTime = 0
  53. end
  54.  
  55. os.startTimer(.01)
  56. end
  57. end
  58. end
  59. function getServerText()
  60. for i = 1, #storeServerText do
  61. serverText[i] = storeServerText[i]
  62. end
  63. end
  64. --Update all of the slaves' text
  65. function updateText()
  66. for i = 1, #nodes do
  67. sendText(i)
  68. end
  69. end
  70. --Update text (rows) for a slave
  71. function sendText(index)
  72. local sendID = nodes[index]
  73. rednet.send(sendID,"update")
  74. for i = 1, heights[index + 1] do
  75. if i > #storeServerText then
  76. rednet.send(sendID," ")
  77. else
  78. rednet.send(sendID,text[index + 1][i])
  79. end
  80. end
  81. end
  82. --Initialize 'text' (make all of the rows the appropriate length, etc)
  83. function textInit()
  84. --Add to heights and text for the first monitor (Master)
  85. local length, height = mon.getSize()
  86. table.insert(heights,height)
  87. table.insert(text, { })
  88. for a = 1, #storeServerText do
  89. table.insert(text[1],textCreate(length))
  90. end
  91. print("a")
  92. --Add to heights and text for the rest of the monitors (Slaves)
  93. for i = 1, #nodes do
  94. length = getLength(nodes[i])
  95. height = getHeight(nodes[i])
  96. --Create Slave Height
  97. table.insert(heights,height)
  98. --Initialize text (rows) for Slaves
  99. table.insert(text, { })
  100. for a = 1, #storeServerText do
  101. table.insert(text[#text],textCreate(length))
  102. end
  103. end
  104. end
  105. --Create a blank string based on a specific length
  106. function textCreate(len)
  107. local ret = ""
  108. for i = 1, len do
  109. ret = ret .. " "
  110. end
  111. return ret
  112. end
  113. function updateCharLine(index)
  114. text[1][index] = text[1][index] .. string.sub(serverText[index],1,1)
  115. for i = 2, #text do
  116. text[i][index] = text[i][index] .. string.sub(text[i - 1][index],1,1)
  117. end
  118.  
  119. for i = 1, #text do
  120. text[i][index] = string.sub(text[i][index],2,string.len(text[i][index]))
  121. end
  122.  
  123. serverText[index] = string.sub(serverText[index], 2, string.len(serverText[index]))
  124. if serverText[index] == "" then
  125. serverText[index] = storeServerText[index]
  126. end
  127. end
  128. --Draw text on the Master's monitor
  129. function drawText()
  130. for i = 1, #text[1] do
  131. if i > heights[1] then
  132. return
  133. end
  134. mon.setCursorPos(1,i)
  135. mon.write(text[1][i])
  136. end
  137. end
  138. --Get the length of a slave's monitor
  139. function getLength(index)
  140. -- term.write(1,1,"Waiting on ".. index .." to send")
  141. print("Waiting on ".. index)
  142. rednet.send(index,"length")
  143. while true do
  144. id, message = rednet.receive()
  145. if id == index then
  146. return tonumber(message)
  147. end
  148. end
  149. end
  150. --Get the height of a slave's monitor
  151. function getHeight(index)
  152. rednet.send(index,"height")
  153. while true do
  154. id, message = rednet.receive()
  155. if id == index then
  156. return tonumber(message)
  157. end
  158. end
  159. end
  160. --Start the monitor (detect side, etc)
  161. function startMon()
  162. local monSide
  163. for i=1,#rs.getSides() do
  164. if peripheral.isPresent(rs.getSides()[i]) and peripheral.getType(rs.getSides()[i]) == "monitor" then
  165. monSide = rs.getSides()[i]
  166. mon = peripheral.wrap( monSide )
  167. return
  168. end
  169. end
  170. error()
  171. end
  172. --Start the monitor (detect side, etc)
  173. function startModem()
  174. local modemSide
  175. for i=1,#rs.getSides() do
  176. if peripheral.isPresent(rs.getSides()[i]) and peripheral.getType(rs.getSides()[i]) == "modem" then
  177. modemSide = rs.getSides()[i]
  178. rednet.open(modemSide)
  179. return
  180. end
  181. end
  182. -- error()
  183. end
  184. --Gets the scroll time that is the longest
  185. function getLongestTime()
  186. local max = scrollSpeeds[1]
  187. for i = 2, #scrollSpeeds do
  188. if scrollSpeeds[i] > max then
  189. max = scrollSpeeds[i]
  190. end
  191. end
  192. return max
  193. end
  194. --Load messages and speeds from a file
  195. function getMessageData()
  196. local file, error = io.open("scrollFile", "r")
  197. if error then
  198. return error
  199. end
  200.  
  201. local readText
  202. local boolean = true
  203. local num = 0
  204.  
  205. while boolean do
  206. readText = file:read()
  207. if readText ~= nil then
  208. if num == 0 then
  209. table.insert(scrollSpeeds,tonumber(readText))
  210. num = 1
  211. else
  212. table.insert(serverText,"")
  213. table.insert(storeServerText,readText)
  214. print(" Inserting "..readText)
  215. num = 0
  216. end
  217. else
  218. boolean = false
  219. end
  220. end
  221. file:close()
  222. end
  223. function getNodeData()
  224. local file, error = io.open("scrollNodes", "r")
  225. if error then
  226. return error
  227. end
  228.  
  229. local readText
  230. local boolean = true
  231. local num = 0
  232.  
  233. while boolean do
  234. readText = file:read()
  235. if readText ~= nil then
  236. table.insert(nodes,tonumber(readText))
  237. else
  238. boolean = false
  239. end
  240. end
  241. file:close()
  242. end
  243.  
  244. --[[ Run Program ]]--
  245. main()
Add Comment
Please, Sign In to add comment