Skip to content

Commit

Permalink
Headless Client πŸ—£πŸ—‘
Browse files Browse the repository at this point in the history
  • Loading branch information
Kooshaba committed Jan 31, 2024
1 parent 5bdc0ec commit 4e3c37f
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 251 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"dev": "pnpm --filter ecs-browser run build && pnpm --filter phaserx run build && run-pty % % pnpm run dev:client % pnpm run dev:contracts % pnpm run dev:upload-map % pnpm run dev:copy-maps % pnpm run dev:create-debug-matches",
"dev:client": "pnpm --filter client run dev",
"dev:contracts": "DEBUG=mud:* pnpm --filter contracts run dev",
"dev:upload-map": "wait-port localhost:8545 && sleep 60 && pnpm --filter bots run upload-map:dev",
"dev:copy-maps": "wait-port localhost:8545 && sleep 70 && pnpm --filter bots run copy-maps:dev",
"dev:create-debug-matches": "wait-port localhost:8545 && pnpm --filter bots run create-debug-matches:dev"
"dev:upload-map": "wait-port localhost:8545 && sleep 60 && pnpm --filter headless-client run upload-map:dev",
"dev:copy-maps": "wait-port localhost:8545 && sleep 70 && pnpm --filter headless-client run copy-maps:dev",
"dev:create-debug-matches": "wait-port localhost:8545 && pnpm --filter headless-client run create-debug-matches:dev"
},
"devDependencies": {
"@latticexyz/cli": "2.0.0-main-5ab67e33",
Expand Down
3 changes: 0 additions & 3 deletions packages/bots/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions packages/bots/mass_bots.sh

This file was deleted.

10 changes: 0 additions & 10 deletions packages/bots/new_bot_player.sh

This file was deleted.

9 changes: 0 additions & 9 deletions packages/bots/new_map_copier.sh

This file was deleted.

9 changes: 0 additions & 9 deletions packages/bots/new_match_creator.sh

This file was deleted.

File renamed without changes.
17 changes: 17 additions & 0 deletions packages/headless-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Sky Strife Headless Client

This is a full-featured Sky Strife client that does not require a browser/UI to play.

## Getting Started

The scripts/example.ts file contains a simple example of how to use the client.

You can run it against the current, live Sky Strife world with `pnpm example:redstone`.

```typescript
import { createSkyStrife } from "../src/createSkyStrife";

const skyStrife = await createSkyStrife();
```

`createSkyStrife` will connect to an exsiting Sky Strife world and sync all onchain data locally.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "bots",
"name": "headless-client",
"version": "0.2.0",
"private": "true",
"type": "module",
"description": "Sky Strife botting tools",
"description": "Sky Strife headless client.",
"dependencies": {
"@ethersproject/providers": "^5.7.2",
"@latticexyz/block-logs-stream": "2.0.0-main-5ab67e33",
Expand Down Expand Up @@ -73,17 +73,14 @@
},
"scripts": {
"four-bots:dev": "CHAIN_ID=31337 run-pty % % pnpm create-bot:dev % pnpm create-bot:dev % pnpm create-bot:dev % pnpm create-bot:dev",
"copy-maps:dev": "PRIVATE_KEY=0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6 CHAIN_ID=31337 pnpm tsx src/mapCopier.ts",
"upload-map:dev": "PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 CHAIN_ID=31337 pnpm tsx src/mapUploader.ts",
"create-bot:dev": "CHAIN_ID=31337 pnpm tsx src/createBotPlayer.ts",
"create-debug-matches:dev": "CHAIN_ID=31337 pnpm tsx src/matchCreator.ts",
"copy-maps:redstone": "CHAIN_ID=901 pnpm tsx src/mapCopier.ts",
"upload-map:testnet": "CHAIN_ID=4243 pnpm tsx src/mapUploader.ts",
"copy-maps:testnet": "CHAIN_ID=4243 pnpm tsx src/mapCopier.ts",
"create-bot:testnet": "CHAIN_ID=4243 pnpm tsx src/createBotPlayer.ts",
"create-matches:testnet": "CHAIN_ID=4243 pnpm tsx src/matchCreator.ts",

"create-matches:redstone": "CHAIN_ID=17001 pnpm tsx src/matchCreator.ts"
"copy-maps:dev": "PRIVATE_KEY=0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6 CHAIN_ID=31337 pnpm tsx scripts/mapCopier.ts",
"upload-map:dev": "PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 CHAIN_ID=31337 pnpm tsx scripts/mapUploader.ts",
"create-bot:dev": "CHAIN_ID=31337 pnpm tsx scripts/createBotPlayer.ts",
"create-debug-matches:dev": "CHAIN_ID=31337 pnpm tsx scripts/matchCreator.ts",

"example:redstone": "CHAIN_ID=17001 pnpm tsx scripts/example.ts",
"copy-maps:redstone": "CHAIN_ID=17001 pnpm tsx scripts/mapCopier.ts",
"create-matches:redstone": "CHAIN_ID=17001 pnpm tsx scripts/matchCreator.ts"
},
"devDependencies": {
"tsx": "^3.13.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
runQuery,
setComponent,
} from "@latticexyz/recs";
import { HeadlessSetupResult, env, headlessSetup } from "./headlessSetup";
import { SkyStrife, env, createSkyStrife } from "../src/createSkyStrife";
import { sleep } from "@latticexyz/utils";
import { encodeSystemCalls } from "@latticexyz/world";
import { BFS } from "client/src/utils/pathfinding";
Expand All @@ -30,8 +30,8 @@ const { curry, sample } = lodash;

type MatchCoord = { x: number; y: number };

async function createBotPlayer(headlessSetup: HeadlessSetupResult) {
const { networkLayer, headlessLayer, createPlayer } = headlessSetup;
async function createBotPlayer(skyStrife: SkyStrife) {
const { networkLayer, headlessLayer, createPlayer } = skyStrife;

let activeMatch: Entity | undefined;
let activePlayerEntity: Entity | undefined;
Expand Down Expand Up @@ -354,7 +354,7 @@ async function createBotPlayer(headlessSetup: HeadlessSetupResult) {
};
}

const headlessClient = await headlessSetup();
const headlessClient = await createSkyStrife();
const botPlayer = await createBotPlayer(headlessClient);

botPlayer.start();
18 changes: 18 additions & 0 deletions packages/headless-client/scripts/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Has, defineEnterSystem, getComponentValueStrict } from "@latticexyz/recs";

import { createSkyStrife } from "../src/createSkyStrife";

const skyStrife = await createSkyStrife();

const {
networkLayer: {
world,
components: { MatchName },
},
} = skyStrife;

defineEnterSystem(world, [Has(MatchName)], ({ entity }) => {
const matchName = getComponentValueStrict(MatchName, entity);

console.log(`Match ${matchName.value} was created.`);
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Hex } from "viem";
import { Has, Not, getComponentValue, getComponentValueStrict, runQuery } from "@latticexyz/recs";
import { sleep } from "@latticexyz/utils";
import { env, headlessSetup } from "./headlessSetup";
import { env, createSkyStrife } from "../src/createSkyStrife";

const { networkLayer, createPlayer } = await headlessSetup();
const { networkLayer, createPlayer } = await createSkyStrife();

const mapCopier = createPlayer(env.PRIVATE_KEY);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bulkUploadMap } from "client/src/app/ui/Admin/bulkUploadMap";
import { headlessSetup } from "./headlessSetup";
import { createSkyStrife } from "../src/createSkyStrife";
import debug from "../../../maps/debug.json" assert { type: "json" };
import Cauldron from "../../../maps/4p-cauldron.json" assert { type: "json" };
import KnifeFight from "../../../maps/2p-knife-fight.json" assert { type: "json" };
Expand All @@ -11,7 +11,7 @@ export type Level = Array<{
values: object;
}>;

const { networkLayer } = await headlessSetup();
const { networkLayer } = await createSkyStrife();

await Promise.all([
bulkUploadMap(networkLayer, debug as Level, "debug"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Entity, Has, Not, getComponentValue, runQuery } from "@latticexyz/recs";
import { env, headlessSetup } from "./headlessSetup";
import { env, createSkyStrife } from "../src/createSkyStrife";
import { Hex, padHex, stringToHex } from "viem";
import { sleep } from "@latticexyz/utils";
import { createMatchEntity } from "client/src/createMatchEntity";
import { getOldestMatchInWindow, getSkypoolConfig } from "client/src/app/amalgema-ui/utils/skypool";

const { networkLayer } = await headlessSetup();
const { networkLayer } = await createSkyStrife();

const {
components: { MatchConfig, MatchFinished, LevelTemplates },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NetworkConfig, getChain, getWorldFromChainId } from "client/src/mud/utils";
import { env } from "./headlessSetup";
import { env } from "./createSkyStrife";

export function createNetworkConfig(chainId: number, disableIndexer?: boolean): NetworkConfig {
const chain = getChain(chainId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export const env = z
}),
});

export type HeadlessSetupResult = Awaited<ReturnType<typeof headlessSetup>>;
export type SkyStrife = Awaited<ReturnType<typeof createSkyStrife>>;

export async function headlessSetup(): Promise<{
export async function createSkyStrife(): Promise<{
networkLayer: NetworkLayer;
headlessLayer: HeadlessLayer;
createPlayer: (privateKey?: Hex) => {
Expand All @@ -64,6 +64,8 @@ export async function headlessSetup(): Promise<{
};
}> {
const networkConfig = createNetworkConfig(env.CHAIN_ID, env.DISABLE_INDEXER);

console.log(`Connecting to ${networkConfig.chain.name}...`);
const networkLayer = await createNetworkLayer(networkConfig);
const headlessLayer = await createHeadlessLayer(networkLayer);

Expand All @@ -75,6 +77,11 @@ export async function headlessSetup(): Promise<{
const live$ = SyncProgress.update$.pipe(
filter((progress) => {
const [val] = progress.value;

if (val) {
console.log(val.message);
}

return val?.step === SyncStep.LIVE;
}),
take(1)
Expand Down Expand Up @@ -117,6 +124,7 @@ export async function headlessSetup(): Promise<{
};
}

console.log("Waiting for sync...");
await waitForSync();
console.log("Synced!");

Expand Down
File renamed without changes.
Loading

0 comments on commit 4e3c37f

Please sign in to comment.