iap.monkey2 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. Namespace iap
  2. #If __MOBILE_TARGET__
  3. #Import "<std>"
  4. #Import "<mojo>"
  5. #If __TARGET__="android"
  6. #Import "iap_android"
  7. #Elseif __TARGET__="ios"
  8. #Import "iap_ios"
  9. #Endif
  10. Using std..
  11. Using mojo..
  12. Enum ProductType
  13. Consumable=1
  14. NonConsumable=2
  15. End
  16. Class IAPStore
  17. Field OpenStoreComplete:Void( result:Int,interrupted:Product[],owned:Product[] )
  18. Field BuyProductComplete:Void( result:Int,product:Product )
  19. Field GetOwnedProductsComplete:Void( result:Int,owned:Product[] )
  20. Method New()
  21. _iap=New IAPStoreRep
  22. End
  23. Property Open:Bool()
  24. Return _state>0
  25. End
  26. Property Busy:Bool()
  27. Return _state>1
  28. End
  29. Method OpenStore( products:Product[] )
  30. If _state>0 Return
  31. _products=products
  32. _state=2
  33. App.Idle+=UpdateState
  34. _iap.OpenStoreAsync( _products )
  35. End
  36. Method BuyProduct( product:Product )
  37. If _state<>1 Return
  38. _buying=product
  39. _state=3
  40. App.Idle+=UpdateState
  41. _iap.BuyProductAsync( product )
  42. End
  43. Method GetOwnedProducts()
  44. If _state<>1 Return
  45. _state=4
  46. App.Idle+=UpdateState
  47. _iap.GetOwnedProductsAsync()
  48. End
  49. Private
  50. Field _products:Product[]
  51. Field _iap:IAPStoreRep
  52. Field _state:=0
  53. Field _buying:Product
  54. Method UpdateState()
  55. If _iap.IsRunning()
  56. App.Idle+=UpdateState
  57. Return
  58. Endif
  59. Local result:=_iap.GetResult()
  60. Local state:=_state
  61. _state=1
  62. Select state
  63. Case 2 'openstore
  64. If result<0 _state=0
  65. Local interrupted:=New Stack<Product>
  66. Local owned:=New Stack<Product>
  67. For Local product:=Eachin _products
  68. If product.Interrupted interrupted.Push( product )
  69. If product.Owned owned.Push( product )
  70. Next
  71. OpenStoreComplete( result,interrupted.ToArray(),owned.ToArray() )
  72. Case 3 'buyproduct
  73. Local buying:=_buying
  74. _buying=Null
  75. BuyProductComplete( result,buying )
  76. Case 4 'GetOwnedProducts
  77. Local owned:=New Stack<Product>
  78. For Local product:=Eachin _products
  79. If product.Owned owned.Push( product )
  80. Next
  81. GetOwnedProductsComplete( result,owned.ToArray() )
  82. End
  83. End
  84. End
  85. #Endif