Advertisement
gpgautier

RedNet Chat No Monitor

Nov 1st, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.53 KB | None | 0 0
  1. -- [ --------------------------------------------------------------------- ] --
  2. -- [ RedNetChat                                                           ] --
  3. -- [ A public chat client for ComputerCraft                                ] --
  4. -- [ Created by GPG Incorporated.                                          ] --
  5. -- [ --------------------------------------------------------------------- ] --
  6.  
  7. -- [ --------------------------------------------------------------------- ] --
  8.  
  9. local VERSION = "0.3 beta"
  10. local MODEM = nil
  11. local NICKNAME = nil
  12. local ACTIVE = false
  13. local BUFFER = {}
  14. local POINTER = 0
  15. local ONLINE = {}
  16. local ISONLINE = false
  17.  
  18. -- [ --------------------------------------------------------------------- ] --
  19.  
  20. -- Application entry
  21. function main()
  22.   term.clear()
  23.   term.setCursorPos(1, 1)
  24.   print("Starting RedNetChat " .. VERSION)
  25.  
  26.   --sleep(0.5)
  27.  
  28.   if not setPeripherals() then
  29.     return false
  30.   end
  31.  
  32.   print("")
  33.   print("")
  34.   print("Peripherals are setup.")
  35.   print("")
  36.   term.write("Starting application")
  37.  
  38.   --simulateLoading(8)
  39.  
  40.   welcome()
  41. end
  42.  
  43. -- Simulate a loading effect
  44. function simulateLoading(amount)
  45.   local i
  46.  
  47.   sleep(0.2)
  48.   term.write(" ")
  49.   for i = 0, amount do
  50.     term.write(".")
  51.     sleep(0.2)
  52.   end
  53.   term.write " Done"
  54. end
  55.  
  56. -- Set the attached peripherals. Opens rednet modem and warps monitor
  57. function setPeripherals()
  58.   local i, side
  59.  
  60.   for i, side in pairs(redstone.getSides()) do
  61.     if peripheral.isPresent(side) then
  62.       if peripheral.getType(side) == "modem" then
  63.         MODEM = side
  64.         print("")
  65.         term.write("Opening modem on " .. side)
  66.         rednet.open(MODEM)
  67.         --simulateLoading(6)
  68.       end
  69.     end
  70.   end
  71.  
  72.   -- Exit with a fatal error when modem not found
  73.   if MODEM == nil then
  74.     print("[FATAL ERROR] No modem was detected. Plase attach a modem on any side.")
  75.     return false
  76.   end
  77.  
  78.   return true
  79. end
  80.  
  81. -- Start the welcome screen
  82. function welcome()
  83.   local x, y
  84.  
  85.   term.clear()
  86.   writeHeader()
  87.   print("")
  88.   print("")
  89.   print("Enter a nickname and press [enter].")
  90.   print("")
  91.   term.write("Nickname: ")
  92.  
  93.   x, y = term.getCursorPos()
  94.  
  95.   while NICKNAME == nil or NICKNAME == "" do
  96.     term.setCursorPos(x, y)
  97.     NICKNAME = read()
  98.     execute("/online")
  99.     appendBuffer("[RedNet Chat]: Type /help for a list of commands")
  100.   end
  101.  
  102.   start()
  103. end
  104.  
  105. -- Writes the screen header
  106. function writeHeader()
  107.   term.setCursorPos(1, 1)
  108.   term.write("RedNet Chat " .. VERSION .. "           by GPG Incorporated")
  109.   term.setCursorPos(1, 2)
  110.   term.write("--------------------------------------------------")
  111. end
  112.  
  113. -- Writes the list of online users
  114. function writeOnlineList()
  115.   local i, v, count, x, y
  116.  
  117.   count = 0
  118.  
  119.   x, y = term.getCursorPos()
  120.  
  121.   term.setCursorPos(1, 17)
  122.   term.write("--------------------------------------------------")
  123.   term.setCursorPos(1, 18)
  124.   term.clearLine()
  125.   term.write("Online: ")
  126.  
  127.   for i, v in ipairs(ONLINE) do
  128.     if i == 1 then
  129.       term.write(ONLINE[i])
  130.     else
  131.       term.write(", " .. ONLINE[i])
  132.     end
  133.  
  134.     count = count + 1
  135.   end
  136.  
  137.   if count == 0 then
  138.     term.write("There is nobody online at this moment")
  139.   end
  140.  
  141.   term.setCursorPos(x, y)
  142. end
  143.  
  144. -- Start the chat
  145. function start()
  146.   term.clear()
  147.   writeHeader()
  148.   writeOnlineList()
  149.  
  150.   ACTIVE = true
  151.  
  152.   showBuffer()
  153.  
  154.   parallel.waitForAll(input, watchEvents, receive)
  155. end
  156.  
  157. -- Stop the application
  158. function stop()
  159.   ACTIVE = false
  160. end
  161.  
  162. -- Reset the application
  163. function reset()
  164.   execute("/offline")
  165.   rednet.close(MODEM)
  166.   sleep(2)
  167.   os.reboot()
  168. end
  169.  
  170. -- Watch all input to provide possible shortcuts (for example usernames)
  171. function watchEvents()
  172.   local type, param, param2, param3, i, v
  173.  
  174.   while ACTIVE do
  175.     type, param, param2, param3 = os.pullEvent()
  176.   end
  177. end
  178.  
  179. -- Handle input from the prompt
  180. function input()
  181.   local message
  182.  
  183.   term.setCursorPos(1, 4)
  184.   term.write("--------------------------------------------------")
  185.  
  186.   while ACTIVE do
  187.     term.setCursorPos(1, 3)
  188.     term.clearLine()
  189.     term.write("Send > ")
  190.  
  191.     message = read()
  192.  
  193.     if message ~= nil and message ~= "" then
  194.       execute(message, "local")
  195.     end
  196.   end
  197. end
  198.  
  199. -- Send a message
  200. function send(message)
  201.   local request = {protocol = "rnc", nickname = NICKNAME, message = message}
  202.   rednet.broadcast(textutils.serialize(request))
  203. end
  204.  
  205. -- Recieve a message
  206. function receive()
  207.   local sender, message, distance, request
  208.  
  209.   while ACTIVE do
  210.     sender, message, distance = rednet.receive(1)
  211.  
  212.     if message ~= nil and message ~= "" then
  213.       request = textutils.unserialize(message)
  214.       if request.protocol == "rnc" then
  215.         if request.nickname ~= nil and request.nickname ~= "" then
  216.           execute(request, "remote")
  217.         end
  218.       end
  219.     end
  220.   end
  221. end
  222.  
  223. -- Execute a command or add a chat message
  224. function execute(message, source)
  225.   local command
  226.  
  227.   if message.nickname ~= nil then
  228.     executeRemote(message)
  229.     return
  230.   end
  231.  
  232.   if message:sub(0, 1) == "/" then
  233.       command = message:sub(2)
  234.  
  235.       if command == "quit"
  236.           or command == "reset"
  237.           or command == "restart"
  238.           or command == "reboot"
  239.           or command == "stop"
  240.         then
  241.           appendBuffer("[RedNet Chat]: Restarting application")
  242.           reset()
  243.       elseif command == "online" then
  244.         if not ISONLINE then
  245.           send("/online")
  246.           putOnline()
  247.           appendBuffer("[RedNet Chat]: You are now online")
  248.           ISONLINE = true
  249.         else
  250.           appendBuffer("[RedNet Chat]: You are already online")
  251.         end
  252.       elseif command == "offline" then
  253.         if ISONLINE then
  254.           send("/offline")
  255.           takeOffline()
  256.           appendBuffer("[RedNet Chat]: You are now offline")
  257.           ISONLINE = false
  258.         else
  259.           appendBuffer("[RedNet Chat]: You are already offline")
  260.         end
  261.       elseif command:sub(0, 5) == "nick " then
  262.         takeOffline()
  263.         NICKNAME = command:sub(6)
  264.         putOnline()
  265.         appendBuffer("[RedNet Chat]: Your nickname has been changed")
  266.       elseif command:sub(0, 5) == "slap " then
  267.         appendBuffer(command:sub(6) .. " was slapped by " .. NICKNAME)
  268.       elseif command == "help" then
  269.         appendBuffer("[RedNet Chat] Commands:")
  270.         appendBuffer("/quit : Exit the chat")
  271.       end
  272.      
  273.       return
  274.   end
  275.  
  276.   appendBuffer(NICKNAME .. ": " .. message)
  277.   send(message)
  278. end
  279.  
  280. --
  281. function putOnline(nickname)
  282.   local i, v, exists
  283.  
  284.   if nickname == nil then
  285.     nickname = NICKNAME
  286.   end
  287.  
  288.   for i, v in ipairs(ONLINE) do
  289.     if ONLINE[i] == nickname then
  290.       exists = true
  291.     end
  292.   end
  293.  
  294.   if not exists then
  295.     table.insert(ONLINE, nickname)
  296.   end
  297.  
  298.   writeOnlineList()
  299. end
  300.  
  301. --
  302. function takeOffline(nickname)
  303.   local i, v
  304.  
  305.   if nickname == nil then
  306.     nickname = NICKNAME
  307.   end
  308.  
  309.   for i, v in ipairs(ONLINE) do
  310.     if ONLINE[i] == nickname then
  311.       table.remove(ONLINE, i)
  312.     end
  313.   end
  314.  
  315.   writeOnlineList()
  316. end
  317.  
  318. --
  319. function executeRemote(request)
  320.   local command
  321.  
  322.   if request.message:sub(0, 1) == "/" then
  323.     command = request.message:sub(2)
  324.  
  325.     if command == "online" then
  326.       putOnline(request.nickname)
  327.       appendBuffer("[RedNet Chat]: " .. request.nickname .. " is now online")
  328.       send("/metoo")
  329.     elseif command == "offline" then
  330.       takeOffline(request.nickname)
  331.       appendBuffer("[RedNet Chat]: " .. request.nickname .. " is now offline")
  332.     elseif command == "metoo" then
  333.       putOnline(request.nickname)
  334.     end
  335.     return
  336.   end
  337.  
  338.   appendBuffer(request.nickname .. ": " .. request.message)
  339. end
  340.  
  341. --
  342. function appendBuffer(message)
  343.   local length
  344.  
  345.   length = message:len()
  346.  
  347.   if length > 50 then
  348.     table.insert(BUFFER, message:sub(1, 50))
  349.     POINTER = POINTER + 1
  350.     appendBuffer(message:sub(51))
  351.   else
  352.     table.insert(BUFFER, message)
  353.     POINTER = POINTER + 1
  354.   end
  355.  
  356.   showBuffer()
  357. end
  358.  
  359. --
  360. function showBuffer()
  361.   local i, line, bufferPointer, x, y
  362.  
  363.   if POINTER == 0 then
  364.     return
  365.   end
  366.  
  367.   x, y = term.getCursorPos()
  368.  
  369.   line = 5
  370.  
  371.   bufferPointer = -(11 - POINTER)
  372.  
  373.   for i = bufferPointer, bufferPointer + 11 do
  374.     term.setCursorPos(1, line)
  375.     term.clearLine()
  376.    
  377.     if BUFFER[i] ~= nil then
  378.       term.write(tostring(BUFFER[i]))
  379.     end
  380.    
  381.     line = line + 1
  382.   end
  383.  
  384.   term.setCursorPos(x, y)
  385. end
  386.  
  387. -- [ --------------------------------------------------------------------- ] --
  388.  
  389. -- Fire up the application
  390. --welcome()
  391. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement