Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] add new action (JoinArena) #2946

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions @planetarium/lib9c/src/actions/join_arena.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { Address } from "@planetarium/account";
import { BencodexDictionary, type Dictionary } from "@planetarium/bencodex";
import type { RuneSlotInfo } from "../models/rune_slot_info.js";
import { GameAction, type GameActionArgs } from "./common.js";

export type JoinArenaArgs = {
avatarAddress: Address;
championshipId: bigint;
round: bigint;
costumes: Array<Uint8Array>;
equipments: Array<Uint8Array>;
runeInfos: Array<RuneSlotInfo>;
} & GameActionArgs;

export class JoinArena extends GameAction {
protected readonly type_id: string = "join_arena4";

public readonly avatarAddress: Address;
public readonly championshipId: bigint;
public readonly round: bigint;
public readonly costumes: Array<Uint8Array>;
public readonly equipments: Array<Uint8Array>;
public readonly runeInfos: Array<RuneSlotInfo>;

constructor({
avatarAddress,
championshipId,
round,
costumes,
equipments,
runeInfos,
id,
}: JoinArenaArgs) {
super({ id });
this.avatarAddress = avatarAddress;
this.championshipId = championshipId;
this.round = round;
this.costumes = costumes;
this.equipments = equipments;
this.runeInfos = runeInfos;
}

protected plain_value_internal(): Dictionary {
return new BencodexDictionary([
["avatarAddress", this.avatarAddress.toBytes()],
["championshipId", this.championshipId.toString()],
["round", this.round.toString()],
["costumes", this.costumes.map((costume) => costume)],
["equipments", this.equipments.map((equipment) => equipment)],
["runeInfos", this.runeInfos.map((rune) => rune.serialize())],
]);
}
}
5 changes: 5 additions & 0 deletions @planetarium/lib9c/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export {
ChargeActionPoint,
type ChargeActionPointArgs,
} from "./actions/charge_action_point.js";
export {
JoinArena,
type JoinArenaArgs,
} from "./actions/join_arena.js";
export { DailyReward, type DailyRewardArgs } from "./actions/daily_reward.js";
export {
TransferAsset,
Expand Down Expand Up @@ -47,6 +51,7 @@ export {
} from "./models/currencies.js";
export { HashDigest, type AlgorithmNames } from "./models/hashdigest.js";
export { ODIN_GENESIS_HASH, HEIMDALL_GENESIS_HASH } from "./models/networks.js";
export { RuneSlotInfo } from "./models/rune_slot_info.js";

export {
CreateAvatar,
Expand Down
22 changes: 22 additions & 0 deletions @planetarium/lib9c/src/models/rune_slot_info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Value } from "@planetarium/bencodex";

export class RuneSlotInfo {
public readonly slotIndex: bigint;
public readonly runeId: bigint;

constructor(slotIndex: bigint, runeId: bigint) {
this.slotIndex = slotIndex;
this.runeId = runeId;
}

public serialize(): Value[] {
return [this.slotIndex.toString(), this.runeId.toString()];
}

static deserialize(data: {
slotIndex: string;
runeId: string;
}): RuneSlotInfo {
return new RuneSlotInfo(BigInt(data.slotIndex), BigInt(data.runeId));
}
}
33 changes: 33 additions & 0 deletions @planetarium/lib9c/tests/actions/join_arena.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe } from "vitest";
import { JoinArena, RuneSlotInfo, uuidToGuidBytes } from "../../src/index.js";
import { runTests } from "./common.js";
import { avatarAddress } from "./fixtures.js";

describe("JoinArena", () => {
describe("odin", () => {
runTests("valid case", [
new JoinArena({
id: uuidToGuidBytes("ae195a5e-b43f-4c6d-8fd3-9f38311a45eb"),
avatarAddress,
championshipId: BigInt(1),
round: BigInt(1),
costumes: [uuidToGuidBytes("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")],
equipments: [uuidToGuidBytes("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb")],
runeInfos: [new RuneSlotInfo(BigInt(1), BigInt(1))],
}),
]);
});
describe("heimdall", () => {
runTests("valid case", [
new JoinArena({
id: uuidToGuidBytes("ae195a5e-b43f-4c6d-8fd3-9f38311a45eb"),
avatarAddress,
championshipId: BigInt(1),
round: BigInt(1),
costumes: [uuidToGuidBytes("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")],
equipments: [uuidToGuidBytes("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb")],
runeInfos: [new RuneSlotInfo(BigInt(1), BigInt(1))],
}),
]);
});
});
Loading