-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfind-game-id.js
More file actions
executable file
Β·195 lines (164 loc) Β· 5.67 KB
/
Copy pathfind-game-id.js
File metadata and controls
executable file
Β·195 lines (164 loc) Β· 5.67 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
#!/usr/bin/env node
/**
* Find Available Gamedays & Games on Stage
*
* Workflow:
* 1. Fetch all published gamedays
* 2. Find one that's not finished (status != "beendet")
* 3. Get games from that gameday
* 4. Suggest a game to test
*
* Usage:
* node find-game-id.js --password=PASSWORD
*/
const https = require('https');
const { URL } = require('url');
const args = process.argv.slice(2).reduce((acc, arg) => {
const [key, value] = arg.replace('--', '').split('=');
acc[key] = value;
return acc;
}, {});
const TARGET_HOST = 'https://stage.leaguesphere.app';
const TEST_USERNAME = args.username || 'chrisd';
const TEST_PASSWORD = args.password;
if (!TEST_PASSWORD) {
console.error('ERROR: Password required. Use --password=YOUR_PASSWORD');
process.exit(1);
}
function makeRequest(method, path, data = null, cookies = {}) {
return new Promise((resolve, reject) => {
const url = new URL(path, TARGET_HOST);
const options = {
method,
hostname: url.hostname,
port: url.port,
path: url.pathname + url.search,
headers: {
'User-Agent': 'find-game-id/1.0',
'Cookie': Object.entries(cookies)
.map(([k, v]) => `${k}=${v}`)
.join('; '),
},
};
if (data) {
const body = JSON.stringify(data);
options.headers['Content-Type'] = 'application/json';
options.headers['Content-Length'] = Buffer.byteLength(body);
}
const req = https.request(options, (res) => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
const newCookies = {};
const setCookie = res.headers['set-cookie'];
if (setCookie) {
(Array.isArray(setCookie) ? setCookie : [setCookie]).forEach(cookie => {
const parts = cookie.split(';')[0].split('=');
if (parts.length === 2) {
newCookies[parts[0].trim()] = parts[1].trim();
}
});
}
resolve({ status: res.statusCode, body, cookies: newCookies });
});
});
req.on('error', reject);
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
async function main() {
console.log('π Finding available gamedays on stage...\n');
// Login
console.log('1οΈβ£ Logging in as ' + TEST_USERNAME);
let cookies = {};
// Get CSRF token
const loginPageRes = await makeRequest('GET', '/login/', null, cookies);
const csrfMatch = loginPageRes.body.match(/name="csrfmiddlewaretoken"\s+value="([^"]+)"/);
if (!csrfMatch) {
console.error('β Failed to extract CSRF token');
process.exit(1);
}
Object.assign(cookies, loginPageRes.cookies);
const csrfToken = csrfMatch[1];
// POST login
const loginRes = await makeRequest('POST', '/login/', {
username: TEST_USERNAME,
password: TEST_PASSWORD,
csrfmiddlewaretoken: csrfToken,
}, cookies);
Object.assign(cookies, loginRes.cookies);
console.log('β
Logged in\n');
// Fetch published gamedays
console.log('2οΈβ£ Fetching published gamedays...');
const gamedaysRes = await makeRequest('GET', '/api/gamedays/?status=PUBLISHED', null, cookies);
let gamedaysData;
try {
gamedaysData = JSON.parse(gamedaysRes.body);
} catch {
console.error('β Failed to parse gamedays response');
process.exit(1);
}
if (!gamedaysData.results || gamedaysData.results.length === 0) {
console.error('β No published gamedays found');
process.exit(1);
}
console.log(`β
Found ${gamedaysData.results.length} published gameday(s)\n`);
// Find first gameday with fresh games (Geplant status, no events yet)
let selectedGameday = null;
let gamesData = null;
console.log('3οΈβ£ Looking for a gameday with fresh games (Geplant)...');
for (const gameday of gamedaysData.results) {
// Skip if gameday is finished
if (gameday.status === 'beendet') continue;
const gamesRes = await makeRequest('GET', `/api/gamedays/${gameday.id}/games/`, null, cookies);
let games;
try {
games = JSON.parse(gamesRes.body);
} catch {
continue;
}
// Find games that are ready (Geplant) with no events recorded
const freshGames = Array.isArray(games)
? games.filter(g => g.status === 'Geplant' && (!g.gameStarted || g.gameStarted === null))
: [];
if (freshGames.length > 0) {
selectedGameday = gameday;
gamesData = freshGames;
break;
}
}
if (!selectedGameday || !gamesData) {
console.error('β No non-finished gamedays with games found');
process.exit(1);
}
console.log(`β
Found gameday: ${selectedGameday.name}`);
console.log(` Date: ${selectedGameday.date}`);
console.log(` Status: ${selectedGameday.status}\n`);
console.log(`4οΈβ£ Games in this gameday: ${gamesData.length}\n`);
// Display games
console.log('π Available Games:');
console.log('β'.repeat(80));
gamesData.slice(0, 5).forEach((game, i) => {
const home = game.results?.find(r => r.isHome);
const away = game.results?.find(r => !r.isHome);
console.log(`\n${i + 1}. Game ID: ${game.id}`);
console.log(` ${home?.team_name || 'Home'} vs ${away?.team_name || 'Away'}`);
console.log(` Time: ${game.scheduled}, Field: ${game.field}`);
console.log(` Status: ${game.status}`);
});
if (gamesData.length > 5) {
console.log(`\n... and ${gamesData.length - 5} more game(s)`);
}
console.log('\n' + 'β'.repeat(80));
console.log('\nπ Run E2E Test:');
const gameId = gamesData[0].id;
const gamedayId = selectedGameday.id;
console.log(` node e2e-game-test.js --gameid=${gameId} --gamedayid=${gamedayId} --password=YOUR_PASSWORD\n`);
}
main().catch(err => {
console.error('Error:', err.message);
process.exit(1);
});