db.query("select id,authentication_token from users where username=$1",[req.body.username])
.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.")}})
returndb.query('select id from users where username=$1',[username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;returndb.query('select * from plays where userid=$1 order by score desc limit 100',[userId])}else{return0}})
returndb.query('select id from users where username=$1',[username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;returndb.query('select * from songs order by id asc;')}else{return0}})
.then((data)=>{if(data.rows.length>0){songs=data.rows;returnPromise.all(data.rows.map((song)=>{returndb.query('select * from plays where userId=$1 and songId=$2 order by score desc limit 100',[userId,song.id]).then((data)=>{if(data.rows.length>0){debugScoreList+=song.name+"\n";songs[song.id-1].score=data.rows.reduce((sum,play,i)=>{debugScoreList+=" "+(play.score)+" -> "+(play.score*Math.pow(0.8,i))+"\n";/*console.log("Play score:"+play.score+". Sum:"+sum);*/returnsum+play.score*Math.pow(0.8,i);},0);debugScoreList+=" "+songs[song.id-1].score+"\n";}})}))}})
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){res.status(200).json(data.rows[0])}else{thrownewError("Failed to update score!")}}).catch((err)=>{res.status(500).json(err.message);})
db.query('select id from users where username=$1',[req.params.username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;if(req.params.songname){returndb.query('select id from songs where name=$1 or romanized_name=$1 or english_name=$1',[req.params.songname])}else{returndb.query('select * from plays where userid=$1 order by score desc',[userId])}}else{thrownewError("Cannot find user!")}})
.then((data)=>{if(req.params.songname&&data.rows.length>0){songId=data.rows[0].id;if(req.params.difficulty){returndb.query('select * from plays where userid=$1 and songid=$2 and difficulty=$3 order by score desc,percent desc limit 1',[userId,songId,req.params.difficulty])}else{returndb.query('select * from plays where userid=$1 and songid=$2 order by score desc,percent desc limit 1',[userId,songId])}}else{res.status(400).json("Could not find song!")}})
db.query('select id from users where username=$1',[req.params.username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;if(req.params.songname){returndb.query('select id from songs where name=$1 or romanized_name=$1 or english_name=$1',[req.params.songname])}else{returndb.query('select * from plays where userid=$1',[userId])}}else{thrownewError("Cannot find user!")}})
.then((data)=>{if(req.params.songname&&data.rows.length>0){songId=data.rows[0].id;if(req.params.difficulty){returndb.query('select * from plays where userid=$1 and songid=$2 and difficulty=$3 order by score desc',[userId,songId,req.params.difficulty])}else{returndb.query('select * from plays where userid=$1 and songid=$2',[userId,songId])}}else{res.status(400).json("Could not find song!")}})
db.query('select id from users where username=$1',[req.params.username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;if(req.params.songname){returndb.query('select id from songs where name=$1 or romanized_name=$1 or english_name=$1',[req.params.songname])}else{returndb.query('select * from plays where userid=$1 and score>0',[userId])}}else{thrownewError("Cannot find user!")}})
.then((data)=>{if(req.params.songname&&data.rows.length>0){songId=data.rows[0].id;if(req.params.difficulty){returndb.query('select * from plays where userid=$1 and songid=$2 and difficulty=$3 and score>0',[userId,songId,req.params.difficulty])}else{returndb.query('select * from plays where userid=$1 and songid=$2 and score>0',[userId,songId])}}else{res.status(400).json("Could not find song!")}})
db.query('select id from users where username=$1',[req.params.username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;if(req.params.songname){returndb.query('select id from songs where name=$1 or romanized_name=$1 or english_name=$1',[req.params.songname])}else{returndb.query('select * from plays where userid=$1 and safe=0 and sad=0 and worst=0',[userId])}}else{thrownewError("Cannot find user!")}})
.then((data)=>{if(req.params.songname&&data.rows.length>0){songId=data.rows[0].id;if(req.params.difficulty){returndb.query('select * from plays where userid=$1 and songid=$2 and difficulty=$3 and safe=0 and sad=0 and worst=0',[userId,songId,req.params.difficulty])}else{returndb.query('select * from plays where userid=$1 and songid=$2 and safe=0 and sad=0 and worst=0',[userId,songId])}}else{res.status(400).json("Could not find song!")}})