Skip to main content

Basic Game Server

Example - Basic Game Server

Details

This example focuses on creating a basic game server demonstration for integration with Haste. The example will authenticate the game server with the Haste API via a Machine to Machine (M2M) authentication token. This process is dependent upon the game server client id and game server secret that can be found in the Haste Developer Portal. In addition you will need to find your player id. You can find your player id by logging into the the Haste Arcade and going to My Account. The player id will be showing as the 'referralid' in the "My Referral Link" section of the page. All three of these values are UUIDs and will be required in the .env file that you must create.

The example will only perform a call to get the leaderboards for a game and the payouts for a player. The example code does implement the play and score endpoints as well to demonstrate the process.

Link to Github

Run Example

In order to run the example please perform the following commands:

git clone git@github.com:hastearcade/haste-sdk.git
cd haste-sdk/examples/server
npm install
cp .env-example .env
npm start

App.tsx

import "dotenv/config";
import { Haste } from "@hastearcade/server";
import { Leaderboard, Play, Player } from "@hastearcade/models";

let haste: Haste;

// authenticate your game server against the Haste
// API. This will require the .env to be updated
// appropriately to run the example.
async function authenticate() {
// see Testing for more details
const environment = "production";
haste = await Haste.build(
// Retrieve from Developer Portal
process.env.HASTE_SERVER_CLIENT_ID,
// Retrieve from Developer Portal
process.env.HASTE_SERVER_CLIENT_SECRET,
environment
);
}

// this function will retrieve the leaderboards
// to display to the player. This will allow
// the player to select which payment tier
// they would like to play at. Please use
// the 'formattedName' property when populating
// your game client UI to display to the user.
async function getLeaderboards() {
// if you want to include Party Mode leaderboards, pass in playerId to .leaderboards()
const leaderBoards = await haste.game.leaderboards();
// eslint-disable-next-line no-console
console.log(leaderBoards);
}

// this function will retrieve all payouts that
// have been paid to the player for this game
// The payouts will be across all leaderboards.
async function getPayouts(playerId: string) {
const player = new Player(playerId);
const payouts = await haste.game.payouts(player);
// eslint-disable-next-line no-console
console.log(JSON.stringify(payouts, null, 2));
}

// perform a play against a specific leaderboard.
// The player should select a leaderboard from your
// game client and pass the id to the game server.
// The game server will then perform the 'insert quarter into arcade'
// process. This process will payout that quarter to all existing
// players on the leaderboard. The play endpoint will return a Play
// object which contains a play id. The play id and leaderboard id
// must be passed to the subsequent call to .score.
async function play(playerId: string, leaderboardId: string) {
const play = await haste.game.play(
new Player(playerId),
new Leaderboard(leaderboardId)
);
// eslint-disable-next-line no-console
console.log(JSON.stringify(play));
}

// send the score to the Haste API once the game has completed.
async function score(play: Play, scoreValue: number) {
const score = await haste.game.score(play, scoreValue);
// eslint-disable-next-line no-console
console.log(JSON.stringify(score));
}

async function run() {
await authenticate();
await getLeaderboards();
await getPayouts(process.env.PLAYER_ID ?? "");
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
run();