Skip to content

Commit

Permalink
feat(server): updating use of Play Resource
Browse files Browse the repository at this point in the history
A request was made to allow the developers to retrieve the Play object with an id only. This feature
implements the functionality with a corresponding API change. In addition, the ability to retrieve a
play transaction was moved as well to get the surface area consistent.
  • Loading branch information
rallieon committed Nov 1, 2021
1 parent 6e69497 commit 93aedac
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,37 @@ Upon hitting an end state for your game (i.e. the player gets hit by a bomb and
await haste.game.score(currentPlay, score);
```
##### Get Play
There are times when you may prefer not to serialize the entire Play object and store in a database or in memory. If you want to store the id only, then you will need to retrieve the play object before calling `score`. To retrieve the full Play object from the Haste API you may use the following:
```typescript
const play = await haste.play.find(playId);
console.log(play);
/*
output:
{
id: "guid",
gameId: "your game guid",
playerId: "player guid",
leaderboard: {
id: "guid",
name: "Beginner",
cost: 2000, // cost to play in this leaderboard in Duro.
}
}
```
##### Payment Transaction
When submitting a play the Haste ecosystem is performing a payout on behalf of the player. The underlying system uses the wallet to perform the payout, and every payout has a transaction hash associated with it. If you need access to the transaction you can use the following function call:
```typescript
const haste = await Haste.build(process.env.HASTE_SERVER_CLIENT_ID, process.env.HASTE_SERVER_CLIENT_SECRET);
const play = await haste.game.play(new Player(playerId), new Leaderboard(leaderboardId));
const transaction = await haste.game.getPlayTransaction(play);
const transaction = await haste.play.transaction(play);
console.log(transaction);
/*
Expand Down
25 changes: 24 additions & 1 deletion packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,37 @@ Upon hitting an end state for your game (i.e. the player gets hit by a bomb and
await haste.game.score(currentPlay, score);
```
##### Get Play
There are times when you may prefer not to serialize the entire Play object and store in a database or in memory. If you want to store the id only, then you will need to retrieve the play object before calling `score`. To retrieve the full Play object from the Haste API you may use the following:
```typescript
const play = await haste.play.find(playId);
console.log(play);
/*
output:
{
id: "guid",
gameId: "your game guid",
playerId: "player guid",
leaderboard: {
id: "guid",
name: "Beginner",
cost: 2000, // cost to play in this leaderboard in Duro.
}
}
```
##### Payment Transaction
When submitting a play the Haste ecosystem is performing a payout on behalf of the player. The underlying system uses the wallet to perform the payout, and every payout has a transaction hash associated with it. If you need access to the transaction you can use the following function call:
```typescript
const haste = await Haste.build(process.env.HASTE_SERVER_CLIENT_ID, process.env.HASTE_SERVER_CLIENT_SECRET);
const play = await haste.game.play(new Player(playerId), new Leaderboard(leaderboardId));
const transaction = await haste.game.getPlayTransaction(play);
const transaction = await haste.play.transaction(play);
console.log(transaction);
/*
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/api/haste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import axios from 'axios';
import { buildUrl } from '../util/urlBuilder';
import { validateAuthenticationToken } from './auth/validate';
import { isBrowser } from '../util/environmentCheck';
import { PlayResource } from './resources/game/playResource';
export class Haste {
private configuration?: HasteConfiguration;
game: GameResource;
play: PlayResource;

private constructor(configuration: HasteConfiguration, gameDetails: Game) {
this.configuration = configuration;
this.game = new GameResource(this.configuration, gameDetails);
this.play = new PlayResource(this.configuration);
}

static async getJwt(
Expand Down
7 changes: 1 addition & 6 deletions packages/server/src/api/resources/game/gameResource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreatePlay, Play, CreateScore, Score, Leaderboard, Game, Player, PlayTransaction } from '@hastearcade/models';
import { CreatePlay, Play, CreateScore, Score, Leaderboard, Game, Player } from '@hastearcade/models';
import { HasteConfiguration } from '../../../config/hasteConfiguration';
import { BaseResource } from '../baseResource';

Expand All @@ -18,11 +18,6 @@ export class GameResource extends BaseResource {
return play;
}

async getPlayTransaction(play: Play) {
const path = `/arcades/${this.configuration.arcadeId}/games/${this.configuration.gameId}/play/${play.id}/transaction`;
return await this.get<PlayTransaction>(path);
}

async score(play: Play, score: number) {
const payload = new CreateScore(play.id, play.leaderboard.id, score);
const path = `/arcades/${this.configuration.arcadeId}/games/${this.configuration.gameId}/score`;
Expand Down
14 changes: 14 additions & 0 deletions packages/server/src/api/resources/game/playResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Play, PlayTransaction } from '@hastearcade/models';
import { BaseResource } from '../baseResource';

export class PlayResource extends BaseResource {
async transaction(id: string) {
const path = `/arcades/${this.configuration.arcadeId}/games/${this.configuration.gameId}/play/${id}/transaction`;
return await this.get<PlayTransaction>(path);
}

async find(id: string) {
const path = `/arcades/${this.configuration.arcadeId}/games/${this.configuration.gameId}/play/${id}`;
return await this.get<Play>(path);
}
}

0 comments on commit 93aedac

Please sign in to comment.