Skip to content

Commit

Permalink
documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
euZebe committed May 16, 2018
1 parent 603051a commit 7b6325b
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/molkky-vanilla-redux.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const { createStore } = require('redux');

// -------
// ACTIONS
// -------
/*
ACTIONS (and rather ACTION CREATORS)
*/

/**
* @param players: string[]
* @returns {{ type: string, players: string[] }}
*/
function initGame(players) {
console.log('=> jeu initialisé');
return {
Expand All @@ -12,6 +16,12 @@ function initGame(players) {
};
}

/**
*
* @param fallenPins: number[]
* @param player: string
* @returns {{ type: string, player: string, fallenPins: number[] }}
*/
function throwPin(fallenPins = [], player) {
if (fallenPins.length) {
console.log(`${player} a fait tomber la/les quille(s) ${fallenPins}`);
Expand All @@ -25,9 +35,23 @@ function throwPin(fallenPins = [], player) {
};
}

// -------
// REDUCER
// -------

/*
REDUCER
*/

/**
* @param state shape = {
* fallenPins: number,
* playerX: { // iterated over players
* name: string,
* score: number,
* consecutiveFailures: number,
* }
* }
* @param action
* @returns a new state, with the shape described above
*/
function rootReducer(state = {}, action) {
switch (action.type) {

Expand All @@ -45,7 +69,7 @@ function rootReducer(state = {}, action) {
const { player, fallenPins } = action;
const previousPlayerState = state[player];

// no pin falled
// no pin felt
if (!fallenPins.length) {
const nextPlayerState = {
...previousPlayerState,
Expand All @@ -58,7 +82,7 @@ function rootReducer(state = {}, action) {
};
}

// one pin falled
// one pin felt
if (fallenPins.length === 1) {
const nextPlayerState = {
...previousPlayerState,
Expand All @@ -72,7 +96,7 @@ function rootReducer(state = {}, action) {
};
}

// else, several pins falled
// else, several pins felt

// TODO: toggle this block and the next one to make reducer unpure
const nextPlayerState = {
Expand Down

0 comments on commit 7b6325b

Please sign in to comment.