Advertisement
ylamagan

main.lua

Oct 9th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.49 KB | None | 0 0
  1. --Load IAP Badger
  2. local iap = require("iap_badger")
  3.  
  4. --Progress spinner
  5. local spinner=nil
  6. --Text object indicating how many coins the user is currently holding
  7. local coinText=nil
  8.  
  9. --Forward declaration
  10. local buyCoins10=nil
  11.  
  12. --Create the catalogue
  13. local catalogue = {
  14.  
  15.     --Information about the product on the app stores
  16.     products = {  
  17.    
  18.         --buy50coins is the product identifier.
  19.         --Always use this identifier to talk to IAP Badger about the purchase.
  20.         buy50coins = {
  21.                 --A list of product names or identifiers specific to apple's App Store or Google Play.
  22.                 productNames = { apple="buy50coins", google="50_coins", amazon="COINSx50"},
  23.                 --The product type
  24.                 productType = "consumable",
  25.                 --This function is called when a purchase is complete.
  26.                 onPurchase=function() iap.addToInventory("coins", 50) end,
  27.                 --The function is called when a refund is made
  28.                 onRefund=function() iap.removeFromInventory("coins", 50) end,
  29.         },
  30.        
  31.         --buy100coins is the product identifier.
  32.         --Always use this identifier to talk to IAP Badger about the purchase.
  33.         buy100coins = {
  34.                 --A list of product names or identifiers specific to apple's App Store or Google Play.
  35.                 productNames = { apple="buy100coins", google="100_coins", amazon="COINSx100"},
  36.                 --The product type
  37.                 productType = "consumable",
  38.                 --This function is called when a purchase is complete.
  39.                 onPurchase=function() iap.addToInventory("coins", 100) end,
  40.                 --The function is called when a refund is made
  41.                 onRefund=function() iap.removeFromInventory("coins", 100) end,
  42.         },
  43.     },
  44.  
  45.     --Information about how to handle the inventory item
  46.     inventoryItems = {
  47.         coins = { productType="consumable" }
  48.     }
  49. }
  50.  
  51. --Called when a purchase fails
  52. local function failedListener()
  53.    
  54.     --If the spinner is on screen, remove it
  55.     if (spinner) then
  56.         spinner:removeSelf()
  57.         spinner=nil
  58.     end
  59.    
  60. end
  61.  
  62.  
  63. local iapOptions = {
  64.     --The catalogue generated above
  65.     catalogue=catalogue,
  66.     --The filename in which to save the inventory
  67.     filename="example2.txt",
  68.     --Salt for the hashing algorithm
  69.     salt = "something tr1cky to gue55!",
  70.        
  71.     --Listeners for failed and cancelled transactions will just remove the spinner from the screen
  72.     failedListener=failedListener,
  73.     cancelledListener=failedListener,
  74.     --Once the product has been purchased, it will remain in the inventory.  Uncomment the following line
  75.     --to test the purchase functions again in future.
  76.     --doNotLoadInventory=true,
  77.     -- debugMode=true,
  78. }
  79.  
  80. --Initialise IAP badger
  81. iap.init(iapOptions)
  82.  
  83. print (iap.getTargetStore())
  84.  
  85.  
  86. local function purchaseListener(product)
  87.     --Remove the spinner
  88.     spinner:removeSelf()
  89.     spinner=nil
  90.    
  91.     --Any changes to the value in 'coins' in the inventory has been taken care of.
  92.     --Update the text object to reflect how many coins are now in the user's inventory
  93.     coinText.text = iap.getInventoryValue("coins") .. " coins"
  94.    
  95.     --Save the inventory change
  96.     iap.saveInventory()
  97.    
  98.     --Tell user their purchase was successful
  99.     native.showAlert("Info", "Your purchase was successful", {"Okay"})
  100.    
  101. end
  102.  
  103. --Purchase function
  104. buyCoins=function(event)
  105.    
  106.     --Place a progress spinner on screen and tell the user the app is contating the store
  107.     local spinnerBackground = display.newRect(160,240,360,600)
  108.     spinnerBackground:setFillColor(1,1,1,0.75)
  109.     --Spinner consumes all taps so the user cannot tap the purchase button twice
  110.     spinnerBackground:addEventListener("tap", function() return true end)
  111.     local spinnerText = display.newText("Contacting " .. iap.getStoreName() .. "...", 160,180, native.systemFont, 18)
  112.     spinnerText:setFillColor(0,0,0)
  113.     --Add a little spinning rectangle
  114.     local spinnerRect = display.newRect(160,260,35,35)
  115.     spinnerRect:setFillColor(0.25,0.25,0.25)
  116.     transition.to(spinnerRect, { time=4000, rotation=360, iterations=999999, transition=easing.inOutQuad})
  117.     --Create a group and add all these objects to it
  118.     spinner=display.newGroup()
  119.     spinner:insert(spinnerBackground)
  120.     spinner:insert(spinnerText)
  121.     spinner:insert(spinnerRect)
  122.    
  123.     --Tell IAP to initiate a purchase - the product name will be stored in target.product
  124.     iap.purchase(event.target.product, purchaseListener)
  125.    
  126. end
  127.  
  128. ---------------------------------
  129. --
  130. -- Main game code
  131. --
  132. ---------------------------------
  133.  
  134. --Remove status bar
  135. display.setStatusBar( display.HiddenStatusBar )
  136.  
  137. --Background
  138. local background = display.newRect(160,240,360,600)
  139. background:setFillColor({type="gradient", color1={ 0,0,0 }, color2={ 0,0,0.4 }, direction="down"})
  140.  
  141. --Draw "buy 50 coins" button
  142.     --Create button background
  143.     local buy50Background = display.newRect(240, 400, 150, 50)
  144.     buy50Background.stroke = { 0.5, 0.5, 0.5 }
  145.     buy50Background.strokeWidth = 2
  146.     --Create "buy IAP" text object
  147.     local buy50Text = display.newText("Buy 50 coins", buy50Background.x, buy50Background.y, native.systemFont, 18)
  148.     buy50Text:setFillColor(0,0,0)
  149.    
  150.     --Store the name of the product this button relates to
  151.     buy50Background.product="buy50coins"
  152.     --Create tap listener
  153.     buy50Background:addEventListener("tap", buyCoins)
  154.    
  155. --Draw "buy 100 coins" button
  156.     --Create button background
  157.     local buy100Background = display.newRect(80, 400, 150, 50)
  158.     buy100Background.stroke = { 0.5, 0.5, 0.5 }
  159.     buy100Background.strokeWidth = 2
  160.     --Create "buy IAP" text object
  161.     local buy100Text = display.newText("Buy 100 coins", buy100Background.x, buy100Background.y, native.systemFont, 18)
  162.     buy100Text:setFillColor(0,0,0)
  163.    
  164.     --Store the name of the product this button relates to
  165.     buy100Background.product="buy100coins"
  166.     --Create tap listener
  167.     buy100Background:addEventListener("tap", buyCoins)
  168.    
  169. --Text indicating how many coins the player currently holds
  170.  
  171. --Get how many coins are currently being held by the player
  172. local coinsHeld = iap.getInventoryValue("coins")
  173. --If no coins are held in the inventory, nil will be returned - this equates to no coins
  174. if (not coinsHeld) then coinsHeld=0 end
  175.  
  176. coinText = display.newText(coinsHeld .. " coins", 160, 20, native.systemFont, 18)
  177. coinText:setFillColor(1,1,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement