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

gamesparks-node

v1.0.0

Published

Communication with GameSparks Platform

Downloads

5

Readme

gamesparks-node

This repository is a fork of https://bitbucket.org/gamesparks/gamesparks-node-server-sdk.

Introduction

This library provides communication capability between node.js and the GameSparks platform.

This allows node.js to send requests and receive the responses from the platform on behalf of players.

Sockets can also be configured to optionally listen for push messages to a particular player.

Include the library as you would any other module

npm install gamesparks-node

var gameSparks = require( 'gamesparks-node' );

The library can be configured to connect to either the development, or live servers and also be configured as a sender (request/response) or a listener (push messages)

Listener configurations are capable of sending requests, senders can never receive messages.

Initialisation Options

You need to select an initialisation option. The library can only be initialised once, subsequent calls to initialisation are ignored.

Initialise as a sender to the development servers. No push messages will be received.

gameSparks.initPreviewSender(gameApiKey, secret, socketCount, onInit, onError)

Initialises as a listener to the development servers. Push messages will be received.

gameSparks.initPreviewListener(gameApiKey, secret, socketCount, onMessage, onInit, onError)

Initialises as a sender to the live servers. No push messages will be received.

gameSparks.initLiveSender(gameApiKey, secret, socketCount, onInit, onError);

Initialises as a listener to the live servers. Push messages will be received.

gameSparks.initLiveListener(gameApiKey, secret, socketCount, onMessage, onInit, onError);

Initialse as a sender with a url

gameSparks.init("wss://service.gamesparks.net/ws/server/{gameApiKey}", secret, 10, onMessage, onInit, onError);    

Initialse as a listener with a url

gameSparks.init("wss://service.gamesparks.net/ws/server-send/{gameApiKey}", secret, 10, onMessage, onInit, onError);    

Parameters

  • gameApiKey - The is the apiKey of you game within the gamesparks platform. This value is available through the portal.
  • secret - This is the server secret for you game. You will need to request this value for each game you want to use. IT IS NOT THE API SECRET in the portal. A different secret is configured for server to server communications as the access permissions are elevated.
  • socketCount - The maximum number of open sockets to maintain between platforms. Suggest a relativly low number (20) to start with in development. The maximum number for development servers is 50.
  • onMessage(listeners only) - Provide the function you want to be called when a message is received. This method will be passed the json object received from the server as the first parameter.
  • onInit - A callback function that is invoken when the SDK connects sucessfully to gamesparks
  • onError - A callback function invoked when there is an error. It is passed a single node Error object.

Sending Requests

To send a request, you need to construct the JSON data you want to send, and call the following method

gameSparks.sendAs( playerId, requestType, data, callback )

The previously published versions of this library used a non-standard way of calling asynchronious functions. You can continue to use this non-standard pattern in existing code like this:

gameSparks.sendAsDepricated( playerId, requestType, data, onResponse, onError )

Parameters

  • playerId - The ID of the player you are sending the request for (or null when authenticating)
  • requestType - The type of request. For posting scores this will be ".LogEventRequest" but other calls are available.
  • data - The additional data to pass as part of the request. See the example further down.
  • callback( err, response ) - If there was an error, err will be a node Error object. Otherwise response is the data back from gamesparks.

The depricated options are:

  • onResponse - Your function to process the response from gamesparks.
  • onError - A callback function invoked when there is an error. It is passed a single node Error object.

Authentication

To get a playerId to make sendAs calls, you must authenticate a user or a device. You can use sendAs like the following to get a playerId:

gameSparks.sendAs( null, ".AuthenticationRequest", { userName: "testUser", password: "123" }, function( err, response ) {
  if ( err ) console.log( err.messaage );
  else playerId = response.userId;
});

The playerId obtained can now be used as the first argument to subsequent sendAs calls.

Sending a score

Assumes you have an event configured with event code HIGH_SCORE, and a single attribute named "SCORE".

gameSparks.sendAs(
  playerId,
  ".LogEventRequest", 
  { "eventKey" : "HIGH_SCORE", "SCORE" : 1000 }, 
  function( err, response ) {
    if ( err ) console.log( err.message );
else console.log( JSON.stringify( response ) );
  });

Getting the global top 5 of a leaderbord

Assumes you have a leaderboard with short code "LB1".

gameSparks.sendAs(
  playerId,
  ".LeaderboardDataRequest", 
  { "leaderboardShortCode": "LB1","social": "false","entryCount": "5","offset": "0" }, 
  function( err, response ) {
    if ( err ) console.log( err.message );
else console.log( JSON.stringify( response ) );
  });

Example

There is an example script located in ./example. Please read the README.md file located there for details.

Client/Server

There is a more extensive set of scripts in ./client-server which you can use as a command line interface alternative to the Gamesparks test harness. Please read the README.md file located there for details.