deljr15

delchat v1.1.0

Dec 8th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. local functionStack = {}
  2. local chat = {}
  3. local users = {}
  4.  
  5. -- config
  6. chat[0] = "Welcome To DelChat"
  7. local bar = "-" --character the header/footer of input section
  8. -- end
  9.  
  10. local hScreen, vScreen = term.getSize()
  11. local update = true
  12. local loc = vScreen - 1
  13. local cache = 1
  14. local devider = bar
  15. local localuser
  16.  
  17. function menu()
  18.     term.clear()
  19.     term.setCursorPos(1, 1)
  20.     print("Enter Your Username:")
  21.     localuser = read()
  22. end
  23.  
  24. function clear()
  25.     for i=2, hScreen do
  26.         devider = devider .. bar
  27.     end
  28.     header = vScreen - 2
  29.     term.clear()
  30.     term.setCursorPos(1, header)
  31.     term.write(devider)
  32.     term.setCursorPos(1, vScreen)
  33.     term.write(devider)
  34.     term.setCursorPos(1, loc)
  35. end
  36.  
  37. function openModem()
  38.     sides = rs.getSides()
  39.     for index, side in pairs(sides) do
  40.        
  41.         if peripheral.isPresent(sides[index]) and peripheral.getType(sides[index]) == "modem" then
  42.             rednet.open(sides[index])
  43.             return
  44.         end
  45.     end
  46.     error("No modem found to perform rednet reception.")
  47. end
  48.  
  49. function getUser(i)
  50.     if users[i] == nil then
  51.         rednet.send(i, "/userRequest")
  52.         id, msg = rednet.receive(10)
  53.        
  54.         if id == nil then
  55.             error("No response from client")
  56.         else
  57.             users[i] = msg
  58.         end
  59.     end
  60.     return users[i]
  61. end
  62.  
  63. function receiveChat()
  64.     while true do
  65.         id, message = rednet.receive()
  66.        
  67.         if message == "/userRequest" then
  68.             rednet.send(id, tostring(localuser))
  69.         else
  70.             uName = getUser(id)
  71.             if uName == nil then
  72.                 chat[cache] = "From " .. id .. ": " .. message
  73.             else
  74.                 chat[cache] = uName .. ": " .. message
  75.             end
  76.             cache = cache + 1
  77.             update = true
  78.         end
  79.     end
  80. end
  81.  
  82. function sendChat()
  83.     while true do
  84.         term.setCursorPos(1, loc)
  85.         term.clearLine()
  86.         send = read()
  87.         rednet.broadcast(send)
  88.         chat[cache] = localuser .. ": " .. send
  89.         cache = cache + 1
  90.         update = true
  91.     end
  92. end
  93.  
  94. function display()
  95.     while true do
  96.         if update == true then
  97.             term.setCursorPos(1, 1)
  98.             screen = vScreen - 4
  99.             line = cache - screen
  100.            
  101.             if line < 0 then
  102.                 line = 0
  103.             end
  104.            
  105.             for i=0, screen do
  106.                 term.clearLine()
  107.                 print(chat[line + i])
  108.             end
  109.            
  110.             term.setCursorPos(1, loc)
  111.             update = false
  112.         else
  113.             sleep(0.25)
  114.         end
  115.     end
  116. end
  117.  
  118. table.insert(functionStack, receiveChat)
  119. table.insert(functionStack, sendChat)
  120. table.insert(functionStack, display)
  121.  
  122. menu()
  123. openModem()
  124. clear()
  125.  
  126. while true do
  127.         parallel.waitForAny(unpack(functionStack))
  128. end
Advertisement
Add Comment
Please, Sign In to add comment