npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-redux-game-starter

v0.1.0

Published

Boilerplate for a game based on React+Redux.

Downloads

5

Readme

#React+Redux game starter

You are gonna to build a new super-rock-star game. How can you start? Right, use the starter kit. There are many boring things you have to boilerplate instead of building real game features and fun. Games have a lot of in common, starting from player flow to gameplay and logic.

Whatever game you build you have the next game phases:

  • players registration (one or many)
  • a game session is started
  • playing with a score calculation
  • a game session is done
  • a winner dedicated and shown
  • restart game etc.

This starter kit allows you to have a good start point with half of work done already.

To start using:

clone the repository locally
npm install
npm start

Code structure.

The root folders

  • actions/ action creators and action types (constants)
  • components/ stateless ui components
  • containers/ stateful ui components
  • reducers/ reducers and game logic
  • store/ configure app store
  • utils/ other helpers

Action creators

- addPlayer
- gotoNextGamePhase
- gotoNextGamePhaseWithDelay
- doPlayerNextStep
- doAutoPlay
- showWinner
- restartGame

Game phases ('start game', 'stop game', etc.) can be changed, extended with additional ones and it's nice to have a mechanism to keep things flexible. That's why we try to avoid actions like 'start game', instead, we do gotoNextGamePhase, and keep the queue of game phases as a separated list.

Currently list looks like:

var GamePhasesQueue = [PLAYERS_REGISTRATION, GAME_IN_PROGRESS, GAME_SESSION_DONE, GAME_WINNER];

It means if you need to add SHOW_LEVEL_COMICS, after players registration, you don't have to change that logic in a few components, just put it a sequence and define UI behavior on that phase. Sometimes exact game phase can mean game sceen change or just rect somehow on it.

getScreen () {
    let props = this.props;

    switch (props.currentGamePhase) {
        case PLAYERS_REGISTRATION:
            return <StartView />;

        case GAME_IN_PROGRESS:
            return  <PlayView />;

        case GAME_WINNER:
            return  <FinishView />;

        default:
            return <StartView />;
    }
}

/**
 *
 * @param {Object} nextProps
 * @returns {boolean}
 */
shouldComponentUpdate(nextProps) {
    return [PLAYERS_REGISTRATION, GAME_IN_PROGRESS, GAME_WINNER].indexOf(nextProps.currentGamePhase) !== -1;
}

To do actual player's action (step, move) you can use doPlayerNextStep action creator. If you want to have a nice demo or just test entire gameplay you can call doAutoPlay (thunk is used to make recursive calls).

Components

Game related UI located in game-page/ sub-folder. It contains three main game screen: players registration, game board, and game end. The main component in game board is ScoreTable which renders what's is going on during the game session. That's a good place to change to whatever UI or game you currently build.

Containers

There is a folder inside called 'my-game', you have to rename regarding the game topic you build (i.e. bowling-game, poker, chess, etc.). Go further you can see three sub-folders as well, for start, play, and finish screens.

Reducers

That's probably the most interesting thing because all you game magic will happen there. Due to actions creators described above, you can already figure out the reducers list.

Let's have a look at DO_PLAYER_NEXT_STEP reducer. What happens when the player does move? Several things:

  1. Player changes his score.
  2. The Current player index could be changed if the player has already done his action.
  3. The current frame (actions round) could be changed if all players did their actions.
  4. The current game phase could be changed as well, i.e. someone won or failed, or just game time ended.

Games usually contain a lot of logic, how a player does his move, how his last step influence on the game process and other players, etc. All that logic is moved to separate service (reducers/logic/myGameLogic), and its public APIs used in the reducer. Of course to do all things right you should keep methods as pure functions without any side effects and dependencies. Like

/**
 *
 * @param {Number} currentPlayerIndex
 * @param {Array} players
 * @returns {boolean}
 */
isCurrentGameFrameDone(currentPlayerIndex, players) {
    return currentPlayerIndex === players.length - 1;
},

/**
 *
 * @param {Number} currentFrame
 * @returns {number}
 */
getNextGameFrame(currentFrame) {
    return ++currentFrame;
},

/**
 *
 * @param {Number} currentFrame
 * @param {Number} totalFame
 * @returns {boolean}
 */
isGameSessionDone(currentFrame, totalFame = TOTAL_FRAMES_NUMBER) {
    return currentFrame >= totalFame;
}

Demo

Please check /demo.gif

TODO

Tests

License

MIT