forked from Geir1983/Screeps-SC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrank.leaderboard.js
180 lines (151 loc) · 6.92 KB
/
rank.leaderboard.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
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
/**
* Adds a link to the alliance of a user with data from load and some other bits i'm unsure of
*/
module.exports.init = function () {
module.dispatchEvent(
{ event: "xhttp", url: "http://www.leagueofautomatednations.com/alliances.js" },
function (response) {
module.alliances = JSON.parse(response.data);
module.userToAlliance = {};
for (var alliance in module.alliances) {
var members = module.alliances[alliance].members;
for (var member in members) {
var memberName = members[member];
module.userToAlliance[memberName] = alliance;
}
}
module.exports.update();
}
);
// We bind url-change to detect when scope data is updated. We do this by deleting an insufficient variable in the scope object.
$(window).bind("hashchange", function () {
var scope = angular.element(document.getElementsByClassName("leaderboard-list ng-scope")).scope();
if (scope && scope.LeaderboardList && scope.LeaderboardList.pageSize) {
delete scope.LeaderboardList.pageSize;
}
});
};
module.exports.update = function () {
module.getScopeData(
"leaderboard-list",
"LeaderboardList",
["LeaderboardList.list", "LeaderboardList.pageSize"],
function (leaderBoard) {
var tableElement = document.getElementsByClassName("table table-striped");
if ($("#th-alliance").length == 0) {
// Todo find a bettwer way to fix month swap
delete leaderBoard.pageSize;
$(".table.table-striped tbody tr th:nth-child(2)").after('<th id="th-alliance">Alliance</th>');
for (var i = 2; i < leaderBoard.list.length + 2; i++) {
var playerColumn = $(".table.table-striped tbody tr:nth-child(" + i + ") td:nth-child(2)");
var playerName = playerColumn[0].innerText.trim();
var alliance = module.userToAlliance[playerName];
var html = "";
if (alliance) {
var logo = module.alliances[alliance].logo;
if (logo) {
var logoURL = "http://www.leagueofautomatednations.com/obj/" + module.alliances[alliance].logo;
html = `<td><a href='http://www.leagueofautomatednations.com/a/${alliance}'>
<img src='${logoURL}' height='16' width='16'>${alliance}</a></td>`;
} else {
html = `<td><a href='http://www.leagueofautomatednations.com/a/${alliance}'>${alliance}</a></td>`;
}
} else {
html = "<td></td>";
}
playerColumn.after(html);
}
}
$(".table.table-striped tbody tr th:last-child").css("text-align", "right");
$(".table.table-striped tbody tr td:last-child").css("text-align", "right");
if ($("#th-gcl-h").length == 0) {
$(".table.table-striped tbody tr th:last-child").after('<th id="th-gcl-h">Last hour</th>');
for (var i = 0; i < leaderBoard.list.length; i++) {
var lastColumn = $(".table.table-striped tbody tr:nth-child(" + (i + 2) + ") td:last-child");
var newColumn = $(`<td id='th-gcl-h-${i}'></td>`);
lastColumn.after(newColumn);
(function (column) {
module.ajaxGet(
`https://screeps.com/api/user/stats?id=${leaderBoard.list[i].user}&interval=8`,
function (result, err) {
var html = "";
if (result) {
var amount = 0;
if (window.location.href.includes("/power/")) {
amount = Math.round(result.stats.powerProcessed) || 0;
column.text(`${amount}`);
} else {
amount = Math.round(result.stats.energyControl / 1000) || 0;
column.text(`${amount}K`);
}
}
}
);
})(newColumn);
}
}
$(".table.table-striped tbody tr th:last-child").css("text-align", "right");
$(".table.table-striped tbody tr td:last-child").css("text-align", "right");
if ($("#th-gcl").length == 0) {
if (window.location.href.includes("/power/"))
$(".table.table-striped tbody tr th:last-child").after('<th id="th-gcl">Processed</th>');
else $(".table.table-striped tbody tr th:last-child").after('<th id="th-gcl">GCL</th>');
for (var i = 0; i < leaderBoard.list.length; i++) {
var lastColumn = $(".table.table-striped tbody tr:nth-child(" + (i + 2) + ") td:last-child");
var playerColumn = $(".table.table-striped tbody tr:nth-child(" + (i + 2) + ") td:nth-child(2)");
var playerName = playerColumn[0].innerText.trim();
var newColumn = $(`<td id='th-gcl-${i}'></td>`);
lastColumn.after(newColumn);
(function (column, player) {
module.ajaxGet(`https://screeps.com/api/user/find?username=${player}`, function (result, err) {
var html = "";
if (result) {
if (window.location.href.includes("/power/")) {
column.text(`${format_number(result.user.power)}`);
} else {
var gcl = result.user.gcl;
var gclLevel = Math.floor(Math.pow((gcl || 0) / 1000000, 1 / 2.4)) + 1;
column.text(`${gclLevel}`);
}
}
function format_number(n) {
if (n < 1000) {
return parseInt(n);
} else if (n < 1000000) {
return parseInt(n / 1000) + "K";
} else if (n < 1000000000) {
return parseInt(n / 1000000) + "M";
}
}
});
})(newColumn, playerName);
}
}
$(".table.table-striped tbody tr th:last-child").css("text-align", "right");
$(".table.table-striped tbody tr td:last-child").css("text-align", "right");
if ($("#th-start-date").length == 0) {
$(".table.table-striped tbody tr th:last-child").after('<th id="th-start-date">Start date</th>');
for (var i = 0; i < leaderBoard.list.length; i++) {
var playerColumn = $(".table.table-striped tbody tr:nth-child(" + (i + 2) + ") td:nth-child(2)");
var lastColumn = $(".table.table-striped tbody tr:nth-child(" + (i + 2) + ") td:last-child");
var playerName = playerColumn[0].innerText.trim();
var newColumn = $(`<td id='th-start-date-${i}'></td>`);
lastColumn.after(newColumn);
(function (column, player) {
module.ajaxGet(
`https://screeps.com/api/leaderboard/find?mode=world&username=${player}`,
function (result, err) {
var html = "";
if (result && result.list.length) {
var season = result.list[0].season;
season = season.replace("-rel", "");
column.text(season);
}
}
);
})(newColumn, playerName);
}
}
}
);
};