@cardsjd/portal 0.1.3 → 0.1.4

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.
package/lib/BasePortal.js CHANGED
@@ -1,11 +1,10 @@
1
- import { Advertisement } from './Advertisement.js';
2
1
  export class BasePortal {
3
2
 
4
3
  async init() { }
5
4
 
6
5
  async load() { }
7
6
 
8
- async save(data, forceSave) { }
7
+ async save(_data, _forceSave) { }
9
8
 
10
9
  async delete() { }
11
10
 
@@ -21,26 +20,17 @@ export class BasePortal {
21
20
 
22
21
  async getInvite() { }
23
22
 
24
- async inviteSend(user, room) { }
23
+ async inviteSend(_user, _room) { }
25
24
 
26
- async findAndInvite(userFrom, room) { }
25
+ async findAndInvite(_userFrom, _room) { }
27
26
 
28
27
  async getFriends() { }
29
28
 
30
- tryShowInterstitialAd(options) {
31
- const readyToShowAd = Advertisement.isInterstitialAdReady(options);
32
- if (readyToShowAd) {
33
- this.showInterstitialAd();
34
- } else {
35
- this.loadInterstitialAd();
36
- }
37
- }
38
-
39
29
  setupAds() { }
40
30
 
41
- loadInterstitialAd(_force) { }
31
+ async loadInterstitialAd(_force) { }
42
32
 
43
- showInterstitialAd() { }
33
+ async showInterstitialAd() { }
44
34
 
45
35
  loadRewardedAd(_onLoaded) { }
46
36
 
@@ -48,7 +38,7 @@ export class BasePortal {
48
38
 
49
39
  loadAppOpenAd() { }
50
40
 
51
- showAppOpenAd(maxDelay) { }
41
+ async showAppOpenAd(_maxDelay) { }
52
42
 
53
43
  isAdLoaded() { }
54
44
 
@@ -59,4 +49,79 @@ export class BasePortal {
59
49
  isErrorState() { }
60
50
 
61
51
  createShortCut() { }
52
+
53
+ async tryShowInterstitialAd(options) {
54
+ //only load/show if ads enabled
55
+ if (this.isSetup === false) { return; }
56
+
57
+ const config = this.config;
58
+ const offsetStartdelay = options?.offsetStartdelay || config.offsetStartdelay || 120; //start with 1 min passed to get the first ad to show faster
59
+ const delayBetweenAds = options?.delayBetweenAds || config.delayBetweenAds || 240; //check for 240 (4 min) seconds passed was 180
60
+ 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
61
+
62
+ //1. only allow ad to be shown/loaded once every 150 second (Advertisement.delayBetweenAds)
63
+ //2. check if over 20 min, may have not been in the app and reopened it with out restarting
64
+ //3. load ad ether way
65
+ //4. show ad if conditions met
66
+
67
+ let readyToShowAd = false;
68
+
69
+ //reset timer
70
+ if (this.lastShownInterstitial === null) {
71
+ this.lastShownInterstitial = Date.now();//now
72
+ //add start offset delay
73
+ const newDate = new Date();
74
+ newDate.setSeconds(newDate.getSeconds() - offsetStartdelay);
75
+ this.lastShownInterstitial = newDate;
76
+ }
77
+
78
+ const secondsSinceLastAd = (Date.now() - this.lastShownInterstitial) / 1000;
79
+
80
+ //check for delayBetweenAds seconds passed
81
+ if (secondsSinceLastAd > delayBetweenAds) {
82
+ readyToShowAd = true;
83
+ }
84
+
85
+ //check if over 20 min, may have not been in the app and reopened it with out restarting
86
+ if (secondsSinceLastAd > awayResetTime) {
87
+ readyToShowAd = false;
88
+ this.lastShownInterstitial = Date.now();//now, reset
89
+ }
90
+
91
+ console.log('readyToShowInterstitialAd: ' + readyToShowAd + ' : ' + secondsSinceLastAd + ' of ' + delayBetweenAds);
92
+
93
+ // show ad or just load ad (not load and show)
94
+ if (readyToShowAd) {
95
+ await this.showInterstitialAd();
96
+ } else {
97
+ await this.loadInterstitialAd();
98
+ }
99
+ }
100
+
101
+ async showAppOpenAdWithMaxDelay(maxDelay) {
102
+ if (this.isSetUp === false) { return; }
103
+ if (this.adProvider == null) { return; }
104
+
105
+ if (!maxDelay) {
106
+ maxDelay = 4000;
107
+ }
108
+
109
+ //get start time
110
+ const startTime = Date.now();
111
+ if (this.loadAppOpenAd) {
112
+ await this.loadAppOpenAd();
113
+ }
114
+
115
+ //this wont work...
116
+ //is timer over maxDelay
117
+ const timePassed = Date.now() - startTime;
118
+ if (timePassed > maxDelay) {
119
+ console.log(`showAppOpenAdWithMaxDelay took too long to load [${timePassed}ms/${maxDelay}ms], skipping ad`);
120
+ return;
121
+ }
122
+
123
+ if (this.showAppOpenAd) {
124
+ await this.showAppOpenAd();
125
+ }
126
+ }
62
127
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cardsjd/portal",
3
3
  "private": false,
4
- "version": "0.1.3",
4
+ "version": "0.1.4",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "publishConfig": {
@@ -1,99 +0,0 @@
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
- }