TapSdk
The main SDK class for initializing the TapTap PC SDK, handling user authentication, and checking game/DLC ownership.
Import
import { TapSdk } from 'tapsdk-pc';Static Methods
restartAppIfNecessary()
Check if the app needs to restart. Must be called before creating a TapSdk instance.
static restartAppIfNecessary(clientId: string): booleanParameters:
clientId- The client ID from TapTap developer center
Returns: true if app needs restart, false otherwise
Example:
if (TapSdk.restartAppIfNecessary('your_client_id')) {
// TapTap will relaunch the game
process.exit(0);
}Important
If this method returns true, you must exit your application immediately. TapTap will relaunch it properly.
isInitialized()
Check if the SDK is initialized.
static isInitialized(): booleanReturns: true if SDK is initialized, false otherwise
Example:
if (TapSdk.isInitialized()) {
console.log('SDK is ready');
}Constructor
new TapSdk()
Initialize the SDK with your public key.
constructor(pubKey: string)Parameters:
pubKey- The public key from TapTap developer center
Throws: Error if SDK initialization fails
Example:
const sdk = new TapSdk('your_public_key');Instance Methods
getClientId()
Get the client ID.
getClientId(): string | nullReturns: The client ID or null if not available
runCallbacks()
Poll for events from the SDK. Call this regularly in your game loop.
runCallbacks(): TapEvent[]Returns: Array of events that occurred since the last poll
Example:
function gameLoop() {
const events = sdk.runCallbacks();
for (const event of events) {
switch (event.eventId) {
case EventId.SYSTEM_STATE_CHANGED:
handleSystemState(event);
break;
case EventId.AUTHORIZE_FINISHED:
handleAuth(event);
break;
// ... handle other events
}
}
requestAnimationFrame(gameLoop);
}authorize()
Request user authorization.
authorize(scopes: string): voidParameters:
scopes- Permission scopes to request (e.g.,"public_profile")
Example:
sdk.authorize('public_profile');
// Handle the result in runCallbacks()
const events = sdk.runCallbacks();
for (const event of events) {
if (event.eventId === EventId.AUTHORIZE_FINISHED) {
if (event.token) {
console.log('Authorized! OpenID:', sdk.getOpenId());
}
}
}getOpenId()
Get the current user's OpenID.
getOpenId(): string | nullReturns: The user's OpenID or null if not authorized
isGameOwned()
Check if the user owns the current game.
isGameOwned(): booleanReturns: true if user owns the game, false otherwise
Example:
if (!sdk.isGameOwned()) {
console.log('Please purchase the game on TapTap');
process.exit(1);
}isDlcOwned()
Check if the user owns a specific DLC.
isDlcOwned(dlcId: string): booleanParameters:
dlcId- The DLC identifier
Returns: true if user owns the DLC, false otherwise
Example:
if (sdk.isDlcOwned('expansion_pack_1')) {
// Enable DLC content
enableExpansionPack();
}showDlcStore()
Show the store page for a specific DLC.
showDlcStore(dlcId: string): booleanParameters:
dlcId- The DLC identifier
Returns: true if store page opened, false otherwise
Example:
if (!sdk.isDlcOwned('expansion_pack_1')) {
// Prompt user to purchase
console.log('This content requires the Expansion Pack');
sdk.showDlcStore('expansion_pack_1');
}shutdown()
Shut down the SDK. The SDK instance cannot be used after this.
shutdown(): voidExample:
// When exiting the game
sdk.shutdown();
process.exit(0);Complete Example
import { TapSdk, EventId, SystemState } from 'tapsdk-pc';
// 1. Check restart before anything else
if (TapSdk.restartAppIfNecessary('your_client_id')) {
process.exit(0);
}
// 2. Initialize
const sdk = new TapSdk('your_public_key');
// 3. Verify ownership
if (!sdk.isGameOwned()) {
console.log('Game not owned');
process.exit(1);
}
// 4. Request authorization
sdk.authorize('public_profile');
// 5. Game loop
let running = true;
while (running) {
const events = sdk.runCallbacks();
for (const event of events) {
switch (event.eventId) {
case EventId.SYSTEM_STATE_CHANGED:
if (event.state === SystemState.PLATFORM_SHUTDOWN) {
running = false;
}
break;
case EventId.AUTHORIZE_FINISHED:
if (event.token) {
console.log('OpenID:', sdk.getOpenId());
}
break;
}
}
// Your game update logic here...
}
// 6. Cleanup
sdk.shutdown();