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

player-info

v0.2.1

Published

A client-side only solution to manage player state and data.

Downloads

12

Readme

NPM version Known Vulnerabilities npm NPM downloads Gitter

Table of Contents

Why

Sometimes you need to have persistent data tied to a player but you don't want to/can't have access to a server. PlayerInfo allows you to tie data to a player purely client side so you can still have state management with a serverless game.

Installation

To install PlayerInfo, use:

$ npm install player-info

and then to use it your project, you import it as an ES6 module:

// Browser
import PlayerInfo from './path/to/player-info.js';

// Webpack
import PlayerInfo from 'player-info';

and finally just create a new instance of it to be able to use PlayerInfo:

const playerInfo =  new PlayerInfo();

Properties

The following properties are available on any instance of PlayerInfo:

player

The player object contains all data about the current player of the game including their unique id and browser information.

| property | type | description | |----------------- |--------- |--------------------------------------------------------------------------------------------------------------------------- | | id | string | The unique id of the player. If the player is a returning player the id will be the same as it was last time they played. | | browser | Object | | | browser.name | string | The name of the browser the player is using. | | browser.version | string | The version number of the browser being used. | | isMobile | boolean | Indicates whether the player is using a mobile device or not. | | referringLink | string | The previous URL that the player came from. This helps you know what sites are linking to your game. | | screen | Object | | | screen.width | number | The player's screen width. | | screen.height | number | The player's screen height. | | viewport | Object | | | viewport.width | number | The player's viewport width. | | viewport.height | number | The player's viewport height. | | arrived | Date | The timestamp of when the player landed on the game's page. | | departed | Date | The timestamp of when the player left the game's page. This is only available after the user has left |

So if you initialized PlayerInfo like the example above, you could do the following:

// Get the player's browser name:
const browserName = playerInfo.player.browser.name;

// Get whether the player is using a mobile device or not.
const playerIsMobile = playerInfo.player.isMobile;

API

save

Saves an item to the persistent storage that is tied to this player.

For example, if your game has levels you can save the current level that the user is on so that when they come back, they can start/access that level.

| param | type | description | default | |------- |:------: |---------------------------------------------------------------------------------------------------------------: |--------- | | key | string | A key to identify the item to save | | | item | string | The actual item to save. This can be any type of value on your end but it should be a string when being saved. | |

example:

const level = 5;

playerInfo.save('level', level);

load

Loads an item from the persistent storage.

| param | type | description | default | |------- |:------: |-------------------------------------------------: |--------- | | key | string | The key used to save the item when it was saved. | |

example:

const level = playerInfo.load('level');

Signals

PlayerInfo offers two signals, onConnect and onDisconnect.

onConnect

This signal gets dispatched whenever a player connects to the game and it includes the player data object.

example:

playerInfo.onConnect.add(playerConnected);

function playerConnected(player) {
  console.log(player) // Same as accessing playerInfo.player
}

onDisconnect

This signal gets dispatched whenever a player leaves the game and it includes the player object along with two extra properties added to it: disconnected (a Date object of when the player disconnected) and elapsed (the amount of time between connecting and disconnecting, in milliseconds).

example:

playerInfo.onDisconnect.add(playerDisconnected);

function playerDisconnected(player) {
  console.log(player.disconnected, player.elapsed);
}

Tests

Since the tests are very browser specific and some parts are hard to mock, all tests are run in a browser. To start up the local server to see the tests use:

$ npm run test

Then navigate to http://localhost/test/index.html to run the tests.

License

MIT