Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local functionStack = {}
- local chat = {}
- local users = {}
- -- config
- chat[0] = "Welcome To DelChat"
- local bar = "-" --character the header/footer of input section
- -- end
- local hScreen, vScreen = term.getSize()
- local update = true
- local loc = vScreen - 1
- local cache = 1
- local devider = bar
- local localuser
- function menu()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter Your Username:")
- localuser = read()
- end
- function clear()
- for i=2, hScreen do
- devider = devider .. bar
- end
- header = vScreen - 2
- term.clear()
- term.setCursorPos(1, header)
- term.write(devider)
- term.setCursorPos(1, vScreen)
- term.write(devider)
- term.setCursorPos(1, loc)
- end
- function openModem()
- sides = rs.getSides()
- for index, side in pairs(sides) do
- if peripheral.isPresent(sides[index]) and peripheral.getType(sides[index]) == "modem" then
- rednet.open(sides[index])
- return
- end
- end
- error("No modem found to perform rednet reception.")
- end
- function getUser(i)
- if users[i] == nil then
- rednet.send(i, "/userRequest")
- id, msg = rednet.receive(10)
- if id == nil then
- error("No response from client")
- else
- users[i] = msg
- end
- end
- return users[i]
- end
- function receiveChat()
- while true do
- id, message = rednet.receive()
- if message == "/userRequest" then
- rednet.send(id, tostring(localuser))
- else
- uName = getUser(id)
- if uName == nil then
- chat[cache] = "From " .. id .. ": " .. message
- else
- chat[cache] = uName .. ": " .. message
- end
- cache = cache + 1
- update = true
- end
- end
- end
- function sendChat()
- while true do
- term.setCursorPos(1, loc)
- term.clearLine()
- send = read()
- rednet.broadcast(send)
- chat[cache] = localuser .. ": " .. send
- cache = cache + 1
- update = true
- end
- end
- function display()
- while true do
- if update == true then
- term.setCursorPos(1, 1)
- screen = vScreen - 4
- line = cache - screen
- if line < 0 then
- line = 0
- end
- for i=0, screen do
- term.clearLine()
- print(chat[line + i])
- end
- term.setCursorPos(1, loc)
- update = false
- else
- sleep(0.25)
- end
- end
- end
- table.insert(functionStack, receiveChat)
- table.insert(functionStack, sendChat)
- table.insert(functionStack, display)
- menu()
- openModem()
- clear()
- while true do
- parallel.waitForAny(unpack(functionStack))
- end
Advertisement
Add Comment
Please, Sign In to add comment