Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create rankingService.js #33

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/service/rankingService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
let user = {
name: 'Player1',
score: 15,
timeInSeconds: 93
};
// check localStorage for current Top scores
// if empty create object with objects mode1, mode2, and mode3
// PokemonApiRanking - data from/to localStorage
const PokemonApiRanking = JSON.parse(localStorage.getItem('PokemonApiRanking')) || {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should get it every time you call rankingService function, not only at the start.

If you once add score to localStorage then this PokemonApiRanking is updated, okay... but the second score that would be added is based not on the content of the local storage (that could be somehow changed between first and second game), but is based on the local copy you have in this variable. (Not sure if this is clear enough - if not - contact me)

mode1: {
name : "Whos that pokemon?",
scores : []
},
mode2: {
name : "What it looks like?",
scores : []
},
mode3: {
name : "Guess the type!",
scores : []
}
};

export const rankingService = (mode, score) => {

let currentMode; //path to scores array for current mode
switch(mode) {
case WHO_IS_THAT_POKEMON:
currentMode = PokemonApiRanking.mode1.scores;
break;
case WHAT_DOES_THIS_POKEMON_LOOK_LIKE:
currentMode = PokemonApiRanking.mode2.scores;
break;
case GUESS_THE_TYPE:
currentMode = PokemonApiRanking.mode3.scores;
break;
default:
trow `Wrong mode`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw*, typo

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the best is to use throw new Error('message')

}
currentMode.push(user);
currentMode.sort( (a,b) => b.timeInSeconds - a.timeInSeconds );
currentMode.sort( (a,b) => b.score - a.score );
if(currentMode.length > 3) {
currentMode.length = 3;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this approach looks better for me

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but setting array length is not good 🤮

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best thing would be to:

  1. add new record to old array
  2. sort it
  3. create new array
  4. add only 3 first elements of old array to new array
  5. set "new array" as right property of whole PokemonApiRanking


printTopUsers(currentMode, htmlList); //print to HTML
localStorage.setItem('PokemonApiRanking', JSON.stringify(PokemonApiRanking)); //save in localStorage
}

// ^ powyższa funkcjia dodawanie wyniku do PokemonApiRanking ver.1 - nie korzysta z argumentu score (pod funkcja printTopUsers wersja druga dla rankingService)

// PRINT TO HTML // only print without adding player
// htmlList - html ul (or whatever) where we want to put Top scores
// users - array with top players (scores)
export const printTopUsers = (users = [], htmlList) => {
htmlList.innerHTML = users.map((user, i) => {
return `
<li>
<label for="item${i}"><strong>${user.name}</strong>: ${user.score}points in ${user.timeInSeconds}seconds</label>
</li>
`;
}).join('');
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

displaying records and whole rankingService functionality should be in separate files (separation of responsibilities)


//funkcjia dodawanie wyniku do PokemonApiRanking ver.2 - korzysta z argumentu score jeśli w Top jest już 3 graczy
export const rankingServiceV2 = (mode, score) => {
let currentMode; // path to scores array for current mode
switch(mode) {
case WHO_IS_THAT_POKEMON:
currentMode = PokemonApiRanking.mode1.scores;
break;
case WHAT_DOES_THIS_POKEMON_LOOK_LIKE:
currentMode = PokemonApiRanking.mode2.scores;
break;
case GUESS_THE_TYPE:
currentMode = PokemonApiRanking.mode3.scores;
break;
default:
trow `Wrong mode`;
}
if(currentMode.length < 3){
currentMode.push(user);
currentMode.sort( (a,b) => b.timeInSeconds - a.timeInSeconds );
currentMode.sort( (a,b) => b.score - a.score );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two separate sorts are bad - you cannot be sure that sorting is stable.
More on sort stability and what it even is: https://medium.com/@fsufitch/is-javascript-array-sort-stable-46b90822543f

It is better to make one sorting function, in example:

(a,b) => {
 const scoreCompare = b.score - a.score;
 if(scoreCompare != 0) { // if scores are different, then it is all we want.
   return scoreCompare;
 } else { // if score is the same, then we need to compare only time of those elements.
   return b.timeInSeconds - a.timeInSeconds;
 }
}

} else {
for(let i = 0; i < 3; i++) {
if(score >= currentMode[i].score) {
currentMode.push(user);
currentMode.sort( (a,b) => b.timeInSeconds - a.timeInSeconds );
currentMode.sort( (a,b) => b.score - a.score );
currentMode.length = 3;
}
}
}

printTopUsers(currentMode, htmlList); //print to HTML
localStorage.setItem('PokemonApiRanking', JSON.stringify(PokemonApiRanking)); //save in localStorage
}