Improved logic for song ratings and perform recalculation of player ratings on song submission along with score rechecks. Player ratings now stored in database for quick lookups.
.then((data)=>{if(data.rows.length>0){if(data.rows[0].authentication_token===req.body.authentication_token){userId=data.rows[0].id;returndb.query("select id from songs where name=$1 or romanized_name=$1 or english_name=$1",[req.body.song])}else{thrownewError("Could not authenticate!")}}else{thrownewError("Could not find user.")}
.then((data)=>{if(data.rows.length>0){if(data.rows[0].authentication_token===req.body.authentication_token){userId=data.rows[0].id;returndb.query("select id from songs where name=$1 or romanized_name=$1 or english_name=$1",[req.body.song])}else{thrownewError("Could not authenticate!")}}else{thrownewError("Could not find user.")}
})
})
.then((data)=>{if(data.rows.length>0){songId=data.rows[0].id;varscore=CalculateSongScore({cool:req.body.cool,fine:req.body.fine,safe:req.body.safe,sad:req.body.sad,worst:req.body.worst,percent:req.body.percent,difficulty:req.body.difficulty,fail:fail});returndb.query("insert into plays(songId,userId,difficulty,cool,fine,safe,sad,worst,percent,date,score,fail) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) returning *",[songId,userId,req.body.difficulty,req.body.cool,req.body.fine,req.body.safe,req.body.sad,req.body.worst,req.body.percent,newDate(),score,fail])}else{thrownewError("Could not find song.")}})
.then((data)=>{if(data.rows.length>0){songId=data.rows[0].id;varscore=CalculateSongScore({cool:req.body.cool,fine:req.body.fine,safe:req.body.safe,sad:req.body.sad,worst:req.body.worst,percent:req.body.percent,difficulty:req.body.difficulty,fail:fail});returndb.query("insert into plays(songId,userId,difficulty,cool,fine,safe,sad,worst,percent,date,score,fail) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) returning *",[songId,userId,req.body.difficulty,req.body.cool,req.body.fine,req.body.safe,req.body.sad,req.body.worst,req.body.percent,newDate(),score,fail])}else{thrownewError("Could not find song.")}})
.then((data)=>{if(data.rows.length>0){res.status(200).json(data.rows[0])}else{thrownewError("Could not submit song.")}})
.then((data)=>{if(data.rows.length>0){
varsongsubmitdata=data.rows[0];
returnCalculateRating(req.body.username).then((data)=>{db.query("update users set rating=$1 where username=$2",[data,req.body.username])})
.then(()=>{returnsongsubmitdata;})}else{thrownewError("Could not submit song.")}})
.then((data)=>{res.status(200).json(data);})
.catch((err)=>{
.catch((err)=>{
console.log(req.body);
console.log(req.body);
res.status(500).json(err.message);})
res.status(500).json(err.message);})
@ -121,9 +125,16 @@ CalculateRating=(username)=>{
}
}
app.get('/recalculatescore/:playid',(req,res)=>{
app.get('/recalculatescore/:playid',(req,res)=>{
varuserId=-1;
varusername=null;
db.query('select * from plays where id=$1',[req.params.playid])
db.query('select * from plays where id=$1',[req.params.playid])
.then((data)=>{if(data.rows.length>0){varsong=data.rows[0];console.log(song);varscore=CalculateSongScore({cool:song.cool,fine:song.fine,safe:song.safe,sad:song.sad,worst:song.worst,percent:song.percent,difficulty:song.difficulty,fail:song.fail});returndb.query('update plays set score=$1 where id=$2 returning *',[score,req.params.playid]);}else{thrownewError("This play does not exist!")}})
.then((data)=>{if(data.rows.length>0){varsong=data.rows[0];userId=song.userid;console.log(song);varscore=CalculateSongScore({cool:song.cool,fine:song.fine,safe:song.safe,sad:song.sad,worst:song.worst,percent:song.percent,difficulty:song.difficulty,fail:song.fail});returndb.query('update plays set score=$1 where id=$2 returning *',[score,req.params.playid]);}else{thrownewError("This play does not exist!")}})
.then((data)=>{if(data.rows.length>0){res.status(200).json(data.rows[0])}else{thrownewError("Failed to update score!")}}).catch((err)=>{res.status(500).json(err.message);})
.then((data)=>{if(data.rows.length>0){
varscoreData=data.rows[0];
returndb.query('select username from users where id=$1',[userId]).then((data)=>{username=data.rows[0].username;returnCalculateRating(username)}).then((data)=>{db.query("update users set rating=$1 where username=$2",[data,username])})