Advertisement
Guest User

chat

a guest
May 27th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.89 KB | None | 0 0
  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 nickname = args[1]
  37. local sentHistory = {}
  38. local recHistory = {}
  39. local chat = true
  40. peripheral.find("modem").transmit(2048,1,{["name"] = "Join",["msg"] = "Player "..args[1].." Joined!"})
  41. peripheral.find("modem").closeAll()
  42. peripheral.find("modem").open(2048)
  43. local maxX,maxY = term.getSize()
  44. local function redraw()
  45.   local curX,curY = term.getCursorPos()
  46.   term.setBackgroundColor(colors.black)
  47.   term.clear()
  48.   term.setCursorPos(1,maxY)
  49.   term.setTextColor(colors.lightGray)
  50.   write(": ")
  51.   term.setCursorPos(1,1)
  52.   term.setTextColor(colors.lightBlue)
  53.   for i=1,#recHistory do
  54.     term.setTextColor(colors.lightBlue)
  55.     write(recHistory[i]["name"])
  56.     term.setTextColor(colors.blue)
  57.     write(": ")
  58.     term.setTextColor(colors.white)
  59.     local curMessage = recHistory[i]["msg"]
  60.     local colored = false
  61.     for i=1,#curMessage do
  62.       local curChar = string.sub(curMessage,i,i)
  63.       if curChar == "&" then
  64.         colored = true
  65.       else
  66.         if colored then
  67.           if colorCodes[curChar] ~= nil then
  68.             term.setTextColor(colorCodes[curChar])
  69.           end
  70.           colored = false
  71.         else
  72.           write(curChar)
  73.         end
  74.       end
  75.     end
  76.     print("")
  77.   end
  78.   term.setCursorPos(1,maxY)
  79.   term.setTextColor(colors.lightGray)
  80.   write(":")
  81.   term.setCursorPos(3,maxY)
  82.   term.setTextColor(colors.white)
  83. end
  84. local function rec()
  85.   while chat do
  86.     event,side,to,from,message,distance = os.pullEvent("modem_message")
  87.     if message then
  88.       if message.name and message.msg then
  89.         redraw()
  90.         recHistory[#recHistory + 1] = {}
  91.         recHistory[#recHistory]["name"] = message.name
  92.         recHistory[#recHistory]["msg"] = message.msg
  93.         if #recHistory > maxY then
  94.           recHistory[1] = nil
  95.           local newHist = {}
  96.           for i,v in pairs(recHistory) do
  97.             newHist[i - 1] = v
  98.           end
  99.           recHistory = newHist
  100.         end
  101.         redraw()
  102.       end
  103.     end
  104.   end
  105. end
  106. local function snd()
  107.   while chat do
  108.     redraw()
  109.     local input = read(nil,sentHistory)
  110.     sentHistory[#sentHistory + 1] = input
  111.     local sendTable = {}
  112.     sendTable["name"] = nickname
  113.     sendTable["msg"] = input
  114.     recHistory[#recHistory + 1] = sendTable
  115.     if #recHistory == maxY then
  116.       recHistory[1] = nil
  117.       local newHist = {}
  118.       for i,v in pairs(recHistory) do
  119.         newHist[i - 1] = v
  120.       end
  121.       recHistory = newHist
  122.     end
  123.     peripheral.find("modem").transmit(2048,os.getComputerID(),sendTable)
  124.   end
  125. end
  126. local function termin()
  127.   while chat do
  128.     e = os.pullEventRaw("terminate")
  129.     if e == "terminate" then
  130.       peripheral.find("modem").transmit(2048,1,{["name"] = "Leave",["msg"] = "Player "..nickname.." Has left."})
  131.       chat = false
  132.       peripheral.find("modem").closeAll()
  133.       os.pullEvent = oldPull
  134.       term.setBackgroundColor(colors.black)
  135.       term.clear()
  136.       term.setCursorPos(1,1)
  137.       os.queueEvent("key",28)
  138.       sleep(0.1)
  139.       term.setCursorPos(1,1)
  140.       os.queueEvent("terminate")
  141.       break
  142.     end
  143.   end
  144. end
  145. parallel.waitForAll(snd,rec,termin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement