@cardsjd/portal 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -140,7 +140,7 @@ export class AdMobPortal extends BasePortal {
140
140
  return this._isRewardedAdLoaded;
141
141
  }
142
142
 
143
- async loadAd() {
143
+ async loadInterstitialAd() {
144
144
  if (this.isSetup === false) {
145
145
  return;
146
146
  }
@@ -154,9 +154,9 @@ export class AdMobPortal extends BasePortal {
154
154
  });
155
155
  }
156
156
 
157
- async showAd() {
157
+ async showInterstitialAd() {
158
158
  if (!this.isAdLoaded()) {
159
- await this.loadAd();
159
+ await this.loadInterstitialAd();
160
160
  return; // no auto show
161
161
  }
162
162
 
@@ -0,0 +1,99 @@
1
+ export class Advertisement {
2
+ isInterstitialAdReady(options) {
3
+ //only load/show if ads enabled
4
+ if (this.isSetup === false) { return; }
5
+
6
+ const config = this.config;
7
+ const offsetStartdelay = options?.offsetStartdelay || config.offsetStartdelay || 120; //start with 1 min passed to get the first ad to show faster
8
+ const delayBetweenAds = options?.delayBetweenAds || config.delayBetweenAds || 240; //check for 240 (4 min) seconds passed was 180
9
+ const awayResetTime = options?.awayResetTime || config.awayResetTime || 1200; //check if over 20 min, may have not been in the app and reopened it with out restarting
10
+ const delayBetweenRewardedAds = options?.delayBetweenRewardedAds || config.delayBetweenRewardedAds || 300; //check for 600 seconds passed
11
+
12
+
13
+ //0. if ads removed, dont even load the ad
14
+ //1. only allow ad to be shown/loaded once every 150 second (Advertisement.delayBetweenAds)
15
+ //2. check if over 20 min, may have not been in the app and reopened it with out restarting
16
+ //3. load ad ether way
17
+ //4. show ad if conditions met
18
+ if (this.removeAds) { return false; }
19
+
20
+ let readyToShowAd = false;
21
+
22
+ //reset timer
23
+ if (this.lastShownInterstitial === null) {
24
+ this.lastShownInterstitial = Date.now();//now
25
+ //add start offset delay
26
+ const newDate = new Date();
27
+ newDate.setSeconds(newDate.getSeconds() - offsetStartdelay);
28
+ this.lastShownInterstitial = newDate;
29
+ }
30
+
31
+ const secondsSinceLastAd = (Date.now() - this.lastShownInterstitial) / 1000;
32
+
33
+ //check for delayBetweenAds seconds passed
34
+ if (secondsSinceLastAd > delayBetweenAds) {
35
+ readyToShowAd = true;
36
+ }
37
+
38
+ //check if over 20 min, may have not been in the app and reopened it with out restarting
39
+ if (secondsSinceLastAd > awayResetTime) {
40
+ readyToShowAd = false;
41
+ this.lastShownInterstitial = Date.now();//now, reset
42
+ }
43
+
44
+ console.log('readyToShowInterstitialAd: ' + readyToShowAd + ' : ' + secondsSinceLastAd + ' of ' + delayBetweenAds);
45
+
46
+ // show ad or just load ad
47
+ if (readyToShowAd) {
48
+ true;
49
+ } else {
50
+ false;
51
+ }
52
+ }
53
+ async showAppOpenAdWithMaxDelay(maxDelay) {
54
+ if (this.isSetUp === false) { return; }
55
+ if (this.adProvider == null) { return; }
56
+
57
+
58
+
59
+ if (!maxDelay) {
60
+ maxDelay = 4000;
61
+ }
62
+
63
+ //todo: fix if long loader time than maxDelay, then skip ad
64
+ //something like this:
65
+ //return new Promise(async (resolve, reject) => {
66
+ // if (this.adProvider && this.adProvider.showAppOpenAd) {
67
+ // timeoutPromise = setTimeout(() => {
68
+ // //stop ad from showing
69
+ // if (this.adProvider) this.adProvider.appOpenAdAutoShow = false;
70
+ // reject(new Error('showAppOpenAd has not fired after 4 seconds. Skipping ad'));
71
+ // }, maxDelay);
72
+ // await this.adProvider.showAppOpenAd();
73
+ // clearTimeout(timeoutPromise);
74
+ // resolve();
75
+ // }
76
+ // else {
77
+ // reject();
78
+ // }
79
+
80
+
81
+ //get start time
82
+ const startTime = Date.now();
83
+ if (this.adProvider.loadAppOpenAd) {
84
+ await this.adProvider.loadAppOpenAd();
85
+ }
86
+
87
+ //this wont work...
88
+ //is timer over maxDelay
89
+ const timePassed = Date.now() - startTime;
90
+ if (timePassed > maxDelay) {
91
+ console.log(`showAppOpenAdWithMaxDelay took too long to load [${timePassed}ms/${maxDelay}ms], skipping ad`);
92
+ return;
93
+ }
94
+
95
+ if (this.adProvider.showAppOpenAd) {
96
+ await this.adProvider.showAppOpenAd();
97
+ }
98
+ }
99
+ }
package/lib/BasePortal.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { Advertisement } from './Advertisement.js';
1
2
  export class BasePortal {
2
3
 
3
4
  async init() { }
@@ -5,7 +6,7 @@ export class BasePortal {
5
6
  async load() { }
6
7
 
7
8
  async save(data, forceSave) { }
8
-
9
+
9
10
  async delete() { }
10
11
 
11
12
  gameStart() { }
@@ -26,20 +27,29 @@ export class BasePortal {
26
27
 
27
28
  async getFriends() { }
28
29
 
29
- tryShowInterstitialAd() { }
30
-
30
+ tryShowInterstitialAd(options) {
31
+ const readyToShowAd = Advertisement.isInterstitialAdReady(options);
32
+ if (readyToShowAd) {
33
+ this.showInterstitialAd();
34
+ } else {
35
+ this.loadInterstitialAd();
36
+ }
37
+ }
38
+
31
39
  setupAds() { }
32
40
 
33
- loadAd(_force) { }
41
+ loadInterstitialAd(_force) { }
34
42
 
35
- showAd() { }
36
-
37
- loadAndShowAd() { }
43
+ showInterstitialAd() { }
38
44
 
39
45
  loadRewardedAd(_onLoaded) { }
40
46
 
41
47
  showRewardedAd() { }
42
48
 
49
+ loadAppOpenAd() { }
50
+
51
+ showAppOpenAd(maxDelay) { }
52
+
43
53
  isAdLoaded() { }
44
54
 
45
55
  isRewardedAdLoaded() { }
@@ -245,7 +245,7 @@ export class CrazyGamesPortal extends BasePortal {
245
245
  return this.isCrazyGames();
246
246
  }
247
247
 
248
- async loadAd() {
248
+ async loadInterstitialAd() {
249
249
  console.log('CrazyGames.loadAd');
250
250
 
251
251
  if (!this.isCrazyGames()) {
@@ -257,7 +257,7 @@ export class CrazyGamesPortal extends BasePortal {
257
257
  }
258
258
  }
259
259
 
260
- async showAd() {
260
+ async showInterstitialAd() {
261
261
  console.log('CrazyGames.showAd');
262
262
 
263
263
  if (!this.isCrazyGames()) {
@@ -159,25 +159,25 @@ export class FacebookPortal extends BasePortal {
159
159
  this._errorCount = (this._errorCount || 0) + 1;
160
160
  }
161
161
 
162
- loadAd(force) {
163
- console.log('FB_Ads.loadAd');
162
+ loadInterstitialAd(force) {
163
+ console.log('FB_Ads.loadInterstitialAd');
164
164
 
165
165
  if (typeof FBInstant === 'undefined' || !globalThis.FBInstant) {
166
166
  console.log('FBInstant plugin not ready'); return false;
167
167
  }
168
168
 
169
169
  if (!force && this._isAdLoading) {
170
- console.log('FBInstant.loadAd() - _isAdLoading = true');
170
+ console.log('FBInstant.loadInterstitialAd() - _isAdLoading = true');
171
171
  return true;
172
172
  }
173
173
 
174
174
  if (!force && this._isAdLoaded) {
175
- console.log('FBInstant.loadAd() - _isAdLoaded = true');
175
+ console.log('FBInstant.loadInterstitialAd() - _isAdLoaded = true');
176
176
  return true;
177
177
  }
178
178
 
179
179
  if (!force && this.isErrorState()) {
180
- console.log('FBInstant.loadAd() - ErrorState = true');
180
+ console.log('FBInstant.loadInterstitialAd() - ErrorState = true');
181
181
  return true;
182
182
  }
183
183
 
@@ -228,27 +228,27 @@ export class FacebookPortal extends BasePortal {
228
228
  }
229
229
  }
230
230
 
231
- showAd() {
232
- console.log('FB_Ads.showAd');
231
+ showInterstitialAd() {
232
+ console.log('FB_Ads.showInterstitialAd');
233
233
 
234
234
  if (typeof FBInstant === 'undefined' || !globalThis.FBInstant) {
235
235
  console.log('FBInstant plugin not ready'); return false;
236
236
  }
237
237
 
238
238
  if (this.isErrorState()) {
239
- console.log('FBInstant.showAd() - ErrorState = true');
239
+ console.log('FBInstant.showInterstitialAd() - ErrorState = true');
240
240
  return true;
241
241
  }
242
242
 
243
243
  const isAdLoaded = this._isAdLoaded;
244
244
 
245
245
  if (!isAdLoaded) {
246
- this.loadAd();
246
+ this.loadInterstitialAd();
247
247
  }
248
248
 
249
249
  if (isAdLoaded) {
250
250
  if (!this.preloadedInterstitial) {
251
- console.log('FBInstant.showAd() - error: preloadedInterstitial = null');
251
+ console.log('FBInstant.showInterstitialAd() - error: preloadedInterstitial = null');
252
252
  return false;
253
253
  }
254
254
 
@@ -273,11 +273,11 @@ export class FacebookPortal extends BasePortal {
273
273
 
274
274
  }
275
275
 
276
- loadAndShowAd() {
276
+ loadAndShowInterstitialAd() {
277
277
  console.log('FB_Ads.showAd');
278
278
 
279
279
  if (this.isErrorState()) {
280
- console.log('FBInstant.showAd() - isErrorState = true');
280
+ console.log('FBInstant.loadAndShowInterstitialAd() - isErrorState = true');
281
281
  return true;
282
282
  }
283
283
 
@@ -51,7 +51,7 @@ export class GameDistribution extends BasePortal {
51
51
  return true;
52
52
  }
53
53
 
54
- async loadAd() {
54
+ async loadInterstitialAd() {
55
55
  if (!this.isReady()) {
56
56
  return false;
57
57
  }
@@ -64,7 +64,7 @@ export class GameDistribution extends BasePortal {
64
64
  }
65
65
  }
66
66
 
67
- async showAd() {
67
+ async showInterstitialAd() {
68
68
  if (!this.isReady()) {
69
69
  return false;
70
70
  }
@@ -80,7 +80,7 @@ export class GameDistribution extends BasePortal {
80
80
  return this.isRewardedAdReady;
81
81
  }
82
82
 
83
- loadRewardedAd() {
83
+ async loadRewardedAd() {
84
84
  if (!this.isReady()) {
85
85
  return false;
86
86
  }
package/lib/PokiPortal.js CHANGED
@@ -182,11 +182,11 @@ export class PokiPortal extends BasePortal {
182
182
  return true;
183
183
  }
184
184
 
185
- async loadAd() {
185
+ async loadInterstitialAd() {
186
186
  // Poki commercial breaks do not require manual preloading
187
187
  }
188
188
 
189
- async showAd() {
189
+ async showInterstitialAd() {
190
190
  if (!this.isReady()) {
191
191
  return false;
192
192
  }
package/lib/WebPortal.js CHANGED
@@ -115,11 +115,11 @@ export class WebPortal extends BasePortal {
115
115
  });
116
116
  }
117
117
 
118
- loadAd() {
118
+ loadInterstitialAd() {
119
119
  console.log('GoogleH5.loadAd');
120
120
  }
121
121
 
122
- showAd() {
122
+ showInterstitialAd() {
123
123
  console.log('GoogleH5.showAd');
124
124
 
125
125
  if (typeof adBreak === 'undefined' || !globalThis.adBreak) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cardsjd/portal",
3
3
  "private": false,
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "publishConfig": {