Skip to content

Commit

Permalink
add recs client
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Aug 22, 2024
1 parent 5c5aa5f commit 5a26d4d
Show file tree
Hide file tree
Showing 32 changed files with 3,503 additions and 11 deletions.
12 changes: 12 additions & 0 deletions clients/react-client/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
## Bare Bones Dojo world using only React State and torii client

### 1. Build Contracts

### 2. Generate Bindings

Run from within the vite app

```
sozo build --typescript --manifest-path ../../contracts/Scarb.toml --bindings-output ./src
```

### 3. Run App
2 changes: 1 addition & 1 deletion clients/react-client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Button } from "./components/ui/button";
import { setupWorld } from "./typescript/contracts.gen";
import { DojoProvider } from "@dojoengine/core";
import { BurnerManager } from "@dojoengine/create-burner";
import { Account, num, shortString } from "starknet";
import { Account } from "starknet";
import { Dungeon, Player } from "./types";

const client = await setupWorld(
Expand Down
14 changes: 14 additions & 0 deletions clients/react-recs-app/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
env: { browser: true, es2020: true },
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: { ecmaVersion: "latest", sourceType: "module" },
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": "warn",
},
};
24 changes: 24 additions & 0 deletions clients/react-recs-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
21 changes: 21 additions & 0 deletions clients/react-recs-app/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Dojo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions clients/react-recs-app/components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "default",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "tailwind.config.js",
"css": "src/index.css",
"baseColor": "slate",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils"
}
}
7 changes: 7 additions & 0 deletions clients/react-recs-app/dojoConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import manifest from "../../contracts/manifests/dev/deployment/manifest.json";

import { createDojoConfig } from "@dojoengine/core";

export const dojoConfig = createDojoConfig({
manifest,
});
12 changes: 12 additions & 0 deletions clients/react-recs-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dojo React Vite</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
54 changes: 54 additions & 0 deletions clients/react-recs-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "react-example",
"private": false,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@dojoengine/core": "1.0.0-alpha.6",
"@dojoengine/create-burner": "1.0.0-alpha.6",
"@dojoengine/react": "1.0.0-alpha.6",
"@dojoengine/recs": "2.0.13",
"@dojoengine/state": "1.0.0-alpha.6",
"@dojoengine/torii-client": "1.0.0-alpha.6",
"@dojoengine/utils": "1.0.0-alpha.6",
"@latticexyz/react": "^2.0.12",
"@latticexyz/utils": "^2.0.12",
"@radix-ui/react-slot": "^1.1.0",
"autoprefixer": "^10.4.20",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"ethers": "^5.7.2",
"lucide-react": "^0.429.0",
"mobx": "^6.9.0",
"postcss": "^8.4.41",
"proxy-deep": "^3.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rxjs": "^7.8.1",
"starknet": "6.11.0",
"tailwind-merge": "^2.5.2",
"tailwindcss": "^3.4.10",
"tailwindcss-animate": "^1.0.7",
"vite-plugin-top-level-await": "^1.3.1",
"vite-plugin-wasm": "^3.2.2"
},
"devDependencies": {
"@types/node": "^20.16.1",
"@types/react": "^18.2.33",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"@vitejs/plugin-react": "^4.0.0",
"eslint": "^8.38.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"typescript": "^5.5.4",
"vite": "^5.4.2"
}
}
6 changes: 6 additions & 0 deletions clients/react-recs-app/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
3 changes: 3 additions & 0 deletions clients/react-recs-app/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Dojo React Vite App

[book](https://book.dojoengine.org/)
Empty file.
122 changes: 122 additions & 0 deletions clients/react-recs-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import "./App.css";
import { useComponentValue } from "@dojoengine/react";
import { Entity } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { useDojo } from "./dojo/useDojo";
import { Button } from "./components/ui/button";
import * as torii from "@dojoengine/torii-client";

function App() {
const {
setup: {
clientComponents: { Dungeon, Player },
toriiClient,
client,
contractComponents,
},
account: { account },
} = useDojo();

// entity id we are syncing
const entityId = getEntityIdFromKeys([BigInt(account.address)]) as Entity;

const player = useComponentValue(Player, entityId);

console.log(player);

return (
<>
<div className="bg-black text-green-600 h-screen w-screen p-10">
<h4>RPG Starter</h4>
<div className="mb-1">
<Button
onClick={async () => {
await client.actions.spawn({
account,
name: BigInt(torii.cairoShortStringToFelt("Loaf")),
role: 1,
});
}}
variant={"default"}
>
Spawn
</Button>
</div>

<div className="grid grid-cols-2 gap-1">
<Button
onClick={async () => {
await client.actions.move({
account,
direction: 1, // North
});
}}
variant={"outline"}
>
North
</Button>
<Button
onClick={async () => {
await client.actions.move({
account,
direction: 2, // South
});
}}
variant={"outline"}
>
South
</Button>
<Button
onClick={async () => {
await client.actions.move({
account,
direction: 3, // East
});
}}
variant={"outline"}
>
East
</Button>
<Button
onClick={async () => {
await client.actions.move({
account,
direction: 4, // West
});
}}
variant={"outline"}
>
West
</Button>
</div>

<div className="my-1">
{" "}
<Button
onClick={async () => {
await client.actions.heal({
account,
quantity: 1,
});
}}
variant={"secondary"}
>
Heal
</Button>
<Button
onClick={async () => {
await client.actions.attack({
account,
});
}}
variant={"destructive"}
>
Attack
</Button>
</div>
</div>
</>
);
}

export default App;
56 changes: 56 additions & 0 deletions clients/react-recs-app/src/components/ui/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@/lib/utils";

const buttonVariants = cva(
"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary:
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
);

export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button";
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
);
}
);
Button.displayName = "Button";

export { Button, buttonVariants };
Loading

0 comments on commit 5a26d4d

Please sign in to comment.