-
Notifications
You must be signed in to change notification settings - Fork 2
/
highscore.js
89 lines (82 loc) · 2.21 KB
/
highscore.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
/**
* Frontend usage of Highscore
* https://github.com/Calamari/highscore
*
* Copyright 2013, Georg Tavonius
*
* Requires jQuery
*
* Version: 0.0.1
*/
(function($, win) {
'use strict';
win.jaz = win.jaz || {};
function makeGetRequest(url, reverse, options) {
var scope = null,
limit = 10,
cb;
$.each(options, function(i, val) {
if (typeof val === 'string') {
scope = val;
} else if (typeof val === 'number') {
limit = val;
} else if (typeof val === 'function') {
cb = val;
}
});
$.getJSON(url + '?callback=?', {
reverse: reverse
}, function(data) {
cb(data.items);
});
}
/*
* Creates an jaz.Highscore instance
* @param {String} gameId Id of game
* @param {String} apiUrl URL of API endpoint
*/
win.jaz.Highscore = function(gameId, apiUrl) {
this.gameId = gameId;
this.url = apiUrl + '/' + this.gameId;
};
win.jaz.Highscore.VERSION = '0.0.1';
win.jaz.Highscore.prototype = {
/*
* Calls for the highscore (the top x)
* @param {String} [limit] How much to get (default 10)
* @param {String} [scope] The scope to get in (default none)
* @param {function} cb Callback that gets the items
*/
getHighest: function(limit, scope, cb) {
makeGetRequest(this.url, false, arguments);
},
/*
* Calls for the highscore for the lowest entries (the x loosers)
* @param {String} [limit] How much to get (default 10)
* @param {String} [scope] The scope to get in (default none)
* @param {function} cb Callback that gets the items
*/
getLowest: function(limit, scope, cb) {
makeGetRequest(this.url, true, arguments);
},
/*
* Sets a highscore
* @param {String} player Name of player
* @param {String} [score] How much points did he make?
* @param {String} [scope] The scope the score was created
*/
set: function(player, score, scope) {
$.ajax({
url: this.url,
data: {
player: player,
score: score,
scope: scope
},
dataType: 'json',
type: 'POST',
crossDomain: true
});
}
};
}(jQuery, window));