LittleJS 1.28.0

Newgrounds

NewgroundsPlugin

Newgrounds API object

Constructor

new NewgroundsPlugin(app_id, cipher)newgrounds.js:129

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
app_id string

The Newgrounds App ID

cipher string 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

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

Members

Methods

Members

app_id : stringnewgrounds.js:151

The Newgrounds App ID

cipher : string|undefinednewgrounds.js:154

AES-128/Base64 encryption key, if any

cryptoKey : CryptoKey|undefinednewgrounds.js:156
host : stringnewgrounds.js:159

Hostname sent with the view the plugin logs when it starts

loadFailures : Map<number, ('failed'|'notSave')>newgrounds.js:167
medals : Array<Object>newgrounds.js:162

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

pendingUnlocks : Map<NewgroundsMedal, Promise<boolean>>newgrounds.js:174

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

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

scoreboards : Array<Object>newgrounds.js:165

Scoreboards fetched from Newgrounds, empty until ready

session_id : string|nullnewgrounds.js:179

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

user : Object|nullnewgrounds.js:170

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

Methods

call(component, parameters, session_id) → Promise<Object>asyncnewgrounds.js:455

Send a message to call a component of the Newgrounds API

component string

Name of the component

parameters Object optional

Parameters to use for call

session_id string|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
cloudLoad(slot) → Promise<*>asyncnewgrounds.js:366

Load the value a cloud save slot holds: null when Newgrounds says it holds none, undefined when it could not be loaded or the player is not logged in, said in the console when logged in

  • Do not save over a slot that loaded as undefined, it may hold the player's save; loadFailure(slot) says why it did: 'notSave' for a file there that is not a save, where a game may offer to start over, 'failed' otherwise
  • A saved null loads as null, as an empty slot does
slot number

The slot number

Returns
  • The value saved, null for none, undefined for a load that failed
const save = await newgrounds.cloudLoad(1);
cloudSave(slot, data) → Promise<boolean>asyncnewgrounds.js:344

Save a value to one of the player's cloud save slots, which needs a logged in player

  • Any value JSON can hold; the slots are numbered from 1, as many as the app's Newgrounds settings give it
slot number

The slot number

data *

The value to save

Returns
  • Whether it saved, false when not logged in
newgrounds.cloudSave(1, {level, coins});
encrypt(text) → Promise<string>asyncnewgrounds.js:430

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

text string
getScores(id, user, social, skip, limit, period) → Promise<Object>newgrounds.js:329

Get scores from a scoreboard

id number

The scoreboard id

user string|number optional

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

social boolean optional, default false

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

skip number optional, default 0

Number of scores to skip over

limit number optional, default 10

Number of scores to include in the list

period string 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
loadFailure(slot) → 'failed'|'notSave'|undefinednewgrounds.js:379

Why a slot's last cloudLoad gave undefined: 'notSave' when the slot holds a file that is not a save, where a game may offer to start over, 'failed' when it could not be loaded, as when not logged in, where a game must not save over it; undefined when it loaded a save or found the slot empty, or was not loaded

slot number

The slot number

logEvent(name) → Promise<Object>newgrounds.js:421

Count an event of the game's own on its Newgrounds stats page, like a level finished or a button pressed

name string

The event's name

Returns
  • The response JSON object, undefined when the call failed
postScore(id, value) → Promise<Object>newgrounds.js:309

Send message to post score

id number

The scoreboard id

value number

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
resendUnlocks()newgrounds.js:287

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>newgrounds.js:301

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

id number

The medal id

Returns
  • The response JSON object, undefined when the call failed