Advertisement
Guest User

chat remix

a guest
May 27th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local args = {...}
  2. if #args < 1 then
  3.   print("Usage:")
  4.   print(shell.getRunningProgram().." <nickname>")
  5.   return
  6. end
  7. if not peripheral.find("modem") then
  8.   term.setBackgroundColor(colors.black)
  9.   term.clear()
  10.   term.setCursorPos(1,1)
  11.   term.setTextColor(colors.blue)
  12.   print("No Modem Found.")
  13.   return
  14. end
  15. local colorCodes = {
  16.   ["0"] = colors.black,
  17.   ["1"] = colors.blue,
  18.   ["2"] = colors.green,
  19.   ["3"] = colors.cyan,
  20.   ["4"] = colors.red,
  21.   ["5"] = colors.purple,
  22.   ["6"] = colors.orange,
  23.   ["7"] = colors.lightGray,
  24.   ["8"] = colors.gray,
  25.   ["9"] = colors.cyan,
  26.   ["a"] = colors.lime,
  27.   ["b"] = colors.lightBlue,
  28.   ["c"] = colors.red,
  29.   ["d"] = colors.pink,
  30.   ["e"] = colors.yellow,
  31.   ["f"] = colors.white,
  32. }
  33.  
  34. local oldPull = os.pullEvent
  35. os.pullEvent = os.pullEventRaw
  36. local function clearMost()
  37.     scr_x, scr_y = term.getSize()
  38.     for y = 1, scr_y-1 do
  39.         term.setCursorPos(1,y)
  40.         term.clearLine()
  41.     end
  42. end
  43. local nickname = args[1]
  44. local sentHistory = {}
  45. local recHistory = {}
  46. local chat = true
  47. peripheral.find("modem").transmit(2048,1,{["name"] = "Join",["msg"] = "Player "..args[1].." Joined!"})
  48. peripheral.find("modem").closeAll()
  49. peripheral.find("modem").open(2048)
  50. local maxX,maxY = term.getSize()
  51. local function redraw()
  52.   local curX,curY = term.getCursorPos()
  53.   term.setBackgroundColor(colors.black)
  54.   clearMost()
  55.   term.setCursorPos(1,maxY)
  56.   term.setTextColor(colors.lightGray)
  57.   write(": ")
  58.   term.setCursorPos(1,1)
  59.   term.setTextColor(colors.lightBlue)
  60.   for i=1,#recHistory do
  61.     term.setTextColor(colors.lightBlue)
  62.     write(recHistory[i]["name"])
  63.     term.setTextColor(colors.blue)
  64.     write(": ")
  65.     term.setTextColor(colors.white)
  66.     local curMessage = recHistory[i]["msg"]
  67.     local colored = false
  68.     for i=1,#curMessage do
  69.       local curChar = string.sub(curMessage,i,i)
  70.       if curChar == "&" then
  71.         colored = true
  72.       else
  73.         if colored then
  74.           if colorCodes[curChar] ~= nil then
  75.             term.setTextColor(colorCodes[curChar])
  76.           end
  77.           colored = false
  78.         else
  79.           write(curChar)
  80.         end
  81.       end
  82.     end
  83.     print("")
  84.   end
  85.   term.setCursorPos(1,maxY)
  86.   term.setTextColor(colors.lightGray)
  87.   write(":")
  88.   term.setCursorPos(3,maxY)
  89.   term.setTextColor(colors.white)
  90. end
  91. local function rec()
  92.   while chat do
  93.     event,side,to,from,message,distance = os.pullEvent("modem_message")
  94.     if message then
  95.       if message.name and message.msg then
  96.         redraw()
  97.         recHistory[#recHistory + 1] = {}
  98.         recHistory[#recHistory]["name"] = message.name
  99.         recHistory[#recHistory]["msg"] = message.msg
  100.         if #recHistory > maxY then
  101.           recHistory[1] = nil
  102.           local newHist = {}
  103.           for i,v in pairs(recHistory) do
  104.             newHist[i - 1] = v
  105.           end
  106.           recHistory = newHist
  107.         end
  108.         redraw()
  109.       end
  110.     end
  111.   end
  112. end
  113. local function snd()
  114.   while chat do
  115.     redraw()
  116.     local input = read(nil,sentHistory)
  117.     sentHistory[#sentHistory + 1] = input
  118.     local sendTable = {}
  119.     sendTable["name"] = nickname
  120.     sendTable["msg"] = input
  121.     recHistory[#recHistory + 1] = sendTable
  122.     if #recHistory == maxY then
  123.       recHistory[1] = nil
  124.       local newHist = {}
  125.       for i,v in pairs(recHistory) do
  126.         newHist[i - 1] = v
  127.       end
  128.       recHistory = newHist
  129.     end
  130.     peripheral.find("modem").transmit(2048,os.getComputerID(),sendTable)
  131.   end
  132. end
  133. local function termin()
  134.   while chat do
  135.     e = os.pullEventRaw("terminate")
  136.     if e == "terminate" then
  137.       peripheral.find("modem").transmit(2048,1,{["name"] = "Leave",["msg"] = "Player "..nickname.." Has left."})
  138.       chat = false
  139.       peripheral.find("modem").closeAll()
  140.       os.pullEvent = oldPull
  141.       term.setBackgroundColor(colors.black)
  142.       term.clear()
  143.       term.setCursorPos(1,1)
  144.       os.queueEvent("key",28)
  145.       sleep(0.1)
  146.       term.setCursorPos(1,1)
  147.       os.queueEvent("terminate")
  148.       break
  149.     end
  150.   end
  151. end
  152. parallel.waitForAll(snd,rec,termin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement