Advertisement
Guest User

class

a guest
Aug 26th, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.52 KB | None | 0 0
  1. local ChartboostAdapter ={}
  2.  
  3. local chartboost = require( "plugin.chartboost" )
  4.  
  5.  
  6. --[[This is an adapter that automatically gets information
  7. from the ads.setup class, and it is only created when
  8. this ad service is being used. This adapter is meant to work with:
  9.  
  10. https://github.com/swipeware/CoronaChartboostPlugin
  11.  
  12. ]]
  13.  
  14. function ChartboostAdapter:new()
  15. local adapter = {}
  16. local keys
  17. local currentCallback
  18. local currentFailCallback
  19. local onCached
  20. local failTrigger
  21. local gotAWillDisplay = false
  22.  
  23.  
  24. local function eraseCallbacks()
  25. -- currentCallback = nil
  26. currentFailCallback = nil
  27. onCached = nil
  28. gotAWillDisplay = false
  29.  
  30. end
  31.  
  32. local function log(action)
  33. if not action then return end
  34. local s = string.format("Ad:Chartboost:%s", tostring(action))
  35. Analytics.event(s)
  36. end
  37.  
  38. local function refireAfterCacheIfNecessary(event)
  39. print("REFIRING AFTER CACHED")
  40. dump(onCached)
  41. if onCached and event.location == onCached.location and event.type == onCached.kind then
  42. onCached.action()
  43. end
  44. end
  45.  
  46. local function chartboostListener(event)
  47. print "chartboost listener says:"
  48. if dump then dump(event) end
  49. log(event.response or "nil")
  50. if event.type == "interstitial" then
  51. if event.response == "didDisplay" then
  52. elseif event.response == "closed" and currentCallback then
  53. currentCallback()
  54. eraseCallbacks()
  55. elseif event.response == "clicked" then
  56. elseif event.response == "cached" then
  57. refireAfterCacheIfNecessary(event)
  58. end
  59. elseif event.type == "moreApps" then
  60. if event.response == "cached" then
  61. print("MOREAPPS CACHED ===========")
  62. refireAfterCacheIfNecessary(event)
  63. elseif event.response == "failed" then
  64. if currentFailCallback then
  65. currentFailCallback()
  66. eraseCallbacks()
  67. end
  68.  
  69. print("MOREAPPS FAILED ===========")
  70.  
  71. end
  72. elseif event.type == "rewardedVideo" then
  73. if event.response == "willDisplay" then
  74. gotAWillDisplay = true
  75. elseif event.response == "reward" then
  76. print("REWARD VIDEO COMPLETED")
  77. if currentCallback then
  78. currentCallback()
  79. eraseCallbacks()
  80. end
  81. elseif event.response == "failed" then
  82. print("REWARD VIDEO FAILED")
  83. failTrigger = Trigger.create(1500, function()
  84. if not gotAWillDisplay then
  85. if currentFailCallback then
  86. currentFailCallback()
  87. eraseCallbacks()
  88. end
  89. end
  90. gotAWillDisplay = false
  91. end)
  92. elseif event.response == "cached" then
  93. refireAfterCacheIfNecessary(event)
  94. end
  95. end
  96. end
  97.  
  98. function adapter:init()
  99. print("Running chartboost plugin: version", chartboost.getPluginVersion())
  100. keys = app.product.ads.chartboost
  101. local args = copy(keys)
  102. args.listener = chartboostListener
  103. chartboost.init(args)
  104. self:setAutoCache(true)
  105. end
  106.  
  107. function adapter:onResumeApp()
  108. chartboost.startSession( keys.appID, keys.appSignature )
  109. end
  110.  
  111. function adapter:onSuspendApp()
  112. end
  113.  
  114. function adapter:cacheAd(kind, location)
  115. print("KIND:")
  116. dump(kind)
  117. print("LOCATION:")
  118. dump(location)
  119. chartboost.cache(kind, location)
  120. end
  121.  
  122. function adapter:setAutoCache(isAutoCaching)
  123. chartboost.autoCacheAds(isAutoCaching)
  124. end
  125.  
  126. function adapter:isAdCached(kind, location)
  127. --print("CHECKED IF CACHED", kind)
  128. if kind == "interstitial" then
  129. return chartboost.hasCachedInterstitial(location)
  130. elseif kind == "rewardedVideo"
  131. then return chartboost.hasCachedRewardedVideo(location)
  132. elseif kind == "moreApps" then
  133. return chartboost.hasCachedMoreApps(location)
  134. else print("ChartboostAdapter: Invalid kind...", tostring(kind), tostring(location)) end
  135. end
  136.  
  137.  
  138. function adapter:showAd(kind, location, callback, failCallback)
  139. print("adapter:showAd(kind, location, callback, failCallback)")
  140.  
  141. eraseCallbacks()
  142. currentCallback = callback
  143. currentFailCallback = failCallback
  144.  
  145. if app.isSimulator then
  146. print("Failed because in simulator.")
  147. if currentFailCallback then
  148. currentFailCallback()
  149. eraseCallbacks()
  150. end
  151. return
  152. end
  153.  
  154. if not kind or type(kind) ~= "string" then
  155. print("Kind was invalid. replaced it.")
  156. kind = "interstitial"
  157. end
  158. if not location or type(location) ~= "string" then
  159. print("Location was invalid. replaced it.")
  160. location = "default" end
  161.  
  162. if self:isAdCached(kind, location) then
  163. print("CHARTBOOST.SHOW")
  164. chartboost.show(kind, location)
  165. else
  166. print("CHARTBOOST.CACHE")
  167. chartboost.cache(kind, location)
  168. onCached = { -- we fire the event being cached when it is ready.
  169. kind = kind,
  170. location = location,
  171. action = function()
  172. print("DID ON CACHED FUNCTION BECAUSE NOT CACHED")
  173. self:showAd(kind, location, callback, failCallback)
  174. end
  175. }
  176. print("Finished creating OnCACHE")
  177. end
  178.  
  179. end
  180.  
  181. function adapter:isShowingAd()
  182. local isShowingAd = chartboost.isAnyAdVisible()
  183. print ("Chartboost:isShowingAd", isShowingAd)
  184. return isShowingAd
  185. end
  186.  
  187. function adapter:destroy()
  188. chartboost = nil
  189. adapter = nil
  190. end
  191.  
  192. return adapter
  193.  
  194. end
  195.  
  196. return ChartboostAdapter
  197. -- RIPPED FROM APP EVENTS CONTROLLER
  198. -- self:startCachingLoop{
  199. -- cacheAction = function() chartboostAdapter:cacheAd (kind, location) end,
  200. -- checkAction = function() return chartboostAdapter:isAdCached (kind, location) end,
  201. -- completeAction = function() chartboostAdapter:showAd (kind, location, callback) end
  202. -- }
  203.  
  204. -- function controller:startCachingLoop(fnTable)
  205. -- print("AppEventsController:attemping to cache ad")
  206. -- fnTable.cacheAction()
  207. -- controller:cacheLoop(fnTable, 1)
  208. -- end
  209.  
  210. -- function controller:cacheLoop(fnTable, attempts)
  211. -- if system.isSimulator then
  212. -- -- print("APPEVENTS: No interstitials can be cached or played in the simulator.")
  213. -- return
  214. -- end
  215.  
  216. -- if attempts == model.maxCachingAttempts then
  217. -- print("AppEventsController: No ad available.");
  218. -- return
  219. -- end
  220.  
  221. -- t1 = trigger.create(model.cacheCheckTime, function()
  222.  
  223. -- local isCached = fnTable:checkAction()
  224. -- if isCached then
  225. -- print "AppEventsController:Showing ad"
  226. -- fnTable.completeAction()
  227. -- else
  228. -- controller:cacheLoop(fnTable, attempts+1)
  229. -- end
  230.  
  231. -- end)
  232. -- end
  233.  
  234.  
  235.  
  236.  
  237. --[[
  238. event.name: "chartboost"
  239. event.type: "interstitial"
  240. event.response: "willDisplay"
  241. -- sent before an ad is displayed
  242. event.location: namedLocation (string)
  243.  
  244. event.response: "didDisplay"
  245. -- sent when an ad is displayed
  246. event.location: namedLocation (string)
  247.  
  248. event.response: "closed"
  249. -- sent when an ad is closed
  250. event.location: namedLocation (string)
  251.  
  252. event.response: "clicked"
  253. -- sent when an ad is clicked
  254. event.location: namedLocation (string)
  255.  
  256. event.response: "cached"
  257. -- sent when an ad is cached
  258. event.location: namedLocation (string)
  259.  
  260. event.response: "failed"
  261. -- sent when an ad has failed to load
  262. event.location: namedLocation (string)
  263. event.info: detailed error info (string)
  264.  
  265. event.type: "rewardedVideo"
  266. event.response: "willDisplay"
  267. -- sent before an ad is displayed
  268. event.location: namedLocation (string)
  269.  
  270. event.response: "didDisplay"
  271. -- sent when an ad is displayed
  272. event.location: namedLocation (string)
  273.  
  274. event.response: "closed"
  275. -- sent when an ad is closed
  276. event.location: namedLocation (string)
  277.  
  278. event.response: "clicked"
  279. -- sent when an ad is clicked
  280. event.location: namedLocation (string)
  281.  
  282. event.response: "cached"
  283. -- sent when an ad is cached
  284. event.location: namedLocation (string)
  285.  
  286. event.response: "failed"
  287. -- sent when an ad has failed to load
  288. event.location: namedLocation (string)
  289. event.info: detailed error info (string)
  290.  
  291. event.response: "reward"
  292. -- Sent when a video has been viewed, and a reward should be given
  293. event.location: namedLocation (string)
  294. event.info: reward amount (number in string)
  295. (Reward amount is specified in the Chartboost dashboard)
  296.  
  297. event.type: "moreApps"
  298. event.response: "willDisplay"
  299. -- sent before More Apps is displayed
  300. event.location: namedLocation (string)
  301.  
  302. event.response: "didDisplay"
  303. -- sent when More Apps is displayed
  304. event.location: namedLocation (string)
  305.  
  306. event.response: "closed"
  307. -- sent when More Apps is closed
  308. event.location: namedLocation (string)
  309.  
  310. event.response: "clicked"
  311. -- sent when an app in More Apps is clicked
  312. event.location: namedLocation (string)
  313.  
  314. event.response: "cached"
  315. -- sent when More Apps is cached
  316. event.location: namedLocation (string)
  317.  
  318. event.response: "failed"
  319. -- sent when More Apps has failed to load
  320. event.location: namedLocation (string)
  321. event.info: detailed error info (string)
  322. Error codes (given in event.info)
  323. 0 Unknown internal error
  324. 1 Network is currently unavailable
  325. 2 Too many requests are pending for that location
  326. 3 Interstitial loaded with wrong orientation
  327. 4 Interstitial disabled, first session
  328. 5 Network request failed
  329. 6 No ad received (no inventory / no campaigns)
  330. 7 Session not started
  331. 8 User manually cancelled the impression
  332. 9 No location detected
  333. Example
  334.  
  335. -- Require the Chartboost library
  336.  
  337.  
  338. -- Initialize the Chartboost library
  339. chartboost.init {
  340. appID = "app_ID_generated_from_chartboost_here",
  341. appSignature = "app_signature_generated_from_chartboost_here",
  342. listener = function( event )
  343. -- Print the events key/pair values
  344. for k,v in pairs( event ) do
  345. print( k, ":", v )
  346. end
  347. end
  348. }
  349.  
  350. Location Description
  351. ---------------- ---------------------------------------------------------------------
  352. "Default" Legacy default location
  353. "Startup" Initial startup of game
  354. "Home Screen" Home screen the player first sees
  355. "Main Menu" Menu that provides game options
  356. "Game Screen" Game screen where all the magic happens
  357. "Achievements" Screen with list of achievements in the game
  358. "Quests" Quest, missions or goals screen describing things for a player to do
  359. "Pause" Pause screen
  360. "Level Start" Start of the level
  361. "Level Complete" Completion of the level
  362. "Turn Complete" Finishing a turn in a game
  363. "IAP Store" The store where the player pays real money for currency
  364. "Item Store" The store where a player buys virtual goods
  365. "Game Over" The game over screen after a player is finished playing
  366. "Leaderboard" List of leaders in the game
  367. "Settings" Screen where player can change settings such as sound
  368. "Quit" Screen displayed right before the player exits a game
  369.  
  370. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement