This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtierlist.js
254 lines (235 loc) · 7.89 KB
/
tierlist.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
var userIP, userID, ids = [], ranked = false;
function setIP(json) {
userIP = json.ip;
}
var config = {
apiKey: "AIzaSyCDLf7DOm_cv8AQfMg_1O-3-OGXHYyOtTs",
authDomain: "takatomon-tierlist.firebaseapp.com",
databaseURL: "https://takatomon-tierlist.firebaseio.com",
projectId: "takatomon-tierlist",
storageBucket: "takatomon-tierlist.appspot.com",
messagingSenderId: "58653453526"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
var isAnonymous = user.isAnonymous;
userID = user.uid;
console.log("Authentication issued.")
} else {
console.log("Authentication revoked.")
}
});
firebase.auth().signInAnonymously().catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(error.code, error.message);
});
database = firebase.database();
function rate() {
var mon = this.parentElement.parentElement.id;
var value = parseInt(this.dataset.value);
firebase.database().ref([
mon,
userID
].join("/")).set({
"ip": userIP,
"vote": value
});
updateRating(mon);
}
function updateRating(mon) {
var row = document.getElementById(mon);
var cell = row.cells[3];
try {
firebase.database().ref(mon).once('value').then(function (snapshot) {
var snapshot = snapshot.val();
var total = 0;
var weightedCount = 0;
var count = 0;
var userVote;
var ipVotes = {};
for (var id in snapshot) {
if (snapshot[id].ip in ipVotes) {
ipVotes[snapshot[id].ip].subtotal += snapshot[id].vote;
ipVotes[snapshot[id].ip].subcount++;
}
else {
ipVotes[snapshot[id].ip] = {
"subtotal": snapshot[id].vote,
"subcount": 1
}
}
count++;
if (id == userID) {
userVote = snapshot[id].vote;
}
}
for (var ip in ipVotes) {
var weight = Math.log(Math.E * ipVotes[ip].subcount);
var subvote = ipVotes[ip].subtotal / ipVotes[ip].subcount;
total += subvote * weight;
weightedCount += weight;
}
if (userVote >= 0) {
var passed = false;
for (var star of cell.children) {
if (passed) {
star.classList.remove("underlined");
}
else {
star.classList.add("underlined");
}
if (star.dataset.value == userVote) {
passed = true;
}
}
}
var passed = count <= 0;
var ratio = total / weightedCount;
var clip = (ratio % 1) * 100;
for (var star of cell.children) {
if (passed) {
star.children[1].style.opacity = 0;
}
else {
star.children[1].style = "";
}
if (star.dataset.value == Math.floor(ratio) + 1) {
passed = true;
if (clip > 0) {
star.children[1].style = [
"-webkit-clip-path: polygon(0 0, " + clip + "% 0, " + clip + "% 100%, 0 100%)",
"clip-path: polygon(0 0, " + clip + "% 0, " + clip + "% 100%, 0 100%)"
].join(";");
}
else {
star.children[1].style.opacity = 0;
}
}
}
row.dataset.value = ratio || 0;
row.dataset.count = count;
});
}
catch (e) {
console.log(e);
}
}
function insertMoniker(cell, mon) {
for (var i in digi[mon].name) {
var moniker = document.createElement("div");
moniker.innerHTML = digi[mon].name[i];
cell.appendChild(moniker);
}
}
function insertPortrait(cell, mon) {
for (var i of digi[mon].images) {
var img = document.createElement("img");
img.width = 24;
img.src = "img/portrait/" + i + ".png";
cell.appendChild(img);
}
}
function insertSkill(cell, skill) {
var dna = document.createElement("div");
var attribute = document.createElement("img");
attribute.src = "img/tribe/" + skill.attribute + ".png";
dna.appendChild(attribute);
if (skill.physical) {
var physical = document.createElement("img");
physical.src = "img/skill/physical.png";
dna.appendChild(physical);
}
if (skill.magical) {
var magical = document.createElement("img");
magical.src = "img/skill/magical.png";
dna.appendChild(magical);
}
var effect = document.createElement("span");
effect.innerHTML = ["Support", "ST", "AoE"][skill.effect];
dna.appendChild(effect);
cell.appendChild(dna);
}
function insertStars(cell, mon) {
for (var i = 0; i < 5; i++) {
var star = document.createElement("span");
star.className = "star";
star.dataset.value = i;
var shadow = document.createElement("img");
shadow.className = "shadow";
shadow.src = "img/birdramon.png";
star.appendChild(shadow);
var light = document.createElement("img");
light.className = "light";
light.src = "img/birdramon.png";
star.appendChild(light);
star.addEventListener("click", rate);
cell.appendChild(star);
}
}
function sort() {
var table = document.getElementById("mon-score");
var rows = Array.from(table.rows);
if (ranked) {
rows.sort(function (a, b) {
return ids.indexOf(a.id) - ids.indexOf(b.id);
});
this.innerHTML = "Sort";
ranked = !ranked;
}
else {
rows.sort(function (a, b) {
return b.dataset.value - a.dataset.value;
});
this.innerHTML = "Unsort";
ranked = !ranked;
}
for (var row of rows) {
table.appendChild(row);
}
}
function initTierlist() {
var meta = document.getElementById("meta-score");
var metarow = meta.insertRow();
metarow.id = "meta";
var metacell0 = metarow.insertCell();
metacell0.innerHTML = "EVALUATION";
var metacell1 = metarow.insertCell();
metacell1.innerHTML = "This page was created on January 11, 2019 partly as an experiment."
var metacell2 = metarow.insertCell();
metacell2.innerHTML = "How would you rate the accuracy of this community tier list thus far?";
var metacell3 = metarow.insertCell();
insertStars(metacell3, "meta");
updateRating("meta");
var table = document.createElement("table");
table.id = "mon-score"
document.body.appendChild(table);
for (var mon in digi) {
if (digi[mon].evol >= 5) {
for (var skill of digi[mon].skills) {
var row = table.insertRow();
row.id = [
mon,
skill.attribute,
skill.physical,
skill.magical,
skill.effect
].join("-");
ids.push(row.id);
var cell0 = row.insertCell();
insertMoniker(cell0, mon);
var cell1 = row.insertCell();
insertPortrait(cell1, mon);
var cell2 = row.insertCell();
insertSkill(cell2, skill);
var cell3 = row.insertCell();
insertStars(cell3, mon);
updateRating(row.id);
}
}
}
var sorter = document.getElementById("sorter");
sorter.addEventListener("click", sort);
}
window.addEventListener("DOMContentLoaded", initTierlist)