Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Coded by HPWebcamAble--
- --Note: There are no variables that the user can change here!
- --Variables--
- local vers = "2.0"
- local continue = true
- local w,h = term.getSize()
- local tID = os.getComputerID()
- local x = 14
- local y = 5
- local cPos = 2
- local tUser = nil
- local users = {}
- local hMessage = ""
- local aMessage = ""
- local messages = {}
- local colorsT = {}
- local menus = {
- ["main"] = {
- ["options"] = {"Online Users","Back","Quit"},
- ["pos"] = {y+3,y+4,y+5},
- },
- ["users"] = {
- ["options"] = {"Back"},
- ["pos"] = {y+10},
- },
- ["confirmQuit"] = {
- ["options"] = {"Yes","No"},
- ["pos"] = {y+3,y+4},
- }
- }
- local cMenu = "main"
- local select = 1
- local menuS = false
- --Functions--
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function printCentered(text,ypos,color)
- if ypos == nil then
- error("Expected String, Number, got String, Nil")
- end
- if color ~= nil then
- term.setTextColor(color)
- end
- term.setCursorPos(w/2-#text/2,ypos)
- term.write(text)
- end
- function changeMenu()
- local selected = menus[cMenu]["options"][select]
- if cMenu == "main" then
- if selected == "Online Users" then
- cMenu = "users"
- elseif selected == "Back" then
- menuS = false
- elseif selected == "Quit" then
- cMenu = "confirmQuit"
- end
- elseif cMenu == "users" and selected == "Back" then
- cMenu = "main"
- elseif cMenu == "confirmQuit" then
- if selected == "Yes" then
- continue = false
- elseif selected == "No" then
- cMenu = "main"
- end
- end
- select = 1
- end
- function drawMenuBG(h)
- printCentered("+-----------------------------------+",y)
- for i = 1, h do
- printCentered("| |",y+i)
- end
- printCentered("+-----------------------------------+",y+h+1)
- end
- function drawOptions()
- for i = 1, #menus[cMenu]["options"] do
- printCentered(menus[cMenu]["options"][i],menus[cMenu]["pos"][i])
- end
- if select > #menus[cMenu]["options"] then
- select = #menus[cMenu]["options"]
- end
- if select < 1 then
- select = 1
- end
- printCentered("[ "..menus[cMenu]["options"][select].." ]",menus[cMenu]["pos"][select])
- end
- function drawScreen()
- clear()
- while #messages > h-2 do
- table.remove(messages,1)
- table.remove(colorsT,1)
- end
- for i = 1, #messages do
- term.setCursorPos(1,i)
- term.setTextColor(colorsT[i])
- term.write(messages[i])
- end
- if menuS then
- term.setCursorBlink(false)
- term.setCursorPos(1,h)
- term.setTextColor(colors.black)
- term.write("Press Ctrl to close menu")
- if cMenu == "main" then
- drawMenuBG(5)
- printCentered("Menu",y+1)
- drawOptions()
- elseif cMenu == "users" then
- drawMenuBG(10)
- printCentered("Users Online",y+1)
- local t = 0
- for a,b in pairs(users) do
- printCentered(b,y+t+3)
- t = t+1
- if t == 5 then
- break
- end
- end
- term.setCursorPos(1,h-1)
- local numUsers = 0
- for a,b in pairs(users) do
- numUsers=numUsers+1
- end
- if numUsers > 5 then
- printCentered("+"..(numUsers-5).." more",y+8)
- end
- drawOptions()
- elseif cMenu == "confirmQuit" then
- drawMenuBG(4)
- printCentered("Quit?",y+1)
- drawOptions()
- end
- else
- term.setCursorPos(1,h)
- term.setTextColor(colors.orange)
- term.write(">")
- term.write(hMessage)
- term.setCursorPos(cPos,h)
- term.setCursorBlink(true)
- end
- end
- function keyPress(cKey)
- if cKey == 29 then --Left ctrl
- menuS = not menuS
- if not menuS then
- cMenu = "main"
- end
- elseif cKey == 200 then --Up
- if menuS then
- select = select-1
- end
- elseif cKey == 208 then --Down
- if menuS then
- select = select+1
- end
- elseif cKey == 28 then --Enter
- if menuS then
- changeMenu()
- else
- if hMessage ~= "" then
- sendMsg()
- end
- end
- elseif cKey == 205 then --Right
- if not menuS then
- if cPos <= #hMessage+1 then
- cPos = cPos+1
- end
- end
- elseif cKey == 203 then --Left
- if not menuS then
- if cPos > 2 then
- cPos = cPos-1
- end
- end
- elseif cKey == 14 then --Backspace
- if not menuS and cPos > 2 then
- hMessage = string.sub(hMessage,1,cPos-3)..string.sub(hMessage,cPos-1)
- cPos = cPos-1
- end
- elseif cKey == 211 then --Delete
- if not menuS then
- hMessage = string.sub(hMessage,1,cPos-2)..string.sub(hMessage,cPos)
- end
- elseif cKey == 56 then --Alt
- if not menuS then
- hMessage = ""
- cPos = 2
- end
- end
- end
- function charPress(char)
- if not menuS then
- hMessage = string.sub(hMessage,1,cPos-2)..char..string.sub(hMessage,cPos-1)
- cPos = cPos+1
- end
- end
- function formatMessage(channel,aID,aMessage)
- if channel == 2 then
- if users[aID] == nil then
- if aMessage ~= "~offline~" then
- m.transmit(2,tID,tUser)
- users[aID] = aMessage
- table.insert(messages,"*"..aMessage.." is online*")
- table.insert(colorsT,colors.lime)
- end
- elseif aMessage == "~offline~" then
- table.insert(messages,"*"..users[aID].." is offline*")
- table.insert(colorsT,colors.red)
- users[aID] = nil
- end
- elseif channel == 1 then
- if users[aID] == nil then
- table.insert(messages,"Error: Channel has unknown interferrence")
- table.insert(messages,"Message:"..aMessage)
- table.insert(messages,"Reply Chanel:"..aID)
- for i = 1, 3 do
- table.insert(colorsT,colors.black)
- end
- else
- table.insert(messages,"<"..users[aID].."> "..aMessage)
- table.insert(colorsT,colors.blue)
- end
- end
- end
- function sendMsg()
- m.transmit(1,tID,hMessage)
- table.insert(messages,"<"..tUser.."> "..hMessage)
- table.insert(colorsT,colors.orange)
- hMessage = ""
- cPos = 2
- end
- --Program--
- if not term.isColor() then
- clear()
- print("Message System "..vers.." requires and advanced computer")
- return
- elseif not fs.exists("msbg") then
- clear()
- print("'msbg' file not found, delete Message System and reinstall it with the installer")
- return
- elseif peripheral.getType("top") ~= "modem" then
- clear()
- print("To use Message System, please place a wireless modem on top of this computer")
- return
- end
- term.setBackgroundColor(colors.white)
- term.clear()
- paintutils.drawImage(paintutils.loadImage("msbg"),1,1)
- sleep(2)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(23,16)
- term.write("Loading...")
- m = peripheral.wrap("top")
- m.open(1)
- m.open(2)
- sleep(4)
- clear()
- term.setCursorPos(x-1,y+1)
- term.write("Welcome to ")
- term.setTextColor(colors.orange)
- print("Message System!")
- term.setTextColor(colors.black)
- printCentered("Enter a user name:",y+2,colors.blue)
- term.setCursorPos(x+5,y+3)
- term.setTextColor(colors.blue)
- term.write(">")
- printCentered("Version "..vers,h,colors.lightGray)
- term.setCursorPos(x+6,y+3)
- term.setTextColor(colors.black)
- tUser = read()
- m.transmit(2,tID,tUser)
- clear()
- table.insert(messages,"[Username:"..tUser.."]")
- table.insert(messages,"[You are now online]")
- table.insert(messages,"[Press Ctrl to access menu]")
- table.insert(colorsT,colors.lightBlue)
- table.insert(colorsT,colors.lime)
- table.insert(colorsT,colors.black)
- drawScreen()
- term.setCursorBlink(true)
- --Main loop
- while continue do
- event,par1,par2,par3,par4,par5 = os.pullEvent()
- if event == "key" then
- keyPress(par1)
- elseif event == "char" then
- charPress(par1)
- elseif event == "modem_message" then
- formatMessage(par2,par3,par4)
- end
- drawScreen()
- end
- --Tell other computers you are offline
- --If you terminate the program before it does this, you may encounter weird errors when running it again
- m.transmit(2,tID,"~offline~")
- m.closeAll()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- clear()
- print("Network Connections have been saftely closed")
- term.write("Thank you for choosing")
- term.setTextColor(colors.orange)
- print(" Message System!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement