Advertisement
massacring

cashout

Jan 5th, 2025 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. local Commons = require('Commons')
  2. local storage = peripheral.find("minecraft:chest")
  3. local modem = peripheral.wrap("bottom")
  4. local printer = peripheral.find("printer")
  5. local barrel = peripheral.find("minecraft:barrel")
  6.  
  7. local IOUActive = false
  8.  
  9. local function getItemIndex(id)
  10.     for i = 1, storage.size(), 1 do
  11.         local item = storage.getItemDetail(i)
  12.         if item then
  13.             if item.name == id then return i end
  14.         end
  15.     end
  16.     print("Item '" .. id .. "' not found.")
  17.     return 0
  18. end
  19.  
  20. local function clearTurtle()
  21.     for i = 1, 16, 1 do
  22.         storage.pullItems(modem.getNameLocal(), i)
  23.     end
  24. end
  25.  
  26. local function getKey()
  27.     local characters = "12345678901234567890!#%&/=?+@$!#%&/=?+@$!#%&/=?+@$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  28.     local ranSeq = ""
  29.     for _ = 1, 16, 1 do
  30.         local random = math.random(1, #characters)
  31.         ranSeq = ranSeq .. characters:sub(random,random)
  32.     end
  33.     return ranSeq
  34. end
  35.  
  36. local function writeReceipt(key)
  37.     local file = fs.open("receipts", "a")
  38.     file.write("[\"" .. key .. "\"]:\n")
  39.     file.write("    \"type\": \"" .. Commons.Credits.selectedCredit .. "\"\n")
  40.     file.write("    \"score\": " .. tostring(Commons.Score:getScore()) .. "\n")
  41.     file.close()
  42. end
  43.  
  44. local function generateIOU(key)
  45.     IOUActive = true
  46.     clearTurtle()
  47.     printer.newPage()
  48.     printer.setPageTitle("I.O.U -Mass")
  49.     printer.write("The machine ran out of")
  50.     printer.setCursorPos(1,2)
  51.     printer.write("some resource.")
  52.     printer.setCursorPos(1,3)
  53.     printer.write("This receipt will hold")
  54.     printer.setCursorPos(1,4)
  55.     printer.write("yor earnings.")
  56.     printer.setCursorPos(1,6)
  57.     printer.write("Credit Type: " .. Commons.Credits.selectedCredit)
  58.     printer.setCursorPos(1,7)
  59.     printer.write("Credit Score: " .. Commons.Score:getScore())
  60.     printer.setCursorPos(1,8)
  61.     printer.write("Key: " .. key .. "-")
  62.     printer.setCursorPos(1,9)
  63.     printer.write("Please do not show this")
  64.     printer.setCursorPos(1,10)
  65.     printer.write("key to another player.")
  66.     printer.endPage()
  67. end
  68.  
  69. local function getIndex(id)
  70.     local index = getItemIndex(id)
  71.     local success = index ~= 0
  72.     if not success then
  73.     local key = getKey()
  74.         generateIOU(key)
  75.         writeReceipt(key)
  76.         barrel.pullItems(peripheral.getName(printer), 8)
  77.         barrel.pushItems(modem.getNameLocal(), 1)
  78.         return 0
  79.     end
  80.     return index
  81. end
  82.  
  83. local function countCoins()
  84.     local largeValue = Commons.Credits:getValueBySize(Commons.Credits.LARGE) or math.maxinteger
  85.     local mediumValue = Commons.Credits:getValueBySize(Commons.Credits.MEDIUM)
  86.     while Commons.Score:getScore() >= largeValue do
  87.         local largeIndex = getIndex(Commons.Credits:getCurrentId(Commons.Credits.LARGE))
  88.         if IOUActive then goto skip end
  89.         storage.pushItems(modem.getNameLocal(), largeIndex, 1)
  90.         Commons.Score:updateScore(Commons.Score:getScore() - largeValue)
  91.     end
  92.     while Commons.Score:getScore() >= mediumValue do
  93.         local mediumIndex = getIndex(Commons.Credits:getCurrentId(Commons.Credits.MEDIUM))
  94.         if IOUActive then goto skip end
  95.         storage.pushItems(modem.getNameLocal(), mediumIndex, 1)
  96.         Commons.Score:updateScore(Commons.Score:getScore() - mediumValue)
  97.     end
  98.     while Commons.Score:getScore() > 0 do
  99.         local smallIndex = getIndex(Commons.Credits:getCurrentId(Commons.Credits.SMALL))
  100.         if IOUActive then goto skip end
  101.         storage.pushItems(modem.getNameLocal(), smallIndex, 1)
  102.         Commons.Score:updateScore(Commons.Score:getScore() - 1)
  103.     end
  104.     ::skip::
  105.     for i = 1,16,1 do
  106.         turtle.select(i)
  107.         turtle.drop()
  108.     end
  109.     turtle.select(1)
  110. end
  111.  
  112. return countCoins
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement