Advertisement
Guest User

test

a guest
Jan 30th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. drive = peripheral.find("drive")
  2. delay = 0.05
  3. tX, tY = term.getSize()
  4. operatorName = "Console"
  5.  
  6. function updateBlink()
  7.  term.setCursorPos(tX,tY)
  8.  if toggle ~= true then
  9.   toggle = true
  10.   term.setTextColor(colors.gray)
  11.   else
  12.   toggle = false
  13.   term.setTextColor(colors.lightGray)
  14.  end
  15.  term.write("X")
  16. end
  17.  
  18. function commandCheck()
  19.  local potCommand = chat[#chat]
  20.  if potCommand == "/clear" then
  21.   chat = {}
  22.   rawChat = {}
  23.   messageOwners = {}
  24.   sendMessage = 2
  25.  elseif string.sub(potCommand,1,1) == "/" then
  26.   sendMSG("[/clear] [/changename <new name>]",true)
  27.   sendMessage = 2
  28.  end
  29. end
  30.  
  31. function waitChar()
  32.  if entered == nil then
  33.   entered = {}
  34.  end
  35.  waitForTimer = false
  36.  enterDelay = os.startTimer(delay)
  37.  event = {os.pullEvent()}
  38.  if event[1] ~= "timer" then
  39.   os.cancelTimer(enterDelay)
  40.  end
  41.  if event[1] == "char" then
  42.   table.insert(entered,event[2])
  43.  elseif event[1] == "key" then
  44.   if event[2] == 28 then -- Enter
  45.    sendMessage = 2
  46.    textToSend = tableToText(entered)
  47.    if string.len(textToSend) > 12 and string.sub(textToSend,1,12) == "/changename " then
  48.     newPCName = string.sub(textToSend,13)
  49.     sendMSG(pcName.." is now: \""..newPCName.."\"",true)
  50.     pcName = newPCName
  51.     os.setComputerLabel(newPCName)    
  52.    else
  53.     sendMSG(textToSend)
  54.    end
  55.    entered = {}
  56.   elseif event[2] == 14 then -- Backspace
  57.    if #entered > 0 then
  58.     table.remove(entered,#entered)
  59.    end
  60.   end
  61.  end
  62.  getMSG()
  63.  commandCheck()
  64.  updateInterface()
  65. end
  66.  
  67. function tableToText(tableDef)
  68.  local add = ""
  69.  for i=1,#tableDef do
  70.   add = add..tableDef[i]
  71.  end
  72.  return add
  73. end
  74.  
  75. function updateInterface()
  76.  if chat ~= oldChat or sendMessage > 0 then
  77.   term.clear()
  78.   for i=1,#chat do
  79.    term.setCursorPos(1,i)
  80.    ownerColor(messageOwners[i])
  81.    term.write(messageOwners[i]..": ")
  82.    term.setTextColor(colors.white)
  83.    term.write(chat[i])
  84.   end
  85.   sendMessage = sendMessage - 1
  86.  end
  87.  oldChat = chat
  88.  term.setCursorPos(1,tY)
  89.  term.setTextColor(colors.green)
  90.  term.write(pcName..": ")
  91.  term.setTextColor(colors.white)
  92.  term.write(tableToText(entered).."  ")
  93.  cX, cY = term.getCursorPos()
  94.  -- updateBlink() -- Enable to see chat-update speed
  95.  term.setCursorPos(cX-2,cY) -- Cursor Blink pos
  96.  term.setCursorBlink(true)
  97. end
  98.  
  99. function ownerColor(owner)
  100.  if owner == operatorName then
  101.  term.setTextColor(colors.lightGray)
  102.  elseif owner ~= pcName then
  103.  term.setTextColor(colors.red)
  104.  else
  105.  term.setTextColor(colors.lime)
  106.  end
  107. end
  108.  
  109. if not drive then
  110.  error("Place this computer next to a drive!")
  111. end
  112.  
  113. function init()
  114.  sendMessage = 0
  115.  term.setBackgroundColor(colors.black)
  116.  term.clear()
  117.  term.setCursorPos(1,1)
  118.  pcName = os.getComputerLabel()
  119.  if not pcName then
  120.   pcName = tostring(os.getComputerID())
  121.  end
  122.  local count = 0
  123.  for k, v in pairs(peripheral.getNames()) do
  124.   if peripheral.getType(v) == "drive" then
  125.    count = count + 1
  126.    driveSide = v
  127.   end
  128.  end
  129.  if count > 1 then
  130.   error("Too many drives!")
  131.  end
  132.  chat = {}
  133.  messageOwners = {}
  134.  rawChat = {}
  135.  sendMSG(pcName.." joined the chat", true)
  136. end
  137.  
  138. function getMSG()
  139.  messageRaw = disk.getLabel(driveSide)
  140.  if messageRaw ~= rawChat[#rawChat] then
  141.   newMSG = true
  142.   sendMessage = 1
  143.   local subStart = string.find(messageRaw,"#$#")
  144.   if subStart then
  145.    message = string.sub(messageRaw,subStart+3)
  146.    messageOwner = string.sub(messageRaw,1,subStart-1)
  147.    table.insert(chat, message)
  148.    table.insert(messageOwners, messageOwner)
  149.    table.insert(rawChat,messageRaw)
  150.    if #chat > tY-3 then
  151.     table.remove(chat,1)
  152.     table.remove(messageOwners, 1)
  153.     table.remove(rawChat, 1)
  154.    end
  155.    else
  156.    sendMSG("Failed to read label, fixed.",true)
  157.   end
  158.   else
  159.   newMSG = false
  160.  end
  161.  return newMSG
  162. end
  163.  
  164. function sendMSG(message, overRide)
  165.  if overRide ~= nil then
  166.   sleep(delay*2)
  167.   disk.setLabel(driveSide,operatorName.."#$#"..message)
  168.   overRide = nil
  169.  else
  170.   disk.setLabel(driveSide,pcName.."#$#"..message)
  171.  end
  172. end
  173.  
  174. init()
  175. while true do
  176.  waitChar()
  177. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement