Advertisement
Guest User

ScoreHandling

a guest
Dec 1st, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function saveScoreToCookie()
  4. {
  5.     var scores = getAllScores();
  6.  
  7.     //set a new cookie with the same name as the previous cookie
  8.     //which means that it overwrites the previous cookie
  9.     //the value will be the previous values + the new value
  10.     document.cookie = "sudokuScores=" + scores + sudokuScore + ",;";
  11. }
  12.  
  13. function scoresToInt(scores)
  14. {
  15.     var intScores = [];
  16.  
  17.     scores = scores.split(",");
  18.  
  19.     for (var i = 0; i < scores.length - 1; i++)
  20.     {
  21.         intScores[i] = parseInt(scores[i]);
  22.     }
  23.  
  24.     return intScores;
  25. }
  26.  
  27. function getAllScores()
  28. {
  29.     var scoresCookie = getCookie("sudokuScores");
  30.  
  31.     //split the cookie
  32.     //scoresCookie[0] is cookie name
  33.     //scoresCookie[1] is the current value
  34.     var scores = scoresCookie.split("=")[1];
  35.  
  36.     return scores;
  37. }
  38.  
  39. function getCookie(name)
  40. {
  41.     //get all current cookies
  42.     var allCookies = document.cookie;
  43.     //split the cookies into an array with ; as the delimiter
  44.     var allCookiesArray = allCookies.split(";");
  45.  
  46.     var searchedCookie;
  47.     //iterate through the cookies array
  48.     for (var i = 0; i < allCookiesArray.length; i++)
  49.     {  
  50.         //if the currently iterated cookies name is sudokuScores
  51.         if (allCookiesArray[i].indexOf(name) > -1)
  52.         {
  53.             //set currentCookies to only the sudokuScores cookie
  54.             searchedCookie = allCookiesArray[i];
  55.             break;
  56.         }
  57.     }
  58.  
  59.     return searchedCookie;
  60. }
  61.  
  62. function printScoresGraph(scores){
  63.     var scores = getAllScores();
  64.    
  65.     scores = scoresToInt(scores);
  66.  
  67.         var trace = {
  68.             x: scores.length,
  69.             y: scores,
  70.             mode: 'lines+markers',
  71.             type: 'scatter'
  72.         };
  73.  
  74.         var data = [trace];
  75.  
  76.         Plotly.newPlot('myDiv', data);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement