From d192a02cc51bb087a2f8664fcd8f1830e961c5ff Mon Sep 17 00:00:00 2001 From: root Date: Mon, 25 May 2026 09:27:31 +0000 Subject: [PATCH] feat: match_type (ranked/friendly) + /api/matches/:id/type endpoint --- db.cjs | 13 +++++++++---- server.js | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/db.cjs b/db.cjs index ba50555..3d72ac7 100644 --- a/db.cjs +++ b/db.cjs @@ -75,6 +75,9 @@ function migrateSchema() { CREATE INDEX IF NOT EXISTS idx_matchp_match ON match_players(match_id); CREATE INDEX IF NOT EXISTS idx_matchp_player ON match_players(player_id); + -- Migration: match type kolom + ALTER TABLE matches ADD COLUMN match_type TEXT NOT NULL DEFAULT 'friendly'; + CREATE TABLE IF NOT EXISTS sessions ( id TEXT PRIMARY KEY, player_id TEXT REFERENCES players(id) ON DELETE SET NULL, @@ -249,8 +252,8 @@ function addMatch(match) { const db = getDb(); const id = match.id || String(Date.now()); const stmt = db.prepare(` - INSERT INTO matches (id, status, responses, proposed_at, date, start, end, location, day, score, booker_id, booker_name) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO matches (id, status, responses, proposed_at, date, start, end, location, day, score, booker_id, booker_name, match_type) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `); stmt.run(id, match.status || 'proposed', JSON.stringify(match.responses || {}), @@ -260,7 +263,8 @@ function addMatch(match) { match.day || null, typeof match.score === 'object' ? JSON.stringify(match.score) : (match.score || null), match.booker_id || null, - match.booker_name || null); + match.booker_name || null, + match.match_type || 'friendly'); if (Array.isArray(match.players)) { const pstmt = db.prepare(`INSERT INTO match_players (match_id, player_id, team, score) VALUES (?, ?, ?, ?)`); @@ -286,7 +290,7 @@ function updateMatch(id, match) { // Build SET dynamically to only update provided fields const fields = []; const values = []; - for (const key of ['status', 'responses', 'proposed_at', 'date', 'start', 'end', 'location', 'day', 'score', 'booker_id', 'booker_name', 'proposed_teams']) { + for (const key of ['status', 'responses', 'proposed_at', 'date', 'start', 'end', 'location', 'day', 'score', 'booker_id', 'booker_name', 'proposed_teams', 'match_type']) { if (match[key] !== undefined) { fields.push(`${key}=?`); let val = merged[key]; @@ -463,6 +467,7 @@ function rowToMatch(r) { day: r.day, score: score, proposed_teams: proposed_teams, + match_type: r.match_type || 'friendly', booker_id: r.booker_id || null, booker_name: r.booker_name || null, players: players diff --git a/server.js b/server.js index 13914e5..43d9e7b 100644 --- a/server.js +++ b/server.js @@ -383,7 +383,20 @@ app.get("/api/matches/:id/teams", (req, res) => { const m = db.getMatch(String(req.params.id)); if (!m) return res.status(404).json({ error: "match not found" }); const teams = m.proposed_teams || null; - res.json({ teams, match: { id: m.id, date: m.date, start: m.start, status: m.status } }); + res.json({ teams, match: { id: m.id, date: m.date, start: m.start, status: m.status, match_type: m.match_type || 'friendly' } }); +}); + +// ─── MATCH TYPE (ranked/friendly) ─── +app.post("/api/matches/:id/type", (req, res) => { + let m = db.getMatch(String(req.params.id)); + if (!m) return res.status(404).json({ error: "match not found" }); + const { match_type } = req.body; + if (match_type !== 'ranked' && match_type !== 'friendly') + return res.status(400).json({ error: "match_type must be 'ranked' or 'friendly'" }); + db.updateMatch(String(req.params.id), { match_type }); + m = db.getMatch(String(req.params.id)); + const playerIds = (m.players || []).map(p => p.id); + res.json({ ...m, players: playerIds, players_array: m.players }); }); // ─── SCORES ───