Advertisement
LDDestroier

OurChat with read()

Oct 11th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- pastebin get zvV7Le0F ourchat
  2. -- std pb zvV7Le0F ourchat
  3.  
  4. getChatURL = "http://minechat.x10host.com/index.php?true=true"
  5. postChatURL = "http://minechat.x10host.com/index.php?words="
  6.  
  7. tMess = {}
  8. tDisplay = {}
  9. tArg = {...}
  10. scr_x, scr_y = term.getSize()
  11. if tArg[1] then
  12.     playername = table.concat(tArg, " ")
  13. else
  14.     term.clear()
  15.     term.setCursorPos(2,scr_y-2)
  16.     print("Enter your name:")
  17.     repeat
  18.         term.setCursorPos(2,scr_y-1)
  19.         term.clearLine()
  20.         write(">>")
  21.         playername = read()
  22.     until playername ~= ""
  23. --  print(fs.getName(shell.getRunningProgram()) .. " <username>")
  24. --  return false
  25. end
  26.  
  27. function send(data)
  28.     local a = http.post(postChatURL..textutils.urlEncode(data))
  29.     return a
  30. end
  31.  
  32. commands = {
  33.     ["me"] = function()
  34.         if string.len(arg1) > 0 then
  35.             send( "* "..playername.." "..arg1 )
  36.         else
  37.             putInDisplay("/me <whatever>")
  38.         end
  39.     end,
  40.     ["nick"] = function()
  41.         if string.len(arg1) > 0 then
  42.             local sOldName = playername
  43.             playername = arg1
  44.             send( "* "..sOldName.." is now known as "..playername )
  45.         else
  46.             putInDisplay("/nick <newname>")
  47.         end
  48.     end,
  49.     ["help"] = function()
  50.         putInDisplay("Available commands:")
  51.         local sCommands = "*"
  52.         for sCommand, fnCommand in pairs( commands ) do
  53.             sCommands = sCommands .. " /" .. sCommand
  54.         end
  55.         putInDisplay( sCommands.." /logout" )
  56.     end,
  57.     ["insult"] = function()
  58.         insults = {
  59.             "USERNAME can go eat a bag of shiza.",
  60.             "USERNAME is fat and ugly and really, really stupid.",
  61.             "USERNAME is a fat sack o' crap.",
  62.             "If USERNAME was admin...eh, I don't want to think of such things...",
  63.             "No one is more pathetic than USERNAME.",
  64.             "USERNAME is known to suck more than Kirby.",
  65.             "USERNAME was the inspiration of Bubsy 3D.",
  66.             "USERNAME's IQ is his age...times two!",
  67.             "USERNAME's programming skills rival that of the Software Toolworks.",
  68.             "Down with USERNAME, the killer of kings!",
  69.             "USERNAME can go suck it!",
  70.             "Stop being so adversarial, USERNAME!",
  71.             "USERNAME cannot beat Air Man!",
  72.             "USERNAME likes Daikatana and Bubsy.",
  73.             "USERNAME is like King Midas, except with coagulated blood rather than gold.",
  74.             "USERNAME's mother was a hampster, and his father smells of elderberry!",
  75.             "Ni!",
  76.             "USERNAME is a dishonorable Ferrengi p'tach!",
  77.             "Pepperoni is too spicy for USERNAME.",
  78.         }
  79.         send(string.gsub(insults[math.random(1,#insults)],"USERNAME",arg1))
  80.     end,
  81.     ["kill"] = function()
  82.         if #arg1 < 1 then
  83.             send(playername.." was killed.")
  84.         else
  85.             send(arg1.." was killed.")
  86.         end
  87.     end,
  88.     ["id"] = function()
  89.         send(playername.."'s computer ID is "..os.getComputerID()..".")
  90.     end,
  91.     ["clear"] = function()
  92.         send(string.rep(" ",(51*19))..playername.." cleared the screen.")
  93.     end,
  94.     ["exit"] = function()
  95.         send(playername.." has left the chat")
  96.         return true
  97.     end,
  98.     ["logout"] = function()
  99.         send(playername.." has left the chat")
  100.         return true
  101.     end,
  102. }
  103.  
  104. local function scrollClear(speed)
  105.     scr_x, scr_y = term.getSize()
  106.     for a = 1, scr_y/speed do
  107.         term.scroll(speed*speed)
  108.         sleep(0)
  109.     end
  110. end
  111.  
  112. function putInDisplay(text)
  113.     tDisplay[#tDisplay+1] = tostring(text)
  114.     redraw = true
  115.     return tostring(text)
  116. end
  117.  
  118. function seperateMethods(input)
  119.     output={}
  120.     for key,value in pairs(input) do
  121.         table.insert(output, {key,value})
  122.     end
  123.     return output
  124. end
  125.  
  126. local function b()
  127.     send(playername.." joined the chat!")
  128.     while true do
  129.         term.setCursorPos(1,scr_y)
  130.         term.clearLine()
  131.         write(":")
  132.         mess = io.read()
  133.         term.setCursorPos(1,scr_y-1)
  134.         term.clearLine()
  135.         local sCommand = string.match( mess, "^/([a-z]+)" )
  136.         if sCommand then
  137.             local fnCommand = commands[sCommand]
  138.             if fnCommand then
  139.                 if string.find(mess," ") then
  140.                     arg1 = string.sub(mess,string.find(mess," ")+1)
  141.                     a = fnCommand(arg1)
  142.                 else
  143.                     arg1 = ""
  144.                     a = fnCommand()
  145.                 end
  146.                 if a == true then
  147.                     return true
  148.                 end
  149.             else
  150.                 print("Bad command!")
  151.             end
  152.         else
  153.             send( "<"..playername.."> "..mess )
  154.         end
  155.     end
  156. end
  157.  
  158. local function clearLines(top, bottom)
  159.     for a = top, bottom do
  160.         term.setCursorPos(1,a)
  161.         term.clearLine()
  162.     end
  163. end
  164.  
  165. function chatPrint(log)
  166.     if not type(log) == "table" then log = {log} end
  167.     local prevX, prevY = term.getCursorPos()
  168.     clearLines(1,scr_y-1)
  169.     indent = 0 --in case any line is greater than the length of the screen
  170.     indentIn = 0 --in the same case, mid writing
  171.     for a = 1, #log do
  172.         indent = indent + math.floor(#log[a]/scr_x)
  173.     end
  174.     for a = 1, #log do
  175.         for b = 0, math.floor(#log[a]/scr_x) do
  176.             if b > 0 then indentIn = indentIn + 1 end
  177.             term.setCursorPos(1,((((scr_y-1)+a-#log))-indent)+indentIn)
  178.             write(string.sub(log[a],(b*scr_x),((b*scr_x)+scr_x)-1))
  179.         end
  180.     end
  181.     term.setCursorPos(prevX,scr_y)
  182. end
  183.  
  184. function a()
  185.     while true do
  186.         for a = 1, 5 do
  187.             get = http.post(getChatURL)
  188.             if get then break else
  189.                 print("Cannot connect to server...retrying...")
  190.                 sleep(1)
  191.             end
  192.         end
  193.         if not get then error("Failed to reach server. Try again later.") end
  194.         get = get.readAll()
  195.         if (tMess[#tMess] ~= get) and (redraw ~= true) then putInDisplay(get) end
  196.         if get ~= tMess[#tMess] or redraw == true then
  197.             if redraw == true then tMess[#tMess+1] = get end
  198.             chatPrint(tDisplay)
  199.             redraw = false
  200.         end
  201.     end
  202. end
  203. term.clear()
  204. parallel.waitForAny(a, b)
  205. scrollClear(2)
  206. term.setCursorPos(1,1)
  207. print("Thank you for using OurChat!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement