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) {
if (vote < 0 || vote > 100) return errorHandler(res, 400, 'Vote must be between 0 and 100');
// check if player exists (tmio API check)
tmioPlayer.player(player).then((playerData)=>{
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);
errorHandler(res, 500, 'Internal server error');
} else {
const voteDate = new Date();
if (sqlRes.length === 0) { // Create a new vote for this player
sql.query('INSERT INTO votes (map, player, vote, date) VALUES (?, ?, ?, ?)', [mapId, player, vote, voteDate], (err)=>{
if (err) {
console.error(err);
errorHandler(res, 500, 'Internal server error');
} else {
res.json({
mapUid: mapId,
player: player,
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) {
console.error(err);
errorHandler(res, 500, 'Internal server error');
} else {
res.json({
mapUid: mapId,
player: player,
vote: vote,
date: voteDate,
type: "update"
});
}
});
tmioPlayer.player(player).then(()=>{
sql.query('SELECT * FROM votes WHERE map = ? AND player = ?', [mapId,player], (err, sqlRes)=>{
if (err) {
console.log(err);
errorHandler(res, 500, 'Internal server error');
} else {
const voteDate = new Date();
if (sqlRes.length === 0) { // Create a new vote for this player
sql.query('INSERT INTO votes (map, player, vote, date) VALUES (?, ?, ?, ?)', [mapId, player, vote, voteDate], (err)=>{
if (err) {
console.error(err);
errorHandler(res, 500, 'Internal server error');
} else {
res.json({
mapUid: mapId,
player: player,
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) {
console.error(err);
errorHandler(res, 500, 'Internal server error');
} else {
res.json({
mapUid: mapId,
player: player,
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