-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
369 lines (332 loc) · 14 KB
/
Copy pathscript.js
File metadata and controls
369 lines (332 loc) · 14 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
const STATS = ['economy', 'security', 'people', 'technology'];
const ICONS = {
economy: '€',
security: '⚔',
people: '☺',
technology: '⌁'
};
const questions = [
buildQuestion('Criminality is on the rise!', 'Fund surveillance AI systems', [-10, 30, -40, 20], 'Increase police presence', [-20, 10, -10, 0]),
buildQuestion('The economy is struggling!', 'Invest in infrastructure', [10, -10, 10, 10], 'Cut taxes', [-20, -10, 20, -10]),
buildQuestion('Technology development is stagnant!', 'Fund research grants', [-10, 10, 10, 20], 'Encourage private sector innovation', [0, -10, 5, 30]),
buildQuestion('Public health is overloaded!', 'Build more hospitals', [-30, 10, 20, 10], 'Promote preventive care', [-10, 0, 10, 0]),
buildQuestion('Energy prices spike!', 'Do nothing', [0, 0, -40, 0], 'Subsidize households', [-30, 10, 20, 0]),
buildQuestion('Big companies create a bad influence on people', 'Regulate them!!!!', [-20, 10, 20, -20], 'Not our problem', [20, -10, -20, 10]),
buildQuestion('Big bad country wants to attack us!', 'Increase military spending', [-20, 30, -20, 10], 'Appease them', [10, -20, -20, 10]),
buildQuestion('Memeber state breaking human rights!', 'Impose sanctions', [-10, 0, 20, 0], 'Ignore it', [10, 0, -20, 0]),
buildQuestion('Foreign-funded media outlets spreading misinformation!', 'Ban them', [0, 20, -10, 0], 'Informational campaign', [0, -20, 10, 0]),
buildQuestion('Startups leave; too much regulations!', "Didn't need them anyway", [-20, 0, 10, 10], 'Ease regulation', [10, -10, -10, 10]),
buildQuestion('Agining population!', 'Raise retirement age', [10, 0, -20, 0], 'Do nothing', [-20, 0, 10, 0]),
buildQuestion('Too much dependency on foreign sources of electricity!', 'Diversify sources', [-10, 10, 10, 0], 'Do nothing', [0, 0, 0, 0]),
buildQuestion('Foreign surveillance drone crashes', 'Put sanctions on the country!', [-10, 10, 10, 0], 'Invest in radar/AA systems', [-20, 20, 10, 0]),
buildQuestion('Floods!', 'Limit environmental impact, invest in green energy', [-20, -10, 20, 10], 'Do nothing', [20, -10, -20, 0]),
buildQuestion('New revolutionary technology!', 'Invest!!!!!!!', [10, 10, 10, 10], 'Leave to private sector', [20, -10, -10, 20]),
buildQuestion('Humanitarian crisis in the Middle East!', 'Provide aid to the region', [-10, 0, -10, 0], 'Allow immigration', [10, -30, 10, 0]),
buildQuestion('Civilian plane shot down over foreign country!', 'Ask for apology', [0, -10, 5, 0], 'Sanctions', [-20, 5, 10, 0]),
buildQuestion('Foreign drone incursion!', 'Act of aggression! (militarise)', [-30, 20, 30, 0], 'Resolve issue diplomatically', [0, -10, 5, 0]),
buildQuestion('Housing prices skyrocket!', 'Its the students, tourists & migrants!!!!', [-20, 5, 20, -20], 'Exponentially tax ownership of 3+ housing spaces', [10, 0, -10, 0]),
buildQuestion('Push through chat control?', 'Yes', [0, 20, -20, 0], 'No', [0, -10, 10, 0]),
buildQuestion('Cyberattack shuts down major bank!', 'Invest heavily in recovery', [-20, 20, 10, 0], 'Let it stabilise naturally', [0, 0, -20, 0]),
buildQuestion('Heatwave causes crops to die!!', 'Subsidize farmers', [-20, 0, 20, 0], 'Do nothing', [10, 0, -20, 0]),
buildQuestion('Internet cables severed by foreign ship!', 'Take over said ship & input sanctions', [-20, 20, 10, 0], 'Appease and resolve diplomatically', [20, -10, 0, 0]),
buildQuestion('Strategic ally commits war crimes!', 'Denounce & sanctions', [-10, -10, 20, 0], 'Do nothing', [10, 10, -20, 0]),
buildQuestion('Protests block cities!', 'Crackdown', [10, 0, -40, 0], 'Try to appease', [-20, 0, 10, 0]),
buildQuestion('Mass disinformation during election campaign!', 'Crack down on the posts', [-10, 10, -10, 0], 'Do nothing', [0, -20, 10, 0]),
buildQuestion('Foreign power imposes tarrifs!', 'Retaliate back with tarrifs!', [-30, 10, 10, 0], 'Accept tarrifs', [-10, 0, -10, 0]),
buildQuestion('Lake becomes contaminated!', 'Clean the water', [-20, 0, 10, 0], 'Advise caution', [0, 0, -20, 0]),
buildQuestion('New tech clashes with regulation', 'Ease regulation', [20, -10, 0, 0], 'Do nothing', [-10, 0, 0, 0]),
buildQuestion('Spike in fraud targetting seniors', 'Awareness campaigns', [-10, 0, 10, 0], 'Do nothing', [0, 0, -10, 0]),
buildQuestion('Critical machine used in hospitals malfunctions!', 'Quit using immediately', [-20, 10, 10, 15], 'Wait and monitor', [0, -10, -5, -10]),
buildQuestion('Quantum research falls behind!', 'Invest heavily', [-25, 0, 0, 20], 'Let private sector lead', [0, 0, 0, -15]),
buildQuestion('Critical satellite malfunctions!', 'Repair immediately', [-20, 10, 0, 15], 'Temporary, cheap, fixes', [0, -10, 0, -10]),
buildQuestion('AI algorithms discriminate!', 'Force to redesign algorithm', [-15, 0, 10, 10], 'Ignore issue', [0, 0, -15, -5]),
buildQuestion('Kim Kardashian launches eco-friendly perfume line!', "How did this shit get on my desk?!", [0, 0, 0, 0], 'Invest EU funds', [-5, 0, 0, 0]),
buildQuestion('George Clooney gets French citizenship!', "Seriously… this is my problem now?", [0, 0, 0, 0], 'Celebrate with decree', [0, 0, 5, 0]),
buildQuestion('Taylor Swift writes a song about inflation getting too high!', 'Im getting paid to little to deal with this', [5, 0, 0, 0], 'Its kindof good', [0, 0, 5, 0]),
buildQuestion('Elon Musk tweets about EU bureaucracy!', "Subpoena him", [0, 0, 0, 0], 'Fine his companies', [10, 10, 0, 0]),
buildQuestion('Reality TV star becomes climate ambassador!', "Is this actually a thing now?", [0, 0, 0, 0], 'Fund the campaign', [-5, 0, 5, 0]),
buildQuestion('Celebrity chef opens a zero-carbon restaurant in Brussels!', "Why am I handling this?", [0, 0, 0, 0], 'How does that even work?', [0, 0, 0, 0]),
buildQuestion('Famous pop star proposes new holiday in EU!', "I have a better idea, declare his birthday a national holiday", [0, 0, 5, 0], 'Declare the holiday', [0, 0, 5, 0])
];
const initialStats = STATS.reduce((acc, key) => ({ ...acc, [key]: 100 }), {});
let stats = { ...initialStats };
let deck = shuffle([...questions]);
let currentQuestion = null;
let gameOver = false;
const questionCard = document.getElementById('question-card');
const questionText = document.getElementById('question-text');
const endScreen = document.getElementById('end-screen');
const endTitle = document.getElementById('end-title');
const endMessage = document.getElementById('end-message');
const endSubmessage = document.getElementById('end-submessage');
const answerElements = [
document.getElementById('answer-left-text'),
document.getElementById('answer-right-text')
];
const answerZones = [
document.querySelector('.answer-left'),
document.querySelector('.answer-right')
];
const statusMessage = document.getElementById('status-message');
const statElements = STATS.reduce((acc, key) => {
return {
...acc,
[key]: {
container: document.querySelector(`.stat[data-key="${key}"]`),
value: document.getElementById(`value-${key}`),
fill: document.getElementById(`fill-${key}`)
}
};
}, {});
const statPulseTimers = {};
const IMPACT_COOLDOWN = 900;
const DRAG_COMMIT_THRESHOLD = 80;
let lastPreviewSide = null;
let lastImpactTime = 0;
let translate = { x: 0, y: 0 };
let dragOrigin = { x: 0, y: 0 };
let activePointer = null;
let isDragging = false;
setupDrag();
resetUI();
function resetUI() {
stats = { ...initialStats };
deck = shuffle([...questions]);
gameOver = false;
document.body.classList.remove('game-over', 'game-win');
endScreen.setAttribute('aria-hidden', 'true');
resetImpact();
updateStatsUI();
nextQuestion();
statusMessage.innerHTML = 'visit <a href="https://github.com/Magicninja7/eu_sim">the Github repo!</a>';
}
function nextQuestion() {
if (!deck.length) {
triggerWin();
return;
}
currentQuestion = deck.pop();
questionText.textContent = currentQuestion.prompt;
answerElements[0].textContent = currentQuestion.options[0].text;
answerElements[1].textContent = currentQuestion.options[1].text;
}
function applyChoice(index) {
if (!currentQuestion || gameOver) {
return;
}
const choice = currentQuestion.options[index];
STATS.forEach((key) => {
stats[key] = clamp(stats[key] + choice.effects[key], 0, 100);
});
updateStatsUI();
if (isCollapse()) {
triggerGameOver();
return;
}
window.setTimeout(() => {
if (!gameOver) {
nextQuestion();
}
}, 400);
}
function updateStatsUI() {
STATS.forEach((key) => {
const value = Math.round(stats[key]);
statElements[key].value.textContent = value;
statElements[key].fill.style.width = `${value}%`;
});
}
function describeEffects(effects) {
const summary = STATS.map((key) => {
const shift = effects[key];
if (!shift) {
return null;
}
const prefix = shift > 0 ? '+' : '';
return `${ICONS[key]} ${prefix}${shift}`;
}).filter(Boolean);
return summary.length ? summary.join(' · ') : 'No immediate change.';
}
function isCollapse() {
return STATS.some((key) => stats[key] <= 0);
}
function triggerGameOver() {
gameOver = true;
currentQuestion = null;
document.body.classList.add('game-over');
const collapsed = STATS.filter((key) => stats[key] <= 0)
.map((key) => `${ICONS[key]} depleted`)
.join(' · ');
endTitle.textContent = 'Crisis!';
endMessage.textContent = collapsed ? `${collapsed}.` : 'The country collapsed.';
endSubmessage.textContent = 'You failed to keep the country running.';
endScreen.setAttribute('aria-hidden', 'false');
showStatus('');
window.addEventListener('pointerdown', restartOnce, { once: true });
}
function restartOnce() {
if (gameOver) {
resetUI();
}
}
function triggerWin() {
gameOver = true;
currentQuestion = null;
document.body.classList.add('game-win');
endTitle.textContent = 'Congrats!';
endMessage.textContent = 'You kept the country running.';
endSubmessage.textContent = 'More events & content coming.';
endScreen.setAttribute('aria-hidden', 'false');
showStatus('');
window.addEventListener('pointerdown', restartOnce, { once: true });
}
function setupDrag() {
questionCard.addEventListener('pointerdown', (event) => {
if (gameOver) {
return;
}
questionCard.setPointerCapture(event.pointerId);
isDragging = true;
activePointer = event.pointerId;
questionCard.classList.add('dragging');
dragOrigin = {
x: event.clientX - translate.x,
y: event.clientY - translate.y
};
});
window.addEventListener('pointermove', (event) => {
if (!isDragging || event.pointerId !== activePointer) {
return;
}
translate = {
x: event.clientX - dragOrigin.x,
y: event.clientY - dragOrigin.y
};
setCardPosition(translate.x, translate.y);
previewSide();
});
const finishDrag = (event) => {
if (!isDragging || event.pointerId !== activePointer) {
return;
}
questionCard.releasePointerCapture(activePointer);
questionCard.classList.remove('dragging');
isDragging = false;
const dragDistance = Math.abs(translate.x);
const pickIndex = translate.x < 0 ? 0 : 1;
resetCardPosition();
clearPreview();
activePointer = null;
if (dragDistance < DRAG_COMMIT_THRESHOLD) {
return;
}
if (!gameOver) {
applyChoice(pickIndex);
}
};
window.addEventListener('pointerup', finishDrag);
window.addEventListener('pointercancel', finishDrag);
}
function previewSide() {
const rect = questionCard.getBoundingClientRect();
const sideIndex = rect.left + rect.width / 2 < window.innerWidth / 2 ? 0 : 1;
highlightAnswer(sideIndex);
previewImpact(sideIndex);
}
function clearPreview() {
answerZones.forEach((zone) => zone.classList.remove('active'));
resetImpact();
}
function highlightAnswer(index) {
answerZones.forEach((zone, idx) => {
zone.classList.toggle('active', idx === index);
});
}
function setCardPosition(x, y) {
questionCard.style.setProperty('--dx', `${x}px`);
questionCard.style.setProperty('--dy', `${y}px`);
}
function resetCardPosition() {
translate = { x: 0, y: 0 };
window.requestAnimationFrame(() => {
setCardPosition(0, 0);
});
}
function showStatus(message) {
statusMessage.textContent = message;
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function previewImpact(sideIndex) {
if (!currentQuestion) {
return;
}
const now = Date.now();
if (sideIndex === lastPreviewSide && now - lastImpactTime < IMPACT_COOLDOWN) {
return;
}
lastPreviewSide = sideIndex;
lastImpactTime = now;
showImpact(sideIndex);
}
function showImpact(index) {
const option = currentQuestion?.options?.[index];
if (!option) {
return;
}
STATS.forEach((key) => {
pulseStat(key, option.effects[key]);
});
}
function pulseStat(statKey, delta) {
const container = statElements[statKey]?.container;
if (!container) {
return;
}
container.classList.remove('pulse-positive', 'pulse-negative');
window.clearTimeout(statPulseTimers[statKey]);
if (!delta) {
return;
}
const className = delta > 0 ? 'pulse-positive' : 'pulse-negative';
// Force reflow so the animation can retrigger.
void container.offsetWidth;
container.classList.add(className);
statPulseTimers[statKey] = window.setTimeout(() => {
container.classList.remove(className);
}, 1000);
}
function resetImpact() {
STATS.forEach((key) => {
const container = statElements[key]?.container;
if (!container) {
return;
}
container.classList.remove('pulse-positive', 'pulse-negative');
window.clearTimeout(statPulseTimers[key]);
statPulseTimers[key] = null;
});
lastPreviewSide = null;
}
function shuffle(items) {
const list = [...items];
for (let i = list.length - 1; i > 0; i -= 1) {
const j = Math.floor(Math.random() * (i + 1));
[list[i], list[j]] = [list[j], list[i]];
}
return list;
}
function buildQuestion(prompt, leftText, leftEffects, rightText, rightEffects) {
return {
prompt,
options: [
{ text: leftText, effects: toEffects(leftEffects) },
{ text: rightText, effects: toEffects(rightEffects) }
]
};
}
function toEffects(values) {
const [economy, security, people, technology] = values;
return { economy, security, people, technology };
}