Skip to content

Commit

Permalink
⚡ Add a configurable option to determine whether to allow multiple sc…
Browse files Browse the repository at this point in the history
…ores per player or not. (#9)

* 📝 Add Issues and PR template

* ✨ Add scores deduplication

* ✨ Add configurable option to allow multiple scores per player

Add configurable option wether to allow or not multiple scores per player

* ♻️ Remove package lock

* ♻️ Reset yarn lock

* ♻️ Code Refactoring

* 🔖 Program v0.1.1

* 📝 Revert to localnet

* ✨ Add option to change order asc/desc of existing leaderboard
  • Loading branch information
GabrielePicco authored Dec 29, 2023
1 parent a73f6cf commit cb857cc
Show file tree
Hide file tree
Showing 20 changed files with 846 additions and 163 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion client/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@magicblock-labs/soar-sdk",
"version": "0.1.21",
"version": "0.1.22",
"description": "Sdk bindings for the SOAR smart contract.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,6 +45,7 @@
"eslint": "^8.33.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-n": "^16.5.0",
"eslint-plugin-react": "^7.32.2",
"prettier": "^2.6.2",
"typedoc": "^0.25.1",
Expand Down
102 changes: 96 additions & 6 deletions client/sdk/src/idl/soar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type Soar = {
version: "0.1.0";
version: "0.1.1";
name: "soar";
constants: [
{
Expand Down Expand Up @@ -233,7 +233,8 @@ export type Soar = {
{
name: "updateLeaderboard";
docs: [
"Update's a leaderboard's description and nft metadata information."
"Update's a leaderboard's description, nft metadata information, min/max score, or whether",
"or not multiple scores are allowed for a single player."
];
accounts: [
{
Expand All @@ -250,6 +251,12 @@ export type Soar = {
name: "leaderboard";
isMut: true;
isSigner: false;
},
{
name: "topEntries";
isMut: true;
isSigner: false;
isOptional: true;
}
];
args: [
Expand All @@ -264,6 +271,30 @@ export type Soar = {
type: {
option: "publicKey";
};
},
{
name: "newMinScore";
type: {
option: "u64";
};
},
{
name: "newMaxScore";
type: {
option: "u64";
};
},
{
name: "newIsAscending";
type: {
option: "bool";
};
},
{
name: "newAllowMultipleScores";
type: {
option: "bool";
};
}
];
},
Expand Down Expand Up @@ -1111,6 +1142,13 @@ export type Soar = {
type: {
option: "publicKey";
};
},
{
name: "allowMultipleScores";
docs: [
"Whether or not multiple scores are allowed for a single player."
];
type: "bool";
}
];
};
Expand Down Expand Up @@ -1467,11 +1505,18 @@ export type Soar = {
type: "u8";
},
{
name: "scoresOrder";
name: "isAscending";
docs: [
"Order by which scores are stored. `true` for ascending, `false` for descending."
];
type: "bool";
},
{
name: "allowMultipleScores";
docs: [
"Whether or not multiple scores are kept in the leaderboard for a single player."
];
type: "bool";
}
];
};
Expand Down Expand Up @@ -1708,7 +1753,7 @@ export type Soar = {
};

export const IDL: Soar = {
version: "0.1.0",
version: "0.1.1",
name: "soar",
constants: [
{
Expand Down Expand Up @@ -1942,7 +1987,8 @@ export const IDL: Soar = {
{
name: "updateLeaderboard",
docs: [
"Update's a leaderboard's description and nft metadata information.",
"Update's a leaderboard's description, nft metadata information, min/max score, or whether",
"or not multiple scores are allowed for a single player.",
],
accounts: [
{
Expand All @@ -1960,6 +2006,12 @@ export const IDL: Soar = {
isMut: true,
isSigner: false,
},
{
name: "topEntries",
isMut: true,
isSigner: false,
isOptional: true,
},
],
args: [
{
Expand All @@ -1974,6 +2026,30 @@ export const IDL: Soar = {
option: "publicKey",
},
},
{
name: "newMinScore",
type: {
option: "u64",
},
},
{
name: "newMaxScore",
type: {
option: "u64",
},
},
{
name: "newIsAscending",
type: {
option: "bool",
},
},
{
name: "newAllowMultipleScores",
type: {
option: "bool",
},
},
],
},
{
Expand Down Expand Up @@ -2821,6 +2897,13 @@ export const IDL: Soar = {
option: "publicKey",
},
},
{
name: "allowMultipleScores",
docs: [
"Whether or not multiple scores are allowed for a single player.",
],
type: "bool",
},
],
},
},
Expand Down Expand Up @@ -3176,12 +3259,19 @@ export const IDL: Soar = {
type: "u8",
},
{
name: "scoresOrder",
name: "isAscending",
docs: [
"Order by which scores are stored. `true` for ascending, `false` for descending.",
],
type: "bool",
},
{
name: "allowMultipleScores",
docs: [
"Whether or not multiple scores are kept in the leaderboard for a single player.",
],
type: "bool",
},
],
},
},
Expand Down
5 changes: 4 additions & 1 deletion client/sdk/src/instructions/accountsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,13 @@ export class AccountsBuilder {
updateLeaderboardAccounts = async (
authority: PublicKey,
leaderboard: PublicKey,
game?: PublicKey
game?: PublicKey,
topEntries?: PublicKey
): Promise<{
authority: PublicKey;
game: PublicKey;
leaderboard: PublicKey;
topEntries: PublicKey | null;
}> => {
const gameAddress =
game ?? (await this.program.account.leaderBoard.fetch(leaderboard)).game;
Expand All @@ -550,6 +552,7 @@ export class AccountsBuilder {
authority,
game: gameAddress,
leaderboard,
topEntries: topEntries ?? null,
};
};

Expand Down
6 changes: 4 additions & 2 deletions client/sdk/src/instructions/ixBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ export class InstructionBuilder {
args: UpdateLeaderboardArgs,
authority: PublicKey,
leaderboard: PublicKey,
game?: PublicKey
game?: PublicKey,
topEntries?: PublicKey
): Promise<
[
InstructionBuilder,
Expand All @@ -275,7 +276,8 @@ export class InstructionBuilder {
const accounts = await this.accounts.updateLeaderboardAccounts(
authority,
leaderboard,
game
game,
topEntries
);
const instruction = await updateLeaderBoardInstruction(
this.program,
Expand Down
10 changes: 9 additions & 1 deletion client/sdk/src/instructions/rawInstructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,19 @@ export const updateLeaderBoardInstruction = async (
authority: PublicKey;
game: PublicKey;
leaderboard: PublicKey;
topEntries: PublicKey | null;
},
pre?: TransactionInstruction[]
): Promise<TransactionInstruction> => {
return program.methods
.updateLeaderboard(args.newDescription, args.newNftMeta)
.updateLeaderboard(
args.newDescription,
args.newNftMeta,
args.newMinScore,
args.newMaxScore,
args.newIsAscending,
args.newAllowMultipleScores
)
.accounts(accounts)
.preInstructions(pre ?? [])
.instruction();
Expand Down
6 changes: 4 additions & 2 deletions client/sdk/src/soar.game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export class GameClient {
scoresOrder: boolean,
decimals?: number,
minScore?: BN,
maxScore?: BN
maxScore?: BN,
allowMultipleScores?: boolean
): Promise<InstructionResult.AddLeaderBoard> {
return this.program.addNewGameLeaderBoard(
this.address,
Expand All @@ -128,7 +129,8 @@ export class GameClient {
scoresOrder,
decimals,
minScore,
maxScore
maxScore,
allowMultipleScores
);
}

Expand Down
21 changes: 17 additions & 4 deletions client/sdk/src/soar.program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ export class SoarProgram {
scoresOrder: boolean,
decimals?: number,
minScore?: BN,
maxScore?: BN
maxScore?: BN,
allowMultipleScores?: boolean
): Promise<InstructionResult.AddLeaderBoard> {
this.builder.clean();

Expand All @@ -246,10 +247,11 @@ export class SoarProgram {
description,
nftMeta,
scoresToRetain,
scoresOrder,
isAscending: scoresOrder,
decimals: decimals ?? null,
minScore: minScore ?? null,
maxScore: maxScore ?? null,
allowMultipleScores: allowMultipleScores ?? false,
},
gameAddress,
authority
Expand All @@ -267,7 +269,12 @@ export class SoarProgram {
authority: PublicKey,
leaderboard: PublicKey,
newDescription?: string,
newNftMeta?: PublicKey
newNftMeta?: PublicKey,
newMinScore?: BN,
newMaxScore?: BN,
newIsAscending?: boolean,
newAllowMultipleScores?: boolean,
topEntries?: PublicKey
): Promise<InstructionResult.UpdateLeaderboard> {
this.builder.clean();
if (newDescription === undefined && newNftMeta === undefined) {
Expand All @@ -280,9 +287,15 @@ export class SoarProgram {
{
newDescription: newDescription ?? null,
newNftMeta: newNftMeta ?? null,
newMinScore: newMinScore ?? null,
newMaxScore: newMaxScore ?? null,
newIsAscending: newIsAscending ?? null,
newAllowMultipleScores: newAllowMultipleScores ?? null,
},
authority,
leaderboard
leaderboard,
undefined,
topEntries ?? undefined
);

return { transaction: step[0].build() };
Expand Down
4 changes: 4 additions & 0 deletions client/sdk/src/state/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class LeaderBoardAccount {
public readonly decimals: number,
public readonly minScore: BN,
public readonly maxScore: BN,
public readonly allowMultipleScores: boolean,
public readonly topEntries: PublicKey | null
) {}

Expand All @@ -31,6 +32,7 @@ export class LeaderBoardAccount {
account.decimals,
account.minScore,
account.maxScore,
account.allowMultipleScores,
account.topEntries
);
}
Expand All @@ -45,6 +47,7 @@ export class LeaderBoardAccount {
decimals: number;
minScore: string;
maxScore: string;
allowMultipleScores: boolean;
topEntries: string | null;
} {
return {
Expand All @@ -56,6 +59,7 @@ export class LeaderBoardAccount {
decimals: this.decimals,
minScore: this.minScore.toString(),
maxScore: this.maxScore.toString(),
allowMultipleScores: this.allowMultipleScores,
topEntries: this.topEntries ? this.topEntries.toBase58() : null,
};
}
Expand Down
Loading

0 comments on commit cb857cc

Please sign in to comment.