Commit c857d223 authored by Matthieu - Greep's avatar Matthieu - Greep

Fix player check

parent bdb7089f
Pipeline #460 passed with stage
in 51 seconds
...@@ -20,49 +20,48 @@ module.exports = function(app, sql, tokenCheck, errorHandler) { ...@@ -20,49 +20,48 @@ module.exports = function(app, sql, tokenCheck, errorHandler) {
if (vote < 0 || vote > 100) return errorHandler(res, 400, 'Vote must be between 0 and 100'); if (vote < 0 || vote > 100) return errorHandler(res, 400, 'Vote must be between 0 and 100');
// check if player exists (tmio API check) // check if player exists (tmio API check)
tmioPlayer.player(player).then((playerData)=>{ tmioPlayer.player(player).then(()=>{
if (playerData.error) return errorHandler(res, 400, playerData.error); sql.query('SELECT * FROM votes WHERE map = ? AND player = ?', [mapId,player], (err, sqlRes)=>{
}); if (err) {
console.log(err);
sql.query('SELECT * FROM votes WHERE map = ? AND player = ?', [mapId,player], (err, sqlRes)=>{ errorHandler(res, 500, 'Internal server error');
if (err) { } else {
console.log(err); const voteDate = new Date();
errorHandler(res, 500, 'Internal server error'); if (sqlRes.length === 0) { // Create a new vote for this player
} else { sql.query('INSERT INTO votes (map, player, vote, date) VALUES (?, ?, ?, ?)', [mapId, player, vote, voteDate], (err)=>{
const voteDate = new Date(); if (err) {
if (sqlRes.length === 0) { // Create a new vote for this player console.error(err);
sql.query('INSERT INTO votes (map, player, vote, date) VALUES (?, ?, ?, ?)', [mapId, player, vote, voteDate], (err)=>{ errorHandler(res, 500, 'Internal server error');
if (err) { } else {
console.error(err); res.json({
errorHandler(res, 500, 'Internal server error'); mapUid: mapId,
} else { player: player,
res.json({ vote: vote,
mapUid: mapId, date: voteDate,
player: player, type: "new"
vote: vote, });
date: voteDate, }
type: "new" });
}); } else { // Update the vote for this player
} sql.query('UPDATE votes SET vote = ?, date = ? WHERE map = ? AND player = ?', [vote, voteDate, mapId, player], (err)=>{
}); if (err) {
} else { // Update the vote for this player console.error(err);
sql.query('UPDATE votes SET vote = ?, date = ? WHERE map = ? AND player = ?', [vote, voteDate, mapId, player], (err)=>{ errorHandler(res, 500, 'Internal server error');
if (err) { } else {
console.error(err); res.json({
errorHandler(res, 500, 'Internal server error'); mapUid: mapId,
} else { player: player,
res.json({ vote: vote,
mapUid: mapId, date: voteDate,
player: player, type: "update"
vote: vote, });
date: voteDate, }
type: "update" });
}); }
}
});
} }
} });
}); })
.catch((err)=>errorHandler(res, 400, err)); // player not found
//}); //});
}); });
}; };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment