Shadows_Player

Computercraft coffe machine

Jun 11th, 2022
1,391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. rednet.open("back")
  2.  
  3. -- Username
  4. local ufile = fs.open("disk/user.lua","r")
  5. local username = ufile.readLine()
  6. ufile.close()
  7.  
  8. -- Money Read
  9. local mrfile = fs.open("disk/money.lua","r")
  10. local money = mrfile.readLine()
  11. mrfile.close()
  12.  
  13. -- Coffe Read
  14. local cffile = fs.open("disk/coffe.lua","r")
  15. local cff = cffile.readLine()
  16. cffile.close()
  17.  
  18. --[[ Local Variables ]]--
  19. local selectedItem = 1
  20. local inMainMenu = true
  21. local insubMenu = false
  22.  
  23. -- Menu Methods --
  24. -- Deposit Start
  25.  
  26. local function insufficientFunds(current, required)
  27.     if current < required then
  28.         print("You don't have enough money")
  29.         sleep(2)
  30.         disk.eject("right")
  31.         os.reboot() -- the reboot will negate the need to return a value.
  32.         -- I still recommend removing the reboots for this program.
  33.     end
  34.     return false -- you have sufficient funds
  35. end
  36.  
  37. if money > 5 then
  38.     print("You don't have enougth money")
  39.     make()
  40.     sleep(2)
  41.     disk.eject("right")
  42.     os.reboot()
  43. end
  44.  
  45. function line()
  46.     term.clear()
  47.     term.setCursorPos(1,1)
  48.     term.write(string.rep("-", 51)) -- Repeat string x amount of times
  49.     term.setCursorPos(1,19)
  50.     term.write(string.rep("-", 51))
  51. end
  52.  
  53. function sugar()
  54.   if tonumber(money) < 5 then
  55.   print("You don't have enougth money")
  56.   make()
  57.   sleep(2)
  58.   disk.eject("right")
  59.   os.reboot()
  60.   else
  61.  
  62.   m = money - 5
  63.   local wfile = fs.open("disk/money.lua","w")
  64.   wfile.write(m)
  65.   wfile.close()
  66.  
  67.   c = cff - 1
  68.   local cffile = fs.open("disk/coffe.lua","w")
  69.   cffile.write(c)
  70.   cffile.close()
  71.  
  72.   rednet.broadcast("sugar","cof1")
  73.   os.reboot()
  74.   end
  75. end
  76.  
  77. function blaze()
  78.  if tonumber(money) < 10 then
  79.  print("You don't have enougth money")
  80.  make()
  81.  sleep(2)
  82.  disk.eject("right")
  83.  os.reboot()
  84.  else
  85.  m = money - 10
  86.  local wfile = fs.open("disk/money.lua","w")
  87.  wfile.write(m)
  88.  wfile.close()
  89.  
  90.  c = cff - 1
  91.  local cffile = fs.open("disk/coffe.lua","w")
  92.  cffile.write(c)
  93.  cffile.close()
  94.  
  95.  rednet.broadcast("blaze","cof1")
  96.  os.reboot()
  97.  end
  98. end
  99.  
  100. function cream()
  101.  if tonumber(money) < 10 then
  102.  print("You don't have enougth money")
  103.  make()
  104.  sleep(2)
  105.  disk.eject("right")
  106.  os.reboot()
  107.  else
  108.  m = money - 10
  109.  local wfile = fs.open("disk/money.lua","w")
  110.  wfile.write(m)
  111.  wfile.close()
  112.  
  113.  c = cff - 1
  114.  local cffile = fs.open("disk/coffe.lua","w")
  115.  cffile.write(c)
  116.  cffile.close()
  117.  
  118.  rednet.broadcast("cream","cof1")
  119.  os.reboot()
  120.  end
  121. end
  122.  
  123. function golden()
  124.  if tonumber(money) < 25 then
  125.  print("You don't have enougth money")
  126.  make()
  127.  sleep(2)
  128.  disk.eject("right")
  129.  os.reboot()
  130.  else
  131.  m = money - 25
  132.  local wfile = fs.open("disk/money.lua","w")
  133.  wfile.write(m)
  134.  wfile.close()
  135.  
  136.  c = cff - 1
  137.  local cffile = fs.open("disk/coffe.lua","w")
  138.  cffile.write(c)
  139.  cffile.close()
  140.  
  141.  rednet.broadcast("golden","cof1")
  142.  os.reboot()
  143.  end
  144. end
  145.  
  146. function tear()
  147.   if tonumber(money) < 30 then
  148.   print("You don't have enougth money")
  149.   make()
  150.   sleep(2)
  151.   disk.eject("right")
  152.   os.reboot()
  153.   else
  154.   m = money - 30
  155.   local wfile = fs.open("disk/money.lua","w")
  156.   wfile.write(m)
  157.   wfile.close()
  158.  
  159.   c = cff - 1
  160.   local cffile = fs.open("disk/coffe.lua","w")
  161.   cffile.write(c)
  162.   cffile.close()
  163.  
  164.   rednet.broadcast("tear","cof1")
  165.   os.reboot()
  166.   end
  167. end
  168.  
  169.  
  170. function Exit()
  171.   inMainMenu = false
  172.   disk.eject("right")
  173.   os.reboot()
  174. end
  175.  
  176.  
  177. function make()
  178.  rednet.broadcast("make","cof1")
  179.  if redstone.getInput("bottom") == true then
  180.   c = 6
  181.   local cffile = fs.open("disk/coffe.lua","w")
  182.   cffile.write(c)
  183.   cffile.close()
  184.   disk.eject("right")
  185.   os.reboot()
  186.   else
  187.   term.clear()
  188.   term.setCursorPos(1,1)
  189.   term.write("---------------------------------------------------")
  190.   term.setCursorPos(1,19)
  191.   term.write("---------------------------------------------------")
  192.   term.setCursorPos(15,9)
  193.   print("Brewing Coffe")
  194.   term.setCursorPos(15,10)
  195.   sleep(3)
  196.   make()
  197.  end
  198. end
  199.  
  200. if tonumber(cff) == 0 then
  201.   make()
  202. end
  203.  
  204. -- Menu Definitions--
  205.  mainMenu = {
  206.  [1] = {text = "Extra Sugar = 5$ (You can only use in the max 1)", handler = sugar},
  207.  [2] = {text = "Strong = 10$ (You can only use in the max 1)", handler = blaze},
  208.  [3] = {text = "Cream = 10$ (You can only use in the max 1)", handler = cream},
  209.  [4] = {text = "Premium = 25$ (You can only use in the max 1)", handler = golden},
  210.  [5] = {text = "Healthy = 30$ (You can only use in the max 1)", handler = tear},
  211.  [6] = {text = "Make Coffe", handler = make},
  212.  [7] = {text = "Cancel", handler = Exit},
  213.  }
  214.  
  215.  -- Printing Methods --
  216.  function printMenu( menu )
  217.   for i=1,#menu do
  218.    if i == selectedItem then
  219.     print(">> "..menu[i].text)
  220.    else
  221.     print("   "..menu[i].text)
  222.    end
  223.  end
  224. end
  225.  
  226.  -- Handler Method --
  227. function onKeyPressed( key, menu)
  228.   if key == keys.enter then
  229.      onItemSelected(menu)
  230.   elseif key == keys.up then
  231.      if selectedItem > 1 then
  232.      selectedItem = selectedItem - 1
  233.      end
  234.   elseif key == keys.down then
  235.      if selectedItem < #menu then
  236.      selectedItem = selectedItem + 1
  237.    end
  238.   end
  239.  end
  240.  
  241. function onItemSelected(menu)
  242.   menu[selectedItem].handler()
  243. end
  244.  
  245. while inMainMenu do
  246.   line()
  247.   term.setCursorPos(1,2)
  248.   print("Username: "..username)
  249.   print("Current Money: "..money.. " Euros")
  250.   print("Number of ingredients remaining: "..cff)
  251.   term.setCursorPos(1,6)
  252.   printMenu(mainMenu)
  253.    event, key = os.pullEvent("key")
  254.    onKeyPressed(key, mainMenu)
  255. end
Advertisement
Add Comment
Please, Sign In to add comment