The Haste Arcade SDK is a client-side integration that does not require server-side components, simplifying the setup process. The platform displays your registered game in a full-width and full-height iFrame, facilitating seamless communication between the host client (arcade) and your game.
Installation
npmihaste-arcade-sdk
Init()
Import and create a new instance of the GameService. Once the DOM is loaded, initialize it.
import { GameService, Origin } from"haste-arcade-sdk";constgame=newGameService("f8c22e6c-1086-4529-8800-2c72f98b9915",// your gameIdOrigin.DEV,// this is the host (arcade) origin. Use DEV or PROD);// Ensure that the DOM is loaded before calling the init methoddocument.addEventListener("DOMContentLoaded",function () {game.init();});// This may be implemented differently depending on your game's framework.
Play()
Your game must include a "Play" or "Start" button to initiate gameplay. Upon clicking this button, a message will be sent to the arcade, prompting it to request that the user lock some HST into a smart-contract to begin playing.
Wait until you receive a playId back from the arcade before starting the game. This is done via the on() method.
myPlayButton.onClick(async()=> {// You should show some suspense hereconstres=awaitgame.play();if (!res?.playId) {// kill suspence and DO NOT start the gamereturn; }; console.log(res.playId);// kill suspence and start the game};// Alternantively, you can call play and listen for a play eventgame.play();game.on("play", (message:ReceivePlayMessage) => {console.log(message.playId);});
submitScore()
When the user has finished playing the game, call the submitScore() method and pass in the playId and score. The user will will sign a message with their wallet and manually submit their score. No need to listen for messages for score submissions.
let score =0;let timeToFinsih =0;let ringsCollected =0;// handle setting the score variable somewhere in your game logic then call submitScore()score += ringsCollected - timeToFinish;constres=awaitgame.submitScore(playId, score);console.log(res.playId);// Alternantively, you can call submitScore and listen for a score eventgame.submitScore();game.on("score", (message:ReceiveSubmitScoreMessage) => {console.log(message.playId);});
getLeaderboard()
If you'd like to display the current leaderboard directly in your game, the SDK makes this simple.
myLeaderboardButton.onClick(()=> {// use suspenseconstres=awaitgame.getLeaderboard();console.log(res.leaderboard);// kill suspence};// Alternantively, you can call getLeaderboard and listen for a leaderboard eventgame.getLeaderboard();game.on("leaderboard", (message:ReceiveLeaderboardMessage) => {console.log(message.leaderboard);});
transferHst()
Monetize your game with HST transfers by requesting payment from within your game. Simply pass in the amount of HST to transfer.
myPaymentButton.onClick(()=> {// use suspenseconstres=awaitgame.hstTransfer(100);if (!res.txid) {// If the user rejects the request, there will be no txid kill suspensereturn; }; console.log(res.txid);// Provision access or give them what they paid for and kil suspense};// Alternantively, you can call hstTransfer and listen for a hstTransfer eventgame.hstTransfer(100);game.on("hstTransfer", (message:ReceiveHstTransferMessage) => {console.log(message.txid);});