Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ChartboostAdapter ={}
- local chartboost = require( "plugin.chartboost" )
- --[[This is an adapter that automatically gets information
- from the ads.setup class, and it is only created when
- this ad service is being used. This adapter is meant to work with:
- https://github.com/swipeware/CoronaChartboostPlugin
- ]]
- function ChartboostAdapter:new()
- local adapter = {}
- local keys
- local currentCallback
- local currentFailCallback
- local onCached
- local failTrigger
- local gotAWillDisplay = false
- local function eraseCallbacks()
- -- currentCallback = nil
- currentFailCallback = nil
- onCached = nil
- gotAWillDisplay = false
- end
- local function log(action)
- if not action then return end
- local s = string.format("Ad:Chartboost:%s", tostring(action))
- Analytics.event(s)
- end
- local function refireAfterCacheIfNecessary(event)
- print("REFIRING AFTER CACHED")
- dump(onCached)
- if onCached and event.location == onCached.location and event.type == onCached.kind then
- onCached.action()
- end
- end
- local function chartboostListener(event)
- print "chartboost listener says:"
- if dump then dump(event) end
- log(event.response or "nil")
- if event.type == "interstitial" then
- if event.response == "didDisplay" then
- elseif event.response == "closed" and currentCallback then
- currentCallback()
- eraseCallbacks()
- elseif event.response == "clicked" then
- elseif event.response == "cached" then
- refireAfterCacheIfNecessary(event)
- end
- elseif event.type == "moreApps" then
- if event.response == "cached" then
- print("MOREAPPS CACHED ===========")
- refireAfterCacheIfNecessary(event)
- elseif event.response == "failed" then
- if currentFailCallback then
- currentFailCallback()
- eraseCallbacks()
- end
- print("MOREAPPS FAILED ===========")
- end
- elseif event.type == "rewardedVideo" then
- if event.response == "willDisplay" then
- gotAWillDisplay = true
- elseif event.response == "reward" then
- print("REWARD VIDEO COMPLETED")
- if currentCallback then
- currentCallback()
- eraseCallbacks()
- end
- elseif event.response == "failed" then
- print("REWARD VIDEO FAILED")
- failTrigger = Trigger.create(1500, function()
- if not gotAWillDisplay then
- if currentFailCallback then
- currentFailCallback()
- eraseCallbacks()
- end
- end
- gotAWillDisplay = false
- end)
- elseif event.response == "cached" then
- refireAfterCacheIfNecessary(event)
- end
- end
- end
- function adapter:init()
- print("Running chartboost plugin: version", chartboost.getPluginVersion())
- keys = app.product.ads.chartboost
- local args = copy(keys)
- args.listener = chartboostListener
- chartboost.init(args)
- self:setAutoCache(true)
- end
- function adapter:onResumeApp()
- chartboost.startSession( keys.appID, keys.appSignature )
- end
- function adapter:onSuspendApp()
- end
- function adapter:cacheAd(kind, location)
- print("KIND:")
- dump(kind)
- print("LOCATION:")
- dump(location)
- chartboost.cache(kind, location)
- end
- function adapter:setAutoCache(isAutoCaching)
- chartboost.autoCacheAds(isAutoCaching)
- end
- function adapter:isAdCached(kind, location)
- --print("CHECKED IF CACHED", kind)
- if kind == "interstitial" then
- return chartboost.hasCachedInterstitial(location)
- elseif kind == "rewardedVideo"
- then return chartboost.hasCachedRewardedVideo(location)
- elseif kind == "moreApps" then
- return chartboost.hasCachedMoreApps(location)
- else print("ChartboostAdapter: Invalid kind...", tostring(kind), tostring(location)) end
- end
- function adapter:showAd(kind, location, callback, failCallback)
- print("adapter:showAd(kind, location, callback, failCallback)")
- eraseCallbacks()
- currentCallback = callback
- currentFailCallback = failCallback
- if app.isSimulator then
- print("Failed because in simulator.")
- if currentFailCallback then
- currentFailCallback()
- eraseCallbacks()
- end
- return
- end
- if not kind or type(kind) ~= "string" then
- print("Kind was invalid. replaced it.")
- kind = "interstitial"
- end
- if not location or type(location) ~= "string" then
- print("Location was invalid. replaced it.")
- location = "default" end
- if self:isAdCached(kind, location) then
- print("CHARTBOOST.SHOW")
- chartboost.show(kind, location)
- else
- print("CHARTBOOST.CACHE")
- chartboost.cache(kind, location)
- onCached = { -- we fire the event being cached when it is ready.
- kind = kind,
- location = location,
- action = function()
- print("DID ON CACHED FUNCTION BECAUSE NOT CACHED")
- self:showAd(kind, location, callback, failCallback)
- end
- }
- print("Finished creating OnCACHE")
- end
- end
- function adapter:isShowingAd()
- local isShowingAd = chartboost.isAnyAdVisible()
- print ("Chartboost:isShowingAd", isShowingAd)
- return isShowingAd
- end
- function adapter:destroy()
- chartboost = nil
- adapter = nil
- end
- return adapter
- end
- return ChartboostAdapter
- -- RIPPED FROM APP EVENTS CONTROLLER
- -- self:startCachingLoop{
- -- cacheAction = function() chartboostAdapter:cacheAd (kind, location) end,
- -- checkAction = function() return chartboostAdapter:isAdCached (kind, location) end,
- -- completeAction = function() chartboostAdapter:showAd (kind, location, callback) end
- -- }
- -- function controller:startCachingLoop(fnTable)
- -- print("AppEventsController:attemping to cache ad")
- -- fnTable.cacheAction()
- -- controller:cacheLoop(fnTable, 1)
- -- end
- -- function controller:cacheLoop(fnTable, attempts)
- -- if system.isSimulator then
- -- -- print("APPEVENTS: No interstitials can be cached or played in the simulator.")
- -- return
- -- end
- -- if attempts == model.maxCachingAttempts then
- -- print("AppEventsController: No ad available.");
- -- return
- -- end
- -- t1 = trigger.create(model.cacheCheckTime, function()
- -- local isCached = fnTable:checkAction()
- -- if isCached then
- -- print "AppEventsController:Showing ad"
- -- fnTable.completeAction()
- -- else
- -- controller:cacheLoop(fnTable, attempts+1)
- -- end
- -- end)
- -- end
- --[[
- event.name: "chartboost"
- event.type: "interstitial"
- event.response: "willDisplay"
- -- sent before an ad is displayed
- event.location: namedLocation (string)
- event.response: "didDisplay"
- -- sent when an ad is displayed
- event.location: namedLocation (string)
- event.response: "closed"
- -- sent when an ad is closed
- event.location: namedLocation (string)
- event.response: "clicked"
- -- sent when an ad is clicked
- event.location: namedLocation (string)
- event.response: "cached"
- -- sent when an ad is cached
- event.location: namedLocation (string)
- event.response: "failed"
- -- sent when an ad has failed to load
- event.location: namedLocation (string)
- event.info: detailed error info (string)
- event.type: "rewardedVideo"
- event.response: "willDisplay"
- -- sent before an ad is displayed
- event.location: namedLocation (string)
- event.response: "didDisplay"
- -- sent when an ad is displayed
- event.location: namedLocation (string)
- event.response: "closed"
- -- sent when an ad is closed
- event.location: namedLocation (string)
- event.response: "clicked"
- -- sent when an ad is clicked
- event.location: namedLocation (string)
- event.response: "cached"
- -- sent when an ad is cached
- event.location: namedLocation (string)
- event.response: "failed"
- -- sent when an ad has failed to load
- event.location: namedLocation (string)
- event.info: detailed error info (string)
- event.response: "reward"
- -- Sent when a video has been viewed, and a reward should be given
- event.location: namedLocation (string)
- event.info: reward amount (number in string)
- (Reward amount is specified in the Chartboost dashboard)
- event.type: "moreApps"
- event.response: "willDisplay"
- -- sent before More Apps is displayed
- event.location: namedLocation (string)
- event.response: "didDisplay"
- -- sent when More Apps is displayed
- event.location: namedLocation (string)
- event.response: "closed"
- -- sent when More Apps is closed
- event.location: namedLocation (string)
- event.response: "clicked"
- -- sent when an app in More Apps is clicked
- event.location: namedLocation (string)
- event.response: "cached"
- -- sent when More Apps is cached
- event.location: namedLocation (string)
- event.response: "failed"
- -- sent when More Apps has failed to load
- event.location: namedLocation (string)
- event.info: detailed error info (string)
- Error codes (given in event.info)
- 0 Unknown internal error
- 1 Network is currently unavailable
- 2 Too many requests are pending for that location
- 3 Interstitial loaded with wrong orientation
- 4 Interstitial disabled, first session
- 5 Network request failed
- 6 No ad received (no inventory / no campaigns)
- 7 Session not started
- 8 User manually cancelled the impression
- 9 No location detected
- Example
- -- Require the Chartboost library
- -- Initialize the Chartboost library
- chartboost.init {
- appID = "app_ID_generated_from_chartboost_here",
- appSignature = "app_signature_generated_from_chartboost_here",
- listener = function( event )
- -- Print the events key/pair values
- for k,v in pairs( event ) do
- print( k, ":", v )
- end
- end
- }
- Location Description
- ---------------- ---------------------------------------------------------------------
- "Default" Legacy default location
- "Startup" Initial startup of game
- "Home Screen" Home screen the player first sees
- "Main Menu" Menu that provides game options
- "Game Screen" Game screen where all the magic happens
- "Achievements" Screen with list of achievements in the game
- "Quests" Quest, missions or goals screen describing things for a player to do
- "Pause" Pause screen
- "Level Start" Start of the level
- "Level Complete" Completion of the level
- "Turn Complete" Finishing a turn in a game
- "IAP Store" The store where the player pays real money for currency
- "Item Store" The store where a player buys virtual goods
- "Game Over" The game over screen after a player is finished playing
- "Leaderboard" List of leaders in the game
- "Settings" Screen where player can change settings such as sound
- "Quit" Screen displayed right before the player exits a game
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement