DB link, place teams in brackets
This commit is contained in:
parent
08e328b8e4
commit
bfd5df5e67
|
@ -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) => {
|
||||||
|
|
|
@ -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 (
|
||||||
|
|
|
@ -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 (err) {
|
if (match.team1Id == null) {
|
||||||
console.log(err);
|
console.log("Assigning team " + teamId + " to match " + match.id + " as team 1");
|
||||||
reject(err);
|
connection.query("UPDATE matches SET team1Id = ? WHERE id = ?", [escapeString(teamId), escapeString(match.id)], (err, sets) => {
|
||||||
} else {
|
if (err) {
|
||||||
resolve(sets);
|
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");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #endregion
|
||||||
|
|
Loading…
Reference in New Issue