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});returndb.query("insert into plays(songId,userId,difficulty,cool,fine,safe,sad,worst,percent,date,score) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11) 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])}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.")}})
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}})
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',[userId,songId,req.params.difficulty])}else{returndb.query('select * from plays where userid=$1 and songid=$2 order by score desc limit 1',[userId,songId])}}else{res.status(400).json("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!")}})
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!")}})