Maxstripe

glasschat3server

Nov 6th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. -- GlassChat 3 Server
  2. -- By Tiiger87 and Alexandrov01
  3.  
  4. --Checks if computer is an advanced computer
  5. if term.isColor() == false then
  6. print("ERROR: Not an advanced computer! Please check your computer setup.")
  7. error()
  8. end
  9.  
  10. --Gets terminal bridge and wired modem side
  11. sides = {"top", "bottom", "left", "right", "front", "back"}
  12. for key1, value1 in pairs(sides) do
  13. if peripheral.getType(value1)=="modem" then
  14. serverM = peripheral.wrap(value1)
  15. rednet.open(value1)
  16. end
  17. end
  18.  
  19. -- Checks if modem and bridge are present.
  20. if serverM == nil then
  21. term.setTextColor(colors.red)
  22. print("ERROR: Missing modem! Please check your computer setup.")
  23. term.setTextColor(colors.white)
  24. error()
  25. end
  26.  
  27. -- Get monitor ID from file
  28. if fs.exists("data/monitor") then
  29. monn = fs.open("data/monitor", "r")
  30. serverMonitorIsSet = true
  31. monID = monn.readAll()
  32. monID = tonumber(monID)
  33. monn.close()
  34. if monID == "" then
  35. term.setTextColor(colors.red)
  36. print("ERROR: Empty monitor ID in data/monitor!")
  37. term.setTextColor(colors.white)
  38. error()
  39. end
  40. else
  41. serverMonitorIsSet = false
  42. term.setTextColor(colors.orange)
  43. print("WARNING: Missing monitor ID file in data/monitor!")
  44. term.setTextColor(colors.white)
  45. end
  46.  
  47. -- Variables
  48. serverV = "3.0.0 BETA";
  49. serverID = os.getComputerID()
  50.  
  51. names = {}
  52. if fs.exists("data/names") then
  53. file3 = fs.open("data/names","r")
  54. snames = file3.readAll()
  55. if snames == "" then
  56. names = {}
  57. else
  58. names = textutils.unserialize(snames)
  59. end
  60. file3.close()
  61. end
  62.  
  63. -- Print basic info to screen
  64. term.clear()
  65. term.setCursorPos(1,1)
  66. term.setTextColor(colors.yellow)
  67. print("GlassChat 3 Server "..serverV)
  68. term.setTextColor(colors.white)
  69. print("ID: "..serverID)
  70. print("U: Update all clients.")
  71. print("R: Restart all clients.")
  72. print("S: Update server.")
  73. print("K: Kill server.")
  74. print("----------------------")
  75.  
  76. -- Count conntected users/clients
  77. function count(input)
  78. if type(input)~='table' then
  79. return 'unknown'
  80. end
  81. local int=0
  82. for k,v in pairs(input) do
  83. int=int+1
  84. end
  85. return int
  86. end
  87.  
  88. -- Send status of server to monitor (if any)
  89. --[[if serverMonitorIsSet == true then
  90. print("Sending messages to monitor...")
  91. rednet.send(monID, "!sysmsg to-monitor status=OK")
  92. sleep(1)
  93. rednet.send(monID, "!sysmsg to-monitor users="..connectedUsersCount)
  94. sleep(3)
  95. print("Waiting for monitor response...")
  96. waitForMonitor = ""
  97. while waitForMonitor == "" do
  98. print("Waiting...")
  99. id, msg = rednet.receive()
  100. if id == monID and msg == "Monitor-OK" then
  101. print("Monitor ready!")
  102. waitForMonitor = true
  103. end
  104. end
  105. else
  106.  
  107. end]]--
  108.  
  109. -- Broadcast status of server to all clients
  110. rednet.broadcast("!sysmsg Chatroom is back online!")
  111. sleep(0.1)
  112. rednet.broadcast("!sysmsg "..count(names).." connected users.")
  113.  
  114.  
  115. -- Functions
  116.  
  117. -- Client Requests
  118. function clientRequests()
  119. while true do
  120. id, msg = rednet.receive()
  121. if string.len(msg) >= 1 then
  122. if string.match(msg, '^!gc') then
  123. if string.match(msg, '^!gc username') then
  124. names[id] = string.sub(msg, 14)
  125. rednet.broadcast("!sysmsg "..names[id].." (ID "..id..") joined the chat.")
  126. print(names[id].."("..id..") joined.")
  127. file2 = fs.open("data/names","w")
  128. file2.write( textutils.serialize( names ) )
  129. file2.close()
  130.  
  131. elseif string.match(msg, '^!gc newusername') then
  132. newname = string.sub(msg, 17)
  133. rednet.broadcast("!sysmsg "..names[id].." has changed his/her name to "..newname..".")
  134. print(names[id].." has changed his/her name to "..newname..".")
  135. names[id] = newname
  136. file2 = fs.open("data/names","w")
  137. file2.write( textutils.serialize( names ) )
  138. file2.close()
  139.  
  140. elseif string.match(msg, '^!gc leaving') then
  141. print(names[id].."("..id..") has left.")
  142. rednet.broadcast("!sysmsg "..names[id].." has left the chat.")
  143.  
  144.  
  145. elseif string.match(msg, '^!gc updating') then
  146. print(names[id].."("..id..") is updating.")
  147. rednet.broadcast("!sysmsg "..names[id].." has left the chat (updating).")
  148. else
  149.  
  150. end
  151. elseif string.len( names[id] ) >= 1 then
  152. print(names[id].."("..id..") - "..msg)
  153. rednet.broadcast(names[id]..": "..msg)
  154. end
  155. end
  156. end
  157. end
  158.  
  159. -- Server key commands
  160. function serverCommands()
  161. while true do
  162. local evt, c = os.pullEvent("char") -- wait for a key press
  163. c = string.lower(c) -- convert to lower case, to register both r & R.
  164. if c == "r" then
  165. print("Server - R pressed. Restarting all clients.")
  166. rednet.broadcast("!gc reboot")
  167. sleep(0.1) -- Delay to stop all computers from connecting at the same time
  168.  
  169. elseif c == "u" then
  170. print("Server - U pressed. Updating all clients.")
  171. rednet.broadcast("!gc update")
  172. sleep(0.1) -- Delay to stop all computers from connecting at the same time
  173.  
  174. elseif c == "s" then
  175. print("Server - S pressed. Updating and Rebooting server.")
  176. rednet.broadcast("!sysmsg Server is updating and restarting.")
  177. shell.run("update")
  178.  
  179. elseif c == "k" then
  180. print("Server - K pressed. Killing server.")
  181. for i=5, 1, -1 do
  182. rednet.broadcast("!sysmsg Server is going down in "..i)
  183. sleep(1)
  184. end
  185. rednet.broadcast("!sysmsg Server has been shut down.")
  186. error()
  187. end
  188. sleep(0)
  189. end
  190. end
  191.  
  192. -- Wait for any...
  193. parallel.waitForAny(clientRequests, serverCommands)
Add Comment
Please, Sign In to add comment