Kouksi44

Untitled

Nov 19th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. --rewritten better chat !
  2. term.clear()
  3. x,y=term.getSize()
  4. midX=x/2
  5. midY=y/2
  6. history={}
  7.  
  8.  
  9. function setUsername()
  10. term.setCursorPos(14,10)
  11. write("Your username: ")
  12. usr=read()
  13. term.setCursorPos(14,11)
  14. write("Channel:")
  15. channel=tonumber(read())
  16. term.clear()
  17. end
  18.  
  19.  
  20.  
  21.  
  22. function showBar()
  23.  
  24. term.setCursorPos(1,1)
  25. term.setBackgroundColor(colors.white)
  26. for i=1,x do
  27. write(" ")
  28. end
  29. term.setCursorPos(1,1)
  30. term.setTextColor(colors.black)
  31.  
  32. print("You are chatting in channel: "..channel)
  33. term.setCursorPos(x-2,1)
  34. write("[X]")
  35.  
  36. term.setTextColor(colors.white)
  37. term.setBackgroundColor(colors.black)
  38. term.setCursorPos(1,2)
  39. for i=1,x do
  40. write("-")
  41. end
  42.  
  43.  
  44.  
  45. end
  46.  
  47.  
  48.  
  49. function exit()
  50.  
  51. local event, button, xPos, yPos = os.pullEvent("mouse_click")
  52.  
  53. if yPos==1 and xPos==x-1 then
  54. term.clear()
  55. os.shutdown()
  56. end
  57. end
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. function drawmessages()
  69. term.setCursorPos(1,3)
  70. for k,v in pairs(history) do
  71. term.clearLine()
  72. term.setCursorPos(1,k+2)
  73. print(v)
  74. end
  75. end
  76.  
  77. function openSide()
  78. for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
  79. if peripheral.isPresent(side)
  80. then
  81. if peripheral.getType(side)=="modem"
  82. then
  83. modemside=side
  84. rednet.open(modemside)
  85. end
  86. end
  87. end
  88. end
  89. openSide()
  90. function receive()
  91.  
  92. local id,message=rednet.receive("chatmessage/"..channel)
  93. if message ~="" then table.insert(history,message)
  94. drawmessages()
  95.  
  96. end
  97. end
  98.  
  99.  
  100. function sendMessage()
  101. showBar()
  102. drawmessages()
  103. term.setCursorPos(1,y-1)
  104. term.clearLine()
  105. term.setCursorPos(1,y)
  106. write(">>")
  107.  
  108. msg=read()
  109. if msg~="" then
  110. msg="["..usr.."]:"..msg
  111. table.insert(history,msg)
  112.  
  113. rednet.broadcast(msg,"chatmessage/"..channel)
  114. end
  115. end
  116.  
  117. setUsername()
  118.  
  119. while true do
  120. parallel.waitForAny(sendMessage,receive,exit)
  121.  
  122. end
Advertisement
Add Comment
Please, Sign In to add comment