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

js-gamemanager

v2.0.1

Published

A set of methods to handle the creating and managing of players and lobbies for a Socket.IO project.

Downloads

6

Readme

JSGameManager

A set of functions designed to make creating and managing a lobby system for web apps or games easily using Socket.IO.

Requirements

  • NPM and Node.JS
  • Install the Socket.IO module using install socket.io --save

Documentation

This documentation is for version 2.0 and up. Documentation of previous versions can be found here.

Version 2.0 and up use ES6 classes to store and manage all data related to the games within the instance of GameManager compared to previous versions where the Games object had to be created manually and passed in with all actions.

Game Object

The Game object consists of the following properties by default:

host which contains the socket ID assigned by socketIO for the host of that lobby.

room which stores the string identifier for this game lobby.

Example:

let game = {
  host: X32BZ342313,
  room: "Lion" 
}  

Player Object

The player object consists of the following properties by default:

username is pretty self explanatory it stores the username of the connecting player.

id stores the socketIO assigned id associated with the player. This is what you use to access this specific player as the id is sent by socketIO on every message and its unique to each client.

score is again pretty simple. Its just provides a way to store the players score. Default value = 0.

Example

let player = {
  username: "James",
  id: X32BZ342313,
  score: 0,
}

Usage

The best method is create your own ES6 based class that extends this js-gamemanager which allows you to take advantage of all the prebuilt methods for connecting and managing players while allowing you to add your own methods and data as well as override existing methods to tailor the module for your project. For an example you can view my TriviaGameManager class I built on top of this module for use in a simple quiz multiplayer quiz game here.

Initializing the manager module and creating a global object to store all active games:

const {GameManager} = require("js-gamemanager");
var games = new GameManager();

The addGame function takes two arguments the socketID of the host and the room name they would like to create and adds them to the array of active games. It will also return a copy of the Game object that was created.

For example:

var game = games.addGame(SOCKETID, "london");
/*
  game = {
    host: SOCKETID,
    room: "london"
  }
*/

The checkUsername function checks if username is taken for given room. It takes two arguments the room and the username and returns a boolean value.

For example:

console.log(games.checkUsername("london", "James"));
// Will output true or false depending on whether the name is taken or not.

The checkRoomName function checks if a room name is already in use. It takes one argument the room name and returns a boolean value.

For example:

console.log(games.checkRoomName("london"));
// Will output true or false depending on whether room is in use or not.

The addPlayer function takes three arguments the room name, username and socketID and adds that player to the list of active players and returns a copy of the created Player object.

For example:

var player = games.addPlayer("london", "Tony", SOCKETID);
/*
  player = {
    username: "Tony",
    id: SOCKETID,
    room: "london",
    score: 0
  }
*/

The isHostOrPlayer function takes one argument the socketID and returns either "HOST" or "PLAYER". For example:

console.log(games.isHostOrPlayer(SOCKETID));
// returns "HOST" or "PLAYER"

The removePlayer function takes one argument the socketID and removes that player from the array of active players and returns a copy of that player.

For example:

var removedPlayer = games.removePlayer(SOCKETID);

The removeGame function takes one argument the socketID of the host and removes the Game object created by that host from the array of active games and returns a copy of the removed Game object. Note: You can call the removeFromRoom function with the returned game.room as the argument to remove all players belonging to that room.

For example:

var removedGame = games.removeGame(SOCKETID);

The removeFromRoom function takes one argument the room name and removes all players who belong to that room. Useful for handling host disconnecting from game. It returns an array of removed players so you can emit a custom disconnect message if required.

For example:

var removedPlayers = games.removeFromRoom("london");
// Removes all players in room called "london"

The getFromRoom function is used the same way as the removeFromRoom function but instead of removing it just returns an array of players in that room.

For Example:

var players = games.getFromRoom("london");
// Returns list of players

The getPlayerBySocket function takes one argument the socketID and returns the relevant player object if it exists. Mainly used internally by other GameManager functions but is available if needed.

For Example:

var player = games.getPlayerBySocket(SOCKETID);
// returns relevant player

The getGameByHost function takes one argument a socketID of a host and it returns the relevant game object if it exists. Mainly used internally by other GameManager functions but available if needed.

For Example:

var game = games.getGameByHost(SOCKETID);
// returns relevant game

The getGamebyRoom function takes one argument the room name and it returns the game object for that room if it exists. Mainly used internally by other GameManager functions but available if needed.

For example:

var game = games.getGameByRoom(SOCKETID);