diff --git a/src/server/index.js b/src/server/index.js index bc96a80..f10e519 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -59,8 +59,9 @@ api.get("/tournament/:tournamentId", (req, res) => { return; } tmdb.getTournament(parseInt(tournamentId)) - .catch(err => res.json({"status": "error", "data": err})) - .then(tournament => res.json({"status": "OK", "data": tournament})); + .then(tournament => res.json({"status": "OK", "data": tournament})) + .catch(err => res.json({"status": "error", "data": err})); + }); api.get("/tournament/:tournamentId/getMatches", (req, res) => { diff --git a/src/server/management/initDB.sql b/src/server/management/initDB.sql index 73ae385..c33966f 100644 --- a/src/server/management/initDB.sql +++ b/src/server/management/initDB.sql @@ -34,7 +34,8 @@ CREATE TABLE matches ( FOREIGN KEY (tournamentId) REFERENCES tournaments (id), FOREIGN KEY (team1Id) REFERENCES teams (id), - FOREIGN KEY (team2Id) REFERENCES teams (id) + FOREIGN KEY (team2Id) REFERENCES teams (id), + FOREIGN KEY (winnerId) REFERENCES teams (id) ); CREATE TABLE players ( diff --git a/src/server/tmdb.js b/src/server/tmdb.js index 131f46f..fd271f1 100644 --- a/src/server/tmdb.js +++ b/src/server/tmdb.js @@ -299,30 +299,57 @@ async function createTeam(tournamentId, name) { return; } - connection.query("INSERT INTO teams (tournamentId, name) VALUES (?, ?)", [escapeString(tournamentId), escapeString(name)], (err, sets) => { + connection.query("INSERT INTO teams (tournamentId, name) VALUES (?, ?)", [escapeString(tournamentId), escapeString(name)], async (err, sets) => { if (err) { console.log(err); reject(err); } else { + await assignFirstMatch(sets.insertId, tournamentId); resolve({message: "Team created", teamId: sets.insertId}); } }); }); } -// #endregion -// Dangerous function, use with caution. -// Used to initialize and manage the database by management tools, not by the main application. -function executeStatement(statement) { +//Private function, assigns a starting match to the given team +async function assignFirstMatch(teamId, tournamentId) { + let tournament = await getTournament(tournamentId); + let matches = await getMatchesByTournamentId(tournamentId); + + let highTier = Math.log2(tournament.teamLimit)-1; + console.log(highTier); + let highTierMatches = matches.filter(match => match.tier == highTier); + console.log(matches); + return new Promise(function(resolve, reject) { - connection.query(statement, (err, sets) => { - if (err) { - console.log(err); - reject(err); - } else { - resolve(sets); + for (let match of highTierMatches) { + if (match.team1Id == null) { + console.log("Assigning team " + teamId + " to match " + match.id + " as team 1"); + connection.query("UPDATE matches SET team1Id = ? WHERE id = ?", [escapeString(teamId), escapeString(match.id)], (err, sets) => { + if (err) { + console.log(err); + reject(err); + } else { + resolve("Team assigned to match " + match.id); + } + }); + return + } else if (match.team2Id == null) { + console.log("Assigning team " + teamId + " to match " + match.id + " as team 2"); + connection.query("UPDATE matches SET team2Id = ? WHERE id = ?", [escapeString(teamId), escapeString(match.id)], (err, sets) => { + if (err) { + console.log(err); + reject(err); + } else { + resolve("Team assigned to match " + match.id); + } + }); + return } - }); + } + reject("Could not assign team to any matches"); }); -} \ No newline at end of file +} + +// #endregion