db.query("select id,authentication_token,playcount,fccount,cool,fine,safe,sad,worst,eclear,nclear,hclear,exclear,exexclear from users where username=$1 limit 1",[req.body.username])
.then((data)=>{if(data&&data.rows.length>0){userObj=data.rows[0];if(req.body.authentication_token===userObj.authentication_token){returndb.query("delete from plays where id=$1 and userid=$2 returning *",[req.body.playid,userObj.id])}else{thrownewError("Could not authenticate user!")}}else{thrownewError("Cannot find user!")}})
.then((data)=>{if(data&&data.rows.length>0){songObj=data.rows[0];returnCalculateRating(req.body.username)}else{thrownewError("Could not find play!")}})
.then((data)=>{rating=data;returndb.query("select * from plays where songid=$1 and userid=$2 and difficulty=$3 limit 1",[songObj.songid,userObj.id,songObj.difficulty])})
.then((data)=>{if(data&&data.rows.length===0){isFirstClear=true;}/*console.log([data,userObj.playcount-1,(songObj.safe==0&&songObj.sad==0&&songObj.worst==0)?userObj.fccount-1:userObj.fccount,userObj.cool-songObj.cool,userObj.fine-songObj.fine,userObj.safe-songObj.safe,userObj.sad-songObj.sad,userObj.worst-songObj.worst,(songObj.difficulty=="E")?userObj.ecount-1:userObj.ecount,(songObj.difficulty=="N")?userObj.ncount-1:userObj.ncount,(songObj.difficulty=="H")?userObj.hcount-1:userObj.hcount,(songObj.difficulty=="EX")?userObj.excount-1:userObj.excount,(songObj.difficulty=="EXEX")?userObj.exexcount-1:userObj.exexcount]);*/returndb.query("update users set rating=$1,playcount=$2,fccount=$3,cool=$4,fine=$5,safe=$6,sad=$7,worst=$8,eclear=$9,nclear=$10,hclear=$11,exclear=$12,exexclear=$13 where id=$14 returning rating,playcount,fccount,cool,fine,safe,sad,worst,eclear,nclear,hclear,exclear,exexclear",[rating,userObj.playcount-1,(songObj.safe==0&&songObj.sad==0&&songObj.worst==0)?userObj.fccount-1:userObj.fccount,userObj.cool-songObj.cool,userObj.fine-songObj.fine,userObj.safe-songObj.safe,userObj.sad-songObj.sad,userObj.worst-songObj.worst,(songObj.difficulty=="E"&&isFirstClear)?userObj.eclear-1:userObj.eclear,(songObj.difficulty=="N"&&isFirstClear)?userObj.nclear-1:userObj.nclear,(songObj.difficulty=="H"&&isFirstClear)?userObj.hclear-1:userObj.hclear,(songObj.difficulty=="EX"&&isFirstClear)?userObj.exclear-1:userObj.exclear,(songObj.difficulty=="EXEX"&&isFirstClear)?userObj.exexclear-1:userObj.exexclear,userObj.id])})
.then((data)=>{if(data&&data.rows.length>0){res.status(200).json({user:data.rows[0],song:songObj})}else{thrownewError("Could not update user information, but song is deleted!")}})
db.query("select id,authentication_token,playcount,fccount,cool,fine,safe,sad,worst,eclear,nclear,hclear,exclear,exexclear from users where username=$1 limit 1",[req.body.username])
fccount=data.rows[0].fccount;playcount=data.rows[0].playcount;userId=data.rows[0].id;returndb.query("select id from songs where name=$1 or romanized_name=$1 or english_name=$1 limit 1",[req.body.song])}else{thrownewError("Could not authenticate!")}}else{thrownewError("Could not find user.")}
.then((data)=>{if(data&&data.rows.length>0){songId=data.rows[0].id;returndb.query('select rating from songdata where songid=$1 and difficulty=$2 limit 1',[songId,req.body.difficulty])}else{thrownewError("Could not find song.")}})
.then((data)=>{songRating=data.rows[0].rating;returndb.query("select id from plays where userid=$1 and score>0 and difficulty=$2 and songid=$3 limit 1",[userId,req.body.difficulty,songId])})
returnCalculateRating(req.body.username)}else{thrownewError("Could not submit song.")}})
.then((data)=>{returndb.query("update users set rating=$1,last_played=$3,playcount=$4,fccount=$5,cool=$6,fine=$7,safe=$8,sad=$9,worst=$10,eclear=$11,nclear=$12,hclear=$13,exclear=$14,exexclear=$15 where username=$2",[data,req.body.username,newDate(),++playcount,fccount+((isFC)?1:0),cool+Number(req.body.cool),fine+Number(req.body.fine),safe+Number(req.body.safe),sad+Number(req.body.sad),worst+Number(req.body.worst),eclear,nclear,hclear,exclear,exexclear])})
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}})
.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.2,i))+"\n";/*console.log("Play score:"+play.score+". Sum:"+sum);*/returnsum+play.score*Math.pow(0.2,i);},0);debugScoreList+=" "+songs[song.id-1].score+"\n";}})}))}})
db.query('select cool,fine,safe,sad,worst from users where username=$1',[req.params.username])
.then((data)=>{if(data.rows.length>0){returnCalculateAccuracy(data.rows[0].cool,data.rows[0].fine,data.rows[0].safe,data.rows[0].sad,data.rows[0].worst)}else{thrownewError("User does not exist!")}})
returndb.query('select rating from songdata where songid=$1 and difficulty=$2 limit 1',[song.songid,song.difficulty])
}else{thrownewError("This play does not exist!")}})
.then((data)=>{if(data.rows.length>0){songRating=data.rows[0].rating;varscore=CalculateSongScore({rating:songRating,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("Failed to retrieve song data!")}})
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])})
db.query('select id from users where username=$1 limit 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 limit 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;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{res.status(400).json("Could not find song!")}})
db.query('select playcount,fccount,rating,last_played,cool,fine,safe,sad,worst,eclear,nclear,hclear,exclear,exexclear from users where username=$1 limit 1',[req.params.username])
db.query('select id from users where username=$1 limit 1',[req.params.username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;returndb.query('select id from songs where name=$1 or romanized_name=$1 or english_name=$1 limit 1',[req.params.songname])}else{thrownewError("Cannot find user!")}})
.then((data)=>{if(req.params.songname&&data.rows.length>0){songId=data.rows[0].id;returndb.query('select * from plays where userid=$1 and songid=$2 and difficulty=$3 order by score desc',[userId,songId,req.params.difficulty])}else{res.status(400).json("Could not find song!")}})
db.query('select id from users where username=$1 limit 1',[req.params.username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;returndb.query('select id from songs where name=$1 or romanized_name=$1 or english_name=$1 limit 1',[req.params.songname])}else{thrownewError("Cannot find user!")}})
.then((data)=>{if(req.params.songname&&data.rows.length>0){songId=data.rows[0].id;returndb.query('select * from plays where userid=$1 and songid=$2 and difficulty=$3 and score>0',[userId,songId,req.params.difficulty])}else{res.status(400).json("Could not find song!")}})
db.query('select id from users where username=$1 limit 1',[req.params.username])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;returndb.query('select id from songs where name=$1 or romanized_name=$1 or english_name=$1 limit 1',[req.params.songname])}else{thrownewError("Cannot find user!")}})
.then((data)=>{if(req.params.songname&&data.rows.length>0){songId=data.rows[0].id;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{res.status(400).json("Could not find song!")}})
db.query('select username,rating,last_played,playcount,fccount from users order by '+req.params.orderby+' '+req.params.sortorder+",rating desc limit $1 offset $2",[req.query.limit,req.query.offset])