Newgrounds. NewgroundsPlugin

Newgrounds API object

Constructor

new NewgroundsPlugin(app_id, cipheropt)

Create the global newgrounds object

  • Logs a view right away, for a guest and a logged in player alike, so a game does not have to
  • Create the medals first: they take their name and icon from the server once it answers, and when logged in they are locked here until it does
  • Call medalsInit too, before or after: an unlock asserts without it, and it keeps the medals while not logged in
Parameters:
NameTypeAttributesDescription
app_idstring

The Newgrounds App ID

cipherstring<optional>

The encryption key from the app's settings, AES-128 as Base64; medal unlocks and posted scores are encrypted with the browser's WebCrypto, which needs a secure page, https or localhost

Example
// create the newgrounds object, replace the app id with your own
 const app_id = 'your_app_id_here';
 new NewgroundsPlugin(app_id);

Members

app_id

Properties
TypeDescription
string

The Newgrounds App ID

cipher :string|undefined

Type:
  • string | undefined
Properties
TypeDescription
string | undefined

AES-128/Base64 encryption key, if any

cryptoKey :CryptoKey|undefined

Type:
  • CryptoKey | undefined

host

Properties
TypeDescription
string

Hostname sent with the view the plugin logs when it starts

medals

Properties
TypeDescription
Array

Medals fetched from Newgrounds, empty until ready, with the unlocks only when logged in

pendingUnlocks :Map.<NewgroundsMedal, Promise.<boolean>>

Type:
  • Map.<NewgroundsMedal, Promise.<boolean>>
Properties
TypeDescription
Map.<NewgroundsMedal, Promise.<boolean>>

Medals whose unlock is in flight or waiting to be resent, with their request's promise; one the server refused leaves it

ready

Properties
TypeDescription
Promise.<NewgroundsPlugin>

Resolves once the session is checked and the lists are in, empty if the server could not be reached

scoreboards

Properties
TypeDescription
Array

Scoreboards fetched from Newgrounds, empty until ready

session_id :string|null

Type:
  • string | null
Properties
TypeDescription
string | null

Newgrounds session id from the URL, null when not logged in or once the session is lost

user :Object|null

Type:
  • Object | null
Properties
TypeDescription
Object | null

The logged in player once ready, null when not logged in

Methods

(async) call(component, parametersopt, session_idopt) → {Promise.<Object>}

Send a message to call a component of the Newgrounds API

Parameters:
NameTypeAttributesDescription
componentstring

Name of the component

parametersObject<optional>

Parameters to use for call

session_idstring | null<optional>

The session to send, the player's by default

Returns:
  • The response JSON object, undefined when the call failed or took over 15 seconds; a component's own success and error are in result.data, and a cipher that is not a key gives error 201
Type: 
Promise.<Object>

(async) encrypt(text) → {Promise.<string>}

Encrypt text the way the Newgrounds gateway expects, AES-128 CBC with a random iv in front, as Base64

Parameters:
NameTypeDescription
textstring
Returns:
Type: 
Promise.<string>

getScores(id, useropt, socialopt, skipopt, limitopt, periodopt) → {Promise.<Object>}

Get scores from a scoreboard

Parameters:
NameTypeAttributesDefaultDescription
idnumber

The scoreboard id

userstring | number<optional>

A user's id or name, to load only their scores

socialboolean<optional>
false

If true, only the scores of the user and their friends, the logged in player when user is left out

skipnumber<optional>
0

Number of scores to skip over

limitnumber<optional>
10

Number of scores to include in the list

periodstring<optional>

'D' today, which the server assumes when left out, 'W' this week, 'M' this month, 'Y' this year or 'A' all time

Returns:
  • The response JSON object, undefined when the call failed; the scores are in result.data.scores, each with user.name, value and formatted_value; without a user or social it is the whole board
Type: 
Promise.<Object>

logView()

Deprecated
  • since 1.20, the view is logged when the plugin starts, so this does nothing

postScore(id, value) → {Promise.<Object>}

Send message to post score

Parameters:
NameTypeDescription
idnumber

The scoreboard id

valuenumber

The score value, a whole number

Returns:
  • The response JSON object, undefined when the call failed; result.data.success says whether it posted, which needs a logged in player; an answer that the session is gone makes the game play as not logged in, and one that timed out may still have posted
Type: 
Promise.<Object>

resendUnlocks()

Send the unlocks whose request did not reach the server again, which the session check does every minute

  • A request still out is left to answer, and while unlocks are prevented they wait

unlockMedal(id) → {Promise.<Object>}

Send a request to unlock a medal by id, the local medal is not changed; NewgroundsMedal.unlock sends this and waits for the answer

Parameters:
NameTypeDescription
idnumber

The medal id

Returns:
  • The response JSON object, undefined when the call failed
Type: 
Promise.<Object>