@@ -13,8 +13,16 @@ enum PokiCallbackType
1313 TYPE_SHARABLE_URL
1414};
1515
16+ // make sure these match with the constants in lib_pokisdk.js
17+ enum PokiRewardedBreakResult
18+ {
19+ REWARDED_BREAK_ERROR = 0 ,
20+ REWARDED_BREAK_SUCCESS = 1 ,
21+ REWARDED_BREAK_START = 2 ,
22+ };
23+
1624typedef void (*CommercialBreakCallback)();
17- typedef void (*RewardedBreakCallback)(int success );
25+ typedef void (*RewardedBreakCallback)(PokiRewardedBreakResult result );
1826typedef void (*ShareableURLCallback)(const char * url, int url_length);
1927
2028extern " C" {
@@ -25,7 +33,7 @@ extern "C" {
2533 void PokiSdkJs_InternalCaptureError (const char * formatted_string);
2634
2735 void PokiSdkJs_CommercialBreak (CommercialBreakCallback callback);
28- void PokiSdkJs_RewardedBreak (RewardedBreakCallback callback);
36+ void PokiSdkJs_RewardedBreak (const char * size, RewardedBreakCallback callback);
2937
3038 int PokiSdkJs_IsAdBlocked ();
3139
@@ -54,7 +62,7 @@ static void PokiSdk_InvokeCallback(PokiCallbackType callbackType, int intArg, co
5462{
5563 if (!dmScript::IsCallbackValid (pokiSdk_Callback))
5664 {
57- dmLogError (" PokiSDK InvokeCallback callback is invalid. Use callback function as an argument when show ADS ." );
65+ dmLogError (" PokiSDK InvokeCallback callback is invalid. Use callback function as an argument when showing ads ." );
5866 return ;
5967 }
6068
@@ -69,9 +77,15 @@ static void PokiSdk_InvokeCallback(PokiCallbackType callbackType, int intArg, co
6977
7078 int numOfArgs = 0 ;
7179
80+ bool destroy_callback = true ;
81+
7282 if (callbackType == TYPE_REWARDED)
7383 {
74- lua_pushboolean (L, intArg);
84+ // do not destroy the callback if the event is REWARDED_BREAK_START
85+ // since we need to use the callback later when the event
86+ // is REWARDED_BREAK_SUCCESS
87+ destroy_callback = (intArg != REWARDED_BREAK_START);
88+ lua_pushnumber (L, intArg);
7589 numOfArgs = 1 ;
7690 }
7791 else if (callbackType == TYPE_SHARABLE_URL)
@@ -86,7 +100,7 @@ static void PokiSdk_InvokeCallback(PokiCallbackType callbackType, int intArg, co
86100
87101 dmScript::TeardownCallback (pokiSdk_Callback);
88102
89- if (pokiSdk_Callback != 0x0 )
103+ if (( pokiSdk_Callback != 0x0 ) && destroy_callback )
90104 {
91105 dmScript::DestroyCallback (pokiSdk_Callback);
92106 pokiSdk_Callback = 0x0 ;
@@ -98,9 +112,9 @@ static void PokiSdk_CommercialBreakCallback()
98112 PokiSdk_InvokeCallback (TYPE_INTERSTITIAL, 0 , 0 );
99113}
100114
101- static void PokiSdk_RewardedBreakCallback (int success )
115+ static void PokiSdk_RewardedBreakCallback (PokiRewardedBreakResult result )
102116{
103- PokiSdk_InvokeCallback (TYPE_REWARDED, success , 0 );
117+ PokiSdk_InvokeCallback (TYPE_REWARDED, result , 0 );
104118}
105119
106120static void PokiSdk_ShareableURLCallback (const char * url, int url_length)
@@ -125,11 +139,13 @@ static int PokiSdk_GameplayStop(lua_State* L)
125139static int PokiSdk_CommercialBreak (lua_State* L)
126140{
127141 int type = lua_type (L, 1 );
128- if (type != LUA_TFUNCTION) {
129- luaL_error (L, " PokiSDK CommercialBreak callback is invalid. Use callback function as an argument when show ADS." );
142+ if (type != LUA_TFUNCTION)
143+ {
144+ luaL_error (L, " PokiSDK CommercialBreak callback is invalid. Use callback function as an argument when showing ads." );
130145 return 0 ;
131146 }
132- if (pokiSdk_Callback != 0x0 ) {
147+ if (pokiSdk_Callback != 0x0 )
148+ {
133149 dmLogError (" PokiSdk_CommercialBreak PokiSDK callback already exist. Rewrite callback" );
134150 }
135151 DM_LUA_STACK_CHECK (L, 0 );
@@ -138,19 +154,38 @@ static int PokiSdk_CommercialBreak(lua_State* L)
138154 return 0 ;
139155}
140156
141- static int PokiSdk_RewardedBreak (lua_State* L)
157+ static void PokiSdk_SetRewardedBreakCallback (lua_State* L, int index )
142158{
143- int type = lua_type (L, 1 );
144- if (type != LUA_TFUNCTION) {
145- luaL_error (L, " PokiSDK RewardedBreak callback is invalid. Use callback function as an argument when show ADS ." );
146- return 0 ;
159+ if ( lua_type (L, index) != LUA_TFUNCTION)
160+ {
161+ luaL_error (L, " PokiSDK RewardedBreak callback is invalid. Use callback function as an argument when showing ads ." );
162+ return ;
147163 }
148- if (pokiSdk_Callback != 0x0 ) {
164+
165+ if (pokiSdk_Callback != 0x0 )
166+ {
149167 dmLogError (" PokiSdk_RewardedBreak PokiSDK callback already exist. Rewrite callback" );
150168 }
169+ pokiSdk_Callback = dmScript::CreateCallback (L, index);
170+ }
171+
172+ static int PokiSdk_RewardedBreak (lua_State* L)
173+ {
151174 DM_LUA_STACK_CHECK (L, 0 );
152- pokiSdk_Callback = dmScript::CreateCallback (L, 1 );
153- PokiSdkJs_RewardedBreak ((RewardedBreakCallback)PokiSdk_RewardedBreakCallback);
175+ const char * size;
176+ if (lua_type (L, 1 ) == LUA_TSTRING)
177+ {
178+ size = luaL_checkstring (L, 1 );
179+ PokiSdk_SetRewardedBreakCallback (L, 2 );
180+ }
181+ else
182+ {
183+ size = " small" ;
184+ PokiSdk_SetRewardedBreakCallback (L, 1 );
185+ }
186+
187+ PokiSdkJs_RewardedBreak (size, (RewardedBreakCallback)PokiSdk_RewardedBreakCallback);
188+
154189 return 0 ;
155190}
156191
@@ -240,12 +275,20 @@ static const luaL_reg Module_methods[] =
240275 {0 , 0 }
241276};
242277
278+ #define SETCONSTANT (name, value ) \
279+ lua_pushnumber (L, (lua_Number) (value)); \
280+ lua_setfield (L, -2 , #name);\
281+
243282static void LuaInit (lua_State* L)
244283{
245284 int top = lua_gettop (L);
246285
247286 luaL_register (L, MODULE_NAME, Module_methods);
248287
288+ SETCONSTANT (REWARDED_BREAK_ERROR, REWARDED_BREAK_ERROR);
289+ SETCONSTANT (REWARDED_BREAK_SUCCESS, REWARDED_BREAK_SUCCESS);
290+ SETCONSTANT (REWARDED_BREAK_START, REWARDED_BREAK_START);
291+
249292 lua_pop (L, 1 );
250293 assert (top == lua_gettop (L));
251294}
0 commit comments