forked from SteamGame/steamSummerMinigame
-
Notifications
You must be signed in to change notification settings - Fork 2
/
leaderboard.user.js
150 lines (123 loc) · 4.89 KB
/
leaderboard.user.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
138
139
140
141
142
143
144
145
146
147
148
149
150
// ==UserScript==
// @name Monster Minigame Leaderboard
// @namespace https://github.com/SteamDatabase/steamSummerMinigame
// @version 1.0
// @description Display leaderboards from http://steamga.me
// @match *://steamcommunity.com/minigame/towerattack*
// @match *://steamcommunity.com//minigame/towerattack*
// @grant GM_xmlhttpRequest
// @updateURL https://raw.githubusercontent.com/SteamDatabase/steamSummerMinigame/master/leaderboard.user.js
// @downloadURL https://raw.githubusercontent.com/SteamDatabase/steamSummerMinigame/master/leaderboard.user.js
// ==/UserScript==
//NOTE: This REQUIRES the use of GreaseMonkey or TamperMonkey
(function(w) {
"use strict";
function initLeaderboard() {
var container = document.createElement('div');
container.id = 'leaderboard_wrapper';
container.style.overflow = "hidden";
container.style.height = "360px";
container.style.width = "261px";
container.style.display = "none";
container.style.position = "relative";
container.style.margin = "50px 0 0 5px";
container.style.padding = "5px";
document.getElementById('col_right').appendChild(container);
var leaderboard = document.createElement('table');
leaderboard.id = 'leaderboard';
var th = document.createElement('tr');
th.style.fontSize = '11px';
th.style.color = '#ddd';
var thc = document.createElement('th');
var thn = document.createElement('th');
var thl = document.createElement('th');
thc.appendChild(document.createTextNode('Rank'));
thn.appendChild(document.createTextNode('Name'));
thl.appendChild(document.createTextNode('Level'));
thn.style.textAlign = "center";
th.appendChild(thc);
th.appendChild(thn);
th.appendChild(thl);
leaderboard.appendChild(th);
document.getElementById('leaderboard_wrapper').appendChild(leaderboard);
var credit = document.createElement('div');
credit.style.fontSize = "12px";
credit.style.textAlign = "center";
credit.innerHTML = 'Data by <a href="http://steamga.me/" style="color:#ddd;" alt="http://steamga.me/" target="_blank">steamga.me</a>';
document.getElementById('leaderboard_wrapper').appendChild(credit);
var toggler = document.createElement('div');
toggler.id = "leaderboard_toggler";
toggler.onclick = toggleLeaderboard;
toggler.style.position = 'absolute';
toggler.style.bottom = "-48px";
toggler.style.color = "black";
toggler.style.textAlign = "center";
toggler.style.width = '261px';
toggler.style.cursor = "pointer";
toggler.appendChild(document.createTextNode("Show Leaderboards"));
document.getElementById('col_right').appendChild(toggler);
if (!w.Leaderboard_Timer) {
clearInterval(w.Leaderboard_Timer);
}
w.Leaderboard_Timer = setInterval(getLeaderboard, 1000 * 30);
getLeaderboard();
}
function drawLeaderboardRoom(room) {
var item = document.createElement('tr');
item.className = 'leaderboard_item';
item.style.height = '23px';
item.style.fontSize = '10px';
var num = document.createElement('td');
num.appendChild(document.createTextNode('#' + room.position));
var name = document.createElement('td');
name.style.textAlign = 'center';
name.appendChild(document.createTextNode(room.name));
var level = document.createElement('td');
level.style.textAlign = 'right';
level.appendChild(document.createTextNode(room.level));
if(room.id == w.g_GameID)
{
item.style.color = '#d4e157';
}
item.appendChild(num);
item.appendChild(name);
item.appendChild(level);
document.getElementById('leaderboard').appendChild(item);
}
function getLeaderboard() {
GM_xmlhttpRequest({
method: "GET",
url: "http://steamga.me/data/api/leaderboard.json",
onload: function(response) {
console.log('Downloading new leaderboard...');
var elements = document.getElementsByClassName('leaderboard_item');
while(elements.length > 0){
elements[0].parentNode.removeChild(elements[0]);
}
var resp = JSON.parse(response.responseText);
var leaderboard = Object.keys(resp).map(function (key) {
return resp[key];
});
leaderboard.sort(function(a, b) {
return b.level - a.level;
});
leaderboard.forEach(drawLeaderboardRoom);
}
});
}
function toggleLeaderboard() {
var a = document.getElementById('leaderboard_wrapper');
var b = document.getElementById('activitylog');
var c = document.getElementById('leaderboard_toggler');
if (a.style.display == 'block') {
a.style.display = 'none';
b.style.display = 'block';
c.innerHTML = "Show Leaderboards";
} else {
a.style.display = 'block';
b.style.display = 'none';
c.innerHTML = "Show Activity";
}
}
initLeaderboard();
}(window));