Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Kosy Chat Advanced Edition #--
- --] [--
- --] This Chat System used Array Object Based Code Simular to a JSON file [--
- --] Project Started By - Redxone [--
- --# #--
- --- Setup Modem ---
- modem = peripheral.find("modem")
- --- Define Program Vars --
- local state = 'title'
- local mcchat = true
- local w, h = term.getSize()
- local mx = (w/2)
- local my = h - 8
- local mlen = 14
- local onChannel = 0
- function c_tran(outof,into) return into-(outof/2) end
- local fadecolors = {colors.black,colors.gray,colors.lightGray,colors.white}
- -- Oh, Hello Im Just Here To Make Life Easier :) --
- local st = term.setTextColor
- local sb = term.setBackgroundColor
- local scp = term.setCursorPos
- local sp = textutils.slowPrint
- local ser = textutils.serialize
- local unser = textutils.unserialize
- local clr = term.clear
- local crl_l = term.clearLine
- function drawCInput()
- sx = 15
- sy = 9
- str = ""
- online = false;
- scp(sx,sy)
- sb(colors.gray)
- print(string.rep(" ",20))
- scp(22+sx,sy)
- sb(colors.red)
- print("X")
- sb(colors.gray)
- scp(sx,sy)
- print("Input Channel Number ")
- for i = 1, 4 do
- sleep(0.02)
- scp(sx,sy+i)
- sb(colors.lightGray)
- print(string.rep(" ",23))
- end
- sb(colors.lightGray)
- scp(sx+18,sy+4)
- --print("CHECK")
- end
- -- Read Functions --
- -- SCREW IT MAKING TWO --
- function hostChannel()
- drawCInput()
- -- Keep the user inside the box event --
- state = 'input_box'
- while state == 'input_box' do
- scp(sx+2,sy+2)
- sb(colors.white)
- st(colors.black)
- write(string.rep(" ",18))
- scp(sx+20,sy+2)
- --if(online)then sb(colors.red) else sb(colors.green) end
- --print(" ")
- sb(colors.green)
- scp(sx,sy+4)
- print(" Host ")
- scp( (sx+2) ,sy+2)
- sb(colors.white)
- write(str)
- term.setCursorBlink(true)
- if(#str >= 5)then term.setCursorBlink(false) end
- -- Read And Button Events --
- a = {os.pullEvent()}
- -- Coustom Read = A PAIN --
- if(a[1] == "char" and #str < 5)then
- if(tonumber(a[2]))then
- str = str..a[2]
- if(tonumber(str) > 65535)then str = tostring(65535) end
- end
- end
- if(a[1] == "key")then
- if(a[2] == keys.backspace)then
- str = str:sub(1,-2)
- elseif(a[2] == keys.enter)then
- --if(tonumber(str))then online = modem.isOpen(tonumber(str)) end
- end
- end
- if(a[1] == "mouse_click")then
- -- Check Button
- if( a[3] >= sx+18 and a[3] <= sx+23 and a[4] == sy+4 and a[2] == 1)then
- --if(tonumber(str))then online = modem.isOpen(tonumber(str)) end
- end
- -- Connect Button
- if( a[3] >= sx and a[3] <= sx+6 and a[4] == sy+4 and a[2] == 1)then
- -- Lobby Connect Code :/ sigh Headache Incomming! --
- onChannel = tonumber(str)
- term.setCursorBlink(false)
- setState('chat_lobby',true)
- end
- if(a[3] == sx+22 and a[4] == sy and a[2] == 1)then
- -- Exit Code --
- term.setCursorBlink(false)
- changeState('title')
- end
- end
- end
- end
- function conChannel()
- drawCInput()
- -- Keep the user inside the box event --
- state = 'input_box'
- while state == 'input_box' do
- scp(sx+2,sy+2)
- sb(colors.white)
- st(colors.black)
- write(string.rep(" ",18))
- scp(sx+20,sy+2)
- --if(online)then sb(colors.green) else sb(colors.red) end
- --print(" ")
- scp(sx,sy+4)
- sb(colors.green)
- print("Connect")
- scp( (sx+2) ,sy+2)
- sb(colors.white)
- write(str)
- term.setCursorBlink(true)
- if(#str >= 5)then term.setCursorBlink(false) end
- -- Read And Button Events --
- a = {os.pullEvent()}
- -- Coustom Read = A PAIN --
- if(a[1] == "char" and #str < 5)then
- if(tonumber(a[2]))then
- str = str..a[2]
- if(tonumber(str) > 65535)then str = tostring(65535) end
- end
- end
- if(a[1] == "key")then
- if(a[2] == keys.backspace)then
- str = str:sub(1,-2)
- elseif(a[2] == keys.enter)then
- -- if(tonumber(str))then online = modem.isOpen(tonumber(str)) end
- end
- end
- if(a[1] == "mouse_click")then
- -- Check Button
- if( a[3] >= sx+18 and a[3] <= sx+23 and a[4] == sy+4 and a[2] == 1)then
- --if(tonumber(str))then online = modem.isOpen(tonumber(str)) end
- end
- -- Connect Button
- if( a[3] >= sx and a[3] <= sx+6 and a[4] == sy+4 and a[2] == 1)then
- -- Lobby Connect Code :/ sigh Headache Incomming! --
- onChannel = tonumber(str)
- term.setCursorBlink(false)
- setState('chat_lobby',false)
- end
- if(a[3] == sx+22 and a[4] == sy and a[2] == 1)then
- -- Exit Code --
- term.setCursorBlink(false)
- changeState('title')
- end
- end
- end
- end
- -- Fades --
- function fadeScr(rate)
- for i = 1, #fadecolors do
- term.setBackgroundColor(fadecolors[i])
- term.clear()
- sleep(rate)
- end
- end
- function center_x(text,y)
- term.setCursorPos((w/2) - (#text/2), y)
- print(text)
- end
- function center_y(text,x)
- term.setCursorPos(x, (h/2))
- print(text)
- end
- function center_complete(text,optional_yoffset)
- if(not optional_yoffset)then term.setCursorPos((w/2) - (#text/2), (h/2) ) end
- if(optional_yoffset)then term.setCursorPos((w/2) - (#text/2), (h/2)+optional_yoffset) end
- write(text)
- end
- -- Text Effects --
- function dist(v1,v2) if(v1-v2 < 0)then return (v1-v2)*-1 else return (v1-v2) end end
- -- Find Modem --
- chat = peripheral.find("modem")
- -- Chat Options --
- local chatoptions = {
- [" Chat Options "] = {x=c_tran(mlen,mx),y=my, run = function() end};
- [" "] = {x=c_tran(mlen,mx),y=my+1, run = function() end};
- [" Change Name "] = {x=c_tran(mlen,mx),y=my+2, run = function() end};
- [" Attach File "] = {x=c_tran(mlen,mx),y=my+3, run = function() end};
- [" Disconnect "] = {x=c_tran(mlen,mx),y=my+4, run = function() end};
- ["________________"] = {x=c_tran(mlen,mx),y=my+5, run = function() end};
- }
- -- State Enviorments --
- local title = {
- bW = 16;
- bH = 2;
- clk_col = colors.gray;
- buttons = {
- {bx=5,by=10,bcol=colors.blue,tcol=colors.white,text="Join Channel", click = function() conChannel() end};
- {bx=30,by=10,bcol=colors.green,tcol=colors.white,text="Host Channel", click = function() hostChannel() end};
- {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};
- };
- update = function(self)
- a = {os.pullEvent("mouse_click")}
- mb, mx, my = a[2], a[3], a[4]
- -- Now For Clicking Of Deh Buttanz --
- for i = 1, #self.buttons do
- if(mx >= self.buttons[i].bx and
- mx <= self.buttons[i].bx+self.bW and
- my >= self.buttons[i].by and
- my <= self.buttons[i].by+self.bH and
- mb == 1)then
- if(self.buttons[i].click)then self.buttons[i].click() end
- end
- end
- end;
- draw = function(self,wait)
- -- Draw Buttons --
- for i = 1, #self.buttons do
- -- Draw Button Dimensions --
- if(wait)then sleep(0.4) end
- for z = self.buttons[i].by, self.buttons[i].by+self.bH do
- scp(self.buttons[i].bx,z)
- sb(self.buttons[i].bcol)
- st(self.buttons[i].tcol)
- print(string.rep(" ",self.bW))
- --Centering Button Text... EHHHh Difficult part --
- scp( (self.buttons[i].bx + self.bW/ 2) - #(self.buttons[i].text)/2,
- math.ceil(self.bH/2)+self.buttons[i].by)
- print(self.buttons[i].text)
- end
- sb(colors.white)
- st(colors.black)
- end
- end,
- clear = function()
- fadeScr(0.1)
- sleep(0.2)
- st(colors.black)
- center_x(" Kosy Chat - Advanced",6)
- end,
- redraw = function(self)
- sb(colors.white)
- clr()
- st(colors.black)
- center_x(" Kosy Chat - Advanced",6)
- self.draw(self,false)
- end,
- }
- local chatlobby = {
- host = false,
- motd = "Welcome To A Chat Lobby!",
- hasCon = false,
- chat_txt = "",
- chat_msgs = {};
- usr = "Undefined",
- host = function(self)
- self.host=true
- end,
- unhost = function(self)
- self.host=false
- end,
- setMOTD = function(self,message)
- self.motd = message
- end,
- update = function(self)
- --A Bit Weird But ALL chat function including
- -- Graphics WILL be handeled here.. --
- scp(1,19)
- sb(colors.lightGray)
- term.clearLine()
- write(self.chat_txt)
- term.setCursorBlink(true)
- sb(colors.white)
- st(colors.blue)
- mes = {os.pullEvent()}
- -- Connecter request options --
- if(mes[1] == "modem_message")then
- if(mes[5] == "REQUEST_MOTD")then
- modem.transmit(onChannel,onChannel,self.motd)
- else
- table.insert(self.chat_msgs,mes[5])
- end
- end
- -- Wrap In Connection --
- if(self.hasCon)then
- -- Do Chat System --
- -- Anada Custam read ._. --
- if(mes[1] == "char" and #self.chat_txt < w)then
- self.chat_txt = self.chat_txt..mes[2]
- end
- if(mes[1] == "key")then
- if(mes[2] == keys.backspace)then self.chat_txt = self.chat_txt:sub(1,-2) end
- 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
- end
- end
- end;
- draw = function(self,wait)
- if(self.hasCon)then
- -- Calculates Draw Offset For AutoScroll --
- if(#self.chat_msgs > h-1)then sb(colors.white) term.clear() self.chat_msgs = {} end
- -- Well Msgs Will Be Drawn Here --
- for i = 1, #self.chat_msgs do
- term.setCursorPos(1,i)
- write(self.chat_msgs[i])
- end
- end
- end,
- start = function(self,wait)
- term.setBackgroundColor(colors.black)
- term.clear()
- st(colors.white)
- modem.open(onChannel)
- if(not self.host)then
- center_complete("Connecting...")
- modem.transmit(onChannel,onChannel,"REQUEST_MOTD")
- os.startTimer(0.1)
- ch = {os.pullEvent()}
- if(ch[1] == "modem_message")then
- center_complete("Enter A Username: ",1)
- self.usr = read()
- center_complete(ch[5],2)
- center_complete("Click To Continue",3)
- os.pullEvent("mouse_click")
- fadeScr(0.05)
- -- Proceed With Connection --
- self.hasCon = true
- elseif(ch[1] == "timer")then
- center_complete("Connection To Lobby Failed!",1)
- center_complete("Click To Continue",2)
- os.pullEvent("mouse_click")
- changeState('title')
- stChat()
- end
- elseif(self.host)then
- center_complete("Checking Host Channel...")
- os.startTimer(0.1)
- modem.transmit(onChannel,onChannel,"REQUEST_MOTD")
- chr = {os.pullEvent()}
- if(chr[1] == "modem_message")then
- modem.close(onChannel)
- center_complete("Channel Already Being Hosted!")
- center_complete("Press Any Key",2)
- os.pullEvent("key")
- changeState('title')
- stChat()
- end
- if(chr[1])then
- center_complete("Enter A Username: ",1)
- self.usr = read()
- center_complete("Press Any Key To Host",2)
- os.pullEvent("key")
- self.hasCon = true
- fadeScr(0.1)
- end
- end
- end,
- clear = function() end,
- redraw = function(self) end,
- }
- -- When State Begin Load ALWAYS Call the Objects Native ReDraw Function
- -- Then Leave the Updating to the Chat Loop
- function changeState(ste)
- if(ste == 'title')then title:redraw() state = 'title' end
- if(ste == 'chat_lobby')then error("chat_lobby is setState only!") state='chat_lobby' end
- end
- function setState(ste,op)
- if(ste == 'title')then title:clear() title:draw(true) state = 'title' end
- if(ste == 'chat_lobby')then if(op)then chatlobby:host() else chatlobby:unhost() end chatlobby:start() state='chat_lobby' end
- end
- setState('title')
- function stChat()
- while mcchat do
- if(state == 'title')then
- title:update()
- end
- if(state == 'chat_lobby')then
- chatlobby:update()
- chatlobby:draw()
- end
- end
- end
- stChat()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement