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
|
<script lang="ts">
import Header from '$lib/Header.svelte'
import Head from '$lib/Head.svelte'
import { colorCodes, formattingCodeToHtml, millisecondsToTime, skyblockTime } from '$lib/utils'
import type { ElectionData } from '$lib/APITypes'
import { onDestroy, onMount } from 'svelte'
import MayorSkin from '../../lib/MayorSkin.svelte'
import { invalidate } from '$app/navigation'
import { browser } from '$app/environment'
export let data: ElectionData
let destroyed = false
let currentTime = Date.now()
// update currentTime every animation frame
function updateTime() {
currentTime = Date.now()
if (!destroyed) requestAnimationFrame(updateTime)
}
$: nextSpecialMayorYear = Math.ceil(((data.current?.year || data.previous.year) + 1) / 8) * 8
const specialMayors = ['Scorpius', 'Derpy', 'Jerry']
let autoInvalidateTimeout: null | NodeJS.Timeout = null
// invalidate at the end of every minute
async function autoInvalidate(first: boolean) {
// don't invalidate the first time the function is called
if (!first) await invalidate('')
const lastUpdatedAgo = Date.now() - data.lastUpdated
autoInvalidateTimeout = setTimeout(() => autoInvalidate(false), lastUpdatedAgo + 10 * 60 * 1000)
}
if (browser) autoInvalidate(true)
onMount(() => {
destroyed = false
updateTime()
})
onDestroy(() => {
destroyed = true
if (autoInvalidateTimeout) clearTimeout(autoInvalidateTimeout)
})
</script>
<Head title="SkyBlock Mayor Election Status" />
<Header />
<main>
{#if data.current}
<div class="next-mayor-update">
<p>
<b>Last API update:</b>
{millisecondsToTime(currentTime - data.lastUpdated, {
smallestUnit: 1,
parts: 3,
})} ago
</p>
</div>
{/if}
<h1>SkyBlock Mayor Election Status</h1>
<p>
<b>Next election:</b>
{millisecondsToTime(skyblockTime(data.previous.year + 1, 6, 27) - currentTime, {
parts: 3,
smallestUnit: 1,
})}
</p>
{#if data.current}
<h2>Ongoing election <span class="candidate-year">(Year {data.current.year})</span></h2>
<p class="election-ends-in-text">
<b>Ends in:</b>
{millisecondsToTime(skyblockTime(data.previous.year + 1, 3, 27) - currentTime)}
</p>
<div class="mayor-candidates">
{#each data.current.candidates.sort((a, b) => a.votes - b.votes) as candidate}
{@const color = candidate.color ? colorCodes[candidate.color] : null}
<div class="mayor">
<div>
<h3 style={color ? `color: ${color}` : undefined}>{candidate.name}</h3>
<p class="mayor-vote-count">
<span class="mayor-vote-count-number" style={color ? `color: ${color}` : undefined}>
{candidate.votes.toLocaleString()}
</span> votes
</p>
<MayorSkin name={candidate.name} />
</div>
<ul class="mayor-perks">
{#each candidate.perks as perk}
<div class="mayor-perk">
<h4 style={color ? `color: ${color}` : undefined}>{perk.name}</h4>
<p>{@html formattingCodeToHtml(perk.description)}</p>
</div>
{/each}
</ul>
</div>
{/each}
</div>
{/if}
<h2>
Previous election <span class="candidate-year">(Year {data.previous.year})</span>
</h2>
<div class="mayor-candidates">
{#each data.previous.candidates.sort((a, b) => b.votes - a.votes) as candidate}
{@const color = candidate.color ? colorCodes[candidate.color] : null}
<div class="mayor">
<div>
<h3 style={color ? `color: ${color}` : undefined}>{candidate.name}</h3>
<p class="mayor-vote-count">
<span class="mayor-vote-count-number" style={color ? `color: ${color}` : undefined}>
{candidate.votes.toLocaleString()}
</span> votes
</p>
{#if candidate.name == data.previous.winner}
<p class="mayor-winner">Winner</p>
{/if}
<MayorSkin name={candidate.name} />
</div>
<ul class="mayor-perks">
{#each candidate.perks as perk}
<div class="mayor-perk">
<h4 style={color ? `color: ${color}` : undefined}>{perk.name}</h4>
<p>{@html formattingCodeToHtml(perk.description)}</p>
</div>
{/each}
</ul>
</div>
{/each}
</div>
<!-- {%- set nextSpecialMayorYear = (((data.current.year or data.previous.year) + 1) / 8)|round(0, 'ceil') * 8 -%}
{%- set specialMayors = ['Scorpius', 'Derpy', 'Jerry'] -%}
<h2>Upcoming special mayors</h2>
<ul>
{%- for i in range(3) -%}
<li><b>{{ specialMayors[((nextSpecialMayorYear / 8) + i) % 3] }}</b> <span class="next-special-mayor-time">({{ skyblockYear(nextSpecialMayorYear + 8 * i, 6, 27) }})</span></li>
{%- endfor -%}
</ul> -->
<h2>Upcoming special mayors</h2>
<ul>
<!-- for i in range(3) -->
{#each Array(3) as _, i}
<li>
<b>{specialMayors[(nextSpecialMayorYear / 8 + i) % 3]}</b>
<span class="next-special-mayor-time">
({millisecondsToTime(
skyblockTime(nextSpecialMayorYear + 8 * i + 1, 3, 27) - currentTime
)})
</span>
</li>
{/each}
</ul>
</main>
<style>
.mayor-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-column-gap: 1em;
grid-row-gap: 1em;
margin-top: 1em;
margin-bottom: 1em;
}
.mayor {
display: inline-block;
width: 12em;
margin-right: 1em;
vertical-align: top;
}
.mayor h3 {
filter: brightness(1.5);
text-align: center;
}
.mayor img {
display: block;
width: 5em;
margin: 0 auto;
}
.mayor-vote-count-number {
filter: brightness(1.2);
}
.mayor-vote-count {
text-align: center;
width: 100%;
margin: 0 0 0.5em 0;
}
.mayor-perk h4 {
margin-bottom: 0;
}
.mayor-perk p {
margin: 0;
}
.mayor-candidates {
/* display: ; */
/* everything next to each other */
/* grid-template-columns: repeat(5, 1fr); */
max-width: fit-content;
}
.mayor-perks {
list-style-type: none;
padding: 0;
}
.candidate-year {
font-weight: normal;
color: var(--theme-darker-text);
}
.mayor-winner {
color: var(--theme-yellow);
font-weight: bold;
margin: 0;
text-align: center;
font-size: 1em;
}
.next-special-mayor-time {
color: var(--theme-darker-text);
}
.election-ends-in-text {
margin-top: 0;
}
.next-mayor-update {
float: right;
color: var(--theme-darker-text);
}
.next-mayor-update p {
margin: 0;
}
</style>
|