-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.ts
78 lines (70 loc) · 1.94 KB
/
game.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
import {InGameItem, IframeSDK, Player, signPayload} from "@playnation/game-sdk";
const gameSDK = IframeSDK.instance;
const app = () => ({
player: {} as unknown as Player,
gameplay: {} as any,
items: [] as InGameItem[],
data: {} as any,
async init() {
await gameSDK.init({
clientId: 'fake-client',
});
this.player = await gameSDK.getPlayer();
const itemData = await gameSDK.getInGameItems().catch();
this.items = itemData.items || [];
},
async play() {
const {gamePlayId, token, energy} = await gameSDK.play();
this.player.energy = energy;
this.gameplay = {
id: gamePlayId,
score: 0,
token,
};
},
newScore(score: number) {
gameSDK.triggerHapticFeedback('impactLight');
this.gameplay.score = score;
gameSDK.trackScore(this.gameplay.id, score);
},
async gameover() {
const signature = await gameSDK.signResult('gameplay-1', this.gameplay.token, this.gameplay.score);
const player = await gameSDK.getPlayer();
Object.assign(this.player, player);
// After sign result, game client should send signature & game token to game server.
// Game server should call S2S API to send score to Wallacy.
console.log('game client should send this data to game server', {
signature,
token: this.gameplay.token,
score: this.gameplay.score,
});
this.gameplay = {};
},
async updateState(data: any) {
const signature = await signPayload(data, 'secret-key');
return await gameSDK.updateState({
gamePlayId: 'gameplay-1',
state: {
signature: signature.toString(),
timestamp: new Date().toISOString(),
data,
},
});
},
getSDKVersion() {
return gameSDK.getVersion();
},
exit() {
return gameSDK.exit(true);
},
reload() {
location.reload();
},
openShop() {
gameSDK.showShop();
},
openLeaderboard() {
gameSDK.showLeaderboard();
},
});
Object.assign(window, {app});