.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!")}})
.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,megamix=$16,futuretone=$17 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,(songdata.mega39s||userObj.megamix),(songdata.futuretone&&!songdata.mega39s)||userObj.futuretone])})
db.query('select * from userupdate where userid=$1',[req.params.userid])
.then((data)=>{
if(data.rows.length>0){
res.status(200).json(data.rows[0])
}else{
res.status(400).send("No user update found!")
}
})
.catch((err)=>{
res.status(500).send(err.message)
})
})
app.post('/updates/:userid',(req,res)=>{
if(req.body&&req.body.password&&req.body.type){
if(req.body.password===process.env.GMAIL){
db.query("insert into userupdate(userid,update_type,date) values($1,$2,$3) on conflict(userid) do update set update_type=$2,date=$3 where userupdate.userid=$1 returning *",[req.params.userid,req.body.type,moment()])
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 megamix,futuretone,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])
db.query('select id,megamix,futuretone,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){
@ -799,7 +844,7 @@ function GetUserInfo(username) {
returndb.query("select id,username,email,code_time from users where username=$1 limit 1",[username])
}
functionGetUserLoginAllowed(username,authCode){
returndb.query("select id,username,email,code_time,playstyle,twitter_name from users where username=$1 and code=$2 limit 1",[username,authCode])
returndb.query("select id,username,email,code_time,playstyle,twitter_name,twitch_name from users where username=$1 and code=$2 limit 1",[username,authCode])