db.query('select songs.*,songdata.rating as rating,songdata.difficulty,songdata.notecount from songs left join songdata on songs.id=songdata.songid',(error,results)=>{
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 and score>0 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,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.")}
.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 and score!=$3 order by score desc limit 100',[userId,song.id,"NaN"]).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))+"";if(i===0&&play.fine+play.safe+play.sad+play.worst===0){songs[play.songid-1].pfc=true;debugScoreList+="+"}elseif(i===0&&play.safe+play.sad+play.worst===0){songs[play.songid-1].fc=true;debugScoreList+="*"}debugScoreList+="\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])})
promises.push(db.query("select * from (select userid,count(*) filter(where difficulty='E' and mod='SD' and score>0) as ESDCount,count(*) filter(where difficulty='N' and mod='SD' and score>0) as NSDCount,count(*) filter(where difficulty='H' and mod='SD' and score>0) as HSDCount,count(*) filter(where difficulty='EX' and mod='SD' and score>0) as EXHDCount,count(*) filter(where difficulty='E' and mod='HD' and score>0) as EHDCount,count(*) filter(where difficulty='N' and mod='HD' and score>0) as NHDCount,count(*) filter(where difficulty='H' and mod='HD' and score>0) as HHDCount,count(*) filter(where difficulty='EX' and mod='HD' and score>0) as EXHDCount,count(*) filter(where difficulty='E' and mod='HS' and score>0) as EHSCount,count(*) filter(where difficulty='N' and mod='HS' and score>0) as NHSCount,count(*) filter(where difficulty='H' and mod='HS' and score>0) as HHSCount,count(*) filter(where difficulty='EX' and mod='HS' and score>0) as EXHSCount,count(*) filter(where difficulty='EXEX' and mod='HS' and score>0) as EXEXHSCount,Count(*) filter(where difficulty='E' and score>0) as EClearCount,Count(*) filter(where difficulty='N' and score>0) as NClearCount,Count(*) filter(where difficulty='H' and score>0) as HClearCount,Count(*) filter(where difficulty='EX' and score>0) as EXClearCount,Count(*) filter(where difficulty='EXEX' and score>0) as EXEXClearCount,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])
.then((data)=>{if(data.rows.length>0){userId=data.rows[0].id;returndb.query("select * from (select distinct on (songid) * from plays where userid=$1 "+((req.query.fails==="false")?"and score!=0":"")+" order by songid,score desc)t order by score desc limit $2 offset $3",[userId,Number(limit),Number(offset)])}else{thrownewError("Cannot find user!")}})
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).send("Could not find song!")}})
.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 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!")}})
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.fine=0 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 plays.* from plays join users on users.id=plays.userid where users.username=$1 and plays.songid=$2 order by score desc,percent desc,date desc limit $3 offset $4",[req.params.username,req.params.songid,Number(limit),Number(offset)])
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).send("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).send("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).send("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 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])
.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).send("Could not find song!")}})
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"])
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])