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

multigame

v0.1.2

Published

Multiplayer game platform

Downloads

2

Readme

multigame

Multigame is a library, or perhaps more of a framework, to help you manage and build JavaScript multiplayer games. It takes care of connecting players and viewers (=spectators) to games and handles the communication between them. It runs on node.js.

There is a server part and a client part of the library. The client part exposes an object that acts a bit like a proxy for the game that runs on the server, that the client is connected to.

A multigame server can handle many games instances, of different game types, simultaneously. The WebSocket protocol is used to communicate between server and clients.

Games must be built using the framework to be able to be hosted on a multigame server.

Install

You need Node.js on your machine, then do:

npm install multigame --save

and in the client code:

<script src="/socket.io/socket.io.js"></script>
<script src="<your directory>/gameProxy.js"></script>

The gameProxy.js file can be found under ./lib.

Usage

Writing a game

When writing a game, using this framework, you create a Rules object to be placed on the server. It shall implement the multigame's Rules interface and this is a place to put game logic. You should only need one Rules object for all your game instances (if they are of the same type that is). A Rule object should be stateless.

You will need a client part of your game too. The client part has access to a game proxy object, that is part of the framework. It takes care of the communication with the server. You decide where you want to put the game logic, server and/or client side.

Multigame Game and Player objects have a property called 'state'. It is empty from the start. A Game's 'state' is what is sent to all who are connected to a Game when there has been an update to the game's state (the notify() method has been called). If the client is a player it will also receive the state of the player object, that resides on the server and is associated with the client/socket.

You yourself fill the state properties with whatever that is relevant for your game. You can also add whatever you need to a Game or Player object (properties and methods), just remember that it is the 'state'-properties that are sent to clients.

A game that uses the framework is Tricker.

Setting up a multigame server

var http = require('http');

// Get the multigame server "factory" function
var GameServer = require('multigame').Server;

// Get the verifyer object
var verifyer = require('multigame').verifyer;

// Create a HTTP server
var server = http.createServer(app);
server.listen(port);

// Register domains from which connections should be accepted
verifyer.registerDomain('localhost:3000');

// Create a new multigame server using the "factory" function
var multiServer = GameServer(server, verifyer);

Setting up a new game

// Get the Manager "singleton"
var Manager = require('multigame').Manager;

// Get the Game constructor function
var Game = require('multigame').Game;

// Get a Rules object for the type of game you want to create
var TrickerRules = require('tricker').Rules;

// Create a new game and register it with the Manager object
Manager.registerGame(new Game({id: 1, name: 'The dungeon', rules: TrickerRules}));

Creating a player for a game

// Get the Player constructor function
var Player = require('multigame').Player;

// Create a Player object
var player = new Player(socket);

// Register the Player object with the game
game.registerObserver(player);

You might want to add additional properties and methods to the Player object.

Retrieving a specific game

// Get the Manager "singleton"
var Manager = require('multigame').Manager;

var game = Manager.getGame(id);

Connect to a game

In the client code, you first need to register an object as an observer to the GameProxy object to receive server updates. The GameProxy object expects the object to have an update(state) method.

// Register the tricker client as an observer to GameProxy events
GameProxy.registerObserver(TrickerClient);

Then connect to a game on the server:

// Connecting to a game
GameProxy.connect(url, gameId, clientType);

Overview

Overview example

Protocol between server and client

Client generated events

  • connection
  • disconnect
  • error (data: error)
  • game_connect (data: game id)
  • add_player
  • add_observer
  • msg (data: game specific event data)

Server generated events

  • connect
  • connect_error (data: error)
  • error (data: error)
  • disconnect (data: disconnect reason)
  • state (data: game and possibly player state)

TODO

These are things that should be addressed:

  • Remove socket.io dependency
  • Be able to connect to a db for players/observers (?!)
  • A better way to handle game and player states