JustDoesGames

Stocks | 2 Hour Challenge

Jul 8th, 2020 (edited)
2,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.33 KB | None | 0 0
  1. --[[
  2.  
  3. Stocks
  4. Game Built in 2 hours
  5.  
  6. Just Does Games
  7.  
  8. ]]--
  9.  
  10. term.clear()
  11. local w,h = term.getSize()
  12. if not term.isColor() then return printError("Advanced Computer Required.") end
  13. local run = true
  14.  
  15. local seed = os.epoch("utc") -- true randomness
  16. math.randomseed(seed)
  17. math.random(1,1) -- to avoid prediction w/ CraftOS-PC
  18.  
  19. local menuGlobalSelected = 1
  20.  
  21. local player = {}
  22. player.money = 200
  23. player.location = 1
  24. player.stock = 0
  25.  
  26. local stock = {}
  27. stock.level = 0 -- current level
  28. stock.value = 1 -- inc and dec values
  29. stock.maxrate = 50 -- rng max
  30. stock.minrate = 2 -- rng min
  31. stock.rate = math.random(1, stock.maxrate) -- controls the ups and downs
  32. stock.caps = 200 -- max up and down
  33. stock.stability = 5 -- 5 seconds of freedom
  34.  
  35. local ms = {}
  36. ms.main = {{"Buy", "Sell", "Exit"}, {
  37.     --function() table.insert(ms.history, ms.stocks) end,
  38.     function() purchase_stock() end,
  39.     function() sell_stock() end,
  40.     --function() end,
  41.     function() run = false end,
  42. }}
  43.  
  44. ms.stocks = {{"test", "Exit"}, {
  45.     function() end,
  46.     function() table.remove(ms.history, #ms.history) menuGlobalSelected = 1 end,
  47. }}
  48.  
  49. ms.history = {ms.main}
  50.  
  51. --[[
  52.  
  53. Menu engine
  54.  
  55. ]]--
  56.  
  57. function m(x,y,x2,y2,menu)
  58.     menu = menu[1] or error("Failed to load Menu.")
  59.     x,y = x or error("Failed to load x"), y or error("Failed to load y")
  60.     x2,y2 = x2 or error("Failed to load x2"), y2 or error("Failed to load y2")
  61.     local sel, scroll, run, w, h = 1,0,true, term.getSize()
  62.     sel = menuGlobalSelected
  63.     while run do
  64.         for i=1, y2-y do
  65.             if menu[i+scroll] ~= nil then
  66.                 term.setCursorPos(x,y+i-1)
  67.                 term.setTextColor(colors.white)
  68.                 if sel == i then write("> ") else write("  ") end write(menu[i+scroll])
  69.             end
  70.         end
  71.         a,b = os.pullEvent("key")
  72.         if b == keys.w or b == keys.up then
  73.             if sel == 1 and scroll ~= 0 then
  74.                 scroll = scroll - 1
  75.             elseif sel ~= 1 then
  76.                 sel = sel - 1
  77.             end
  78.         elseif b == keys.s or b == keys.down then
  79.             if sel == y2-y and scroll ~= #menu-(y2-y) then
  80.                 scroll = scroll + 1
  81.             elseif sel ~= math.min(y2-y,#menu) then
  82.                 sel = sel + 1
  83.             end
  84.         elseif b == keys.e or b == keys.enter then
  85.             --paintutils.drawFilledBox(x,y,x2,y2,colors.black)
  86.             menuGlobalSelected = sel
  87.             return sel+scroll
  88.         end
  89.     end
  90.     paintutils.drawFilledBox(x,y,x2,y2,colors.black)
  91. end
  92.  
  93. --[[
  94.  
  95. Menu engine
  96.  
  97. ]]--
  98.  
  99. function updateGUI()
  100.     term.setCursorPos(1,h-2)
  101.     term.setBackgroundColor(colors.black)
  102.     term.setTextColor(colors.white)
  103.     print("Stock: "..player.stock.."     ")
  104.     print("Money: "..player.money.."     ")
  105. end
  106.  
  107. function purchase_stock()
  108.     local cost = stock.level
  109.     if cost > 0 then cost = cost * 2 end
  110.     if player.money >= math.abs(cost) then
  111.         player.stock = player.stock + 1
  112.         player.money = player.money - math.abs(cost)
  113.     end
  114. end
  115.  
  116. function sell_stock()
  117.     local pay = stock.level
  118.     if pay < 0 then pay = math.ceil(pay/2) end
  119.     if player.stock > 0 then
  120.         player.stock = player.stock - 1
  121.         player.money = player.money + pay
  122.     end
  123. end
  124.  
  125. function runMenu()
  126.     while run do
  127.         ms.history[#ms.history][2][m(3,3,w,h,ms.history[#ms.history])]()
  128.         updateGUI()
  129.     end
  130. end
  131.  
  132. function stock_rate_cycle()
  133.     while run do
  134.         sleep(stock.stability)
  135.         stock.rate = math.random(1, stock.maxrate)
  136.         player.money = player.money + 1
  137.         updateGUI()
  138.         --term.setCursorPos(1,2)
  139.         --print(stock.rate.." | "..math.floor(math.abs(stock.level)/stock.caps*10))
  140.     end
  141. end
  142.  
  143. function runStock()
  144.     while run do
  145.         sleep(.08)
  146.         term.setCursorPos(1,1) term.setTextColor(colors.white) write("Market ")
  147.         term.setBackgroundColor(colors.gray) write("          ")
  148.         term.setBackgroundColor(colors.black)
  149.         if stock.rate < math.floor(stock.maxrate/2) then
  150.             term.setTextColor(colors.lime)
  151.         elseif stock.rate > math.floor(stock.maxrate/2) then
  152.             term.setTextColor(colors.red)
  153.         else
  154.             term.setTextColor(colors.orange)
  155.         end
  156.         write(" $"..stock.level.."  ")
  157.         term.setCursorPos(8,1)
  158.        
  159.         local res = math.random(1,stock.maxrate)
  160.         if res > stock.rate then
  161.             stock.level = math.min(stock.level+stock.value, stock.caps)
  162.             res = "^"
  163.         elseif res < stock.rate then
  164.             stock.level = math.max(stock.level-stock.value, stock.caps*-1)
  165.             res = "!"
  166.         else
  167.             res = "-"
  168.         end
  169.         if stock.level > 0 then term.setBackgroundColor(colors.lime) else term.setBackgroundColor(colors.red) end
  170.         --print("Stock: "..stock.level.." "..res.."     ")
  171.         for i=1, math.floor(math.abs(stock.level)/stock.caps*10) do
  172.             write(" ")
  173.         end
  174.         term.setBackgroundColor(colors.black)
  175.         --print(math.abs(stock.level)/stock.caps.." | "..stock.level.."/"..stock.caps)
  176.     end
  177. end
  178.  
  179. function run()
  180.     updateGUI()
  181.     parallel.waitForAny(runStock, stock_rate_cycle, runMenu)
  182. end
  183.  
  184. --[[
  185. term.setCursorPos(1,1)
  186. term.setTextColor(colors.white)
  187. term.setBackgroundColor(colors.black)
  188. term.clear()
  189. print("How To Play Stocks:")
  190. print("")
  191. print("Buy Stocks while it is in the red.")
  192. print("Buying Stocks while in the green will make them cost twice as much.")
  193. print("")
  194. print("Sell Stocks while it is in the green.")
  195. print("Selling Stocks while in the red will make them lose twice as much.")
  196. print("")
  197. print("Sorry if this seems incomplete, this was a challenge to see what I could make in 2 hours.")
  198. print("")
  199. print("Goal: Become a Millionaire!")
  200. sleep(5)
  201. print("")
  202. print("Press any key to start...")
  203. os.pullEvent()
  204. term.clear()
  205. sleep(1)
  206. --]]
  207.  
  208. run()
  209.  
  210. term.clear()
  211. term.setCursorPos(1,1)
  212. sleep(.2)
Advertisement
Add Comment
Please, Sign In to add comment