JustDoesGames

Game Server

Aug 5th, 2020 (edited)
1,534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.78 KB | None | 0 0
  1. --[[
  2.  
  3. GS (Game Server)
  4. Just Does Games
  5.  
  6. V 1.0.0
  7.  
  8. Init Release
  9.  
  10. V 1.0.1
  11.  
  12. Added Error Support (ish)
  13. Added GS console output when booting a game
  14. Added Game Updater
  15. Added Support to disable updates for GS and games separate
  16.  
  17. V 1.0.2
  18.  
  19. Added game 'Draw'
  20. Removed 'Stocks' due to saving issues and being old
  21. Fixed install by requiring the 'gmupdate' to be true
  22.  
  23. ]]--
  24. local version = "v1.0.2"
  25. local gsupdate = true -- Change this if you want gs updates
  26. local gmupdate = true -- Change this if you want game updates
  27.  
  28. if not http then return printError("Http is not enabled.") end
  29.  
  30. --[[
  31.  
  32. Menu engine
  33.  
  34. ]]--
  35.  
  36. function m(x,y,x2,y2,menu)
  37.     menu = menu or error("Failed to load Menu.")
  38.     x,y = x or error("Failed to load x"), y or error("Failed to load y")
  39.     x2,y2 = x2 or error("Failed to load x2"), y2 or error("Failed to load y2")
  40.     local sel, scroll, run, w, h = 1,0,true, term.getSize()
  41.     while run do
  42.         for i=1, y2-y do
  43.             if menu[i+scroll] ~= nil then
  44.                 term.setCursorPos(x,y+i-1)
  45.                 term.setTextColor(colors.white)
  46.                 if sel == i then write("> ") else write("  ") end
  47.                 if menu[i+scroll] == "Exit" then term.setTextColor(colors.white) elseif fs.exists(menu[i+scroll]..".lua") then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
  48.                 write(menu[i+scroll])
  49.             end
  50.         end
  51.         a,b = os.pullEvent("key")
  52.         if b == keys.w or b == keys.up then
  53.             if sel == 1 and scroll ~= 0 then
  54.                 scroll = scroll - 1
  55.             elseif sel ~= 1 then
  56.                 sel = sel - 1
  57.             end
  58.         elseif b == keys.s or b == keys.down then
  59.             if sel == y2-y and scroll ~= #menu-(y2-y) then
  60.                 scroll = scroll + 1
  61.             elseif sel ~= math.min(y2-y,#menu) then
  62.                 sel = sel + 1
  63.             end
  64.         elseif b == keys.e or b == keys.enter then
  65.             return sel+scroll
  66.         end
  67.     end
  68. end
  69.  
  70. --[[
  71.  
  72. Menu engine
  73.  
  74. ]]--
  75.  
  76. local w,h = term.getSize()
  77. local gm, gameDir = {}, "/"
  78.  
  79. local games = {
  80.     {
  81.         name = "Clicker",
  82.         pb = "FUzxUXuy"
  83.     },
  84.     {
  85.         name = "Draw",
  86.         pb = "DSWtjD9y"
  87.     },
  88.         {
  89.                name = "Beta",
  90.                pb = "x7gg2hbd"
  91.         },
  92. }
  93.  
  94. function searchForUpdates(gameid)
  95.     if games[gameid] then
  96.         print("Searching for updated for "..games[gameid].name.."...")
  97.         local h = http.get("https://pastebin.com/raw/"..games[gameid].pb)
  98.         if h then
  99.             local f = fs.open(gameDir..games[gameid].name..".lua", "r")
  100.             if f then
  101.                 local old, new = f.readAll(), h.readAll()
  102.                 f.close() h.close()
  103.                 if old ~= new then
  104.                     print("Updating...")
  105.                     local f = fs.open(gameDir..games[gameid].name..".lua", "w")
  106.                     f.write(new)
  107.                     f.close()
  108.                 end
  109.             else
  110.                 h.close()
  111.                 error("Failed to install game # "..gameid)
  112.             end
  113.         end
  114.     else
  115.         error("Failed to find game #"..gameid)
  116.     end
  117. end
  118.  
  119. function install(gameid)
  120.     if games[gameid] then
  121.         print("Downloading "..games[gameid].name.."...")
  122.         local h = http.get("https://pastebin.com/raw/"..games[gameid].pb)
  123.         if h then
  124.             local f = fs.open(gameDir..games[gameid].name..".lua", "w")
  125.             if f then
  126.                 print("Installing "..games[gameid].name.."...")
  127.                 f.write(h.readAll())
  128.                 f.close() h.close()
  129.             else
  130.                 h.close()
  131.                 error("Failed to install game # "..gameid)
  132.             end
  133.         else
  134.             error("Failed to download game #"..gameid)
  135.         end
  136.     else
  137.         error("Failed to find game #"..gameid)
  138.     end
  139. end
  140.  
  141. function rungame(gameid)
  142.     term.clear() term.setCursorPos(1,1) print("GS") for i=1, w do write("=") end
  143.     if games[gameid] then
  144.         if not fs.exists(gameDir..games[gameid].name..".lua") and gmupdate then
  145.             install(gameid)
  146.         elseif gmupdate then
  147.             searchForUpdates(gameid)
  148.         end
  149.         local r = os.run({}, gameDir..games[gameid].name..".lua")
  150.         if not r then
  151.             print("")
  152.             print("GS") for i=1, w do write("=") end
  153.             print("Program Crashed with error")
  154.             sleep(1)
  155.             print("")
  156.             print("Press any key to exit.")
  157.             os.pullEvent()
  158.         end
  159.     else
  160.         error("Failed to find game #"..gameid)
  161.     end
  162. end
  163.  
  164. function runtime()
  165.     local run = true
  166.     while run do
  167.         gm = {}
  168.         for i=1, #games do
  169.             table.insert(gm, games[i].name)
  170.         end
  171.         table.insert(gm, "Exit")
  172.         term.setBackgroundColor(colors.black)
  173.         term.setTextColor(colors.white)
  174.         term.clear()
  175.         term.setCursorPos(1,1)
  176.         write("Game Server "..version.."  |  Just Does Games")
  177.         local x = m(1,3,w,h-1,gm)
  178.         if x == #gm then run = false else rungame(x) end
  179.     end
  180. end
  181.  
  182.  
  183.  
  184. print("Booting GS...")
  185.  
  186. if gsupdate then
  187.     local h = http.get("https://pastebin.com/raw/sKFnL4sr")
  188.     if h then
  189.         local f = fs.open(shell.getRunningProgram(), "r")
  190.         if f then
  191.             local old = f.readAll()
  192.             local new = h.readAll()
  193.             h.close()
  194.             f.close()
  195.             if old ~= new then
  196.                 f = fs.open(shell.getRunningProgram(), "w")
  197.                 f.write(new)
  198.                 f.close()
  199.             end
  200.         end
  201.     end
  202. end
  203.  
  204. runtime()
  205.  
  206. term.setBackgroundColor(colors.black)
  207. term.setTextColor(colors.white)
  208. term.clear()
  209. term.setCursorPos(1,1) sleep(.1)
Add Comment
Please, Sign In to add comment