This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kreezxil's profile picture

Need Help With Advanced Chat Monitor

Started by kreezxil, 16 September 2013 - 09:29 PM
kreezxil #1
Posted 16 September 2013 - 11:29 PM
Ok, it's fixed now and the updated code is below. I guess we need this moved to programs.

I am working on a program that interfaces with a chatbox. It currently logs chat fine but what it doesn't do is scrollback or even possibly down.

If someone could give it a whack and tell me what is did that is making it not work right, I'd greatly appreciate it.

Thanks in Advance. :)/>


pastebin code: DEhS9BXJ
pastebin link: http://pastebin.com/DEhS9BXJ

turtlescripts: market get gjdhr1 chat y
turtlescripts link: http://turtlescripts.com/project/gjdhid-Advanced-Chat-Box

*UPDATED: 9/17/2013
Spoiler
--[[
Name: Advanced Chat Box
Description: Monitor chat on your server or in predefined of your chatbox.
Usage: chat <chatbox side> <lines of history to keep>
]]

local chat = {}
local maxX, maxY = term.getSize()
local position = 1

function cls()
    term.clear()
    term.setCursorPos(1,1)
end

function updateScrn()
  cls()
  for i=position,position+maxY-1 do -- -1 to prevent writing on last line
      if chat[i] then
          print(chat[i])
      end
  end
  term.setCursorPos(maxX-7,maxY)
  term.write("down up")
end

args = {...}

if #args < 2 then
    print ("Usage:")
    print ("chat <side> <history>")
    print()
    print("Where <side> is the position of the chatbox in relation to the computer and <history> is the amount of chat lines to buffer.")
    return --exit the program because the user failed
end

local side,lines = args[1],tonumber(args[2])

_ = peripheral.wrap(side)

cls()
while true do
    -- update screen
    updateScrn()

    local event, p1, p2, p3 = os.pullEvent()

    if event == "chat" then
      table.insert(chat,"<".. p1 .."> " .. p2) -- add it to the chat table

      if #chat > lines then
          -- limit size of history
          table.remove(chat,1) -- removes the first entry
      end

      if #chat > maxY - 1 then -- only update position if the screen is not full
        position = position + 1
      end

    elseif event == "monitor_touch" then

      x,y=tonumber(p2),tonumber(p3)

      if y == maxY then -- first test for y (row)

            if x <= maxX and x >= maxX - 1 then -- up was clicked
              position = position -1
            elseif x <= maxX - 3 and x >= maxX - 7 then -- down was clicked
              position = position + 1
            end

      end

    end

    if position < 0 then
      position = 0
    elseif position > lines then
      position = lines
    elseif position > #chat then
      position = #chat
    end

end

It requires an advanced monitor, advanced computer and miscperipheral's chatbox. To run it use monitor <monitor side> chat <chatbox side> <lines of history to keep>.
Edited on 17 September 2013 - 06:12 AM
Lyqyd #2
Posted 17 September 2013 - 01:47 AM
It doesn't move down because you have "positon" instead of "position". And your click detection bounds are off, you should have -1, -3 and -7 as your offsets rather than -2, -4 and -8. Try clicking on the n versus the w when you get it working and you'll see.
kreezxil #3
Posted 17 September 2013 - 07:15 AM
thanks Lyqyd. :)/>