From b62f376837e1647daac2bf722356d7f9ad95d84f Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 14 Jul 2020 17:24:20 -0500 Subject: [PATCH] Improved logic for song ratings and perform recalculation of player ratings on song submission along with score rechecks. Player ratings now stored in database for quick lookups. --- server/app.js | 23 +++++++++++++++++------ server/test.js | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 server/test.js diff --git a/server/app.js b/server/app.js index 0022338..15cebe4 100644 --- a/server/app.js +++ b/server/app.js @@ -66,7 +66,7 @@ app.post('/submit', (req, res) => { var fail = true; if (req.body.fail!==undefined) { fail = (req.body.fail=='true'); - console.log("Fail is "+fail+" type:"+typeof(fail)) + //console.log("Fail is "+fail+" type:"+typeof(fail)) } if (!(req.body.difficulty==="H"||req.body.difficulty==="N"||req.body.difficulty==="E"||req.body.difficulty==="EX"||req.body.difficulty==="EXEX")) @@ -76,7 +76,11 @@ app.post('/submit', (req, res) => { .then((data)=>{if(data.rows.length>0){if (data.rows[0].authentication_token===req.body.authentication_token){userId=data.rows[0].id;return db.query("select id from songs where name=$1 or romanized_name=$1 or english_name=$1",[req.body.song])}else{throw new Error("Could not authenticate!")}}else{throw new Error("Could not find user.")} }) .then((data)=>{if(data.rows.length>0){songId=data.rows[0].id;var score=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,fail:fail});return db.query("insert into plays(songId,userId,difficulty,cool,fine,safe,sad,worst,percent,date,score,fail) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) returning *",[songId,userId,req.body.difficulty,req.body.cool,req.body.fine,req.body.safe,req.body.sad,req.body.worst,req.body.percent,new Date(),score,fail])}else{throw new Error("Could not find song.")}}) - .then((data)=>{if(data.rows.length>0){res.status(200).json(data.rows[0])}else{throw new Error("Could not submit song.")}}) + .then((data)=>{if(data.rows.length>0){ + var songsubmitdata = data.rows[0]; + return CalculateRating(req.body.username).then((data)=>{db.query("update users set rating=$1 where username=$2",[data,req.body.username])}) + .then(()=>{return songsubmitdata;})}else{throw new Error("Could not submit song.")}}) + .then((data)=>{res.status(200).json(data);}) .catch((err)=>{ console.log(req.body); res.status(500).json(err.message);}) @@ -121,9 +125,16 @@ CalculateRating=(username)=>{ } app.get('/recalculatescore/:playid',(req,res)=>{ + var userId=-1; + var username=null; db.query('select * from plays where id=$1',[req.params.playid]) - .then((data)=>{if (data.rows.length>0){var song=data.rows[0];console.log(song);var score=CalculateSongScore({cool:song.cool,fine:song.fine,safe:song.safe,sad:song.sad,worst:song.worst,percent:song.percent,difficulty:song.difficulty,fail:song.fail});return db.query('update plays set score=$1 where id=$2 returning *',[score,req.params.playid]);}else{throw new Error("This play does not exist!")}}) - .then((data)=>{if (data.rows.length>0){res.status(200).json(data.rows[0])}else{throw new Error("Failed to update score!")}}) .catch((err)=>{res.status(500).json(err.message);}) + .then((data)=>{if (data.rows.length>0){var song=data.rows[0];userId=song.userid;console.log(song);var score=CalculateSongScore({cool:song.cool,fine:song.fine,safe:song.safe,sad:song.sad,worst:song.worst,percent:song.percent,difficulty:song.difficulty,fail:song.fail});return db.query('update plays set score=$1 where id=$2 returning *',[score,req.params.playid]);}else{throw new Error("This play does not exist!")}}) + .then((data)=>{if (data.rows.length>0){ + var scoreData=data.rows[0]; + return db.query('select username from users where id=$1',[userId]).then((data)=>{username=data.rows[0].username; return CalculateRating(username)}).then((data)=>{db.query("update users set rating=$1 where username=$2",[data,username])}) + .then(()=>{return scoreData;}) + }else{throw new Error("Failed to update score!")}}) + .then((data)=>res.status(200).json(data)).catch((err)=>{res.status(500).json(err.message);}) }); app.get('/bestplay/:username/:songname/:difficulty',(req,res)=>{ @@ -164,8 +175,8 @@ app.get('/songfccount/:username/:songname/:difficulty',(req,res)=>{ app.get('/rating/:username',(req,res)=>{ if (req.params.username) { - CalculateRating(req.params.username). - then((data)=>res.status(200).json({rating:data})) + db.query('select rating from users where username=$1',[req.params.username]) + .then((data)=>{if(data.rows.length>0){res.status(200).json(data.rows[0])}else{res.status(200).json({rating:0})}}) } else { res.status(400).json("Invalid username!") } diff --git a/server/test.js b/server/test.js new file mode 100644 index 0000000..1f9fbd6 --- /dev/null +++ b/server/test.js @@ -0,0 +1,24 @@ +var characterFrequency = function (string) { + let tempArray=[...string]; // stores [ 'f', 'u', 'n' ] + let finalArray = [] + console.log(JSON.stringify(tempArray)+"/"+JSON.stringify(finalArray)) + for (var i=0; i, [ 'f', 1 ], [ 'u', 1 ], [ 'n', 1 ] ] + if (tempArray[i] === finalArray[x][0]){ //always fails + // if (tempArray[i] == finalArray[x][0]){ //breaks it but seems like the right answer + console.log(finalArray[x][1]) + finalArray[x][1]+=1 + found=true; + } + } + if (!found) { + finalArray.push([tempArray[i],1]) + } + } + return finalArray +}; + +console.log(characterFrequency("Testing more letters!")); \ No newline at end of file