Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Casino Transaction Manager
- Handles transactions between the player and the casino
- credits system. Allows the player to cash in "casino notes"
- for credits
- ]]--
- -- Support Functions
- function modifyPlayer(p,num,t)
- print("Sending info for " .. p)
- rednet.send(server,"modify " .. p .. " " .. num .. " " .. t)
- local msg = nil
- local attempts = 1
- -- wait for message for 10 ticks retry 3 times
- repeat
- id,msg,proto = rednet.receive(.5)
- attempts = attempts + 1
- until msg or (attempts > 3 )
- if msg and id == server then
- print(msg)
- return textutils.unserialize(msg)
- end
- end
- function getPlayer(p)
- print("getting player info for " .. p)
- rednet.send(server,"get " .. p)
- local msg = nil
- local attempts = 1
- -- wait for message for 10 ticks retry 3 times
- repeat
- print("attempt " .. attempts)
- id,msg,proto = rednet.receive(.5)
- attempts = attempts + 1
- until msg or (attempts > 3 )
- if msg and id == server then
- print(msg)
- return msg
- else
- return nil
- end
- end
- function centerM(s,y)
- local x = math.floor( (mW/2 - string.len(s)/2) + .5 )
- m.setCursorPos(x,y)
- m.write(s)
- end
- -- Graphics Functions
- function drawScreen()
- print("drawing the screen")
- m.setBackgroundColor(colors.black)
- m.clear()
- m.setCursorPos(1,1)
- centerM("--------------------",1)
- centerM("CREDIT <-> NOTE",2)
- centerM("EXCHANGE",3)
- centerM("--------------------",4)
- centerM("DEPOSIT WITHDRAW",5)
- end
- function showStats(p)
- print("Displaying Stats")
- stats.setBackgroundColor(colors.black)
- stats.clear()
- stats.setCursorPos(2,3)
- stats.write("Conversion Rate: $1 equals " .. conversionRate .. " Credits")
- stats.setCursorPos(2,4)
- stats.write("Stand On The PIM Then Click Buttons To Use")
- if p then
- print("Showing player stats for " .. p.name)
- stats.setCursorPos(2,1)
- stats.write("Balance in Money: $" .. math.floor(p.balance/conversionRate) .. " Remaining Credits: " .. p.balance%conversionRate)
- stats.setCursorPos(2,2)
- stats.write("Player: " .. p.name)
- stats.setCursorPos(mW - string.len("Credits: " .. p.balance)- 1,2)
- stats.write("Credits: " .. p.balance)
- end
- end
- -- Button Functions
- function defineButtonWindows()
- local bWindows = {}
- for i=1, #buttons do
- bWindows[i] = window.create(m,buttons[i].x,buttons[i].y,buttonWidth,buttonHeight)
- end
- return bWindows
- end
- function drawButtons()
- for i=1, #buttonWindow do
- if buttons[i].pushed then
- buttonWindow[i].setBackgroundColor(buttonPushedColor)
- else
- buttonWindow[i].setBackgroundColor(buttonColor)
- end
- buttonWindow[i].clear()
- local stringLenHalf = math.floor(string.len(tostring( buttons[i].label))/2 +.5)
- local x = math.floor(buttonWidth/2 + .5) - stringLenHalf + 1
- local y = math.floor( buttonHeight/2 + .5 )
- buttonWindow[i].setCursorPos(x, y)
- buttonWindow[i].write(tostring(buttons[i].label))
- end
- end
- function getButton(x,y)
- print("checking if " .. x .. " " .. y .. " is a button")
- local bType = nil
- local button = nil
- for i=1,#buttons do
- if x >= buttons[i].x and x <= (buttons[i].x + buttonWidth - 1) then
- print("X range matches button " .. i)
- if y >= buttons[i].y and y <= (buttons[i].y + buttonHeight - 1) then
- print("Y range matches button " .. i)
- bType = buttons[i].bType
- button = buttons[i].label
- print("Got button press of " .. button .. " " .. bType)
- buttons[i].pushed = true
- end
- end
- if button then
- break
- end
- end
- return button,bType
- end
- function resetButtons()
- for i=1,#buttons do
- buttons[i].pushed = false
- end
- end
- function getWireless()
- local peripherals = peripheral.getNames()
- local wirelessSide = nil
- local gotWireless = false
- for i=1, #peripherals do
- print("Checking " .. peripherals[i])
- if peripheral.getType(peripherals[i]) == "modem" then
- print("It is a Modem")
- if peripheral.call(peripherals[i],"isWireless") then
- print("It is also Wireless")
- wirelessSide = peripherals[i]
- gotWireless = true
- end
- end
- if gotWireless then
- break
- end
- end
- return wirelessSide
- end
- function getInterfaceSide()
- local file = fs.open("config", "r")
- local side = file.readLine()
- file.close()
- print("Interface Side " .. side)
- return side
- end
- function pushSlot(side,slot,amt,toSlot)
- return pim.pushItemIntoSlot(side,slot,amt,toSlot)
- end
- function getStack(slot)
- return pim.getStackInSlot(slot)
- end
- -- Config Options
- conversionRate = 10
- textScale = .5
- buttonHeight = 3
- buttonWidth = 5
- buttonColor = colors.green
- buttonPushedColor = colors.red
- buttonSpacing = 7
- buttonStartY = 7
- server = 267
- -- Peripheral Definitions
- rednet.open(getWireless())
- m = peripheral.find("monitor")
- pim = peripheral.find("pim")
- -- Constants
- interfaceSide = getInterfaceSide()
- m.setTextScale(textScale)
- mW,mH = m.getSize()
- stats = window.create(m,2,mH-3,mW-2,4)
- hash10 = "12c19e31baef070c9bd6c6137465bf60"
- hash100 = "e11ea0f16570423f0ecd8104444f6e53"
- buttons = {
- {
- x = math.floor((mW/2 - buttonWidth - buttonSpacing/2)+.5),
- y = buttonStartY,
- label = "10",
- bType = "deposit"
- },
- {
- x = math.floor((mW/2 - buttonWidth - buttonSpacing/2)+.5),
- y = buttonStartY + buttonHeight + 1,
- label = "100",
- bType = "deposit"
- },
- {
- x = math.floor((mW/2 - buttonWidth - buttonSpacing/2)+.5),
- y = buttonStartY + 2 * (buttonHeight + 1),
- label = "ALL",
- bType = "deposit"
- },
- {
- x = math.floor( (mW/2 + buttonSpacing/2) + .5),
- y = buttonStartY,
- label = "10",
- bType = "withdraw"
- },
- {
- x = math.floor( (mW/2 + buttonSpacing/2) + .5),
- y = buttonStartY + buttonHeight + 1,
- label = "100",
- bType = "withdraw"
- },
- {
- x = math.floor( (mW/2 + buttonSpacing/2) + .5),
- y = buttonStartY + 2 * (buttonHeight + 1),
- label = "ALL",
- bType = "withdraw"
- },
- }
- buttonWindow = defineButtonWindows()
- -- Start Main Program Logic
- drawScreen()
- while true do
- resetButtons()
- drawButtons()
- local event,p1,p2,p3 = os.pullEvent()
- if event == "monitor_touch" then
- --print("monitor touch at x" .. p2 .. " y" .. p3)
- local playerName = pim.getInventoryName()
- -- get player standing on pim
- if playerName ~= "pim" then
- button,bType = getButton(p2,p3)
- if button then
- drawButtons()
- msg = getPlayer(playerName)
- -- continue if message was recieved back from server
- if msg ~=nil then
- player = textutils.unserialize(msg)
- --print(player)
- showStats(player)
- if bType == "deposit" then
- -- Check if all button pushed
- if button == "ALL" then
- for i=1,pim.getInventorySize() do
- print("Checking Slot " .. i)
- local hadIssue = false
- local pass,item = pcall(getStack,i)
- print(item)
- if not pass then
- break
- end
- -- check for an item in the slot
- if item and pass then
- -- check if item is a book
- if item.id == "minecraft:written_book" then
- -- check book if it is mine
- if item.nbt_hash == hash100 or item.nbt_hash == hash10 then
- -- get value of book
- if item.nbt_hash == hash100 then
- value = 100 * conversionRate
- elseif item.nbt_hash == hash10 then
- value = 10 * conversionRate
- end
- local totalPush = 0
- local intSlot = 4
- print("Starting While Loop for slot " .. i)
- while totalPush < item.qty and intSlot <= 8 do
- print("Passed While with totalPush " .. totalPush .. " and intSlot" .. intSlot)
- if pim.getInventorySize() >= i and pim.getInventoryName() == player.name then
- print("Inventory Size " .. pim.getInventorySize())
- print("Attempting Push")
- local pass,mult = pcall(pushSlot,interfaceSide,i,item.qty,intSlot)
- if pass then
- amt = value * mult
- print("Push Passed with Amount " .. mult .. " valued at " .. amt)
- if amt > 0 then
- print("Modifying Player")
- player = modifyPlayer(player.name,amt,"credit")
- end
- totalPush = totalPush + mult
- intSlot = intSlot + 1
- else
- print("Player Stepped of the PIM")
- hadIssue = true
- break
- end
- else
- break
- end
- end
- else
- print("The books are not mine.")
- end
- else
- print("Item is not a written book")
- end
- else
- print("No Item in slot " .. i)
- end
- if hadIssue then
- break
- end
- end
- showStats(player)
- -- Amount button pushed
- elseif button == "100" then
- local complete = false
- for i=1, pim.getInventorySize() do
- local pass,item = pcall(getStack,i)
- print(item)
- if not pass then
- break
- end
- if item then
- if item.id == "minecraft:written_book" then
- if item.nbt_hash == hash100 then
- local mult = pim.pushItemIntoSlot(interfaceSide,i,1,5)
- local amt = 100 * conversionRate * mult
- if amt > 0 then
- player = modifyPlayer(player.name,amt,"credit")
- complete = true
- end
- elseif item.nbt_hash == hash10 and item.qty >= 10 then
- local mult = pim.pushItemIntoSlot(interfaceSide,i,10,4)
- local amt = 10 * conversionRate * mult
- if amt > 0 then
- player = modifyPlayer(player.name,amt,"credit")
- complete = true
- end
- else
- print("Not a 100$ book or 10 10$ books")
- end
- else
- print("Not a book")
- end
- end
- if complete then
- break
- end
- end
- showStats(player)
- elseif button == "10" then
- local complete = false
- for i=1, pim.getInventorySize() do
- local pass,item = pcall(getStack,i)
- print(item)
- if not pass then
- break
- end
- if item then
- if item.id == "minecraft:written_book" then
- if item.nbt_hash == hash10 then
- local mult = pim.pushItemIntoSlot(interfaceSide,i,1,4)
- local amt = 10 * conversionRate * mult
- if amt > 0 then
- player = modifyPlayer(player.name,amt,"credit")
- complete = true
- end
- else
- print("Not a 10$ book")
- end
- else
- print("Not a book")
- end
- end
- if complete then
- break
- end
- end
- showStats(player)
- end
- elseif bType == "withdraw" then
- -- Get button amount pressed
- local fullInventory = false
- if button == "ALL" then
- --[[ Disabled for now till the need calls for it.
- -- Exchange for 10000 or higher balance
- while player.balance >= 1000 * conversionRate and not fullInventory do
- local mult = pim.pullItem(interfaceSide,3,1)
- amt = 1000 * conversionRate * mult
- if amt > 0 then
- player = modifyPlayer(player.name,amt*-1,"credit")
- else
- fullInventory = true
- end
- end
- ]]--
- -- Exchange for 1000 to 9001 balance
- while player.balance >= 100 * conversionRate and not fullInventory do
- local maxNum = math.floor(player.balance/(100*conversionRate))
- if maxNum > 16 then
- maxNum = 16
- end
- local mult = pim.pullItem(interfaceSide,2,maxNum)
- local amt = 100 * conversionRate * mult
- if amt > 0 then
- player = modifyPlayer(player.name,amt*-1,"credit")
- else
- fullInventory = true
- end
- end
- -- Exchange for 100 - 901 balance
- while player.balance >= 10 * conversionRate and not fullInventory do
- local maxNum = math.floor(player.balance/(10 * conversionRate))
- if maxNum > 16 then
- maxNum = 16
- end
- local mult = pim.pullItem(interfaceSide,1,maxNum)
- local amt = 10 * conversionRate * mult
- if amt > 0 then
- player = modifyPlayer(player.name,amt*-1,"credit")
- else
- fullInventory = true
- end
- end
- showStats(player)
- elseif button == "100" then
- if player.balance >= 100 * conversionRate then
- local mult = pim.pullItem(interfaceSide,2,1)
- local amt = 100 * conversionRate * mult
- if amt > 0 then
- player = modifyPlayer(player.name,amt*-1,"credit")
- showStats(player)
- end
- end
- elseif button == "10" then
- if player.balance >= 10 * conversionRate then
- local mult = pim.pullItem(interfaceSide,1,1)
- local amt = 10 * conversionRate * mult
- if amt > 0 then
- player = modifyPlayer(player.name,amt*-1,"credit")
- showStats(player)
- end
- end
- end
- else
- print("Unknown Button Type " .. bType)
- end
- else
- print("Could Not Contact Accounts Server!!!!")
- end
- else
- print("No Button Press Detected")
- end
- else
- print("No player detected")
- end
- elseif event == "key" and p1 == 16 then
- os.reboot()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment