Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- idea: split off two functions one to listen and one to type
- args = {...}
- STD_PING_CHAN = 5
- STD_REPLY_CHAN = 1
- MODEM_SIDE = "left"
- local listeningChan
- local replyChan
- function setup()
- if #args >= 3 then
- replyChan = args[2]
- listeningChan = args[1]
- else
- if #args == 2 then
- listeningChan = args[1]
- else
- listeningChan = STD_PING_CHAN
- end
- replyChan = STD_REPLY_CHAN
- end
- print("Set up to listen on: " ..tostring(listeningChan).. " and reply on: " ..tostring(replyChan).. ".\n")
- write("Name: ")
- name = read()
- end
- function send(modem)
- term.setTextColor(colors.white)
- write("You: ")
- local input = read()
- if input == "quit" or input == "exit" then
- exitChat(modem)
- end
- modem.transmit(listeningChan, replyChan, name..": "..input)
- receiveReply(modem)
- end
- function receiveReply(modem)
- modem.open(replyChan)
- local event, side, senderChan, replyChan, message, dist = os.pullEvent("modem_message")
- term.setTextColor(colors.lightBlue)
- print("\t\t"..message)
- end
- function cursorToStart(textColor)
- term.setTextColor(textColor)
- term.clearLine()
- x,y = term.getCursorPos()
- term.setCursorPos(1, y)
- end
- function receive(modem)
- modem.open(listeningChan)
- local event, side, senderChan, replyChan, message, dist = os.pullEvent("modem_message")
- cursorToStart(colors.green)
- print(message)
- reply(modem)
- end
- function reply(modem)
- modem.transmit(replyChan, listeningChan, "<.>\n")
- end
- function exitChat(modem)
- print("Exiting chat.\n")
- modem.transmit(listeningChan, replyChan, name.." left the chat.\n")
- error()
- end
- function run()
- local modem = peripheral.wrap(MODEM_SIDE)
- term.clear()
- term.setTextColor(colors.white)
- print("Entered chat. type quit or exit to terminate.\n")
- while true do
- parallel.waitForAny(function()
- send(modem)
- end, function()
- receive(modem)
- end)
- end
- end
- setup()
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement