-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-farming.ts
95 lines (84 loc) · 2.45 KB
/
game-farming.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
import {InGameItem, IframeSDK, Player, deepCopy, signPayload} from "@playnation/game-sdk";
const gameSDK = IframeSDK.instance;
const app = () => ({
player: {} as unknown as Player,
gameplay: {} as any,
tournament: {},
items: [] as InGameItem[],
data: {} as any,
async init() {
gameSDK.init({
clientId: 'fake-client',
});
this.player = await gameSDK.getPlayer();
this.tournament = (await gameSDK.getTournament()) || {};
const itemData = await gameSDK.getInGameItems().catch();
this.items = itemData.items || [];
// For farming game only
const defaultState = {'a': 0, 'b': 0};
const data = deepCopy(this.player.state?.data || defaultState);
console.debug('init with data', data);
setInterval(() => {
data.a += 1;
data.b += 3;
this.data = data;
this.updateState(data).catch(console.error);
}, 1000)
},
async play() {
// Game client should call game API to init new gameplay and get gameplayId;
const gameplayId = 'gameplay1';
const {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});