123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- import * as anchor from "@coral-xyz/anchor";
- import { Program } from "@coral-xyz/anchor";
- import { PublicKey } from "@solana/web3.js";
- import { ComponentPosition } from "../target/types/component_position";
- import { ComponentVelocity } from "../target/types/component_velocity";
- import { BoltComponent } from "../target/types/bolt_component";
- import { SystemSimpleMovement } from "../target/types/system_simple_movement";
- import { SystemFly } from "../target/types/system_fly";
- import { SystemApplyVelocity } from "../target/types/system_apply_velocity";
- import { World } from "../target/types/world";
- import { expect } from "chai";
- import BN from "bn.js";
- enum Direction {
- Left = "Left",
- Right = "Right",
- Up = "Up",
- Down = "Down",
- }
- function serializeArgs(args: any = {}) {
- const jsonString = JSON.stringify(args);
- const encoder = new TextEncoder();
- const binaryData = encoder.encode(jsonString);
- return Buffer.from(
- binaryData.buffer,
- binaryData.byteOffset,
- binaryData.byteLength
- );
- }
- describe("bolt", () => {
- const provider = anchor.AnchorProvider.env();
- anchor.setProvider(provider);
- const worldProgram = anchor.workspace.World as Program<World>;
- const boltComponentPositionProgram = anchor.workspace
- .ComponentPosition as Program<ComponentPosition>;
- const boltComponentVelocityProgram = anchor.workspace
- .ComponentVelocity as Program<ComponentVelocity>;
- const boltComponentProgramOrigin = anchor.workspace
- .BoltComponent as Program<BoltComponent>;
- const systemSimpleMovement = (
- anchor.workspace.SystemSimpleMovement as Program<SystemSimpleMovement>
- ).programId;
- const systemFly = (anchor.workspace.SystemFly as Program<SystemFly>)
- .programId;
- const applyVelocity = (
- anchor.workspace.SystemApplyVelocity as Program<SystemApplyVelocity>
- ).programId;
- let entity1: PublicKey;
- let entity2: PublicKey;
- let componentPositionEntity1: PublicKey;
- let componentPositionEntity2: PublicKey;
- let componentVelocityEntity1: PublicKey;
- it("InitializeWorldsRegistry", async () => {
- const registryPda = FindWorldRegistryPda(worldProgram);
- await worldProgram.methods
- .initializeRegistry()
- .accounts({
- registry: registryPda,
- payer: provider.wallet.publicKey,
- })
- .rpc();
- });
- it("InitializeNewWorld", async () => {
- const registryPda = FindWorldRegistryPda(worldProgram);
- const worldPda = FindWorldPda(worldProgram, new BN(0));
- await worldProgram.methods
- .initializeNewWorld()
- .accounts({
- world: worldPda,
- registry: registryPda,
- payer: provider.wallet.publicKey,
- })
- .rpc();
- });
- it("Add entity 1", async () => {
- const worldPda = FindWorldPda(worldProgram, new BN(0));
- entity1 = FindEntityPda(worldProgram, new BN(0), new BN(0));
- await worldProgram.methods
- .addEntity()
- .accounts({
- world: worldPda,
- entity: entity1,
- payer: provider.wallet.publicKey,
- })
- .rpc();
- });
- it("Add entity 2", async () => {
- const worldPda = FindWorldPda(worldProgram, new BN(0));
- entity2 = FindEntityPda(worldProgram, new BN(0), new BN(1));
- await worldProgram.methods
- .addEntity()
- .accounts({
- world: worldPda,
- entity: entity2,
- payer: provider.wallet.publicKey,
- })
- .rpc();
- });
- it("Add entity 3", async () => {
- const worldPda = FindWorldPda(worldProgram, new BN(0));
- const entityPda = FindEntityPda(worldProgram, new BN(0), new BN(2));
- await worldProgram.methods
- .addEntity()
- .accounts({
- world: worldPda,
- entity: entityPda,
- payer: provider.wallet.publicKey,
- })
- .rpc();
- });
- it("Initialize Original Component on Entity 1, trough the world instance", async () => {
- let componentEntity1 = FindComponentPda(
- boltComponentProgramOrigin.programId,
- entity1,
- "origin-component"
- );
- await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- data: componentEntity1,
- componentProgram: boltComponentProgramOrigin.programId,
- entity: entity1,
- })
- .rpc();
- });
- it("Initialize Original Component on Entity 2, trough the world instance", async () => {
- let componentEntity2 = FindComponentPda(
- boltComponentProgramOrigin.programId,
- entity2,
- "origin-component"
- );
- await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- data: componentEntity2,
- componentProgram: boltComponentProgramOrigin.programId,
- entity: entity2,
- })
- .rpc();
- });
- it("Initialize Position Component on Entity 1", async () => {
- componentPositionEntity1 = FindComponentPda(
- boltComponentPositionProgram.programId,
- entity1,
- "component-position"
- );
- console.log("Component Position E1: ", componentPositionEntity1.toBase58());
- await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- data: componentPositionEntity1,
- componentProgram: boltComponentPositionProgram.programId,
- entity: entity1,
- })
- .rpc();
- });
- it("Initialize Velocity Component on Entity 1", async () => {
- componentVelocityEntity1 = FindComponentPda(
- boltComponentVelocityProgram.programId,
- entity1,
- "component-velocity"
- );
- await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- data: componentVelocityEntity1,
- componentProgram: boltComponentVelocityProgram.programId,
- entity: entity1,
- })
- .rpc();
- });
- it("Initialize Position Component on Entity 2", async () => {
- componentPositionEntity2 = FindComponentPda(
- boltComponentPositionProgram.programId,
- entity2,
- "component-position"
- );
- await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- data: componentPositionEntity2,
- componentProgram: boltComponentPositionProgram.programId,
- entity: entity2,
- })
- .rpc();
- });
- it("Check Position on Entity 1 is default", async () => {
- expect(
- (
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- )
- ).x.toNumber()
- ).to.equal(0);
- expect(
- (
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- )
- ).y.toNumber()
- ).to.equal(0);
- expect(
- (
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- )
- ).z.toNumber()
- ).to.equal(0);
- });
- it("Simple Movement System and Up direction on Entity 1", async () => {
- const args = {
- direction: Direction.Up,
- };
- await worldProgram.methods
- .apply(serializeArgs(args)) // Move Up
- .accounts({
- componentProgram: boltComponentPositionProgram.programId,
- boltSystem: systemSimpleMovement,
- boltComponent: componentPositionEntity1,
- })
- .rpc({ skipPreflight: true });
- expect(
- (
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- )
- ).y.toNumber()
- ).to.equal(1);
- const componentData =
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- );
- const x = componentData.x.toNumber();
- const y = componentData.y.toNumber();
- const z = componentData.z.toNumber();
- console.log("+-----------------------------+");
- console.log("| Movement System: Entity 1 |");
- console.log("+----------------+------------+");
- console.log("| Coordinate | Value |");
- console.log("+----------------+------------+");
- console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
- console.log("+----------------+------------+");
- console.log("| |");
- console.log("+-----------------------------+");
- });
- it("Simple Movement System and Right direction on Entity 1", async () => {
- const args = {
- direction: Direction.Right,
- };
- await worldProgram.methods
- .apply(serializeArgs(args)) // Move Right
- .accounts({
- componentProgram: boltComponentPositionProgram.programId,
- boltSystem: systemSimpleMovement,
- boltComponent: componentPositionEntity1,
- })
- .rpc({ skipPreflight: true });
- expect(
- (
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- )
- ).y.toNumber()
- ).to.equal(1);
- expect(
- (
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- )
- ).y.toNumber()
- ).to.equal(1);
- const componentData =
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- );
- const x = componentData.x.toNumber();
- const y = componentData.y.toNumber();
- const z = componentData.z.toNumber();
- console.log("+-----------------------------+");
- console.log("| Movement System: Entity 1 |");
- console.log("+----------------+------------+");
- console.log("| Coordinate | Value |");
- console.log("+----------------+------------+");
- console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
- console.log("+----------------+------------+");
- console.log("| |");
- console.log("+-----------------------------+");
- });
- it("Fly System on Entity 1", async () => {
- await worldProgram.methods
- .apply(Buffer.alloc(0)) // Move Up
- .accounts({
- componentProgram: boltComponentPositionProgram.programId,
- boltSystem: systemFly,
- boltComponent: componentPositionEntity1,
- })
- .rpc();
- expect(
- (
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- )
- ).z.toNumber()
- ).to.equal(1);
- const componentData =
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- );
- const x = componentData.x.toNumber();
- const y = componentData.y.toNumber();
- const z = componentData.z.toNumber();
- console.log("+-----------------------------+");
- console.log("| Fly: Position Entity 1 |");
- console.log("+----------------+------------+");
- console.log("| Coordinate | Value |");
- console.log("+----------------+------------+");
- console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
- console.log("+----------------+------------+");
- console.log("| |");
- console.log("+-----------------------------+");
- });
- it("Apply Velocity on Entity 1", async () => {
- await worldProgram.methods
- .apply2(Buffer.alloc(0))
- .accounts({
- componentProgram1: boltComponentVelocityProgram.programId,
- componentProgram2: boltComponentPositionProgram.programId,
- boltSystem: applyVelocity,
- boltComponent1: componentVelocityEntity1,
- boltComponent2: componentPositionEntity1,
- })
- .remainingAccounts([
- {
- pubkey: componentPositionEntity1,
- isWritable: false,
- isSigner: false,
- },
- ])
- .rpc();
- console.log("Component Velocity: ", componentVelocityEntity1.toBase58());
- let componentData =
- await boltComponentVelocityProgram.account.velocity.fetch(
- componentVelocityEntity1
- );
- let x = componentData.x.toNumber();
- let y = componentData.y.toNumber();
- let z = componentData.z.toNumber();
- const tmp = componentData.lastApplied.toNumber();
- console.log("+-----------------------------+");
- console.log("| Apply Velocity: Velocity Entity 1 |");
- console.log("+----------------+------------+");
- console.log("| Coordinate | Value |");
- console.log("+----------------+------------+");
- console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Timestamp | ${String(tmp).padEnd(10, " ")} |`);
- console.log("+----------------+------------+");
- console.log("| |");
- console.log("+-----------------------------+");
- let positionData =
- await boltComponentPositionProgram.account.position.fetch(
- componentPositionEntity1
- );
- x = positionData.x.toNumber();
- y = positionData.y.toNumber();
- z = positionData.z.toNumber();
- console.log("+-----------------------------+");
- console.log("| Apply Velocity: Position Entity 1 |");
- console.log("+----------------+------------+");
- console.log("| Coordinate | Value |");
- console.log("+----------------+------------+");
- console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
- console.log("| | |");
- console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
- console.log("+----------------+------------+");
- console.log("| |");
- console.log("+-----------------------------+");
- });
- // Utils
- function FindWorldRegistryPda(program: Program<World>) {
- return PublicKey.findProgramAddressSync(
- [Buffer.from("registry")],
- program.programId
- )[0];
- }
- function FindWorldPda(program: Program<World>, id: BN) {
- return PublicKey.findProgramAddressSync(
- [Buffer.from("world"), id.toBuffer("le", 8)],
- program.programId
- )[0];
- }
- function FindEntityPda(program: Program<World>, worldId: BN, entityId: BN) {
- return PublicKey.findProgramAddressSync(
- [
- Buffer.from("entity"),
- worldId.toBuffer("be", 8),
- entityId.toBuffer("be", 8),
- ],
- program.programId
- )[0];
- }
- function FindComponentPda(
- program: PublicKey,
- entity: PublicKey,
- seed: string = "component"
- ) {
- return PublicKey.findProgramAddressSync(
- [Buffer.from(seed), entity.toBytes()],
- program
- )[0];
- }
- });
|