Advertisement
Kouksi44

CC simple chat

Aug 11th, 2014
2,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.35 KB | None | 0 0
  1. local username="";
  2.  
  3. local curPos="1"
  4. local channel="0"
  5. local x,y=term.getSize()
  6. local xzentral=x/2
  7. local yzentral=y/2
  8.  
  9. function joinChannel()
  10. term.clear()
  11. term.setCursorPos(xzentral-4,yzentral)
  12. term.setBackgroundColor(colors.white)
  13. term.setTextColor(colors.black)
  14. textutils.slowWrite("Join channel: ")
  15. channel=read()
  16.  
  17. if channel=="" then term.setCursorPos(12,10) term.clearLine() print("Error: Please enter a Channelname ! ") term.setBackgroundColor(colors.black)
  18. term.setTextColor(colors.white)os.sleep(1) joinChannel() else
  19. term.setBackgroundColor(colors.black)
  20. term.setTextColor(colors.white)
  21. term.setCursorPos(xzentral-4,yzentral)
  22. term.clearLine()
  23. term.setBackgroundColor(colors.white)
  24. term.setTextColor(colors.black)
  25. term.setCursorPos(xzentral-4,yzentral)
  26. textutils.slowWrite("Joining channel...")
  27. os.sleep(1)
  28. rednet.broadcast("*** "..username.." joined the chat ***",channel)
  29. term.setBackgroundColor(colors.black)
  30. term.setTextColor(colors.white)
  31.  
  32. return
  33.  
  34. end
  35. end
  36.  
  37.  
  38.  
  39.  
  40.  
  41. function menu()
  42. fs.delete("/chatfolder")
  43. term.clear()
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.         if curPos=="1" then
  51.  
  52.                 term.clear()
  53.                 term.setCursorPos(xzentral-4,yzentral-1)
  54.                 term.setBackgroundColor(colors.white)
  55.                 term.setTextColor(colors.black)
  56.                 print("Join channel")
  57.                 term.setCursorPos(xzentral-4,yzentral)
  58.                 term.setBackgroundColor(colors.black)
  59.                 term.setTextColor(colors.white)
  60.                 print("Settings")
  61.                 term.setCursorPos(xzentral-4,yzentral+1)
  62.                 term.setBackgroundColor(colors.black)
  63.                 term.setTextColor(colors.white)
  64.                 print("Quit")
  65. local event, key=os.pullEvent("key")
  66.  
  67.                 if key==208
  68.                         then
  69.                                
  70.                                
  71.                                 curPos="2" menu()
  72.        
  73.                         elseif key==28
  74.                                 then
  75.                                         joinChannel()
  76.                                         return
  77.                                         else menu()
  78.                 end
  79.         end
  80.        
  81.  
  82.         if curPos=="2" then
  83.                 term.setBackgroundColor(colors.black)
  84.                 term.clear()
  85.                 term.setBackgroundColor(colors.black)
  86.                 term.setCursorPos(xzentral-4,yzentral-1)
  87.                 term.setBackgroundColor(colors.black)
  88.                 term.setTextColor(colors.white)
  89.                 print("Join channel")
  90.                 term.setCursorPos(xzentral-4,yzentral)
  91.                 term.setBackgroundColor(colors.white)
  92.                 term.setTextColor(colors.black)
  93.                 print("Settings")
  94.                 term.setCursorPos(xzentral-4,yzentral+1)
  95.                 term.setBackgroundColor(colors.black)
  96.                 term.setTextColor(colors.white)
  97.                 print("Quit")
  98.                 term.setBackgroundColor(colors.black)
  99.         local event, key=os.pullEvent("key")
  100.                 if key==200
  101.                         then
  102.                                 curPos="1" menu()
  103.                
  104.  
  105.                         elseif key==208
  106.                                 then
  107.                                         term.setBackgroundColor(colors.black)
  108.                                         curPos="3"
  109.                                         menu()
  110.  
  111.                                 elseif key==28
  112.                                         then
  113.                                                 nickname()
  114.                                                 else menu() term.setBackgroundColor(colors.black)
  115.                 end
  116.         end
  117.                
  118.  
  119.         if curPos=="3" then
  120.                 term.clear()
  121.                 term.setCursorPos(xzentral-4,yzentral-1)
  122.                 term.setBackgroundColor(colors.black)
  123.                 term.setTextColor(colors.white)
  124.                 print("Join channel")
  125.                 term.setCursorPos(xzentral-4,yzentral)
  126.                 term.setBackgroundColor(colors.black)
  127.                 term.setTextColor(colors.white)
  128.                 print("Settings")
  129.                 term.setCursorPos(xzentral-4,yzentral+1)
  130.                 term.setBackgroundColor(colors.white)
  131.                 term.setTextColor(colors.black)
  132.                 print("Quit")
  133.  
  134. local event, key=os.pullEvent("key")
  135.                         if key==200
  136.                                 then
  137.                                         curPos="2" menu()
  138.                
  139.                                 elseif key==28
  140.                                         then
  141.                                                 term.setBackgroundColor(colors.black) term.setTextColor(colors.white) term.clear() term.setCursorPos(xzentral-4,yzentral) textutils.slowPrint("Good Bye") term.setCursorPos(1,1) os.sleep(2) os.shutdown()  
  142.                        
  143. else term.clear() term.setBackgroundColor(colors.black)  menu()
  144.                         end    
  145.         end    
  146. end    
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. function close()
  155.  
  156.  
  157.  
  158.  
  159.  
  160. local event, button, xPos, yPos = os.pullEvent("mouse_click")
  161.  
  162. if xPos==50 and yPos==1 then term.clear()    menu()
  163.         else
  164.         end
  165.  
  166.  
  167.  
  168.  
  169. end
  170.  
  171.  
  172.  
  173. function nickname()
  174. term.clear()
  175. term.setCursorPos(14,10)
  176. write("Username:")
  177.  username=read();
  178. if username=="" then term.setCursorPos(12,10) term.clearLine() print("Error: Please enter a Username ! ") os.sleep(2) nickname() else
  179. term.clear()
  180. term.setCursorPos(12,9)
  181. print("-------------------------------")
  182. term.setCursorPos(12,11)
  183. print("-------------------------------")
  184. term.setCursorPos(16,10)
  185. textutils.slowPrint("Username:"..username.." saved")
  186.  
  187.  
  188. os.sleep(2)
  189. end
  190. end
  191.  
  192.  
  193. -------------------------------------------------------------------------------------------------------------------
  194. local chat="";
  195.  
  196.  
  197.  
  198. function writetoscreen(text)
  199. term.clear()
  200.  
  201. local h=fs.open("/chatfolder/chat"..channel,"a")
  202. h.writeLine(text);
  203. h.flush();
  204. h.close();
  205.  
  206. cursorold()
  207. local g=fs.open("/chatfolder/chat"..channel,"r");
  208.  
  209. print(g.readAll());
  210. g.close()
  211. end
  212.  
  213. function oldmessages()
  214.  
  215.  
  216.         cursorold()
  217. local l=fs.open("/chatfolder/chat"..channel,"r");
  218.  
  219. print(l.readAll());
  220. l.close()
  221.  
  222. end
  223.  
  224. function writetoscreen_local(text)
  225. term.clear()
  226.  
  227. local h=fs.open("/chatfolder/chat"..channel,"a")
  228. h.writeLine("["..username.."]"..":"..text);
  229. h.flush();
  230. h.close();
  231.  
  232. cursorold()
  233. local g=fs.open("/chatfolder/chat"..channel,"r");
  234.  
  235. print(g.readAll());
  236. g.close()
  237. end
  238.  
  239.  
  240. function cursornew()
  241. intx, inty= term.getSize();
  242. term.setCursorPos(1,inty)
  243. end
  244.  
  245.  
  246. function cursorold()
  247. term.setCursorPos(1,2)
  248. end
  249.  
  250.  
  251.  
  252.  
  253.  function getSide()                                                                                                                                            
  254.         for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
  255.                 if peripheral.isPresent(side)
  256.                         then
  257.                                 if peripheral.getType(side)=="modem"
  258.                                         then
  259.                                                 modemside=side
  260.                                                 rednet.open(modemside)
  261.                                 end
  262.                 end
  263.         end
  264. end
  265.  
  266.  
  267.  
  268.  
  269. function send ()
  270. cursornew()
  271. term.clearLine()
  272. term.setCursorPos(1,inty-1)
  273. term.setCursorPos(1,1)
  274. term.setBackgroundColor(colors.white)
  275. term.setTextColor(colors.black)
  276. write("---You are chatting in channel "..channel.."---             ") term.setCursorPos(x-2,1) write("[X]")
  277. term.setBackgroundColor(colors.black)
  278. term.setTextColor(colors.white)
  279. term.setCursorPos(1,inty-1)
  280. print("---------------------------------------------------")
  281. cursornew()
  282. write("Text:")
  283. local msg=read();
  284. term.clearLine()
  285. cursornew()
  286. term.clear()
  287. if msg=="" then oldmessages() else
  288. chat=chat..msg
  289. cursorold()
  290. --write(chat)
  291. writetoscreen_local(msg)
  292. rednet.broadcast("["..username.."]"..":"..msg,channel)
  293. cursornew()
  294. term.clearLine()
  295. end
  296. end
  297.  
  298. function receive()
  299. id,message,protocol=rednet.receive(channel)
  300. cursorold()
  301. chat=chat..message
  302. --write(chat)
  303. writetoscreen(message)
  304. end
  305.  
  306. fs.delete("chat")
  307. getSide();
  308. nickname()
  309. menu()
  310.  
  311.  
  312. term.clear()
  313.  
  314.  
  315. while true do
  316.  
  317. parallel.waitForAny(send,receive,close)
  318. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement