Advertisement
HPWebcamAble

Message System 2

Dec 15th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.04 KB | None | 0 0
  1. --Coded by HPWebcamAble--
  2. --Note: There are no variables that the user can change here!
  3.  
  4. --Variables--
  5. local vers = "2.0"
  6. local continue = true
  7. local w,h = term.getSize()
  8. local tID = os.getComputerID()
  9. local x = 14
  10. local y = 5
  11. local cPos = 2
  12. local tUser = nil
  13. local users = {}
  14. local hMessage = ""
  15. local aMessage = ""
  16. local messages = {}
  17. local colorsT = {}
  18. local menus = {
  19.   ["main"] = {
  20.     ["options"] = {"Online Users","Back","Quit"},
  21.     ["pos"] = {y+3,y+4,y+5},
  22.   },
  23.   ["users"] = {
  24.     ["options"] = {"Back"},
  25.     ["pos"] = {y+10},
  26.   },
  27.   ["confirmQuit"] = {
  28.     ["options"] = {"Yes","No"},
  29.     ["pos"] = {y+3,y+4},
  30.   }
  31. }
  32. local cMenu = "main"
  33. local select = 1
  34. local menuS = false
  35.  
  36. --Functions--
  37. function clear()
  38.   term.clear()
  39.   term.setCursorPos(1,1)
  40. end
  41.  
  42. function printCentered(text,ypos,color)
  43.   if ypos == nil then
  44.     error("Expected String, Number, got String, Nil")
  45.   end
  46.   if color ~= nil then
  47.     term.setTextColor(color)
  48.   end
  49.   term.setCursorPos(w/2-#text/2,ypos)
  50.   term.write(text)
  51. end
  52.  
  53. function changeMenu()
  54.   local selected = menus[cMenu]["options"][select]
  55.   if cMenu == "main" then
  56.     if selected == "Online Users" then
  57.       cMenu = "users"
  58.     elseif selected == "Back" then
  59.       menuS = false
  60.     elseif selected == "Quit" then
  61.       cMenu = "confirmQuit"
  62.     end
  63.   elseif cMenu == "users" and selected == "Back" then
  64.     cMenu = "main"
  65.   elseif cMenu == "confirmQuit" then
  66.     if selected == "Yes" then
  67.       continue = false
  68.     elseif selected == "No" then
  69.       cMenu = "main"
  70.     end
  71.   end
  72.   select = 1
  73. end
  74.  
  75. function drawMenuBG(h)
  76.   printCentered("+-----------------------------------+",y)
  77.   for i = 1, h do
  78.     printCentered("|                                   |",y+i)
  79.   end
  80.   printCentered("+-----------------------------------+",y+h+1)
  81. end
  82.  
  83. function drawOptions()
  84.   for i = 1, #menus[cMenu]["options"] do
  85.     printCentered(menus[cMenu]["options"][i],menus[cMenu]["pos"][i])
  86.   end
  87.   if select > #menus[cMenu]["options"] then
  88.     select = #menus[cMenu]["options"]
  89.   end
  90.   if select < 1 then
  91.     select = 1
  92.   end
  93.   printCentered("[ "..menus[cMenu]["options"][select].." ]",menus[cMenu]["pos"][select])
  94. end
  95.  
  96. function drawScreen()
  97.   clear()
  98.   while #messages > h-2 do
  99.     table.remove(messages,1)
  100.     table.remove(colorsT,1)
  101.   end
  102.   for i = 1, #messages do
  103.     term.setCursorPos(1,i)
  104.     term.setTextColor(colorsT[i])
  105.     term.write(messages[i])
  106.   end
  107.   if menuS then
  108.     term.setCursorBlink(false)
  109.     term.setCursorPos(1,h)
  110.     term.setTextColor(colors.black)
  111.     term.write("Press Ctrl to close menu")
  112.     if cMenu == "main" then
  113.       drawMenuBG(5)
  114.       printCentered("Menu",y+1)
  115.       drawOptions()
  116.     elseif cMenu == "users" then
  117.       drawMenuBG(10)
  118.       printCentered("Users Online",y+1)
  119.       local t = 0
  120.       for a,b in pairs(users) do
  121.         printCentered(b,y+t+3)
  122.         t = t+1
  123.         if t == 5 then
  124.           break
  125.         end
  126.       end
  127.       term.setCursorPos(1,h-1)
  128.       local numUsers = 0
  129.       for a,b in pairs(users) do
  130.         numUsers=numUsers+1
  131.       end
  132.       if numUsers > 5 then
  133.         printCentered("+"..(numUsers-5).." more",y+8)
  134.       end
  135.       drawOptions()
  136.     elseif cMenu == "confirmQuit" then
  137.       drawMenuBG(4)
  138.       printCentered("Quit?",y+1)
  139.       drawOptions()
  140.     end
  141.   else
  142.     term.setCursorPos(1,h)
  143.     term.setTextColor(colors.orange)
  144.     term.write(">")
  145.     term.write(hMessage)
  146.     term.setCursorPos(cPos,h)
  147.     term.setCursorBlink(true)
  148.   end
  149. end
  150.  
  151. function keyPress(cKey)
  152.   if cKey == 29 then --Left ctrl
  153.     menuS = not menuS
  154.     if not menuS then
  155.       cMenu = "main"
  156.     end
  157.   elseif cKey == 200 then --Up
  158.     if menuS then
  159.       select = select-1
  160.     end
  161.   elseif cKey == 208 then --Down
  162.     if menuS then
  163.       select = select+1
  164.     end
  165.   elseif cKey == 28 then --Enter
  166.     if menuS then
  167.       changeMenu()
  168.     else
  169.       if hMessage ~= "" then
  170.         sendMsg()
  171.       end
  172.     end
  173.   elseif cKey == 205 then --Right
  174.     if not menuS then
  175.       if cPos <= #hMessage+1 then
  176.         cPos = cPos+1
  177.       end
  178.     end
  179.   elseif cKey == 203 then --Left
  180.     if not menuS then
  181.       if cPos > 2 then
  182.         cPos = cPos-1
  183.       end
  184.     end
  185.   elseif cKey == 14 then --Backspace
  186.     if not menuS and cPos > 2 then
  187.       hMessage = string.sub(hMessage,1,cPos-3)..string.sub(hMessage,cPos-1)
  188.       cPos = cPos-1
  189.     end
  190.   elseif cKey == 211 then --Delete
  191.     if not menuS then
  192.       hMessage = string.sub(hMessage,1,cPos-2)..string.sub(hMessage,cPos)
  193.     end
  194.   elseif cKey == 56 then --Alt
  195.     if not menuS then
  196.       hMessage = ""
  197.       cPos = 2
  198.     end
  199.   end
  200. end
  201.  
  202. function charPress(char)
  203.   if not menuS then
  204.     hMessage = string.sub(hMessage,1,cPos-2)..char..string.sub(hMessage,cPos-1)
  205.     cPos = cPos+1
  206.   end
  207. end
  208.      
  209. function formatMessage(channel,aID,aMessage)
  210.   if channel == 2 then
  211.     if users[aID] == nil then
  212.       if aMessage ~= "~offline~" then
  213.         m.transmit(2,tID,tUser)
  214.         users[aID] = aMessage
  215.         table.insert(messages,"*"..aMessage.." is online*")
  216.         table.insert(colorsT,colors.lime)
  217.       end
  218.     elseif aMessage == "~offline~" then
  219.       table.insert(messages,"*"..users[aID].." is offline*")
  220.       table.insert(colorsT,colors.red)
  221.       users[aID] = nil
  222.     end
  223.   elseif channel == 1 then
  224.     if users[aID] == nil then
  225.       table.insert(messages,"Error: Channel has unknown interferrence")
  226.       table.insert(messages,"Message:"..aMessage)
  227.       table.insert(messages,"Reply Chanel:"..aID)
  228.       for i = 1, 3 do
  229.         table.insert(colorsT,colors.black)
  230.       end
  231.     else
  232.       table.insert(messages,"<"..users[aID].."> "..aMessage)
  233.       table.insert(colorsT,colors.blue)
  234.     end
  235.   end
  236. end
  237.  
  238. function sendMsg()
  239.   m.transmit(1,tID,hMessage)
  240.   table.insert(messages,"<"..tUser.."> "..hMessage)
  241.   table.insert(colorsT,colors.orange)
  242.   hMessage = ""
  243.   cPos = 2
  244. end
  245.  
  246. --Program--
  247. if not term.isColor() then
  248.     clear()
  249.     print("Message System "..vers.." requires and advanced computer")
  250.     return
  251.   elseif not fs.exists("msbg") then
  252.     clear()
  253.     print("'msbg' file not found, delete Message System and reinstall it with the installer")
  254.     return
  255.   elseif peripheral.getType("top") ~= "modem" then
  256.     clear()
  257.     print("To use Message System, please place a wireless modem on top of this computer")
  258.     return
  259. end
  260. term.setBackgroundColor(colors.white)
  261. term.clear()
  262. paintutils.drawImage(paintutils.loadImage("msbg"),1,1)
  263.  
  264. sleep(2)
  265.  
  266. term.setTextColor(colors.black)
  267. term.setBackgroundColor(colors.white)
  268. term.setCursorPos(23,16)
  269. term.write("Loading...")
  270. m = peripheral.wrap("top")
  271. m.open(1)
  272. m.open(2)
  273.  
  274. sleep(4)
  275.  
  276. clear()
  277. term.setCursorPos(x-1,y+1)
  278. term.write("Welcome to ")
  279. term.setTextColor(colors.orange)
  280. print("Message System!")
  281. term.setTextColor(colors.black)
  282. printCentered("Enter a user name:",y+2,colors.blue)
  283. term.setCursorPos(x+5,y+3)
  284. term.setTextColor(colors.blue)
  285. term.write(">")
  286. printCentered("Version "..vers,h,colors.lightGray)
  287. term.setCursorPos(x+6,y+3)
  288. term.setTextColor(colors.black)
  289. tUser = read()
  290.  
  291. m.transmit(2,tID,tUser)
  292. clear()
  293. table.insert(messages,"[Username:"..tUser.."]")
  294. table.insert(messages,"[You are now online]")
  295. table.insert(messages,"[Press Ctrl to access menu]")
  296. table.insert(colorsT,colors.lightBlue)
  297. table.insert(colorsT,colors.lime)
  298. table.insert(colorsT,colors.black)
  299. drawScreen()
  300. term.setCursorBlink(true)
  301.  
  302. --Main loop
  303. while continue do
  304.   event,par1,par2,par3,par4,par5 = os.pullEvent()
  305.   if event == "key" then
  306.     keyPress(par1)
  307.   elseif event == "char" then
  308.     charPress(par1)
  309.   elseif event == "modem_message" then
  310.     formatMessage(par2,par3,par4)
  311.   end
  312.   drawScreen()
  313. end
  314.  
  315. --Tell other computers  you are offline
  316. --If you terminate the program before it does this, you may encounter weird errors when running it again
  317. m.transmit(2,tID,"~offline~")
  318. m.closeAll()
  319. term.setTextColor(colors.white)
  320. term.setBackgroundColor(colors.black)
  321. clear()
  322. print("Network Connections have been saftely closed")
  323. term.write("Thank you for choosing")
  324. term.setTextColor(colors.orange)
  325. print(" Message System!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement