-
Notifications
You must be signed in to change notification settings - Fork 716
/
facebookinstantgamestools.ts
391 lines (368 loc) · 13.2 KB
/
facebookinstantgamestools.ts
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
namespace gdjs {
const logger = new gdjs.Logger('Facebook instant games');
export namespace evtTools {
export namespace facebookInstantGames {
export let _preloadedInterstitial: any = null;
export let _preloadedInterstitialLoading = false;
export let _preloadedInterstitialLoaded = false;
export let _preloadedRewardedVideo: any = null;
export let _preloadedRewardedVideoLoading = false;
export let _preloadedRewardedVideoLoaded = false;
export const areAdsSupported = function () {
if (typeof FBInstant === 'undefined') {
return false;
}
const supportedAPIs = FBInstant.getSupportedAPIs();
return (
supportedAPIs.indexOf('getInterstitialAdAsync') !== -1 &&
supportedAPIs.indexOf('getRewardedVideoAsync') !== -1
);
};
export const getPlayerId = function () {
if (typeof FBInstant === 'undefined') {
return '';
}
return FBInstant.player.getID() || '';
};
export const getPlayerName = function () {
if (typeof FBInstant === 'undefined') {
return '';
}
return FBInstant.player.getName() || '';
};
export const loadPlayerData = function (
key,
successVariable,
errorVariable
) {
if (typeof FBInstant === 'undefined') {
return;
}
errorVariable.setString('');
successVariable.setString('');
FBInstant.player
.getDataAsync([key])
.then(function (data) {
successVariable.fromJSON(data[key]);
})
.catch(function (error) {
errorVariable.setString(error.message || 'Unknown error');
});
};
export const setPlayerData = function (
key,
variable,
successVariable,
errorVariable
) {
if (typeof FBInstant === 'undefined') {
return;
}
errorVariable.setString('');
successVariable.setString('');
const data = {};
data[key] = JSON.stringify(variable.toJSObject());
FBInstant.player
.setDataAsync(data)
.then(function () {
successVariable.setString('Player data saved');
})
.catch(function (error) {
errorVariable.setString(error.message || 'Unknown error');
});
};
export const setPlayerScore = function (
leaderboardName,
score,
extraDataVariable,
successVariable,
errorVariable
) {
if (typeof FBInstant === 'undefined') {
return;
}
errorVariable.setString('');
successVariable.setString('');
const data = JSON.stringify(extraDataVariable.toJSObject());
FBInstant.getLeaderboardAsync(leaderboardName)
.then(function (leaderboard) {
return leaderboard.setScoreAsync(score, data);
})
.then(function () {
successVariable.setString('Player score saved');
})
.catch(function (error) {
errorVariable.setString(error.message || 'Unknown error');
});
};
export const getPlayerEntry = function (
leaderboardName,
rankVariable,
scoreVariable,
extraDataVariable,
errorVariable
) {
if (typeof FBInstant === 'undefined') {
return;
}
errorVariable.setString('');
extraDataVariable.setString('');
FBInstant.getLeaderboardAsync(leaderboardName)
.then(function (leaderboard) {
return leaderboard.getPlayerEntryAsync();
})
.then(function (entry) {
rankVariable.setNumber(
entry.getRank() === null ? -1 : entry.getRank()
);
scoreVariable.setNumber(
entry.getScore() === null ? -1 : entry.getScore()
);
extraDataVariable.fromJSON(entry.getExtraData());
})
.catch(function (error) {
errorVariable.setString(error.message || 'Unknown error');
});
};
export const loadInterstitialAd = function (
adPlacementId,
errorVariable
) {
if (typeof FBInstant === 'undefined') {
return;
}
if (
gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoading ||
gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoaded
) {
return;
}
gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoading = true;
FBInstant.getInterstitialAdAsync(adPlacementId)
.then(function (interstitial) {
gdjs.evtTools.facebookInstantGames._preloadedInterstitial = interstitial;
return interstitial.loadAsync();
})
.then(function () {
gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoading = false;
gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoaded = true;
logger.info('Facebook Instant Games interstitial preloaded.');
})
.catch(function (err) {
gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoading = false;
gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoaded = false;
logger.error('Interstitial failed to preload: ' + err.message);
errorVariable.setString(err.message || 'Unknown error');
});
};
export const showInterstitialAd = function (errorVariable) {
if (typeof FBInstant === 'undefined') {
return;
}
if (!gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoaded) {
return;
}
gdjs.evtTools.facebookInstantGames._preloadedInterstitial
.showAsync()
.then(function () {
logger.info('Facebook Instant Games interstitial shown.');
})
.catch(function (err) {
logger.error('Interstitial failed to show: ' + err.message);
errorVariable.setString(err.message || 'Unknown error');
})
.then(function () {
gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoaded = false;
});
};
export const isInterstitialAdReady = function () {
return gdjs.evtTools.facebookInstantGames._preloadedInterstitialLoaded;
};
export const loadRewardedVideo = function (adPlacementId, errorVariable) {
if (typeof FBInstant === 'undefined') {
return;
}
if (
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoading ||
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoaded
) {
return;
}
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoading = true;
FBInstant.getRewardedVideoAsync(adPlacementId)
.then(function (rewardedVideo) {
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideo = rewardedVideo;
return rewardedVideo.loadAsync();
})
.then(function () {
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoading = false;
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoaded = true;
logger.info('Facebook Instant Games rewarded video preloaded.');
})
.catch(function (err) {
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoading = false;
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoaded = false;
logger.error('Rewarded video failed to preload: ' + err.message);
errorVariable.setString(err.message || 'Unknown error');
});
};
export const showRewardedVideo = function (errorVariable) {
if (typeof FBInstant === 'undefined') {
return;
}
if (!gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoaded) {
return;
}
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideo
.showAsync()
.then(function () {
logger.info('Facebook Instant Games rewarded video shown.');
})
.catch(function (err) {
logger.error('Rewarded video failed to show: ' + err.message);
errorVariable.setString(err.message || 'Unknown error');
})
.then(function () {
gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoaded = false;
});
};
export const isRewardedVideoReady = function () {
return gdjs.evtTools.facebookInstantGames._preloadedRewardedVideoLoaded;
};
if (typeof FBInstant === 'undefined' && typeof window !== 'undefined') {
logger.log('Creating a mocked version of Facebook Instant Games.');
/**
* A mocked Leaderboard, part of the mock of FBInstant.
* @ignore
*/
class MockedLeaderboard {
_playerScore: any = null;
_playerRank: any = null;
_playerExtraData: any = null;
setScoreAsync(score, extraData): Promise<void> {
const that = this;
return new Promise(function (resolve) {
that._playerScore = score;
that._playerRank = 1;
that._playerExtraData = extraData;
resolve();
});
}
getPlayerEntryAsync() {
const that = this;
return new Promise(function (resolve) {
resolve({
getScore: function () {
return that._playerScore;
},
getRank: function () {
return that._playerRank;
},
getExtraData: function () {
return that._playerExtraData;
},
});
});
}
}
/**
* A mocked RewardedVideo, part of the mock of FBInstant.
* @ignore
*/
class MockedRewardedVideo {
_isLoaded: boolean = false;
loadAsync() {
this._isLoaded = true;
return Promise.resolve();
}
showAsync(): Promise<void> {
if (this._isLoaded) {
logger.info(
'In a real Instant Game, a video reward should have been shown to the user.'
);
return Promise.resolve();
}
return Promise.reject(new Error('Rewarded video is not loaded.'));
}
}
/**
* A mocked MockedInterstitial, part of the mock of FBInstant.
* @ignore
*/
class MockedInterstitial {
_isLoaded: boolean = false;
loadAsync() {
this._isLoaded = true;
return Promise.resolve();
}
showAsync(): Promise<void> {
if (this._isLoaded) {
logger.info(
'In a real Instant Game, an interstitial should have been shown to the user.'
);
return Promise.resolve();
}
return Promise.reject(new Error('Interstitial is not loaded.'));
}
}
const supportedAPIs: Array<string> = [];
const FBInstantMock = {
_mockedPlayerData: {},
_mockedLeaderboards: {},
player: {
getName: function () {
return 'Fake player name';
},
getID: function () {
return '12345678';
},
getDataAsync: function (key) {
return new Promise(function (resolve) {
resolve(FBInstantMock._mockedPlayerData);
});
},
setDataAsync: function (data) {
return new Promise<void>(function (resolve) {
FBInstantMock._mockedPlayerData = data;
resolve();
});
},
},
getLeaderboardAsync: function (leaderboardName) {
return new Promise(function (resolve) {
FBInstantMock._mockedLeaderboards[leaderboardName] =
FBInstantMock._mockedLeaderboards[leaderboardName] ||
new MockedLeaderboard();
resolve(FBInstantMock._mockedLeaderboards[leaderboardName]);
});
},
getInterstitialAdAsync: function () {
return Promise.resolve(new MockedInterstitial());
},
getRewardedVideoAsync: function () {
return Promise.resolve(new MockedRewardedVideo());
},
getSupportedAPIs: function () {
return supportedAPIs;
},
};
// Retrieve the name of the supported APIs in our mock.
for (const property in FBInstantMock) {
if (typeof FBInstantMock[property] == 'object') {
for (const subProperty in FBInstantMock[property]) {
if (typeof FBInstantMock[property][subProperty] == 'function') {
supportedAPIs.push(property + '.' + subProperty);
}
}
} else {
if (typeof FBInstantMock[property] == 'function') {
supportedAPIs.push(property);
}
}
}
// @ts-expect-error
window.FBInstant = FBInstantMock;
}
}
}
}