-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHighScoreManager.js
137 lines (123 loc) · 3.9 KB
/
HighScoreManager.js
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
/*
* Manages high scores
*
* @example
* # To register a new user/game
* jNorthPole.createUser('api_key', 'secret')
*
* @example
* hsm = HighScoreManager.get()
* hsm.api_key = 'api_key'
* hsm.secret = 'secret'
*
* hsm.addScore('kiki', 210)
* hsm.getScores(20) # highest 20 scores
*
* hsm.responseHandler = (data) ->
* # do something else
*
*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
var HighScoreManager = (function() {
let instance = undefined;
HighScoreManager = class HighScoreManager {
static initClass() {
instance = null;
const Cls = (Singleton.HighScoreManager = class HighScoreManager {
static initClass() {
this.prototype.apiKey = 'guest';
this.prototype.secret = 'guest';
}
// Set apiKey and secret and attempt to register if tryRegister is true
//
// @param [String] apiKey
// @param [String] secret
// @param [Boolean] tryRegister default false
auth(apiKey, secret, tryRegister) {
if (tryRegister == null) { tryRegister = false; }
if (tryRegister) {
jNorthPole.createUser(apiKey, secret, data => console.log(`api key registered: ${apiKey}`));
}
this._setTokens(apiKey, secret);
this._ensureTokenPresence();
return this;
}
// Set apiKey and secret
//
// @param [String] apiKey
// @param [String] secret
_setTokens(apiKey, secret) {
this.apiKey = apiKey;
return this.secret = secret;
}
// add a score
//
// @param [String] name
// @param [Number] score
addScore(name, score) {
this._ensureTokenPresence();
if (name == null) { throw new Error('name required'); }
if (!isNumeric(score)) { throw new Error('score needs to be a number'); }
const json = {
api_key: this.apiKey,
secret: this.secret,
type: 'highscore',
name,
score
};
return jNorthPole.createStorage(json, this.responseHandler, this.errorHandler);
}
// get scores
//
// @param [Number] limit
getScores(limit, order) {
if (limit == null) { limit = 10; }
if (order == null) { order = 'desc'; }
if (order == 'asc') { order = '1' }
if (order == 'desc') { order = '-1' }
this._ensureTokenPresence();
const json = {
api_key: this.apiKey,
secret: this.secret,
type: 'highscore',
__limit: limit,
__sort: { score: order }
};
return jNorthPole.getStorage(json, this.responseHandler, this.errorHandler);
}
// override this
responseHandler(data) {
return console.log(data);
}
// override this
errorHandler(data, status) {
return console.log(data);
}
_ensureTokenPresence() {
if (this.apiKey == null) { throw new Error('apiKey missing'); }
if (this.secret == null) { throw new Error('secret missing'); }
}
});
Cls.initClass();
}
static get() {
return instance != null ? instance : (instance = new Singleton.HighScoreManager());
}
static auth(apiKey, secret, tryRegister) {
return this.get().auth(apiKey, secret, tryRegister);
}
static addScore(name, score) {
return this.get().addScore(name, score);
}
static getScores(limit, order) {
return this.get().getScores(limit, order);
}
};
HighScoreManager.initClass();
return HighScoreManager;
})();