Files
OpenTournament/scripts/scheduleAndFillResults.js
2025-07-19 12:21:46 +02:00

60 lines
2.1 KiB
JavaScript

const fetch = require('node-fetch');
const readline = require('readline');
async function prompt(question) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => rl.question(question, ans => { rl.close(); resolve(ans); }));
}
function getRandomScore() {
// No draws allowed, so scores must be different
let a = Math.floor(Math.random() * 10);
let b = Math.floor(Math.random() * 10);
while (a === b) {
b = Math.floor(Math.random() * 10);
}
return [a, b];
}
async function main() {
const username = process.env.ADMIN_USERNAME || await prompt('Admin username: ');
const password = process.env.ADMIN_PASSWORD || await prompt('Admin password: ');
// Login as admin
const loginRes = await fetch('http://localhost:4000/api/admin/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
const loginData = await loginRes.json();
if (!loginRes.ok || !loginData.token) {
console.error('Admin login failed:', loginData.error || loginRes.statusText);
process.exit(1);
}
const token = loginData.token;
console.log('Admin login successful. Filling in match results...');
// Fetch all matches
const matchesRes = await fetch('http://localhost:4000/api/matches');
const matches = await matchesRes.json();
for (const match of matches) {
if (match.result) continue; // Skip if result already exists
const [team1Score, team2Score] = getRandomScore();
const resultRes = await fetch(`http://localhost:4000/api/admin/matches/${match.id}/result`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ team1Score, team2Score })
});
if (resultRes.ok) {
console.log(`Filled result for match ${match.id}: ${team1Score} - ${team2Score}`);
} else {
const err = await resultRes.json();
console.log(`Failed to fill result for match ${match.id}:`, err.error || resultRes.statusText);
}
}
}
main();