.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!")}})
.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])
db.query("select id,authentication_token,playcount,fccount,cool,fine,safe,sad,worst,eclear,nclear,hclear,exclear,exexclear,playstyle 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.")}
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)=>{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])})
.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])})
.then((data)=>{if(data&&data.rows.length>0){alreadyPassed=true;/*console.log(data);*/};varscore=CalculateSongScore({rating:songRating,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,mod,combo,gamescore,src) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16) returning *",[songId,userId,req.body.difficulty,req.body.cool,req.body.fine,req.body.safe,req.body.sad,req.body.worst,req.body.percent,submitDate,score,fail,mod,combo,gameScore,(req.body.src)?req.body.src:""])})
.then((data)=>{if(data&&data.rows.length>0){alreadyPassed=true;/*console.log(data);*/};varscore=CalculateSongScore({rating:songRating,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,mod,combo,gamescore,src,playstyle) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17) returning *",[songId,userId,req.body.difficulty,req.body.cool,req.body.fine,req.body.safe,req.body.sad,req.body.worst,req.body.percent,submitDate,score,fail,mod,combo,gameScore,(req.body.src)?req.body.src:"",(playstyle)?playstyle:""])})
db.query('select cool,fine,safe,sad,worst from users where username=$1',[req.params.username])
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!")}})
.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 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])})
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])
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(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!")}})
.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).send("Could not find song!")}})
.then((data)=>{if(data&&data.rows.length>0){res.status(200).json(data.rows[0])}else{res.status(400).json("No data found!")}})
.then((data)=>{if(data&&data.rows.length>0){res.status(200).json(data.rows[0])}else{res.status(400).send("No data found!")}})
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 playstyle,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])
.then((data)=>{if(data&&data.rows.length>0){finalData=data.rows[0];returndb.query("select t.difficulty,COUNT(t.difficulty) from (select distinct on(songid) songid,*,users.id from plays join users on userid=users.id where users.username=$1 and plays.safe=0 and plays.worst=0 and plays.sad=0)t group by t.difficulty",[req.params.username])}else{thrownewError("Could not retrieve user data!")}})
.then((data)=>{if(data&&data.rows.length>0){finalData=data.rows[0];returndb.query("select t.difficulty,COUNT(t.difficulty) from (select distinct on(songid) songid,*,users.id from plays join users on userid=users.id where users.username=$1 and plays.safe=0 and plays.worst=0 and plays.sad=0)t group by t.difficulty",[req.params.username])}else{thrownewError("Could not retrieve user data!")}})
db.query('select id 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(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!")}})
.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).send("Could not find song!")}})
db.query('select id 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(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!")}})
.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).send("Could not find song!")}})
db.query('select id 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(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!")}})
.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).send("Could not find song!")}})
db.query('select id 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(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 fine=0 and safe=0 and sad=0 and worst=0',[userId,songId,req.params.difficulty])}else{res.status(400).json("Could not find song!")}})
.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 fine=0 and safe=0 and sad=0 and worst=0',[userId,songId,req.params.difficulty])}else{res.status(400).send("Could not find song!")}})
db.query('select id 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(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 COUNT(mod) from (select * from plays where userid=$1 and songid=$2 and difficulty=$3 and mod=$4)t',[userId,songId,req.params.difficulty,"HS"])}else{res.status(400).json("Could not find song!")}})
.then((data)=>{if(req.params.songname&&data.rows.length>0){songId=data.rows[0].id;returndb.query('select COUNT(mod) from (select * from plays where userid=$1 and songid=$2 and difficulty=$3 and mod=$4)t',[userId,songId,req.params.difficulty,"HS"])}else{res.status(400).send("Could not find song!")}})
db.query('select plays.* from plays join users on users.id=plays.userid where users.username=$1 order by plays.id desc limit 10',[req.params.username])
db.query('select plays.* from plays join users on users.id=plays.userid where users.username=$1 order by plays.id desc limit 10',[req.params.username])
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])
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])