//Number of passes,plays,fcs,pfcs, and the best play.
varuserId=-1,songs,promises=[];
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 * from songs')
}else{
thrownewError("Cannot find user!")
}
})
.then((data)=>{
songs=data.rows;
songs.forEach((song)=>{
promises.push(db.query("select * from (select userid,count(*) filter(where difficulty='E') as ECount,count(*) filter(where difficulty='N') as NCount,count(*) filter(where difficulty='H') as HCount,count(*) filter(where difficulty='EX') as EXCount,count(*) filter(where difficulty='EXEX') as EXEXCount,Count(*) filter(where safe=0 and sad=0 and worst=0 and difficulty='E') as EFCCount,Count(*) filter(where safe=0 and sad=0 and worst=0 and difficulty='N') as NFCCount,Count(*) filter(where safe=0 and sad=0 and worst=0 and difficulty='H') as HFCCount,Count(*) filter(where safe=0 and sad=0 and worst=0 and difficulty='EX') as EXFCCount,Count(*) filter(where safe=0 and sad=0 and worst=0 and difficulty='EXEX') as EXEXFCCount,Count(*) filter(where fine=0 and safe=0 and sad=0 and worst=0 and difficulty='E') as EPFCCount,Count(*) filter(where fine=0 and safe=0 and sad=0 and worst=0 and difficulty='N') as NPFCCount,Count(*) filter(where fine=0 and safe=0 and sad=0 and worst=0 and difficulty='H') as HPFCCount,Count(*) filter(where fine=0 and safe=0 and sad=0 and worst=0 and difficulty='EX') as EXPFCCount,Count(*) filter(where fine=0 and safe=0 and sad=0 and worst=0 and difficulty='EXEX') as EXEXPFCCount from plays where userid=$1 and songid=$2 group by userid)t1 join (select rank,t.score,t.percent from (select row_number()over(order by score desc)rank,* from(select distinct on (userid) * from (select * from plays where songid=$2)t order by userid,score desc)t)t where userid=$1)t2 on t1.userid=t1.userid",[userId,song.id])
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("Could not find user!")
}
})
.then((data)=>{
if(data.rows.length>0){
songId=data.rows[0].id
returndb.query("select * from (select row_number()over(order by score desc)rank,* from(select distinct on (userid) * from (select * from plays where songid=$1)t order by userid,score desc)t)t where userid=$2",[songId,userId])
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 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!")}})
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 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(data&&data.rows.length>0){
hs=data.rows[0].count;
}
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,"SD"])
})
.then((data)=>{if(data&&data.rows.length>0){
sd=data.rows[0].count;
}
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,"HD"])