forked from XYZABCEFG/y8slope
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamebreak.js
More file actions
480 lines (394 loc) · 14 KB
/
gamebreak.js
File metadata and controls
480 lines (394 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
var ads_count = 0;
var idnet_appid;
var afg_channel_id = 7503025976; // default channel
var ad_timing = 180; // seconds
var ads_enabled = false;
var lastAdTime = 0;
var allowGamePause = true;
var requestAndPlayAd = false;
var clientOptionsLoadedEnum = 'loading';
var clientOptionsCheckCount = 0;
function InitExternEval(appid) {
// do not initialise ads for version below Unity5.6
var adContainer = document.getElementById("gameContainer");
if (!adContainer) {
clientOptionsLoadedEnum = 'failed';
return;
}
if (typeof appid === 'undefined' || appid === null) {
console.log('app-id is null');
clientOptionsLoadedEnum = 'failed';
return;
}
idnet_appid = appid;
if (idnet_appid) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
try {
data = JSON.parse(xhttp.responseText);
if (!data.success) {
console.log('failed to get client options for appid - ' + idnet_appid);
return;
}
if(data.options.afg_channel_id)
{
afg_channel_id = data.options.afg_channel_id;
}
ads_enabled = data.options.ads_enabled;
ad_timing = data.options.ad_timing;
clientOptionsLoadedEnum = 'loaded';
// only init afg if ads are enabled.
if (ads_enabled) {
initaliseAfg();
}
} catch (e) {
console.log(e);
clientOptionsLoadedEnum = 'failed';
}
}
};
xhttp.open("GET", 'https://account.y8.com/api/v1/json/client_options/' + idnet_appid, true);
xhttp.send();
} else {
console.log('appid is null');
clientOptionsLoadedEnum = 'failed';
}
}
window.Y8ExternEval = function(_allowGamePause) {
if (typeof allowGamePause !== 'undefined') {
allowGamePause = (_allowGamePause === 'true');
}
if (clientOptionsLoadedEnum != 'loaded')
return;
// console.log('window.Y8ExternEval - allowGamePause :'+ allowGamePause);
if (!canShowAds()) {
return;
}
if (ads_enabled) {
showAFG();
}
}
function initaliseAfg() {
if (clientOptionsLoadedEnum == 'loaded') {
// initialise AFG just for the first time.
initAdDiv();
initAfg();
}
}
function canShowAds() {
var date = new Date();
if (lastAdTime + (ad_timing * 1000) > date.getTime()) {
return false;
}
return true;
}
//returns the referrer domain
function getTopDomain() {
var url;
try {
url = window.top.location.href;
} catch (error) {
url = document.referrer;
}
if (!url) {
url = window.location.href;
}
return url;
}
function showAFG() {
if ((screen.availHeight || screen.height - 30) <= window.innerHeight) {
console.log('blocking ad in fullscreen');
return;
}
// lastAdTime is 0 when ad is not shown yet
if (lastAdTime == 0) {
// requestAndPlayAd = false;
// playAds();
requestAndPlayAd = true;
requestAds();
} else {
requestAndPlayAd = true;
requestAds();
}
};
function loadScript(url, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) {
//IE
script.onreadystatechange = function() {
if (script.readyState == "loaded" ||
script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else {
//Others
script.onload = function() {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
function afgLoaded() {
init();
}
function jqueryLoaded() {}
function initAfg() {
var afg_url = 'ima3.js'/*tpa=http://imasdk.googleapis.com/js/sdkloader/ima3.js*/;
loadScript(afg_url, afgLoaded);
}
function initAdDiv() {
/*
if (!window.jQuery) {
var jqueryUrl = 'jquery.min.js'/*tpa=http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js*/;
loadScript(jqueryUrl, jqueryLoaded);
}
*/
var webglcontent = document.getElementsByClassName("webgl-content")[0];
var adv_mainContainer = document.createElement("div");
adv_mainContainer.id = 'adv_mainContainer';
webglcontent.appendChild(adv_mainContainer);
var adv_content = document.createElement("div");
adv_content.id = 'adv_content';
adv_mainContainer.appendChild(adv_content);
var adv_contentElement = document.createElement("video");
adv_contentElement.id = 'adv_contentElement';
adv_content.appendChild(adv_contentElement);
var adv_adContainer = document.createElement("div");
adv_adContainer.id = 'adv_adContainer';
adv_content.appendChild(adv_adContainer);
// loadjscssfile('gamebreak-1.css'/*tpa=http://storage-cf.y8.com/y8-studio/unity/config/shared/gamebreak/gamebreak/gamebreak.css*/, 'css');
}
function loadjscssfile(filename, filetype) {
if (filetype == "js") {
//if filename is a external JavaScript file
var fileref = document.createElement('script')
fileref.setAttribute("type", "text/javascript")
fileref.setAttribute("src", filename)
alert('called');
} else if (filetype == "css") {
//if filename is an external CSS file
var fileref = document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref != "undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
var domain = getTopDomain();
function setTimeScale(value) {
gameInstance.SendMessage('IDNET(Idnet.cs)', 'SetAudio', value);
if (allowGamePause) {
gameInstance.SendMessage('IDNET(Idnet.cs)', 'SetTimeScale', value);
}
}
var adsManager;
var adsLoader;
var adDisplayContainer;
var intervalTimer;
var videoContent;
function init() {
videoContent = document.getElementById('adv_contentElement');
var element = document.getElementById('gameContainer');
var eltWidth = element.offsetWidth;
var eltHeight = element.offsetHeight;
document.getElementById('adv_mainContainer').style.width = eltWidth + 'px';
document.getElementById('adv_mainContainer').style.height = eltHeight + 'px';
createAdLoader();
}
function createAdLoader() {
// Create the ad display container.
createAdDisplayContainer();
// Create ads loader.
adsLoader = new google.ima.AdsLoader(adDisplayContainer);
// Listen and respond to ads loaded and error events.
adsLoader.addEventListener(
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
onAdsManagerLoaded,
false);
adsLoader.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdError,
false);
// An event listener to tell the SDK that our content video
// is completed so the SDK can play any post-roll ads.
var contentEndedListener = function() {
adsLoader.contentComplete();
};
videoContent.onended = contentEndedListener;
// requestAds();
}
function requestAds() {
if (!canShowAds()) {
return;
}
// Request video ads.
var adsRequest = new google.ima.AdsRequest();
var description_url = encodeURIComponent(domain);
adsRequest.adTagUrl = 'https://googleads.g.doubleclick.net/pagead/ads?' +
'ad_type=video_text_image' +
'&videoad_start_delay=0' +
'&max_ad_duration=30000' +
'&hl=en' +
'&description_url=' + description_url +
'&client=ca-games-pub-6129580795478709' +
'&channel=' + afg_channel_id;
// Specify the linear and nonlinear slot sizes. This helps the SDK to
// select the correct creative if multiple are returned.
var adContainer = document.getElementById("gameContainer");
var adW = adContainer.offsetWidth;
var adH = adContainer.offsetHeight;
adsRequest.linearAdSlotWidth = adW;
adsRequest.linearAdSlotHeight = adH;
adsRequest.nonLinearAdSlotWidth = adW;
adsRequest.nonLinearAdSlotHeight = adH;
adsRequest.forceNonLinearFullSlot = true;
adsLoader.requestAds(adsRequest);
}
function createAdDisplayContainer() {
// We assume the adContainer is the DOM id of the element that will house
// the ads.
adDisplayContainer = new google.ima.AdDisplayContainer(
document.getElementById('adv_adContainer'), videoContent);
}
function playAds() {
if (!canShowAds()) {
return;
}
// Initialize the container. Must be done via a user action on mobile devices.
videoContent.load();
adDisplayContainer.initialize();
try {
// Initialize the ads manager. Ad rules playlist will start at this time.
var adContainer = document.getElementById("gameContainer");
var adW = adContainer.offsetWidth;
var adH = adContainer.offsetHeight;
adsManager.init(adW, adH, google.ima.ViewMode.NORMAL);
// Call play to start showing the ad. Single video and overlay ads will
// start at this time; the call will be ignored for ad rules.
adsManager.start();
} catch (adError) {
// An error may be thrown if there was a problem with the VAST response.
videoContent.play();
}
}
function onAdsManagerLoaded(adsManagerLoadedEvent) {
// Get the ads manager.
var adsRenderingSettings = new google.ima.AdsRenderingSettings();
adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
// videoContent should be set to the content video element.
adsManager = adsManagerLoadedEvent.getAdsManager(
videoContent, adsRenderingSettings);
// Add listeners to the required events.
adsManager.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdError);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,
onContentPauseRequested);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,
onContentResumeRequested);
adsManager.addEventListener(
google.ima.AdEvent.Type.ALL_ADS_COMPLETED,
onAdEvent);
// Listen to any additional events, if necessary.
adsManager.addEventListener(
google.ima.AdEvent.Type.LOADED,
onAdEvent);
adsManager.addEventListener(
google.ima.AdEvent.Type.STARTED,
onAdEvent);
adsManager.addEventListener(
google.ima.AdEvent.Type.COMPLETE,
onAdEvent);
if (requestAndPlayAd) {
requestAndPlayAd = false;
playAds();
}
}
function onAdEvent(adEvent) {
// Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED)
// don't have ad object associated.
var ad = adEvent.getAd();
switch (adEvent.type) {
case google.ima.AdEvent.Type.LOADED:
// This is the first event sent for an ad - it is possible to
// determine whether the ad is a video ad or an overlay.
if (!ad.isLinear()) {
// Position AdDisplayContainer correctly for overlay.
// Use ad.width and ad.height.
videoContent.play();
}
break;
case google.ima.AdEvent.Type.STARTED:
// This event indicates the ad has started - the video player
// can adjust the UI, for example display a pause button and
// remaining time.
if (ad.isLinear()) {
// For a linear ad, a timer can be started to poll for
// the remaining time.
intervalTimer = setInterval(
function() {
var remainingTime = adsManager.getRemainingTime();
},
300); // every 300ms
}
break;
case google.ima.AdEvent.Type.COMPLETE:
// This event indicates the ad has finished - the video player
// can perform appropriate UI actions, such as removing the timer for
// remaining time detection.
if (ad.isLinear()) {
clearInterval(intervalTimer);
}
break;
}
}
function onAdError(adErrorEvent) {
// Handle the error logging.
console.log(adErrorEvent.getError());
if(adsManager)
{
adsManager.destroy();
}
document.getElementById("adv_mainContainer").style.display = "none";
setTimeScale('1');
}
function onContentPauseRequested() {
videoContent.pause();
// This function is where you should setup UI for showing ads (e.g.
// display ad timer countdown, disable seeking etc.)
// setupUIForAds();
var date = new Date();
lastAdTime = date.getTime();
setTimeScale('0');
document.getElementById("adv_mainContainer").style.display = "block";
document.getElementById("adv_adContainer").style.display = "block";
updateAdPosition();
}
function onContentResumeRequested() {
videoContent.play();
// This function is where you should ensure that your UI is ready
// to play content. It is the responsibility of the Publisher to
// implement this function when necessary.
// setupUIForContent();
document.getElementById("adv_mainContainer").style.display = "none";
setTimeScale('1');
// requestAds();
}
function updateAdPosition() {
var element = document.getElementById('gameContainer');
var overlay = document.getElementById('adv_mainContainer');
var eltWidth = element.offsetWidth;
var eltHeight = element.offsetHeight;
overlay.style.width = eltWidth + 'px';
overlay.style.height = eltHeight + 'px';
overlay.style.top = 0 + 'px';
overlay.style.left = 0 + 'px';
}