Skip to content

Commit

Permalink
Added support for Yandex Mobile Ads
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysinyavsky committed Dec 6, 2022
1 parent 9e1468b commit 9be4107
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Supported services:
* Unity Ads
* Poki
* Yandex
* Yandex Mobile Ads
* Vk Bridge
* Applovin Max

Expand Down Expand Up @@ -455,6 +456,7 @@ More information can be found at the links:
* [Unity Ads](docs/networks.md#unity-ads)
* [Poki](docs/networks.md#poki)
* [Yandex](docs/networks.md#yandex)
* [Yandex Mobile Ads](docs/networks.md#yandex-mobile-ads)
* [Vk Bridge](docs/networks.md#vk-bridge)
* [Applovin Max](docs/networks.md#applovin-max)
* [Admob and Unity Ads](docs/networks.md#admob-and-unity-ads)
Expand Down
6 changes: 3 additions & 3 deletions ads_wrapper/ads_networks/yandex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

---Call callback in the second frame. Send result.
---It is necessary to use timer for the coroutine to continue.
---@param result hash
---@param result table
local function callback_delay(callback, result)
if callback then
timer.delay(0, false, function()
Expand All @@ -46,7 +46,7 @@ end

---Call saved `module_callback` in the second frame. Send result.
---It is necessary to use timer for the coroutine to continue.
---@param result hash
---@param result table
local function callback_once_delay(result)
if module_callback then
timer.delay(0, false, function()
Expand Down Expand Up @@ -407,7 +407,7 @@ end

---Sets banner position.
---@param position table table `{x = string, y = string}`
---@return hash
---@return table
function M.set_banner_position(position)
if not position then
return helper.error("YANDEX: Position must be given")
Expand Down
238 changes: 238 additions & 0 deletions ads_wrapper/ads_networks/yandex_mobile.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
local ads = require("ads_wrapper.ads_wrapper")
local helper = require("ads_wrapper.ads_networks.helper")

local M = { NAME = "yandex_mobile" }
-- Extention: https://github.com/osov/defold-yandex-sdk-ads

local parameters
local module_callback
local banner_showed = false
local is_yandexads_initialized = false
local is_reward_get = false

---Call saved `module_callback` only once.
---@param response any
local function callback_once(response)
if module_callback then
local callback = module_callback
module_callback = nil
callback(response)
end
end

---Call callback in the second frame.
---It is necessary to use timer for the coroutine to continue.
---@param response table
local function callback_delay(callback, response)
if callback then
timer.delay(0, false, function()
callback(response)
end)
end
end

local function yandexads_callback(self, message_id, message)
if message_id == yandexads.MSG_ADS_INITED then
is_yandexads_initialized = true
if message.event == yandexads.EVENT_LOADED then
callback_once(helper.success())
end
elseif message_id == yandexads.MSG_INTERSTITIAL then
if message.event == yandexads.EVENT_SHOWN then
-- print("yandexads: EVENT_SHOWN: Interstitial AD is showed")
elseif message.event == yandexads.EVENT_DISMISSED then
callback_once(helper.success())
-- print("yandexads: EVENT_DISMISSED: Interstitial AD is dismissed")
elseif message.event == yandexads.EVENT_CLICKED then
-- print("yandexads: EVENT_CLICKED: Interstitial AD is clicked")
elseif message.event == yandexads.EVENT_NOT_LOADED then
callback_once(helper.error("yandexads: EVENT_NOT_LOADED: Interstitial AD not loaded\nError: " .. tostring(message.error)))
elseif message.event == yandexads.EVENT_LOADED then
callback_once(helper.success())
elseif message.event == yandexads.EVENT_ERROR_LOAD then
callback_once(helper.error("yandexads: EVENT_ERROR_LOAD: Interstitial Error load: " .. tostring(message.error)))
elseif message.event == yandexads.EVENT_IMPRESSION then
-- print("yandexads: EVENT_IMPRESSION: Interstitial did record impression")
end
elseif message_id == yandexads.MSG_REWARDED then
if message.event == yandexads.EVENT_SHOWN then
-- print("yandexads: EVENT_SHOWN: Rewarded AD is showed")
elseif message.event == yandexads.EVENT_DISMISSED then
-- print("yandexads: EVENT_DISMISSED: Rewarded AD is dismissed")
if is_reward_get then
callback_once(helper.success())
else
callback_once(helper.skipped())
end
is_reward_get = false
elseif message.event == yandexads.EVENT_NOT_LOADED then
callback_once(helper.error("yandexads: EVENT_NOT_LOADED: Rewarded AD not loaded \nError: " .. tostring(message.error)))
elseif message.event == yandexads.EVENT_CLICKED then
-- print("yandexads: EVENT_CLICKED: Rewarded AD is clicked")
elseif message.event == yandexads.EVENT_ERROR_LOAD then
callback_once(helper.error("yandexads: EVENT_ERROR_LOAD: Rewarded AD failed to load\nError: " .. tostring(message.error)))
elseif message.event == yandexads.EVENT_LOADED then
callback_once(helper.success())
elseif message.event == yandexads.EVENT_REWARDED then
is_reward_get = true
elseif message.event == yandexads.EVENT_IMPRESSION then
-- print("yandexads: EVENT_IMPRESSION: Rewarded did record impression")
end
elseif message_id == yandexads.MSG_BANNER then
if message.event == yandexads.EVENT_LOADED then
callback_once(helper.success("yandexads: EVENT_LOADED: Banner AD loaded"))
elseif message.event == yandexads.EVENT_ERROR_LOAD then
callback_once(helper.error("yandexads: EVENT_ERROR_LOAD: Banner AD failed to load\nError: " .. tostring(message.error)))
elseif message.event == yandexads.EVENT_CLICKED then
-- print("yandexads: EVENT_CLICKED: Banner AD loaded")
elseif message.event == yandexads.EVENT_DESTROYED then
callback_once(helper.success("yandexads: EVENT_DESTROYED: Banner AD destroyed"))
elseif message.event == yandexads.EVENT_IMPRESSION then
-- print("yandexads: EVENT_IMPRESSION: Banner did record impression")
end
end
end

---Api setup
---@param params table
function M.setup(params)
parameters = params
end

---Initializes `yandexads` sdk.
---@param callback function the function is called after execution.
function M.init(callback)
module_callback = callback
if ads.is_debug then
parameters[ads.T_BANNER] = "R-M-DEMO-300x250" -- test unit for banners
parameters[ads.T_INTERSTITIAL] = "R-M-DEMO-interstitial" -- test unit for interstitial
parameters[ads.T_REWARDED] = "R-M-DEMO-rewarded-client-side-rtb" -- test unit for rewarded
end
yandexads.set_callback(yandexads_callback)
yandexads.initialize()
end

---Check if the environment supports yandexads api
---@return bool
function M.is_supported()
return yandexads ~= nil
end

---Check if the yandexads is initialized
---@return bool
function M.is_initialized()
return is_yandexads_initialized
end

---Shows rewarded ads.
---@param callback function the function is called after execution.
function M.show_rewarded(callback)
module_callback = callback
yandexads.set_callback(yandexads_callback)
yandexads.show_rewarded()
end

---Loads rewarded ads
---@param callback function the function is called after execution.
function M.load_rewarded(callback)
module_callback = callback
yandexads.set_callback(yandexads_callback)
yandexads.load_rewarded(parameters[ads.T_REWARDED])
end

---Check if the rewarded ads is loaded
---@return boolean
function M.is_rewarded_loaded()
return yandexads.is_rewarded_loaded()
end

---Shows interstitial ads.
---@param callback function the function is called after execution.
function M.show_interstitial(callback)
module_callback = callback
yandexads.set_callback(yandexads_callback)
yandexads.show_interstitial()
end

---Loads interstitial ads
---@param callback function the function is called after execution.
function M.load_interstitial(callback)
module_callback = callback
yandexads.set_callback(yandexads_callback)
yandexads.load_interstitial(parameters[ads.T_INTERSTITIAL])
end

---Check if the interstitial ads is loaded
---@return boolean
function M.is_interstitial_loaded()
return yandexads.is_interstitial_loaded()
end

---Check if the banner is set up
---@return boolean
function M.is_banner_setup()
return parameters[ads.T_BANNER]
end

---Loads banner. Use `ads.T_BANNER` parameter.
---@param callback function the function is called after execution.
function M.load_banner(callback)
if not M.is_banner_setup() then
callback_delay(callback, helper.error("yandexads: Banner not setup"))
return
end
module_callback = callback
yandexads.set_callback(yandexads_callback)
yandexads.load_banner(parameters[ads.T_BANNER])
end

---Unloads active banner.
---@param callback function the function is called after execution.
function M.unload_banner(callback)
if M.is_banner_loaded() then
module_callback = callback
yandexads.set_callback(yandexads_callback)
banner_showed = false
yandexads.destroy_banner()
else
callback_delay(callback, helper.error("yandexads: Banner not loaded"))
end
end

---Check if the banner is loaded
---@return boolean
function M.is_banner_loaded()
return yandexads.is_banner_loaded()
end

---Shows loaded banner.
---@param callback function the function is called after execution.
function M.show_banner(callback)
if M.is_banner_loaded() then
yandexads.show_banner()
banner_showed = true
callback_delay(callback, helper.success())
else
callback_delay(callback, helper.error("yandexads: Banner not loaded"))
end
end

---Hides loaded banner.
---@param callback function the function is called after execution.
function M.hide_banner(callback)
if M.is_banner_loaded() then
yandexads.hide_banner()
banner_showed = false
callback_delay(callback, helper.success())
else
callback_delay(callback, helper.error("yandexads: Banner not loaded"))
end
end

---Check if the banner is showed
---@return boolean
function M.is_banner_showed()
return banner_showed
end

return M
2 changes: 1 addition & 1 deletion ads_wrapper/ads_wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end

---Call callback in the second frame. Send result.
---It is necessary to use timer for the coroutine to continue.
---@param result hash
---@param result table
local function handle(callback, result)
if callback then
timer.delay(0, false, function()
Expand Down
33 changes: 29 additions & 4 deletions docs/networks.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [Unity Ads](#unity-ads)
* [Poki](#poki)
* [Yandex](#yandex)
* [Yandex Mobile Ads](#yandex-mobile-ads)
* [Vk Bridge](#vk-bridge)
* [Applovin Max](#applovin-max)
* [Admob and Unity Ads](#admob-and-unity-ads)
Expand Down Expand Up @@ -34,7 +35,7 @@ end)

The network uses [this](https://github.com/defold/extension-admob) extension.
In the `debug` mode will always be used test keys.
Verified version: **2.1.2**
Verified version: **3.0.1**

> ⚠️ Don't forget to add settings to game.project
Expand All @@ -47,7 +48,7 @@ You need to set:
* position <kbd>number</kbd> _optional_ banner position. Default `admob.POS_NONE`.

```lua
-- Need to add the extension: https://github.com/defold/extension-admob/archive/refs/tags/2.1.2.zip
-- Need to add the extension: https://github.com/defold/extension-admob/archive/refs/tags/3.0.1.zip
local admob_module = require("ads_wrapper.ads_networks.admob")
local admob_net_id = ads_wrapper.register_network(admob_module, {
[ads_wrapper.T_REWARDED] = "ca-app-pub-3940256099942544/5224354917",
Expand All @@ -64,7 +65,7 @@ local admob_net_id = ads_wrapper.register_network(admob_module, {

The network uses [this](https://github.com/AGulev/DefVideoAds) extension.
In the `debug` mode will always be used test keys.
Verified version: **4.2.2**
Verified version: **4.5.1**

You need to set:
* ids <kbd>table</kbd> _required_
Expand All @@ -78,7 +79,7 @@ You need to set:
* position <kbd>number</kbd> _optional_ banner position. Default `unityads.BANNER_POSITION_TOP_CENTER`.

```lua
-- Need to add the extension: https://github.com/AGulev/DefVideoAds/archive/refs/tags/4.1.2.zip
-- Need to add the extension: https://github.com/AGulev/DefVideoAds/archive/refs/tags/4.5.1.zip
local unity = require("ads_wrapper.ads_networks.unity")
local platform = require("ads_wrapper.platform")
local unity_net_id = ads_wrapper.register_network(unity, {
Expand Down Expand Up @@ -133,6 +134,30 @@ ads_wrapper.setup_video({{id = yandex_net_id, count = 1}}, 1)
ads_wrapper.setup_banner({{id = yandex_net_id, count = 1}}, 1)
```

## Yandex Mobile Ads

The network uses [this](https://github.com/osov/defold-yandex-sdk-ads) extension.
In the `debug` mode will always be used test keys.
Verified version: **1.0**

You need to set:
* [ads_wrapper.T_INTERSTITIAL] <kbd>string</kbd> _required_ key for interstitial ads
* [ads_wrapper.T_REWARDED] <kbd>string</kbd> _required_ key for rewarded ads
* [ads_wrapper.T_BANNER] <kbd>string</kbd> _optional_ key for banner

```lua
-- Need to add the extension: https://github.com/osov/defold-yandex-sdk-ads/releases/download/1.0/defold-yandex-sdk-ads.zip
local yandex_mobiles = require("ads_wrapper.ads_networks.yandex_mobile")
local yandex_mobiles_net_id = ads_wrapper.register_network(yandex_mobiles, {
[ads_wrapper.T_BANNER] = "R-M-DEMO-300x250",
[ads_wrapper.T_INTERSTITIAL] = "R-M-DEMO-interstitial",
[ads_wrapper.T_REWARDED] = "R-M-DEMO-rewarded-client-side-rtb"
})

ads_wrapper.setup_video({ { id = yandex_mobiles_net_id, count = 1 } })
ads_wrapper.setup_banner({ { id = yandex_mobiles_net_id, count = 1 } })
```

## Vk Bridge

The network uses [this](https://github.com/potatojam/defold-vkbridge) extension.
Expand Down
Loading

0 comments on commit 9be4107

Please sign in to comment.