-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
198 lines (191 loc) · 7.78 KB
/
index.html
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
<html>
<head>
<title>Gold Ledger</title>
<meta name="description" content="Top 100 cards ledger data">
<link rel="icon" type="image/x-icon" href="public/favicon.png">
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="controls">
<input type="date" id="dateInput" min="2023-10-05" />
<p class="error"></p>
<button id="toggleHeadersButton">Toggle Additional Headers</button>
</div>
<div class="content">
<div class="table-holder" dir="rtl">
<table dir="ltr"></table>
</div>
</div>
<footer>
<p style="text-align: center; margin-bottom: 3rem">The oldest tracked date is 2023-10-05.</p>
</footer>
<script>
const minDate = '2023-10-05'
let maxDate = new Date().toISOString().split('T')[0]
fetch('/files/leaderboard.txt')
.then(response => response.text())
.then(dates => {
const lines = dates.split('\n').reverse()
maxDate = lines[0]
fillTable(maxDate)
document.querySelector('#dateInput').max = maxDate
})
.catch(error => {
console.error('Error fetching date data:', error)
document.querySelector('.error').textContent = 'Failed to fetch the latest date.'
})
document.querySelector('input').addEventListener('change', async event => {
const selectedDate = event.target.value
if (selectedDate && selectedDate >= '2023-10-05' && selectedDate <= maxDate) {
await fillTable(selectedDate)
}
})
const toggleHeadersButton = document.getElementById('toggleHeadersButton')
toggleHeadersButton.addEventListener('click', () => {
document.querySelectorAll('.hideable').forEach(el => el.classList.toggle('hide'))
})
async function fillTable(fetchDate) {
const data = await fetch(`/data/${fetchDate}.json`)
document.querySelector('.error').textContent = ''
const json = await data.json()
const table = document.querySelector('table')
table.innerHTML = ''
const index = document.createElement('tr')
const validHeaders = [
'Nation',
'Deck Value',
'Junk Value',
'Bank',
'Card Count',
'Deck Capacity',
'S4 Legendary',
]
const hideableHeaders = [
'Common',
'Uncommon',
'Rare',
'Ultra-Rare',
'Epic',
'Legendary',
'S1',
'S2',
'S3',
'S1 Common',
'S1 Uncommon',
'S1 Rare',
'S1 Ultra-Rare',
'S1 Epic',
'S1 Legendary',
'S2 Common',
'S2 Uncommon',
'S2 Rare',
'S2 Ultra-Rare',
'S2 Epic',
'S2 Legendary',
'S3 Common',
'S3 Uncommon',
'S3 Rare',
'S3 Ultra-Rare',
'S3 Epic',
'S3 Legendary',
'S4 Common',
'S4 Uncommon',
'S4 Rare',
'S4 Ultra-Rare',
'S4 Epic',
]
let indexStr = `<th>#</th>`
validHeaders.forEach(header => (indexStr += `<th class='sort' data-order='none'>${header}</th>`))
hideableHeaders.forEach(
header => (indexStr += `<th class='sort hideable hide' data-order='none'>${header}</th>`)
)
indexStr += `</tr>`
index.innerHTML = indexStr
table.appendChild(index)
json.forEach(deck => {
const row = document.createElement('tr')
let rowStr = `<tr><td></td>`
validHeaders.forEach(key => {
switch (key) {
case 'Deck Value' || 'Bank':
rowStr += `<td><a target='_blank' href='https://www.nationstates.net/nation=${
deck.Nation
}/page=deck/value_deck=1'>${deck[key] ? deck[key] : 0}</a></td>`
break
case 'Card Count':
rowStr += `<td><a target='_blank' href='https://www.nationstates.net/nation=${deck.Nation}/page=deck'>${
deck[key] ? deck[key] : 0
}</a></td>`
break
case 'Nation':
rowStr += `<td><a target='_blank' href='https://www.nationstates.net/nation=${deck.Nation}/page=deck'>${
deck[key] ? deck[key] : 0
}</a></td>`
break
default:
rowStr += `<td>${deck[key] ? deck[key] : 0}</td>`
}
})
hideableHeaders.forEach(key => {
if (Object.keys(deck).includes(key)) {
if (key.includes('S') && !key.includes(' ')) {
rowStr += `<td class="hideable hide"><a target='_blank' href='https://www.nationstates.net/nation=${
deck.Nation
}/page=deck/?filter=${key.replace('S', 'season-')}'>${deck[key]}</a></td class="hideable hide">`
} else if (key.includes(' ')) {
let adjKey = key.split(' ')
rowStr += `<td class="hideable hide"><a target='_blank' href='https://www.nationstates.net/nation=${
deck.Nation
}/page=deck/?filter=${adjKey[0].replace('S', 'season-')}+${adjKey[1].toLowerCase()}'>${
deck[key]
}</a></td class="hideable hide">`
} else if (!key.includes('S')) {
rowStr += `<td class="hideable hide"><a target='_blank' href='https://www.nationstates.net/nation=${
deck.Nation
}/page=deck/?filter=${key.toLowerCase()}'>${deck[key]}</a></td class="hideable hide">`
}
} else rowStr += `<td class="hideable hide">0</td>`
})
row.innerHTML = rowStr
table.appendChild(row)
document.querySelector('input').value = fetchDate
document.querySelector('input').max = fetchDate
})
const sortableColumns = document.querySelectorAll('.sort')
sortableColumns.forEach(col => {
col.addEventListener('click', () => {
const table = document.querySelector('table')
const columnIndex = Array.from(col.parentNode.cells).indexOf(col)
const rows = Array.from(table.rows).slice(1)
const currentOrder = col.getAttribute('data-order')
const newOrder = currentOrder === 'asc' ? 'desc' : 'asc'
rows.sort((a, b) => {
const aValue = parseFloat(a.cells[columnIndex].innerText)
const bValue = parseFloat(b.cells[columnIndex].innerText)
if (currentOrder === 'asc' && currentOrder !== 'none') {
return aValue > bValue ? 1 : aValue === bValue ? 0 : -1
} else {
return aValue > bValue ? -1 : aValue === bValue ? 0 : 1
}
return aValue > bValue ? 1 : aValue === bValue ? 0 : -1
})
table.append(...rows)
col.setAttribute('data-order', newOrder)
})
})
const prefersDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
const highlightColor = prefersDarkMode ? '#333333' : 'silver'
document.querySelectorAll('td').forEach(td => {
td.addEventListener('mouseover', e => {
Array.from(e.currentTarget.parentNode.children).forEach(
child => (child.style.backgroundColor = highlightColor)
)
})
td.addEventListener('mouseout', e => {
Array.from(e.currentTarget.parentNode.children).forEach(child => (child.style.backgroundColor = ''))
})
})
}
</script>
</body>
</html>