This is an SDK for integrating games into Playnation.
The first SDK version follows the concept from game-sdk-example.
Online Demo: https://playnation-sdk-demo.netlify.app
- Connect game on the iframe with
postMessage
and window listening. - The game will interact with the game server through the SDK and parent window.
What is implemented in the game.ts or game-farming.ts:
Init flow: Get basic information
init()
getSDKVersion()
getPlayer()
getTournament()
Play casual game flow
play()
start the gametrackScore()
on playingsignResult()
sign and submit the resultgetPlayer()
to retrieve player data
Play farming game
play()
start the gamesignPayload()
sign to get the game state with secret key from the gameupdateState()
update signed payload every time the game state changes
clone this repository and run the example:
git clone
cd ./examples/iframe-implementation
yarn && yarn dev
Include the SDK in your HTML file:
<script src="https://unpkg.com/@playnation/game-sdk@latest/browser/sdk.js"></script>
Create a JavaScript file (e.g., main.js) with something like this example below:
document.addEventListener('DOMContentLoaded', async () => {
const sdk = window.PlaynationGameSDK;
// Run on init
const initParams = { clientId: 'your-client-id' };
const initResponse = await sdk.init(initParams);
console.log('SDK Initialized:', initResponse);
// Get SDK version
const version = sdk.getVersion();
console.log('SDK Version:', version);
// Get player information
let player = await sdk.getPlayer();
console.log('Player Info:', player);
// Get tournament information
const tournament = await sdk.getTournament();
console.log('Tournament Info:', tournament);
// Play the game
async function playGame() {
// Start the game play
const playResponse = await sdk.play();
console.log('Play Response:', playResponse);
// Track score
await sdk.trackScore(playResponse.token, 100);
console.log('Score tracked');
// Sign result
const signedResult = await sdk.signResult(playResponse.token, 'game-token', 100);
console.log('Signed Result:', signedResult);
// Get player information
player = await sdk.getPlayer();
}
});
npm:
npm install @playnation/sdk
or with yarn:
yarn add @playnation/sdk
Use latest version:
<script src="https://unpkg.com/@playnation/game-sdk@latest/browser/sdk.js"></script>
Or specify version:
<script src="https://unpkg.com/@playnation/game-sdk@1.1.0/browser/sdk.js"></script>
Initializes the SDK with the given parameters.
- params: Initialization parameters.
- Returns: A promise that resolves with the current timestamp.
Gets the SDK version.
- Returns: The SDK version.
Gets the player information.
- Returns: A promise that resolves with the player information.
- player: Player information will include the following fields:
- id: The player ID.
- name: The player name.
- totalScore: The player's total score.
- highScore: The player's highest score.
- balanceNPS: The player's NPS.
- energy: The player's energy.
- gameEnergy: Energy cost each play time.
- pointConversionRate: The point conversion rate.
- state: The latest state in case farming game.
Gets the tournament information.
- Returns: A promise that resolves with the tournament information or undefined if no tournament is found.
Starts the game play. Note: This method will return a token that should be used to track the score and sign the result.
- Returns: A promise that resolves with the play response.
Tracks the score for a given game play.
- gamePlayId: The game play ID.
- score: The score to track.
- Returns: A promise that resolves when the score is tracked.
Signs & submits the result of a game play. The game result will be signed and submitted to the Playnation server.
- gamePlayId: The game play ID.
- gameToken: The game token.
- score: The score to sign.
- Returns: A promise that resolves with the signed result.
Updates the state of the game. Playnation only updates the game state but does not validate its details, so the data needs to be signed with signPayload and validated between the game and the server to ensure security.
- payload: The state payload to update.
- Returns: A promise that resolves with a boolean indicating success.
Signs a payload with a given key.
- payload: The payload to sign.
- key: The key to sign the payload with.
- Returns: A promise that resolves with the signed payload.
Enum representing various error codes that can be returned by the SDK.
-
SYSTEM_ERROR
:-1
- Description: Something went wrong.
-
INVALID_REQUEST
:10
- Description: The request is invalid.
-
INVALID_SCORE
:120
- Description: The score was not accepted (cheat detected).
-
USER_REJECT
:130
- Description: The user rejected the transaction (buy tickets or items).
-
NOT_ENOUGH_ENERGY
:150
- Description: Not enough energy to play the game.
-
TOUR_NOT_AVAILABLE
:100
- Description: The tournament has ended or is disabled.
-
NOT_ENOUGH_NPS
:110
- Description: Not enough NPS to buy tickets or items.
-
NOT_ENOUGH_TICKET
:140
- Description: Not enough tickets to play the game.
cd ./examples/iframe-implementation
yarn && yarn dev
You also develop your game from the example by modifying the index.html
file.
Change the src
attribute of the iframe to your game URL.
<button class="game-button" data-url="[your-game-url]" type="button">Casual Game</button>
<button class="game-button" data-url="[your-game-url]" type="button">Farming Game</button>
cd ./examples/iframe-implementation
yarn && yarn build
Example will use the SDK from the src
folder in the root directory instead of from node_modules
.
cd ./examples/iframe-implementation
yarn && yarn dev:source
yarn && yarn build