Advertisement
nitrogenfingers

nitrosoft games catalogue

Oct 9th, 2014
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.51 KB | None | 0 0
  1. --[[
  2.     Nitrosoft Games Catalogue
  3.    
  4.     A listing of all games released and available for installation by Nitrogen Fingers
  5.     Part installer, part advertising tool...
  6. ]]--
  7. local catalogueCode = "KBApP73v"
  8. local entries = {}
  9.  
  10. local _w,_h = term.getSize()
  11.  
  12. local function writeC(msg, col, x, y)
  13.     if x and y then term.setCursorPos(x,y) end
  14.     if term.isColour() and col then
  15.         term.setTextColour(col)
  16.     end
  17.     term.write(msg)
  18. end
  19.  
  20. function wprintOffCenter(msg, height, width, offset)
  21.     local inc = 0
  22.     local ops = 1
  23.     while #msg - ops > width do
  24.         local nextspace = 0
  25.         while string.find(msg, " ", ops + nextspace) and
  26.                 string.find(msg, " ", ops + nextspace) - ops < width do
  27.             nextspace = string.find(msg, " ", nextspace + ops) + 1 - ops
  28.         end
  29.         local ox,oy = term.getCursorPos()
  30.         term.setCursorPos(width/2 - (nextspace)/2 + offset, height + inc)
  31.         inc = inc + 1
  32.         term.write(string.sub(msg, ops, nextspace + ops - 1))
  33.         ops = ops + nextspace
  34.     end
  35.     term.setCursorPos(width/2 - #string.sub(msg, ops)/2 + offset, height + inc)
  36.     term.write(string.sub(msg, ops))
  37.    
  38.     return inc + 1
  39. end
  40.  
  41. if not http then
  42.     print("The catalogue requires HTTP to run. Please ensure it is enabled.")
  43. end
  44.  
  45. local function getCatalogue()
  46.     write("Downloading catalogue, please wait...")
  47.     local httprep = http.get(
  48.         "http://pastebin.com/raw.php?i="..textutils.urlEncode(catalogueCode)
  49.     )
  50.        
  51.     if httprep then
  52.         local line = httprep.readLine()
  53.         local currEntry = nil
  54.         while line do
  55.             if string.find(line, "ENTRY ") == 1 then
  56.                 if currEntry then table.insert(entries, currEntry) end
  57.                 currEntry = { title = string.sub(line, 7); updates = {} }
  58.             elseif string.find(line, "V ") == 1 then
  59.                 currEntry.version = tonumber(string.sub(line, 3))
  60.             elseif string.find(line, "COL ") == 1 then
  61.                 currEntry.mono = string.find(line, "yes") == 5
  62.             elseif string.find(line, "CODE ") == 1 then
  63.                 currEntry.pastecode = string.sub(line, 6)
  64.                 if #currEntry.pastecode ~= 8 then pastecode = nil end
  65.             elseif string.find(line, "DESC ") == 1 then
  66.                 currEntry.desc = string.sub(line, 6)
  67.             elseif string.find(line, "UPDATE ") == 1 then
  68.                 line = string.sub(line, 7)
  69.                 local _n = string.sub(line, 1, string.find(line, " "))
  70.                 table.insert(currEntry.updates, {
  71.                     version = tonumber(_n);
  72.                     desc = string.sub(line, #_n+1)
  73.                 })
  74.             end
  75.             line = httprep.readLine()
  76.         end
  77.         table.insert(entries, currEntry)
  78.         return true
  79.     else
  80.         print("Unable to access the catalogue: "..http.getResponseCode())
  81.         return false
  82.     end
  83. end
  84.  
  85. local states = { "SELECT", "DISPLAY", "GET", "QUIT", "UPDATE" }
  86. local cstate = states[1]
  87. local mensel = 1
  88. local selEntry = nil
  89.  
  90. local function displayNitrosoftTitle()
  91.     shell.run("clear")
  92.     local _t1,_t2,_t3 = "nitrosoft", "games", "Game Catalogue"
  93.     term.setCursorPos(math.floor(_w/2-#_t1), 4)
  94.     if term.isColour() then term.setTextColour(colours.blue) end
  95.     term.write(_t1)
  96.     term.setCursorPos(math.floor(_w/2-#_t2/2),5)
  97.     term.setTextColour(colours.white)
  98.     term.write(_t2)
  99.     if term.isColour() then term.setTextColour(colours.red) end
  100.     term.setCursorPos(math.floor(_w/2-#_t1), 6)
  101.     term.write(string.rep("-",#_t1*2))
  102.     if term.isColour() then
  103.         term.setBackgroundColour(colours.green)
  104.         term.setCursorPos(_w/2+#_t1-4, 2)
  105.         term.write("  ")
  106.         term.setCursorPos(_w/2+#_t1-4, 3)
  107.         term.write("  ")
  108.         term.setBackgroundColour(colours.lime)
  109.         term.setCursorPos(_w/2+#_t1-3, 3)
  110.         term.write("  ")
  111.         term.setCursorPos(_w/2+#_t1-3, 4)
  112.         term.write("  ")
  113.     end
  114.     term.setBackgroundColour(colours.black)
  115.     term.setTextColour(colours.white)
  116. end
  117.  
  118. local function updateTitleMenu()
  119.     term.setBackgroundColour(colours.black)
  120.     term.setTextColour(colours.white)
  121.     for i=1,#entries do
  122.         term.setCursorPos(_w/2-#entries[i].title/2-2, 7+i)
  123.         if mensel == i then
  124.             term.setTextColour(colours.white)
  125.             term.write("> ")
  126.         else term.write("  ") end
  127.         term.setTextColour(colours.yellow)
  128.         term.write(entries[i].title)
  129.         if mensel == i then
  130.             term.setTextColour(colours.white)
  131.             term.write(" <")
  132.         else term.write("  ") end
  133.     end
  134.     local _t4 = "Exit Catalogue"
  135.    
  136.     term.setCursorPos(_w/2-#_t4/2-2, 9+#entries)
  137.     if mensel == #entries + 1 then
  138.         term.setTextColour(colours.white)
  139.         term.write("> ")
  140.     else term.write("  ") end
  141.     term.setTextColour(colours.red)
  142.     term.write(_t4)
  143.     if mensel == #entries + 1 then
  144.         term.setTextColour(colours.white)
  145.         term.write(" <")
  146.     else term.write("  ") end
  147. end
  148.  
  149. local function runTitle()
  150.     displayNitrosoftTitle()
  151.     while true do
  152.         updateTitleMenu()
  153.         local _,key = os.pullEvent("key")
  154.         if key == keys.up then
  155.             if mensel > 1 then mensel = mensel - 1 end
  156.         elseif key == keys.down then
  157.             if mensel <= #entries then mensel = mensel + 1 end
  158.         elseif key == keys.enter or key == keys.space then
  159.             if mensel == #entries + 1 then cstate = states[4]
  160.             else
  161.                 selEntry = entries[mensel]
  162.                 cstate = states[2]
  163.             end
  164.             return
  165.         end
  166.     end
  167. end
  168.  
  169. local function downloadEntry()
  170.     shell.run("clear")
  171.     term.setTextColour(colours.white)
  172.     print("Beginning installation\n*******************")
  173.     local lpath = string.gsub(selEntry.title.."_installer"," ","")
  174.     shell.run("pastebin", "get", selEntry.pastecode, lpath)
  175.     print(" ")
  176.     if fs.exists(shell.resolve(lpath)) then
  177.         print("running "..shell.resolve(lpath))
  178.         local _result = shell.run("/"..shell.resolve(lpath))
  179.         fs.delete(shell.resolve(lpath))
  180.         sleep(1)
  181.         shell.run("clear")
  182.     else
  183.         shell.run("clear")
  184.         displayNitrosoftTitle()
  185.         _msg = selEntry.title.." was unable to download."
  186.         writeC(_msg, colours.white, _w/2-#_msg/2,8)
  187.         _msg = "Press a key to continue"
  188.         writeC(_msg, colours.yellow, _w/2-#_msg/2,_h-3)
  189.         os.pullEvent("key")
  190.     end
  191.     cstate = states[1]
  192.     term.clear()
  193. end
  194.  
  195. local function updateEntryMenu()
  196.     local _opt = { "Install", "Updates", "Back" }
  197.     mensel = 1
  198.    
  199.     repeat
  200.         term.setCursorPos(1,_h-2)
  201.         term.clearLine()
  202.         if mensel == 1 then
  203.             writeC("> "..string.rep(" ",#_opt[mensel]).." <",
  204.             colours.white, 2,_h-2)
  205.         elseif mensel == 2 then
  206.             writeC("> "..string.rep(" ",#_opt[mensel]).." <",
  207.             colours.white,_w/2-#_opt[mensel]/2,_h-2)
  208.         else
  209.             writeC("> "..string.rep(" ",#_opt[mensel]).." <",
  210.             colours.white,_w-#_opt-5,_h-2)
  211.         end
  212.         writeC(_opt[1],colours.yellow,4,_h-2)
  213.         --writeC(_opt[2],colours.yellow,_w/2-#_opt/2,_h-2)
  214.         writeC(_opt[3],colours.yellow,_w-#_opt-3,_h-2)
  215.        
  216.         local _,key = os.pullEvent("key")
  217.         if key == keys.left then
  218.             if mensel > 1 then mensel = mensel - 2 end
  219.         elseif key == keys.right then
  220.             if mensel < #_opt then mensel = mensel + 2 end
  221.         elseif key == keys.enter then
  222.             if mensel == 1 then cstate = states[3]
  223.             elseif mensel == 2 then cstate = states[4]
  224.             elseif mensel == 3 then cstate = states[1]
  225.             end
  226.             break
  227.         end
  228.     until false
  229.     term.clear()
  230.     mensel = 1
  231. end
  232.  
  233. local function runEntry()
  234.     term.setBackgroundColour(colours.black)
  235.     term.clear()
  236.     displayNitrosoftTitle()
  237.     writeC(selEntry.title, colours.yellow, _w/2-#selEntry.title/2,7)
  238.     term.setTextColour(colours.white)
  239.     local inc = 0
  240.     wprintOffCenter(selEntry.desc, 9, _w-3, 3)
  241.     writeC("Version "..selEntry.version, colours.white, 3,_h-4)
  242.     local cmsg = "Monochrome "
  243.     if selEntry.mono then cmsg = cmsg.."supported"
  244.     else cmsg = cmsg.."not supported" end
  245.     writeC(cmsg, colours.white, _w-3-#cmsg,_h-4)
  246.    
  247.     updateEntryMenu()
  248. end
  249.  
  250. if not getCatalogue() then return end
  251. shell.run("clear")
  252. repeat
  253.     if cstate == states[1] then
  254.         runTitle()
  255.     elseif cstate == states[2] then
  256.         runEntry()
  257.     elseif cstate == states[3] then
  258.         downloadEntry()
  259.     end
  260. until cstate == states[4]
  261. shell.run("clear")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement