/
playerstats.js
70 lines (65 loc) · 1.41 KB
/
playerstats.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
function loadStats(username, callback) {
$.ajax({
'url': '/stats/' + username,
'type': 'GET',
'cache': false,
'dataType': 'json',
'success': callback
});
}
function saveStats(username, levelname, stats) {
$.ajax({
'url': '/stats/' + username + '/' + levelname,
'type': 'PUT',
'dataType': 'json',
'data': JSON.stringify({
'statistic': {
'complete': stats.complete,
'got_all_cogs': stats.gotAllCogs
}
}),
'contentType': 'application/json; charset=utf-8'
});
}
function PlayerStats(username, callback) {
this.username = username;
this.stats = {};
if (this.username != null) {
var this_ = this;
loadStats(username, function(stats) {
for (var i = 0; i < stats.length; i++) {
var s = stats[i]['statistic'];
this_.stats[s.levelname] = {
complete: s['complete'],
gotAllCogs: s['got_all_cogs']
};
}
callback();
});
}
}
PlayerStats.prototype.getStatsForLevel = function(levelname) {
if (this.username != null) {
if (levelname in this.stats) {
return this.stats[levelname];
}
} else {
// TODO: cookies
}
return {
complete: false,
gotAllCogs: false
};
};
PlayerStats.prototype.setStatsForLevel = function(levelname, complete, gotAllCogs) {
if (this.username != null) {
var stats = {
complete: complete,
gotAllCogs: gotAllCogs
};
this.stats[levelname] = stats;
saveStats(this.username, levelname, stats);
} else {
// TODO: cookies
}
};