Advertisement
Redxone

Kosy Advanced Chat [Beta]

Jul 14th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.95 KB | None | 0 0
  1. --#                     Kosy Chat Advanced Edition                         #--
  2. --]                                                                        [--
  3. --]   This Chat System used Array Object Based Code Simular to a JSON file [--
  4. --]    Project Started By - Redxone                                        [--
  5. --#                                                                        #--
  6.  
  7. --- Setup Modem ---
  8. modem = peripheral.find("modem")
  9. --- Define Program Vars --
  10. local state = 'title'
  11. local mcchat = true
  12. local w, h = term.getSize()
  13. local mx = (w/2)
  14. local my = h - 8
  15. local mlen = 14
  16. local onChannel = 0
  17. function c_tran(outof,into) return into-(outof/2) end
  18. local fadecolors = {colors.black,colors.gray,colors.lightGray,colors.white}
  19. -- Oh, Hello Im Just Here To Make Life Easier :) --
  20.  
  21. local st = term.setTextColor
  22. local sb = term.setBackgroundColor
  23. local scp = term.setCursorPos
  24. local sp = textutils.slowPrint
  25. local ser = textutils.serialize
  26. local unser = textutils.unserialize
  27. local clr = term.clear
  28. local crl_l = term.clearLine
  29.  
  30. function drawCInput()
  31.      sx = 15
  32.      sy = 9
  33.      str = ""
  34.      online = false;
  35.     scp(sx,sy)
  36.     sb(colors.gray)
  37.     print(string.rep(" ",20))
  38.     scp(22+sx,sy)
  39.     sb(colors.red)
  40.     print("X")
  41.     sb(colors.gray)
  42.     scp(sx,sy)
  43.     print("Input Channel Number  ")
  44.     for i = 1, 4 do
  45.           sleep(0.02)
  46.          scp(sx,sy+i)
  47.          sb(colors.lightGray)
  48.          print(string.rep(" ",23))
  49.     end
  50.  
  51.     sb(colors.lightGray)
  52.     scp(sx+18,sy+4)
  53.     --print("CHECK")
  54. end
  55.  
  56. -- Read Functions --
  57.  
  58. -- SCREW IT MAKING TWO --
  59. function hostChannel()
  60.  
  61.       drawCInput()
  62.     -- Keep the user inside the box event --
  63.     state = 'input_box'
  64.     while state == 'input_box' do
  65.  
  66.           scp(sx+2,sy+2)
  67.           sb(colors.white)
  68.           st(colors.black)
  69.           write(string.rep(" ",18))
  70.           scp(sx+20,sy+2)
  71.           --if(online)then sb(colors.red) else sb(colors.green) end
  72.           --print(" ")
  73.           sb(colors.green)
  74.           scp(sx,sy+4)
  75.           print(" Host ")
  76.           scp( (sx+2) ,sy+2)
  77.           sb(colors.white)
  78.           write(str)
  79.           term.setCursorBlink(true)
  80.  
  81.  
  82.            if(#str >= 5)then term.setCursorBlink(false) end
  83.  
  84.         -- Read And Button Events --
  85.         a = {os.pullEvent()}
  86.  
  87.  
  88.  
  89.         -- Coustom Read = A PAIN --
  90.         if(a[1] == "char" and #str < 5)then
  91.           if(tonumber(a[2]))then
  92.               str = str..a[2]
  93.               if(tonumber(str) > 65535)then str = tostring(65535) end
  94.           end
  95.         end
  96.  
  97.         if(a[1] == "key")then
  98.            if(a[2] == keys.backspace)then
  99.               str = str:sub(1,-2)
  100.             elseif(a[2] == keys.enter)then
  101.               --if(tonumber(str))then online = modem.isOpen(tonumber(str)) end
  102.             end
  103.         end
  104.  
  105.         if(a[1] == "mouse_click")then
  106.            
  107.             -- Check Button
  108.             if( a[3] >= sx+18 and a[3] <= sx+23 and a[4] == sy+4 and a[2] == 1)then
  109.               --if(tonumber(str))then online = modem.isOpen(tonumber(str)) end
  110.             end
  111.  
  112.             -- Connect Button
  113.             if( a[3] >= sx and a[3] <= sx+6 and a[4] == sy+4 and a[2] == 1)then
  114.                   -- Lobby Connect Code :/ sigh Headache Incomming! --
  115.                     onChannel = tonumber(str)
  116.                     term.setCursorBlink(false)
  117.                     setState('chat_lobby',true)
  118.                  
  119.             end
  120.  
  121.             if(a[3] == sx+22 and a[4] == sy and a[2] == 1)then
  122.               -- Exit Code --
  123.               term.setCursorBlink(false)
  124.               changeState('title')
  125.             end
  126.  
  127.         end
  128.  
  129.     end
  130.  
  131. end
  132.  
  133. function conChannel()
  134.  
  135.     drawCInput()
  136.     -- Keep the user inside the box event --
  137.     state = 'input_box'
  138.     while state == 'input_box' do
  139.  
  140.           scp(sx+2,sy+2)
  141.           sb(colors.white)
  142.           st(colors.black)
  143.           write(string.rep(" ",18))
  144.           scp(sx+20,sy+2)
  145.           --if(online)then sb(colors.green) else sb(colors.red) end
  146.           --print(" ")
  147.           scp(sx,sy+4)
  148.           sb(colors.green)
  149.           print("Connect")
  150.           scp( (sx+2) ,sy+2)
  151.           sb(colors.white)
  152.           write(str)
  153.           term.setCursorBlink(true)
  154.  
  155.  
  156.            if(#str >= 5)then term.setCursorBlink(false) end
  157.  
  158.         -- Read And Button Events --
  159.         a = {os.pullEvent()}
  160.  
  161.  
  162.  
  163.         -- Coustom Read = A PAIN --
  164.         if(a[1] == "char" and #str < 5)then
  165.           if(tonumber(a[2]))then
  166.               str = str..a[2]
  167.               if(tonumber(str) > 65535)then str = tostring(65535) end
  168.           end
  169.         end
  170.  
  171.         if(a[1] == "key")then
  172.            if(a[2] == keys.backspace)then
  173.               str = str:sub(1,-2)
  174.             elseif(a[2] == keys.enter)then
  175.              -- if(tonumber(str))then online = modem.isOpen(tonumber(str)) end
  176.             end
  177.         end
  178.  
  179.         if(a[1] == "mouse_click")then
  180.            
  181.             -- Check Button
  182.             if( a[3] >= sx+18 and a[3] <= sx+23 and a[4] == sy+4 and a[2] == 1)then
  183.               --if(tonumber(str))then online = modem.isOpen(tonumber(str)) end
  184.             end
  185.  
  186.             -- Connect Button
  187.             if( a[3] >= sx and a[3] <= sx+6 and a[4] == sy+4 and a[2] == 1)then
  188.                   -- Lobby Connect Code :/ sigh Headache Incomming! --
  189.                     onChannel = tonumber(str)
  190.                     term.setCursorBlink(false)
  191.                     setState('chat_lobby',false)
  192.                  
  193.             end
  194.  
  195.             if(a[3] == sx+22 and a[4] == sy and a[2] == 1)then
  196.               -- Exit Code --
  197.               term.setCursorBlink(false)
  198.               changeState('title')
  199.             end
  200.  
  201.         end
  202.  
  203.     end
  204.  
  205. end
  206.  
  207.  
  208. -- Fades --
  209.  
  210. function fadeScr(rate)
  211.    for i = 1, #fadecolors do
  212.      term.setBackgroundColor(fadecolors[i])
  213.      term.clear()
  214.      sleep(rate)
  215.    end
  216. end
  217.  
  218. function center_x(text,y)
  219.     term.setCursorPos((w/2) - (#text/2), y)
  220.     print(text)
  221. end
  222.  
  223. function center_y(text,x)
  224.     term.setCursorPos(x, (h/2))
  225.     print(text)
  226. end
  227.  
  228. function center_complete(text,optional_yoffset)
  229.     if(not optional_yoffset)then term.setCursorPos((w/2) - (#text/2), (h/2) ) end
  230.     if(optional_yoffset)then term.setCursorPos((w/2) - (#text/2), (h/2)+optional_yoffset) end
  231.     write(text)
  232. end
  233.  
  234. -- Text Effects --
  235.  
  236. function dist(v1,v2) if(v1-v2 < 0)then return (v1-v2)*-1 else return (v1-v2) end end
  237.  
  238.  
  239. -- Find Modem --
  240.  
  241. chat = peripheral.find("modem")
  242.  
  243. -- Chat Options --
  244.  
  245.  
  246. local chatoptions = {
  247.        
  248.     ["  Chat Options  "] = {x=c_tran(mlen,mx),y=my, run = function() end};
  249.     ["                "] = {x=c_tran(mlen,mx),y=my+1, run = function() end};
  250.     ["  Change Name   "] = {x=c_tran(mlen,mx),y=my+2, run = function() end};
  251.     ["  Attach File   "] = {x=c_tran(mlen,mx),y=my+3, run = function() end};
  252.     ["  Disconnect    "] = {x=c_tran(mlen,mx),y=my+4, run = function() end};
  253.     ["________________"] = {x=c_tran(mlen,mx),y=my+5, run = function() end};
  254.  
  255. }
  256.  
  257. -- State Enviorments --
  258.  
  259.  
  260. local title = {
  261.  
  262.      bW = 16;
  263.      bH = 2;
  264.      clk_col = colors.gray;
  265.  
  266.      buttons = {
  267.           {bx=5,by=10,bcol=colors.blue,tcol=colors.white,text="Join Channel", click = function() conChannel() end};
  268.           {bx=30,by=10,bcol=colors.green,tcol=colors.white,text="Host Channel", click = function() hostChannel() end};
  269.           {bx=18,by=15,bcol=colors.red,tcol=colors.white,text="    Exit    ", click = function() mcchat = false sb(colors.black) st(colors.white) term.clear() center_x("Thank You For Using Kosy Chat Advanced",1) center_x("Created By Redxone",2) end};
  270.       };
  271.  
  272.  
  273.    update = function(self)
  274.      
  275.       a = {os.pullEvent("mouse_click")}
  276.  
  277.       mb, mx, my = a[2], a[3], a[4]
  278.  
  279.       -- Now For Clicking Of Deh Buttanz --
  280.  
  281.       for i = 1, #self.buttons do
  282.  
  283.          if(mx >= self.buttons[i].bx and
  284.             mx <= self.buttons[i].bx+self.bW and
  285.             my >= self.buttons[i].by and
  286.             my <= self.buttons[i].by+self.bH and
  287.             mb == 1)then
  288.  
  289.             if(self.buttons[i].click)then self.buttons[i].click() end
  290.          
  291.          end
  292.  
  293.       end
  294.  
  295.    end;
  296.    
  297.    draw = function(self,wait)
  298.         -- Draw Buttons --
  299.             for i = 1, #self.buttons do
  300.  
  301.                 -- Draw Button Dimensions --
  302.                if(wait)then sleep(0.4) end
  303.               for z = self.buttons[i].by, self.buttons[i].by+self.bH do
  304.  
  305.                   scp(self.buttons[i].bx,z)
  306.                   sb(self.buttons[i].bcol)
  307.                   st(self.buttons[i].tcol)
  308.                   print(string.rep(" ",self.bW))
  309.  
  310.                   --Centering Button Text... EHHHh Difficult part --
  311.  
  312.                   scp( (self.buttons[i].bx + self.bW/ 2) - #(self.buttons[i].text)/2,
  313.                   math.ceil(self.bH/2)+self.buttons[i].by)
  314.  
  315.                   print(self.buttons[i].text)
  316.              
  317.               end
  318.                     sb(colors.white)
  319.                     st(colors.black)
  320.            end
  321.    end,
  322.    
  323.    clear = function()
  324.  
  325.      fadeScr(0.1)
  326.      sleep(0.2)
  327.      st(colors.black)
  328.      center_x("  Kosy Chat - Advanced",6)
  329.  
  330.    end,
  331.  
  332.    redraw = function(self)
  333.      sb(colors.white)
  334.      clr()
  335.      st(colors.black)
  336.      center_x("  Kosy Chat - Advanced",6)
  337.      self.draw(self,false)
  338.    end,
  339.  
  340.  
  341. }
  342.  
  343. local chatlobby = {
  344.    
  345.   host = false,
  346.   motd = "Welcome To A Chat Lobby!",
  347.   hasCon = false,
  348.   chat_txt = "",
  349.   chat_msgs = {};
  350.   usr = "Undefined",
  351.  
  352.  
  353.     host = function(self)
  354.         self.host=true
  355.     end,
  356.  
  357.     unhost = function(self)
  358.       self.host=false
  359.     end,
  360.  
  361.     setMOTD = function(self,message)
  362.  
  363.         self.motd = message
  364.  
  365.     end,
  366.  
  367.    update = function(self)
  368.        
  369.       --A Bit Weird But ALL chat function including
  370.       -- Graphics WILL be handeled here.. --
  371.       scp(1,19)
  372.       sb(colors.lightGray)
  373.       term.clearLine()
  374.       write(self.chat_txt)
  375.       term.setCursorBlink(true)
  376.       sb(colors.white)
  377.       st(colors.blue)
  378.  
  379.  
  380.       mes = {os.pullEvent()}
  381.  
  382.       -- Connecter request options --
  383.       if(mes[1] == "modem_message")then
  384.           if(mes[5] == "REQUEST_MOTD")then
  385.             modem.transmit(onChannel,onChannel,self.motd)
  386.           else
  387.               table.insert(self.chat_msgs,mes[5])
  388.           end
  389.       end
  390.  
  391.       -- Wrap In Connection --
  392.       if(self.hasCon)then
  393.         -- Do Chat System --
  394.  
  395.           -- Anada Custam read ._. --
  396.  
  397.             if(mes[1] == "char" and #self.chat_txt < w)then
  398.                 self.chat_txt = self.chat_txt..mes[2]
  399.             end
  400.  
  401.             if(mes[1] == "key")then
  402.  
  403.                 if(mes[2] == keys.backspace)then self.chat_txt = self.chat_txt:sub(1,-2) end
  404.                 if(mes[2] == keys.enter)then modem.transmit(onChannel,onChannel,self.usr..": "..self.chat_txt) table.insert(self.chat_msgs, self.usr..": "..self.chat_txt) self.chat_txt = "" end
  405.  
  406.             end
  407.  
  408.       end
  409.  
  410.  
  411.    end;
  412.  
  413.    draw = function(self,wait)
  414.       if(self.hasCon)then
  415.           -- Calculates Draw Offset For AutoScroll --
  416.          
  417.           if(#self.chat_msgs > h-1)then sb(colors.white) term.clear() self.chat_msgs = {} end
  418.         -- Well Msgs Will Be Drawn Here --
  419.         for i = 1, #self.chat_msgs do
  420.             term.setCursorPos(1,i)
  421.             write(self.chat_msgs[i])
  422.         end
  423.       end
  424.    end,
  425.  
  426.  
  427.    start = function(self,wait)
  428.         term.setBackgroundColor(colors.black)
  429.         term.clear()
  430.         st(colors.white)
  431.         modem.open(onChannel)
  432.  
  433.       if(not self.host)then
  434.         center_complete("Connecting...")
  435.         modem.transmit(onChannel,onChannel,"REQUEST_MOTD")
  436.         os.startTimer(0.1)
  437.         ch = {os.pullEvent()}
  438.  
  439.        
  440.  
  441.           if(ch[1] == "modem_message")then
  442.             center_complete("Enter A Username: ",1)
  443.             self.usr = read()
  444.             center_complete(ch[5],2)
  445.             center_complete("Click To Continue",3)
  446.             os.pullEvent("mouse_click")
  447.             fadeScr(0.05)
  448.  
  449.             -- Proceed With Connection --
  450.             self.hasCon = true
  451.  
  452.             elseif(ch[1] == "timer")then
  453.               center_complete("Connection To Lobby Failed!",1)  
  454.               center_complete("Click To Continue",2)
  455.               os.pullEvent("mouse_click")
  456.               changeState('title')
  457.               stChat()
  458.           end
  459.  
  460.       elseif(self.host)then
  461.           center_complete("Checking Host Channel...")
  462.           os.startTimer(0.1)
  463.           modem.transmit(onChannel,onChannel,"REQUEST_MOTD")
  464.           chr = {os.pullEvent()}
  465.           if(chr[1] == "modem_message")then
  466.             modem.close(onChannel)
  467.             center_complete("Channel Already Being Hosted!")
  468.             center_complete("Press Any Key",2)
  469.             os.pullEvent("key")
  470.             changeState('title')
  471.             stChat()
  472.           end
  473.           if(chr[1])then
  474.             center_complete("Enter A Username: ",1)
  475.             self.usr = read()
  476.             center_complete("Press Any Key To Host",2)
  477.             os.pullEvent("key")
  478.             self.hasCon = true
  479.             fadeScr(0.1)
  480.           end
  481.       end
  482.    end,
  483.    
  484.    clear = function() end,
  485.    redraw = function(self) end,
  486. }
  487.  
  488.  
  489. -- When State Begin Load ALWAYS Call the Objects Native ReDraw Function
  490. -- Then Leave the Updating to the Chat Loop
  491.  
  492. function changeState(ste)
  493.  
  494.       if(ste == 'title')then title:redraw() state = 'title' end
  495.       if(ste == 'chat_lobby')then error("chat_lobby is setState only!") state='chat_lobby' end
  496. end
  497.  
  498. function setState(ste,op)
  499.       if(ste == 'title')then title:clear() title:draw(true) state = 'title' end
  500.       if(ste == 'chat_lobby')then if(op)then chatlobby:host() else chatlobby:unhost() end chatlobby:start() state='chat_lobby' end
  501. end
  502.  
  503.  
  504. setState('title')
  505.  
  506. function stChat()
  507. while mcchat do
  508.  
  509.     if(state == 'title')then
  510.        
  511.        title:update()
  512.        
  513.     end
  514.    
  515.     if(state == 'chat_lobby')then
  516.  
  517.         chatlobby:update()
  518.         chatlobby:draw()
  519.  
  520.     end
  521.    
  522. end
  523. end
  524. stChat()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement