pos.lua
134 lines · 2.9 KB
Paste Update: zQhqgEfz
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/Michael-Reeves808/pos.lua
| 1 | -- Paste Update: zQhqgEfz |
| 2 | |
| 3 | |
| 4 | SLOT_COUNT = 16 |
| 5 | |
| 6 | rednet.open('bottom') |
| 7 | screen = peripheral.wrap('monitor_2') |
| 8 | |
| 9 | prices = { |
| 10 | ['minecraft:iron_ingot']= { |
| 11 | ['value']= 1, |
| 12 | ['display_name']= 'Iron Ingots' |
| 13 | }, |
| 14 | ['minecraft:iron_block']= { |
| 15 | ['value']= 10, |
| 16 | ['display_name']= 'Iron Blocks' |
| 17 | }, |
| 18 | ['minecraft:diamond']= { |
| 19 | ['value']= 14, |
| 20 | ['display_name']= 'Diamonds' |
| 21 | }, |
| 22 | ['minecraft:netherite_scrap']= { |
| 23 | ['value']= 144, |
| 24 | ['display_name']= 'Netherite Scrap' |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | |
| 29 | local width = 23 |
| 30 | |
| 31 | function writePricesToScreen(prices) |
| 32 | local textScale = 1 |
| 33 | screen.clear() |
| 34 | screen.setCursorPos(2, 1) |
| 35 | screen.write('======= Potato Rates =======') |
| 36 | |
| 37 | |
| 38 | local i = textScale + 3 |
| 39 | for key, val in pairs(prices) do |
| 40 | screen.setCursorPos(2, i) |
| 41 | |
| 42 | displaySize = string.len(val['display_name'] .. val['value']) |
| 43 | screen.write(val['display_name']) |
| 44 | |
| 45 | screen.write(' ') |
| 46 | screen.write(string.rep(' ', width - displaySize)) |
| 47 | screen.write(' ') |
| 48 | |
| 49 | screen.write('1:' .. tostring(val['value'])) |
| 50 | |
| 51 | i = i + 1 |
| 52 | end |
| 53 | |
| 54 | screen.setCursorPos(2, i + 3) |
| 55 | screen.write('New payment options soon!') |
| 56 | screen.setCursorPos(2, i + 5) |
| 57 | screen.write('Prices may change') |
| 58 | end |
| 59 | |
| 60 | function writeLimitToScreen(millisRemaining) |
| 61 | screen.clear() |
| 62 | screen.setCursorPos(2, 1) |
| 63 | screen.write('Too Fast!') |
| 64 | |
| 65 | screen.setCursorPos(2, 3) |
| 66 | screen.write('You can purchase again in:') |
| 67 | screen.setCursorPos(2, 4) |
| 68 | screen.write(string.format('%d seconds', millisRemaining / 1000)) |
| 69 | end |
| 70 | |
| 71 | function getFirstItemIndex() |
| 72 | for i = 1, SLOT_COUNT, 1 do |
| 73 | turtle.select(i) |
| 74 | if turtle.getItemDetail() ~= nil then |
| 75 | return i |
| 76 | end |
| 77 | end |
| 78 | |
| 79 | return nil |
| 80 | end |
| 81 | |
| 82 | |
| 83 | |
| 84 | function calculatePayout(item) |
| 85 | |
| 86 | local price = prices[item['name']]['value'] |
| 87 | if price ~= nil then |
| 88 | return price * item['count'] |
| 89 | end |
| 90 | return false |
| 91 | end |
| 92 | |
| 93 | |
| 94 | tickRate = .5 |
| 95 | resetTimer = os.epoch('utc') |
| 96 | resetTime = 1000 * 60 * 5 |
| 97 | |
| 98 | saturation = 0 |
| 99 | saturationLimit = 420 |
| 100 | |
| 101 | writePricesToScreen(prices) |
| 102 | |
| 103 | print('POS Starting Up...') |
| 104 | while true do |
| 105 | if (saturation < saturationLimit) then |
| 106 | if turtle.suckUp(12) then |
| 107 | idx = getFirstItemIndex() |
| 108 | item = turtle.getItemDetail(idx) |
| 109 | turtle.select(idx) |
| 110 | |
| 111 | if (prices[item['name']] ~= nil) then |
| 112 | local payout = calculatePayout(item) |
| 113 | saturation = saturation + payout |
| 114 | |
| 115 | turtle.drop() |
| 116 | rednet.broadcast(payout) |
| 117 | else |
| 118 | print('Invalid Payment') |
| 119 | turtle.dropUp() |
| 120 | end |
| 121 | end |
| 122 | else |
| 123 | writeLimitToScreen(resetTime - (os.epoch('utc') - resetTimer)) |
| 124 | end |
| 125 | |
| 126 | |
| 127 | if ((os.epoch('utc') - resetTimer) > resetTime) then |
| 128 | writePricesToScreen(prices) |
| 129 | saturation = 0 |
| 130 | resetTimer = os.epoch('utc') |
| 131 | end |
| 132 | |
| 133 | os.sleep(tickRate) |
| 134 | end |