Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. local PlayFabClientApi = require("PlayFab.PlayFabClientApi")
  2. BackendInterfaceLootPlayfab = class(BackendInterfaceLootPlayfab)
  3. BackendInterfaceLootPlayfab.init = function (self, backend_mirror)
  4. self._backend_mirror = backend_mirror
  5. self._last_id = 0
  6. self._loot_requests = {}
  7. self._loot_chest_opening_request_callbacks = {}
  8.  
  9. return
  10. end
  11. BackendInterfaceLootPlayfab.ready = function (self)
  12. return true
  13. end
  14. BackendInterfaceLootPlayfab.update = function (self, dt)
  15. return
  16. end
  17. BackendInterfaceLootPlayfab._new_id = function (self)
  18. self._last_id = self._last_id + 1
  19.  
  20. return self._last_id
  21. end
  22. BackendInterfaceLootPlayfab.open_loot_chest = function (self, hero_name, backend_id, callback_function)
  23. local id = self._new_id(self)
  24. local generate_loot_chest_rewards_request = {
  25. FunctionName = "generateLootChestRewards",
  26. FunctionParameter = {
  27. hero_name = hero_name,
  28. playfab_id = backend_id,
  29. id = id
  30. }
  31. }
  32. local loot_chest_rewards_request_cb = callback(self, "loot_chest_rewards_request_cb")
  33.  
  34. PlayFabClientApi.ExecuteCloudScript(generate_loot_chest_rewards_request, loot_chest_rewards_request_cb, loot_chest_rewards_request_cb)
  35.  
  36. self._loot_chest_opening_request_callbacks[id] = callback_function
  37.  
  38. return
  39. end
  40. BackendInterfaceLootPlayfab.loot_chest_rewards_request_cb = function (self, result)
  41. if result.Error then
  42. table.dump(result, nil, 5)
  43. fassert(false, "loot_chest_rewards_request_cb: it failed!")
  44. else
  45. local function_result = result.FunctionResult
  46. local items = function_result.items
  47. local id = function_result.id
  48. local consume_data = function_result.consumed_chest
  49. local chest_backend_id = consume_data.ItemInstanceId
  50. local remaining_uses = consume_data.RemainingUses
  51. local num_items = #items
  52. local loot = {}
  53. local backend_mirror = self._backend_mirror
  54.  
  55. for i = 1, num_items, 1 do
  56. local item = items[i]
  57. local backend_id = item.ItemInstanceId
  58.  
  59. backend_mirror.add_item(backend_mirror, backend_id, item)
  60.  
  61. loot[#loot + 1] = backend_id
  62. end
  63.  
  64. if 0 < remaining_uses then
  65. backend_mirror.update_item_field(backend_mirror, chest_backend_id, "RemainingUses", remaining_uses)
  66. else
  67. backend_mirror.remove_item(backend_mirror, chest_backend_id)
  68. end
  69.  
  70. local callback = self._loot_chest_opening_request_callbacks[id]
  71.  
  72. callback(loot)
  73. end
  74.  
  75. return
  76. end
  77. BackendInterfaceLootPlayfab.generate_end_of_level_loot = function (self, game_won, quick_play_bonus, difficulty, level_key, num_tomes, num_grims, num_loot_dice, hero_name, start_experience, end_experience, deed_item_name, deed_backend_id)
  78. local id = self._new_id(self)
  79. local generate_end_of_level_loot_request = {
  80. FunctionName = "generateEndOfLevelLoot",
  81. FunctionParameter = {
  82. won = game_won,
  83. quick_play_bonus = quick_play_bonus,
  84. difficulty = difficulty,
  85. level_name = level_key,
  86. num_tomes = num_tomes,
  87. num_grims = num_grims,
  88. num_dice = num_loot_dice,
  89. start_experience = start_experience,
  90. end_experience = end_experience,
  91. hero_name = hero_name,
  92. deed_item_name = deed_item_name,
  93. deed_backend_id = deed_backend_id,
  94. id = id
  95. }
  96. }
  97. local end_of_level_loot_request_cb = callback(self, "end_of_level_loot_request_cb")
  98.  
  99. PlayFabClientApi.ExecuteCloudScript(generate_end_of_level_loot_request, end_of_level_loot_request_cb, end_of_level_loot_request_cb)
  100.  
  101. return id
  102. end
  103. BackendInterfaceLootPlayfab.end_of_level_loot_request_cb = function (self, result)
  104. if result.Error then
  105. table.dump(result, nil, 5)
  106. fassert(false, "end_of_level_loot_request_cb: it failed!")
  107. else
  108. local function_result = result.FunctionResult
  109. local rewards = function_result.Rewards
  110. local items = function_result.Result
  111. local random_value = function_result.RandomValue
  112. local consumed_deed_result = function_result.ConsumedDeedResult
  113. local id = function_result.Id
  114. local num_items = #items
  115. local loot_request = {}
  116.  
  117. for item_type, item_data in pairs(rewards) do
  118. local backend_id, item = nil
  119.  
  120. for i = 1, num_items, 1 do
  121. item = items[i]
  122.  
  123. if item_data.ItemId == item.ItemId then
  124. backend_id = item.ItemInstanceId
  125.  
  126. break
  127. end
  128. end
  129.  
  130. loot_request[item_type] = {
  131. backend_id = backend_id
  132. }
  133.  
  134. if item_type == "chest" then
  135. loot_request[item_type].random_value = random_value
  136. end
  137.  
  138. self._backend_mirror:add_item(backend_id, item)
  139. end
  140.  
  141. if consumed_deed_result then
  142. local deed_backend_id = consumed_deed_result.ItemInstanceId
  143.  
  144. self._backend_mirror:remove_item(deed_backend_id)
  145. end
  146.  
  147. self._loot_requests[id] = loot_request
  148. end
  149.  
  150. return
  151. end
  152. BackendInterfaceLootPlayfab.is_loot_generated = function (self, id)
  153. local loot_request = self._loot_requests[id]
  154.  
  155. if loot_request then
  156. return true
  157. end
  158.  
  159. return false
  160. end
  161. BackendInterfaceLootPlayfab.get_loot = function (self, id)
  162. return self._loot_requests[id]
  163. end
  164.  
  165. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement