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

resync-js

v1.0.6

Published

```sh npm install resync-js ```

Downloads

2

Readme

Resync.js

Installation

 npm install resync-js

createGame

The createGame function will create a flux like game store.

const { createGame } = require('resync-js');

The game sotre requires:

  • state - inital state object.
  • mutate - contains all state mutation logic.
  • update - dispatches actions when game state passes certain conditions.
createGame({

    state: {
        players: {
            'player-1': {
                position: { x: 0, y: 0 },
                velocity: { x: 0, y: 0 },
                health: 100,
                onFire: true
            }
        }
    },

    mutate (state, action) {
        switch (action.type) {
            case 'SET_PLAYER_HP': {
                const { id, health } = action;
                state.players[id].health = health;
                return state;
            }
        }
        return state;
    },

    update (game, dt) {
        const players = game.getState().players;
        for (let playerID in players) {
            let player = players[playerID];
            if (player.onFire) {
                const action = {
                    type: 'SET_PLAYER_HP',
                    id: playerID,
                    health: player.health - 10,
                };
                game.dispatch(action);
            }
        }
    },
});

createHost

The createHost function will set up the hosting logic for your backend.

const { createHost } = require('resync-js');
  • game - game instance.
  • onReady - calls when host is ready.
  • onConnection - new client has connected.
  • onDisconnect - client has disconnected.
  • onDispatch - client dispatched an action.
createHost({

    game: createGame ({ /* ... */ }),

    onReady (host) {

        // Start game loop ...
        gameLoop((dt) => {
            host.game.update(dt);
        });
    },

    onConnection (host, event) {
        const { connectionID } = event;

        // Create unique player ID.
        const playerID = 'player-' + connectionID;

        // Create action for adding player to game state.
        const action = addPlayer(playerID);

        // Execute dispatch locally and send to other peer players.
        host.game.dispatch(action);
        host.socket.connections.forEach(connection => {
            if (connection !== connectionID) {
                host.socket.dispatch(connection, action);
            }
        });

        // Get current game state and accept player connection.
        const state = host.game.getState();
        host.socket.connection(connectionID, { playerID, state });
    },

    onDisconnect (host, event) {
        const { connectionID } = event;
        const playerID = 'player-' + connectionID;
        const action = removePlayer(playerID);

        // Sync ...
        host.game.dispatch(action);
        host.socket.connections.forEach(connection => {
            if (connection !== connectionID) {
                host.socket.dispatch(connection, action);
            }
        });
    },

    onDispatch (host, event) {
        const { connectionID, action } = event;
        const playerID = 'player-' + connectionID;

        // Sync ...
        host.game.dispatch(action);
        host.socket.connections.forEach(connection => {
            if (connection !== connectionID) {
                host.socket.dispatch(connection, action);
            }
        });
    },
});

createClient

The createClient function will set up the frontend logic for the player.

const { createClient } = require('resync-js');
  • game - game instance.
  • onReady - calls when host is ready.
  • onConnection - new client has connected.
  • onDisconnect - client has disconnected.
  • onDispatch - client dispatched an action.
createClient({

    game: createGame ({ /* ... */ }),

    onConnection (client, event) {
        const { playerID, state } = event;

        // Sync clients input ...
        onKeyboardInput((input, value) => {
            const action = setPlayerInput(playerID, input, value);
            client.game.dispatch(action);
            client.socket.dispatch(action);
        });

        // Start game loop ...
        gameLoop((dt) => {
            client.game.update(dt);
            render(client.game.getState());
        });
    },

    onDisconnect (client, event) {
        console.log('Disconnected from game server ...');
    },

    onDispatch (client, action) {
        client.game.dispatch(action);
    },
});