DB link, place teams in brackets

This commit is contained in:
Felix Albrigtsen 2022-03-29 14:13:29 +02:00
parent 08e328b8e4
commit bfd5df5e67
3 changed files with 45 additions and 16 deletions

View File

@ -59,8 +59,9 @@ api.get("/tournament/:tournamentId", (req, res) => {
return; return;
} }
tmdb.getTournament(parseInt(tournamentId)) 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) => { api.get("/tournament/:tournamentId/getMatches", (req, res) => {

View File

@ -34,7 +34,8 @@ CREATE TABLE matches (
FOREIGN KEY (tournamentId) REFERENCES tournaments (id), FOREIGN KEY (tournamentId) REFERENCES tournaments (id),
FOREIGN KEY (team1Id) REFERENCES teams (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 ( CREATE TABLE players (

View File

@ -299,30 +299,57 @@ async function createTeam(tournamentId, name) {
return; 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) { if (err) {
console.log(err); console.log(err);
reject(err); reject(err);
} else { } else {
await assignFirstMatch(sets.insertId, tournamentId);
resolve({message: "Team created", teamId: sets.insertId}); resolve({message: "Team created", teamId: sets.insertId});
} }
}); });
}); });
} }
// #endregion
// Dangerous function, use with caution. //Private function, assigns a starting match to the given team
// Used to initialize and manage the database by management tools, not by the main application. async function assignFirstMatch(teamId, tournamentId) {
function executeStatement(statement) { 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) { return new Promise(function(resolve, reject) {
connection.query(statement, (err, 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) { if (err) {
console.log(err); console.log(err);
reject(err); reject(err);
} else { } else {
resolve(sets); 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");
}); });
} }
// #endregion