Created
March 5, 2019 19:55
-
-
Save drenther/3d50ea37306c1d7aa171491853ed37db to your computer and use it in GitHub Desktop.
Final utils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HEROES, COMICS } from './data'; | |
// the Knuth shuffle algorithm | |
export function shuffle(array) { | |
let currentIndex = array.length; | |
let temporaryValue; | |
let randomIndex; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} | |
// method to handle points calculation based on sort order as well as grouping | |
function calculateScore(groupedHeroes, comics) { | |
const correctOrder = HEROES.filter(hero => hero.comics === comics).sort((a, b) => | |
a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1 | |
); | |
return groupedHeroes.reduce((score, { name }, index) => { | |
const maxPoint = HEROES.length; | |
const heroIndex = correctOrder.findIndex(hero => hero.name === name); | |
const penalty = heroIndex >= 0 ? Math.abs(index - heroIndex) : maxPoint; | |
console.log({ name, points: maxPoint - penalty }); | |
return score + (maxPoint - penalty); | |
}, 0); | |
} | |
export function getTotalScore(groups, timeLeft) { | |
const gameScore = Object.values(COMICS).reduce( | |
(sum, comicsName) => sum + calculateScore(groups[comicsName], comicsName), | |
0 | |
); | |
const timeBonus = getSeconds(timeLeft); | |
return gameScore ? gameScore + timeBonus : 0; | |
} | |
// method to handle to the heroe cards movement | |
export const move = (state, source, destination) => { | |
const srcListClone = [...state[source.droppableId]]; | |
const destListClone = | |
source.droppableId === destination.droppableId | |
? srcListClone | |
: [...state[destination.droppableId]]; | |
const [movedElement] = srcListClone.splice(source.index, 1); | |
destListClone.splice(destination.index, 0, movedElement); | |
return { | |
[source.droppableId]: srcListClone, | |
...(source.droppableId === destination.droppableId | |
? {} | |
: { | |
[destination.droppableId]: destListClone, | |
}), | |
}; | |
}; | |
// method to get time left | |
export const getTimeLeft = deadline => deadline - Date.now(); | |
// method to get time left in seconds | |
export const getSeconds = timeLeft => Math.floor(timeLeft / 1000); | |
// enums for representing the game state | |
export const GAME_STATE = { | |
READY: 'ready', | |
PLAYING: 'playing', | |
DONE: 'done', | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment