-
Notifications
You must be signed in to change notification settings - Fork 515
/
Copy pathgamebadges.ts
350 lines (287 loc) · 9.79 KB
/
gamebadges.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
// Copyright (c) 2011-2012 Turbulenz Limited
/*global TurbulenzEngine: false*/
/*global Badge: false*/
//
// GameBadges: Class to manage game badges, and their html elements
//
class GameBadges
{
badgesManager: BadgeManager;
onInitialization: { (): void; };
unachievedBadges: { [name: string]: Badge; };
achievedBadges: { [name: string]: Badge; };
// Stores badges that will be awarded irrespective of current progress
awardedBadges: { [name: string]: boolean; };
hasChanged: boolean;
// Used to know when to write html output
hasChangedData: boolean;
// True only if we have a leaderboard manager, and we are initialised
isActive: boolean;
// Vars to manage situation where connection to badges server is lost
hasConnection: boolean;
updateInterval: number;
maxInterval: number;
lastUpdateTime: number;
// Adds the badges structure into the element div
init()
{
var that = this;
function localInitialiseBadges(badges)
{
that.initialiseBadges(badges);
}
function localErrorCallback(msg, status)
{
that.initialiseBadgesErrorCallback(msg, status);
}
// Store and categorise badges
this.badgesManager.listBadges(localInitialiseBadges, localErrorCallback);
}
// Error function when we fail to list & initialise badges
initialiseBadgesErrorCallback(msg, status)
{
this.isActive = false;
this.onInitialization();
}
// Add all badges to unachieved badge dictionary
initialiseBadges(badges)
{
var that = this;
var unachievedBadges = this.unachievedBadges;
function localInitialiseUserBadges(badges)
{
that.initialiseUserBadges(badges);
}
function localErrorCallback(msg, status)
{
that.initialiseBadgesErrorCallback(msg, status);
}
var i;
var name;
var badge;
var length = badges.length;
for (i = 0; i < length; i += 1)
{
badge = badges[i];
name = badge.key;
// Create an unachieved badge
unachievedBadges[name] = Badge.create(badge.total, badge.predescription, badge.description, badge.title);
}
// Add user badges to badges dictionary
this.badgesManager.listUserBadges(localInitialiseUserBadges, localErrorCallback);
}
// Add user badges to unachieved and achieved badge dictionaries
initialiseUserBadges(badges)
{
var achievedBadges = this.achievedBadges;
var unachievedBadges = this.unachievedBadges;
var i;
var name;
var badge;
var unachievedBadge;
var length = badges.length;
for (i = 0; i < length; i += 1)
{
badge = badges[i];
name = badge.badge_key;
unachievedBadge = unachievedBadges[name];
if (unachievedBadge !== undefined)
{
// If already achieved then store as such
if (!badge.current ||
badge.current >= unachievedBadge.totalRequired)
{
achievedBadges[name] = unachievedBadge;
delete unachievedBadges[name];
}
else
{
// Create an unachieved badge
unachievedBadge.currentProgress = badge.current;
}
}
}
this.isActive = true;
this.onInitialization();
}
// Adds a user badge (with how much they have achieved towards it)
addBadge(badgeName)
{
var unachievedBadge;
// If badge has not been achieved then update
if (this.isActive &&
!this.achievedBadges[badgeName])
{
unachievedBadge = this.unachievedBadges[badgeName];
// If badge is non-progress then award it
if (unachievedBadge.isNonProgress)
{
this.awardBadge(badgeName);
}
// Else update current badge progress
else
{
unachievedBadge.addProgress();
this.hasChanged = true;
}
}
}
// Awards a user badge no matter what the current progress
awardBadge(badgeName)
{
if (this.isActive &&
!this.achievedBadges[badgeName])
{
this.awardedBadges[badgeName] = true;
this.hasChanged = true;
}
}
// Updates the state of all badges
refresh()
{
var that = this;
var unachievedBadges = this.unachievedBadges;
var awardedBadges = this.awardedBadges;
var badgesManager = this.badgesManager;
var badge;
var unachievedBadge;
function localUpdateUserBadgeProgressCallback(badge)
{
that.updateUserBadgeProgressCallback(badge);
}
function localAwardUserBadgeCallback(badge)
{
that.awardUserBadgeCallback(badge);
}
function localUpdateUserBadgeErrorCallback(msg, status, badge)
{
that.updateUserBadgeErrorCallback(msg, status, badge);
}
function localAwardUserBadgeErrorCallback(msg, status, badge)
{
that.awardUserBadgeErrorCallback(msg, status, badge);
}
// Set awarded badges first
for (badge in awardedBadges)
{
if (awardedBadges.hasOwnProperty(badge) &&
!unachievedBadges[badge].isUpdating)
{
unachievedBadges[badge].award();
badgesManager.awardUserBadge(badge, localAwardUserBadgeCallback, localAwardUserBadgeErrorCallback);
}
}
// Update badges which have been incremented
for (badge in unachievedBadges)
{
if (unachievedBadges.hasOwnProperty(badge))
{
unachievedBadge = unachievedBadges[badge];
if (!unachievedBadge.isUpdating &&
unachievedBadge.hasProgressed)
{
unachievedBadge.onBeforeSet();
badgesManager.updateUserBadgeProgress(badge, unachievedBadge.currentProgress,
localUpdateUserBadgeProgressCallback, localUpdateUserBadgeErrorCallback);
}
}
}
}
// Callback when user badge has been added
updateUserBadgeProgressCallback(badge)
{
var unachievedBadges = this.unachievedBadges;
var badgeName = badge.badge_key;
var unachievedBadge = unachievedBadges[badgeName];
this.hasConnection = true;
unachievedBadge.onSuccessfulSet(badge.current);
// Used for writing html output on local
this.hasChangedData = true;
// If badge is now achieved
if (unachievedBadge.isAchieved())
{
this.achievedBadges[badgeName] = unachievedBadges[badgeName];
delete unachievedBadges[badgeName];
}
}
// Error callback when user badge failed to add
updateUserBadgeErrorCallback(msg, status, badgeData)
{
var badgeName = badgeData[0];
var unachievedBadge = this.unachievedBadges[badgeName];
this.hasConnection = false;
this.hasChanged = true;
unachievedBadge.onUnsuccessfulSet();
}
// Callback when user badge has been awarded
awardUserBadgeCallback(badge)
{
var badgeName = badge.badge_key;
this.achievedBadges[badgeName] = this.unachievedBadges[badgeName];
delete this.unachievedBadges[badgeName];
delete this.awardedBadges[badgeName];
this.hasConnection = true;
this.hasChangedData = true;
}
// Callback when user badge has been awarded
awardUserBadgeErrorCallback(msg, status, badgeData)
{
var badgeName = badgeData[0];
this.hasConnection = false;
this.hasChanged = true;
this.unachievedBadges[badgeName].onUnsuccessfulSet();
}
// Update function called in main loop
update(currentTime)
{
var updateInterval = this.updateInterval;
if (this.isActive &&
this.hasChanged)
{
if (!this.hasConnection)
{
if (currentTime - this.lastUpdateTime > updateInterval)
{
this.updateInterval = Math.min((updateInterval * 2), this.maxInterval);
this.lastUpdateTime = currentTime;
this.refresh();
}
}
else
{
this.lastUpdateTime = currentTime;
this.hasChanged = false;
this.refresh();
}
}
}
static create(badgesManager, onInitialization): GameBadges
{
var gameBadges = new GameBadges();
gameBadges.badgesManager = badgesManager;
gameBadges.onInitialization = onInitialization;
gameBadges.unachievedBadges = {};
gameBadges.achievedBadges = {};
// Stores badges that will be awarded irrespective of current progress
gameBadges.awardedBadges = {};
gameBadges.hasChanged = true;
// Used to know when to write html output
gameBadges.hasChangedData = true;
// True only if we have a leaderboard manager, and we are initialised
gameBadges.isActive = false;
// Vars to manage situation where connection to badges server is lost
gameBadges.hasConnection = true;
gameBadges.updateInterval = 1;
gameBadges.maxInterval = 120;
gameBadges.lastUpdateTime = TurbulenzEngine.time;
if (badgesManager)
{
gameBadges.init();
}
else
{
onInitialization();
}
return gameBadges;
}
}