-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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')) || { | ||
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`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throw*, typo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the best is to use |
||
} | ||
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this approach looks better for me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but setting array length is not good 🤮 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. best thing would be to:
|
||
|
||
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(''); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. It is better to make one sorting function, in example:
|
||
} 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 | ||
} |
There was a problem hiding this comment.
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)