The3vilM0nk3y

Transaction Manager

Oct 2nd, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.31 KB | None | 0 0
  1. --[[
  2.   Casino Transaction Manager
  3.     Handles transactions between the player and the casino
  4.     credits system. Allows the player to cash in "casino notes"
  5.     for credits
  6. ]]--
  7.  
  8. -- Support Functions
  9. function modifyPlayer(p,num,t)
  10.   print("Sending info for " .. p)
  11.   rednet.send(server,"modify " ..  p .. " " .. num .. " " .. t)
  12.   local msg = nil
  13.   local attempts = 1
  14.   -- wait for message for 10 ticks retry 3 times
  15.   repeat
  16.     id,msg,proto = rednet.receive(.5)
  17.     attempts = attempts + 1
  18.   until msg or (attempts > 3 )
  19.   if msg and id == server then
  20.     print(msg)
  21.     return textutils.unserialize(msg)
  22.   end
  23. end
  24. function getPlayer(p)
  25.   print("getting player info for " .. p)
  26.   rednet.send(server,"get " ..  p)
  27.   local msg = nil
  28.   local attempts = 1
  29.   -- wait for message for 10 ticks retry 3 times
  30.   repeat
  31.     print("attempt " .. attempts)
  32.     id,msg,proto = rednet.receive(.5)
  33.     attempts = attempts + 1
  34.   until msg or (attempts > 3 )
  35.   if msg and id == server then
  36.     print(msg)
  37.     return msg
  38.   else
  39.     return nil
  40.   end
  41. end
  42. function centerM(s,y)
  43.   local x = math.floor( (mW/2 - string.len(s)/2) + .5 )
  44.   m.setCursorPos(x,y)
  45.   m.write(s)
  46.  
  47. end
  48. -- Graphics Functions
  49. function drawScreen()
  50.   print("drawing the screen")
  51.   m.setBackgroundColor(colors.black)
  52.   m.clear()
  53.   m.setCursorPos(1,1)
  54.   centerM("--------------------",1)
  55.   centerM("CREDIT <-> NOTE",2)
  56.   centerM("EXCHANGE",3)
  57.   centerM("--------------------",4)
  58.   centerM("DEPOSIT     WITHDRAW",5)
  59. end
  60. function showStats(p)
  61.   print("Displaying Stats")
  62.   stats.setBackgroundColor(colors.black)
  63.   stats.clear()
  64.   stats.setCursorPos(2,3)
  65.   stats.write("Conversion Rate: $1 equals " .. conversionRate .. " Credits")
  66.   stats.setCursorPos(2,4)
  67.   stats.write("Stand On The PIM Then Click Buttons To Use")
  68.   if p then
  69.     print("Showing player stats for " .. p.name)
  70.     stats.setCursorPos(2,1)
  71.     stats.write("Balance in Money: $" .. math.floor(p.balance/conversionRate) .. " Remaining Credits: " .. p.balance%conversionRate)
  72.     stats.setCursorPos(2,2)
  73.     stats.write("Player: " .. p.name)
  74.     stats.setCursorPos(mW - string.len("Credits: " .. p.balance)- 1,2)
  75.     stats.write("Credits: " .. p.balance)
  76.   end
  77. end
  78. -- Button Functions
  79. function defineButtonWindows()
  80.   local bWindows = {}
  81.   for i=1, #buttons do
  82.     bWindows[i] = window.create(m,buttons[i].x,buttons[i].y,buttonWidth,buttonHeight)
  83.   end
  84.   return bWindows
  85. end
  86. function drawButtons()
  87.   for i=1, #buttonWindow do
  88.     if buttons[i].pushed then
  89.       buttonWindow[i].setBackgroundColor(buttonPushedColor)
  90.     else
  91.       buttonWindow[i].setBackgroundColor(buttonColor)
  92.     end
  93.     buttonWindow[i].clear()
  94.     local stringLenHalf = math.floor(string.len(tostring( buttons[i].label))/2 +.5)
  95.     local x = math.floor(buttonWidth/2 + .5) - stringLenHalf + 1
  96.     local y = math.floor( buttonHeight/2 + .5 )
  97.     buttonWindow[i].setCursorPos(x, y)
  98.     buttonWindow[i].write(tostring(buttons[i].label))
  99.   end
  100. end
  101. function getButton(x,y)
  102.   print("checking if " .. x .. " " .. y .. " is a button")
  103.   local bType = nil
  104.   local button = nil
  105.   for i=1,#buttons do
  106.     if x >= buttons[i].x and x <= (buttons[i].x + buttonWidth - 1) then
  107.       print("X range matches button " .. i)
  108.       if y >= buttons[i].y and y <= (buttons[i].y + buttonHeight - 1) then
  109.         print("Y range matches button " .. i)
  110.         bType = buttons[i].bType
  111.         button = buttons[i].label
  112.         print("Got button press of " .. button .. " " .. bType)
  113.         buttons[i].pushed = true
  114.       end
  115.     end
  116.     if button then
  117.       break
  118.     end
  119.   end
  120.   return button,bType
  121. end
  122. function resetButtons()
  123.   for i=1,#buttons do
  124.     buttons[i].pushed = false
  125.   end
  126. end
  127. function getWireless()
  128.   local peripherals = peripheral.getNames()
  129.   local wirelessSide = nil
  130.   local gotWireless = false
  131.   for i=1, #peripherals do
  132.     print("Checking " .. peripherals[i])
  133.     if peripheral.getType(peripherals[i]) == "modem" then
  134.       print("It is a Modem")
  135.       if peripheral.call(peripherals[i],"isWireless") then
  136.         print("It is also Wireless")
  137.         wirelessSide = peripherals[i]
  138.         gotWireless = true
  139.       end
  140.     end
  141.     if gotWireless then
  142.       break
  143.     end
  144.   end
  145.   return wirelessSide
  146. end
  147. function getInterfaceSide()
  148.   local file = fs.open("config", "r")
  149.   local side = file.readLine()
  150.   file.close()
  151.   print("Interface Side " .. side)
  152.   return side
  153. end
  154. function pushSlot(side,slot,amt,toSlot)
  155.  return pim.pushItemIntoSlot(side,slot,amt,toSlot)
  156. end
  157. function getStack(slot)
  158.   return pim.getStackInSlot(slot)
  159. end
  160. -- Config Options
  161. conversionRate = 10
  162. textScale = .5
  163. buttonHeight = 3
  164. buttonWidth = 5
  165. buttonColor = colors.green
  166. buttonPushedColor = colors.red
  167. buttonSpacing = 7
  168. buttonStartY = 7
  169. server = 267
  170.  
  171. -- Peripheral Definitions
  172. rednet.open(getWireless())
  173. m = peripheral.find("monitor")
  174. pim = peripheral.find("pim")
  175.  
  176. -- Constants
  177. interfaceSide = getInterfaceSide()
  178. m.setTextScale(textScale)
  179. mW,mH = m.getSize()
  180. stats = window.create(m,2,mH-3,mW-2,4)
  181. hash10 = "12c19e31baef070c9bd6c6137465bf60"
  182. hash100 = "e11ea0f16570423f0ecd8104444f6e53"
  183. buttons = {
  184.   {
  185.     x = math.floor((mW/2 - buttonWidth - buttonSpacing/2)+.5),
  186.     y = buttonStartY,
  187.     label = "10",
  188.     bType = "deposit"
  189.   },
  190.   {
  191.     x = math.floor((mW/2 - buttonWidth - buttonSpacing/2)+.5),
  192.     y = buttonStartY +  buttonHeight + 1,  
  193.     label = "100",
  194.     bType = "deposit"
  195.   },
  196.   {
  197.     x = math.floor((mW/2 - buttonWidth - buttonSpacing/2)+.5),
  198.     y = buttonStartY + 2 * (buttonHeight + 1),
  199.     label = "ALL",
  200.     bType = "deposit"
  201.   },
  202.   {
  203.     x = math.floor( (mW/2 + buttonSpacing/2) + .5),
  204.     y = buttonStartY,  
  205.     label = "10",
  206.     bType = "withdraw"
  207.   },
  208.   {
  209.     x = math.floor( (mW/2 + buttonSpacing/2) + .5),
  210.     y = buttonStartY +  buttonHeight + 1,
  211.     label = "100",
  212.     bType = "withdraw"
  213.   },
  214.   {
  215.     x = math.floor( (mW/2 + buttonSpacing/2) + .5),
  216.     y = buttonStartY + 2 * (buttonHeight + 1),
  217.     label = "ALL",
  218.     bType = "withdraw"
  219.   },
  220. }
  221.  
  222. buttonWindow = defineButtonWindows()
  223. -- Start Main Program Logic
  224. drawScreen()
  225. while true do
  226.   resetButtons()
  227.   drawButtons()
  228.   local event,p1,p2,p3 = os.pullEvent()
  229.   if event == "monitor_touch" then
  230.     --print("monitor touch at x" .. p2 .. " y" .. p3)
  231.     local playerName = pim.getInventoryName()
  232.     -- get player standing on pim
  233.     if playerName ~= "pim" then
  234.       button,bType = getButton(p2,p3)
  235.       if button then
  236.         drawButtons()
  237.         msg = getPlayer(playerName)
  238.         -- continue if message was recieved back from server
  239.         if msg ~=nil then
  240.           player = textutils.unserialize(msg)
  241.           --print(player)
  242.           showStats(player)
  243.           if bType == "deposit" then
  244.             -- Check if all button pushed
  245.             if button == "ALL" then
  246.               for i=1,pim.getInventorySize() do
  247.                 print("Checking Slot " .. i)
  248.                 local hadIssue = false
  249.                 local pass,item = pcall(getStack,i)
  250.                 print(item)
  251.                 if not pass then
  252.                   break
  253.                 end
  254.                 -- check for an item in the slot
  255.                 if item and pass then
  256.                   -- check if item is a book
  257.                   if item.id == "minecraft:written_book" then
  258.                     -- check book if it is mine
  259.                     if item.nbt_hash == hash100 or item.nbt_hash == hash10 then
  260.                       -- get value of book
  261.                       if item.nbt_hash == hash100 then
  262.                         value = 100 * conversionRate
  263.                       elseif item.nbt_hash == hash10 then
  264.                         value = 10 * conversionRate
  265.                       end
  266.                       local totalPush = 0
  267.                       local intSlot = 4
  268.                       print("Starting While Loop for slot " .. i)
  269.                       while totalPush < item.qty and intSlot <= 8 do
  270.                         print("Passed While with totalPush " ..  totalPush .. " and intSlot" .. intSlot)
  271.                         if pim.getInventorySize() >= i and pim.getInventoryName() == player.name then
  272.                           print("Inventory Size " .. pim.getInventorySize())
  273.                           print("Attempting Push")
  274.                           local pass,mult = pcall(pushSlot,interfaceSide,i,item.qty,intSlot)
  275.                           if pass then
  276.                             amt = value * mult
  277.                             print("Push Passed with Amount " .. mult .. " valued at " .. amt)
  278.                             if amt > 0 then
  279.                               print("Modifying Player")
  280.                               player = modifyPlayer(player.name,amt,"credit")
  281.                             end
  282.                             totalPush = totalPush + mult
  283.                             intSlot = intSlot + 1
  284.                           else
  285.                             print("Player Stepped of the PIM")
  286.                             hadIssue = true
  287.                             break
  288.                           end
  289.                         else
  290.                           break
  291.                         end
  292.                       end
  293.                     else
  294.                       print("The books are not mine.")
  295.                     end
  296.                   else
  297.                     print("Item is not a written book")
  298.                   end
  299.                 else
  300.                   print("No Item in slot " .. i)
  301.                 end
  302.                 if hadIssue then
  303.                   break
  304.                 end
  305.               end
  306.               showStats(player)
  307.             -- Amount button pushed
  308.             elseif button == "100" then
  309.               local complete = false
  310.               for i=1, pim.getInventorySize() do
  311.                 local pass,item = pcall(getStack,i)
  312.                 print(item)
  313.                 if not pass then
  314.                   break
  315.                 end
  316.                 if item then
  317.                   if item.id == "minecraft:written_book" then
  318.                     if item.nbt_hash == hash100 then
  319.                       local mult = pim.pushItemIntoSlot(interfaceSide,i,1,5)
  320.                       local amt = 100 * conversionRate * mult
  321.                       if amt > 0 then
  322.                         player = modifyPlayer(player.name,amt,"credit")
  323.                         complete = true
  324.                       end
  325.                     elseif item.nbt_hash == hash10 and item.qty >= 10 then
  326.                       local mult = pim.pushItemIntoSlot(interfaceSide,i,10,4)
  327.                       local amt = 10 * conversionRate * mult
  328.                       if amt > 0 then
  329.                         player = modifyPlayer(player.name,amt,"credit")
  330.                         complete = true
  331.                       end  
  332.                     else
  333.                       print("Not a 100$ book or 10 10$ books")
  334.                     end
  335.                   else
  336.                     print("Not a book")
  337.                   end
  338.                 end
  339.                 if complete then
  340.                   break
  341.                 end
  342.               end
  343.               showStats(player)
  344.              
  345.             elseif button == "10" then
  346.               local complete = false
  347.               for i=1, pim.getInventorySize() do
  348.                 local pass,item = pcall(getStack,i)
  349.                 print(item)
  350.                 if not pass then
  351.                   break
  352.                 end
  353.                 if item then
  354.                   if item.id == "minecraft:written_book" then
  355.                     if item.nbt_hash == hash10 then
  356.                       local mult = pim.pushItemIntoSlot(interfaceSide,i,1,4)
  357.                       local amt = 10 * conversionRate * mult
  358.                       if amt > 0 then
  359.                         player = modifyPlayer(player.name,amt,"credit")
  360.                         complete = true
  361.                       end  
  362.                     else
  363.                       print("Not a 10$ book")
  364.                     end
  365.                   else
  366.                     print("Not a book")
  367.                   end
  368.                 end
  369.                 if complete then
  370.                   break
  371.                 end
  372.               end
  373.               showStats(player)
  374.             end
  375.            
  376.           elseif bType == "withdraw" then
  377.             -- Get button amount pressed
  378.             local fullInventory = false
  379.             if button == "ALL" then
  380.               --[[ Disabled for now till the need calls for it.
  381.               -- Exchange for 10000 or higher balance
  382.               while player.balance >= 1000 * conversionRate and not fullInventory do
  383.                 local mult = pim.pullItem(interfaceSide,3,1)
  384.                 amt = 1000 * conversionRate * mult
  385.                 if amt > 0 then
  386.                   player = modifyPlayer(player.name,amt*-1,"credit")
  387.                 else
  388.                   fullInventory = true
  389.                 end
  390.               end
  391.               ]]--
  392.               -- Exchange for 1000 to 9001 balance
  393.               while player.balance >= 100 * conversionRate and not fullInventory do
  394.                 local maxNum = math.floor(player.balance/(100*conversionRate))
  395.                 if maxNum > 16 then
  396.                   maxNum = 16
  397.                 end
  398.                 local mult = pim.pullItem(interfaceSide,2,maxNum)
  399.                 local amt = 100 * conversionRate * mult
  400.                 if amt > 0 then
  401.                   player = modifyPlayer(player.name,amt*-1,"credit")
  402.                 else
  403.                   fullInventory = true
  404.                 end
  405.               end
  406.               -- Exchange for 100 - 901 balance
  407.               while player.balance >= 10 * conversionRate and not fullInventory do
  408.                 local maxNum = math.floor(player.balance/(10 * conversionRate))
  409.                 if maxNum > 16 then
  410.                   maxNum = 16
  411.                 end
  412.                 local mult = pim.pullItem(interfaceSide,1,maxNum)
  413.                 local amt = 10 * conversionRate * mult
  414.                 if amt > 0 then
  415.                   player = modifyPlayer(player.name,amt*-1,"credit")
  416.                 else
  417.                   fullInventory = true
  418.                 end
  419.               end
  420.               showStats(player)  
  421.             elseif button == "100" then
  422.               if player.balance >= 100 * conversionRate then
  423.                 local mult = pim.pullItem(interfaceSide,2,1)
  424.                 local amt = 100 * conversionRate * mult
  425.                 if amt > 0 then
  426.                   player = modifyPlayer(player.name,amt*-1,"credit")
  427.                   showStats(player)
  428.                 end
  429.               end
  430.             elseif button == "10" then
  431.               if player.balance >= 10 * conversionRate then
  432.                 local mult = pim.pullItem(interfaceSide,1,1)
  433.                 local amt = 10 * conversionRate * mult
  434.                 if amt > 0 then
  435.                   player = modifyPlayer(player.name,amt*-1,"credit")
  436.                   showStats(player)
  437.                 end
  438.               end
  439.             end
  440.           else
  441.             print("Unknown Button Type " .. bType)
  442.           end
  443.         else
  444.           print("Could Not Contact Accounts Server!!!!")
  445.         end
  446.       else
  447.         print("No Button Press Detected")
  448.       end
  449.     else
  450.       print("No player detected")
  451.     end
  452.   elseif event == "key" and p1 == 16 then
  453.     os.reboot()
  454.   end
  455. end
Advertisement
Add Comment
Please, Sign In to add comment