Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local user_use = "x"
- local userList = {}
- local msgList = {}
- local ix = false
- shell.run("delete caAPI")
- shell.run("pastebin get EDLdR1nF caAPI")
- os.loadAPI("caAPI")
- local monitor_number = caAPI.get_hardware("monitor")
- local found = fs.exists("config/monitor.cfg")
- if found == true then
- file = fs.open("config/monitor.cfg","r")
- local fileData = {}
- local line = file.readLine()
- repeat
- table.insert(fileData,line)
- line = file.readLine()
- until line == nil
- file.close()
- monitor_number = fileData[1]
- end
- local mon = peripheral.wrap(monitor_number)
- os.loadAPI("caAPI")
- local ModemSide = caAPI.detectDivice("modem")
- rednet.open(ModemSide)
- --draw screen
- function draw_screen(userw)
- mon.setBackgroundColor(colors.white)
- mon.clear()
- mon.setBackgroundColor(colors.white)
- mon.setCursorPos(1,1)
- mon.setBackgroundColor(colors.blue)
- mon.write("NetChat 1.0 ")
- if userw ~= "new" then
- mon.setCursorPos(20,1)
- mon.write("Login as "..userw)
- end
- mon.setCursorPos(16,26)
- mon.write("Chat: ")
- mon.setCursorPos(50,1)
- mon.setBackgroundColor(colors.red)
- mon.setTextColor(colors.black)
- mon.write("X")
- local startline = 2
- mon.setBackgroundColor(colors.gray)
- for x=1,25 do
- mon.setCursorPos(1,startline)
- mon.write(" ")
- startline = startline + 1
- end
- end
- --set user name--
- function user_set()
- mon.setCursorPos(20,15)
- mon.setBackgroundColor(colors.red)
- mon.write("UserName: ")
- mon.setCursorPos(29,15)
- local user_name = read()
- mon.setCursorPos(29,15)
- mon.write(user_name)
- sleep(3)
- send("us:"..user_name)
- draw_screen(user_name)
- user_use = user_name
- touch()
- end
- --inputs touch--
- function touch()
- event,side,x,y = os.pullEvent()
- if event == "monitor_touch" then
- if x == 50 and y == 1 then
- local sdaa = "logoff:"..user_use
- send(sdaa)
- sleep(0.5)
- ix = true
- end
- if x > 16 and x < 50 and y == 26 then
- mon.setCursorPos(21,26)
- mon.setBackgroundColor(colors.blue)
- mon.write(" ")
- mon.setCursorPos(21,26)
- local chat = read()
- local send_chat = "msg:"..user_use..":"..chat
- write_text(user_use..":"..chat)
- send(send_chat)
- draw_screen(user_use)
- refresh_text()
- refresh_user()
- end
- end
- if ix == true then
- rednet.close(ModemSide)
- shell.run("clear")
- else
- red_in()
- end
- end
- --rednet input--
- function red_in()
- local id,msg = rednet.receive()
- local inpT = {}
- inpT = caAPI.split(msg,":")
- if inpT[1] == "us" then
- write_user(inpT[2])
- end
- if inpT[1] == "msg" then
- write_text(inpT[2]..":"..inpT[3])
- local found = caAPI.in_table(userList,inpT[2])
- if found == false then
- write_user(inpT[2])
- end
- end
- touch()
- end
- -- write text--
- function write_text(msgU)
- if #msgList > 24 then
- msgList = {}
- end
- table.insert(msgList,msgU)
- local startMline = 2
- for x=1,#msgList do
- mon.setCursorPos(16,startMline)
- mon.setBackgroundColor(colors.white)
- mon.write(msgList[x])
- startMline = startMline + 1
- end
- end
- --write user--
- function write_user(nameU)
- table.insert(userList,nameU)
- local startUline = 2
- for x=1,#userList do
- mon.setCursorPos(1,startUline)
- mon.setBackgroundColor(colors.gray)
- mon.write(userList[x])
- startUline = startUline + 1
- end
- end
- --refresh text--
- function refresh_text()
- local startMline = 2
- for x=1,#msgList do
- mon.setCursorPos(16,startMline)
- mon.setBackgroundColor(colors.white)
- mon.write(msgList[x])
- startMline = startMline + 1
- end
- end
- --refresh user--
- function refresh_user()
- local startUline = 2
- for x=1,#userList do
- mon.setCursorPos(1,startUline)
- mon.setBackgroundColor(colors.gray)
- mon.write(userList[x])
- startUline = startUline + 1
- end
- end
- -- send --
- function send(sda)
- rednet.broadcast(sda)
- end
- --program
- draw_screen("new")
- user_set()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement