From f7f8b17fdd5c03ec609eb78c1d3cfda075d3c936 Mon Sep 17 00:00:00 2001 From: broody Date: Wed, 6 Sep 2023 10:59:42 -0700 Subject: [PATCH] back in business! --- Scarb.toml | 4 +- scripts/default_auth.sh | 2 +- src/components/risks.cairo | 10 ++-- src/systems/create.cairo | 10 ++-- web/.env.development | 24 +++++----- web/src/constants.ts | 2 +- web/src/generated/graphql.ts | 9 ++-- web/src/graphql/entities.graphql | 48 +++++++++++++++++-- web/src/hooks/dojo/entities/useGameEntity.tsx | 3 -- .../hooks/dojo/entities/useLocationEntity.tsx | 18 +++---- .../hooks/dojo/entities/usePlayerEntity.tsx | 23 ++++----- web/src/hooks/dojo/systems/useSystems.tsx | 1 + .../pages/[gameId]/[locationSlug]/index.tsx | 2 +- web/src/pages/[gameId]/end.tsx | 2 +- web/src/utils/event.ts | 17 ++++--- 15 files changed, 103 insertions(+), 72 deletions(-) diff --git a/Scarb.toml b/Scarb.toml index 7d66a2650..484c564ec 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,7 +1,7 @@ [package] name = "rollyourown" version = "0.1.0" -cairo-version = "2.0.0-rc4" +cairo-version = "2.2.0" [cairo] sierra-replace-ids = true @@ -24,4 +24,4 @@ private_key = "0x1800000000300000180000000000030000000000003006001800006600" #account_address = "0x2" #private_key = "0xc1cf1490de1352865301bb8705143f3ef938f97fdf892f1090dcb5ac7bcd1d" -#world_address = "0x617a865eaaf42b325cc29f44c08b4fbe5face88aff560ed2743ae510efbf0f7" +#world_address = "0x3c3dfeb374720dfd73554dc2b9e0583cb9668efb3055d07d1533afa5d219fd5" diff --git a/scripts/default_auth.sh b/scripts/default_auth.sh index f1ddb8e6e..0c6c37644 100755 --- a/scripts/default_auth.sh +++ b/scripts/default_auth.sh @@ -2,7 +2,7 @@ set -euo pipefail pushd $(dirname "$0")/.. -export WORLD_ADDRESS="0x4b603970f151d639330fc35ddd591d7bac3c94505369270a969e54304a7e3f6"; +export WORLD_ADDRESS="0x3c3dfeb374720dfd73554dc2b9e0583cb9668efb3055d07d1533afa5d219fd5"; # make sure all components/systems are deployed COMPONENTS=("Game" "Market" "Name" "Player" "Risks") diff --git a/src/components/risks.cairo b/src/components/risks.cairo index 54530a313..611f921a9 100644 --- a/src/components/risks.cairo +++ b/src/components/risks.cairo @@ -50,7 +50,7 @@ impl RisksImpl of RisksTrait { let mut event_occured = false; if occurs(seed, *self.travel) { - seed = pedersen(seed, seed); + seed = pedersen::pedersen(seed, seed); event_occured = true; // TEMP: for testing, mugging is only risk @@ -79,7 +79,7 @@ fn occurs(seed: felt252, likelihood: u8) -> bool { #[test] #[available_gas(1000000)] fn test_never_occurs() { - let seed = pedersen(1, 1); + let seed = pedersen::pedersen(1, 1); let risks = Risks { game_id: 0, location_id: 0, travel: 0, hurt: 0, mugged: 0, arrested: 0, }; let (event_occured, result) = risks.travel(seed); @@ -92,7 +92,7 @@ fn test_never_occurs() { #[test] #[available_gas(1000000)] fn test_always_occurs() { - let seed = pedersen(1, 1); + let seed = pedersen::pedersen(1, 1); let risks = Risks { game_id: 0, location_id: 0, travel: 100, hurt: 100, mugged: 100, arrested: 100, }; @@ -104,7 +104,7 @@ fn test_always_occurs() { #[test] #[available_gas(1000000)] fn test_occurs() { - let seed = pedersen(1, 1); + let seed = pedersen::pedersen(1, 1); let event = occurs(seed, 10); assert(!event, 'should not occur'); } @@ -112,7 +112,7 @@ fn test_occurs() { #[test] #[available_gas(1000000)] fn test_not_occurs() { - let seed = pedersen(1, 1); + let seed = pedersen::pedersen(1, 1); let event = occurs(seed, 28); assert(event, 'should occur'); } diff --git a/src/systems/create.cairo b/src/systems/create.cairo index ac70ad4df..3904bbfc0 100644 --- a/src/systems/create.cairo +++ b/src/systems/create.cairo @@ -102,14 +102,14 @@ mod create_game { ); let mut seed = starknet::get_tx_info().unbox().transaction_hash; - seed = pedersen(seed, *location_id); + seed = pedersen::pedersen(seed, *location_id); let mut drugs = DrugTrait::all(); loop { match drugs.pop_front() { Option::Some(drug_id) => { // HACK: temp hack to get some randomness - seed = pedersen(seed, *drug_id); + seed = pedersen::pedersen(seed, *drug_id); let market_cash = random(seed, MIN_CASH, MAX_CASH); let rand = random(seed, MIN_QUANITTY.into(), MAX_QUANTITY.into()); let market_quantity: usize = rand.try_into().unwrap(); @@ -142,7 +142,11 @@ mod create_game { emit!(ctx.world, PlayerJoined { game_id, player_id: ctx.origin, location_id: location_id }); // emit game created - emit!(ctx.world, GameCreated { game_id, creator: ctx.origin, start_time, max_players, max_turns }); + emit!( + ctx.world, GameCreated { + game_id, creator: ctx.origin, start_time, max_players, max_turns + } + ); (game_id, ctx.origin) } diff --git a/web/.env.development b/web/.env.development index fc00fbd6f..127a60088 100644 --- a/web/.env.development +++ b/web/.env.development @@ -1,14 +1,14 @@ -#NEXT_PUBLIC_DISABLE_MEDIAPLAYER_AUTOPLAY="true" -#NEXT_PUBLIC_RPC_ENDPOINT="http://localhost:5050" -#NEXT_PUBLIC_GRAPHQL_ENDPOINT="http://localhost:8080" -#NEXT_PUBLIC_ADMIN_ADDRESS="0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973" -#NEXT_PUBLIC_ADMIN_PRIVATE_KEY="0x1800000000300000180000000000030000000000003006001800006600" -#NEXT_PUBLIC_ACCOUNT_CLASS_HASH="0x04d07e40e93398ed3c76981e72dd1fd22557a78ce36c0515f679e27f0bb5bc5f" - - NEXT_PUBLIC_DISABLE_MEDIAPLAYER_AUTOPLAY="true" -NEXT_PUBLIC_RPC_ENDPOINT="https://api.cartridge.gg/x/shinai/madara" +NEXT_PUBLIC_RPC_ENDPOINT="http://localhost:5050" NEXT_PUBLIC_GRAPHQL_ENDPOINT="http://localhost:8080" -NEXT_PUBLIC_ADMIN_ADDRESS="0x2" -NEXT_PUBLIC_ADMIN_PRIVATE_KEY="0xc1cf1490de1352865301bb8705143f3ef938f97fdf892f1090dcb5ac7bcd1d" -NEXT_PUBLIC_ACCOUNT_CLASS_HASH="0x006280083f8c2a2db9f737320d5e3029b380e0e820fe24b8d312a6a34fdba0cd" \ No newline at end of file +NEXT_PUBLIC_ADMIN_ADDRESS="0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973" +NEXT_PUBLIC_ADMIN_PRIVATE_KEY="0x1800000000300000180000000000030000000000003006001800006600" +NEXT_PUBLIC_ACCOUNT_CLASS_HASH="0x04d07e40e93398ed3c76981e72dd1fd22557a78ce36c0515f679e27f0bb5bc5f" + + +# NEXT_PUBLIC_DISABLE_MEDIAPLAYER_AUTOPLAY="true" +# NEXT_PUBLIC_RPC_ENDPOINT="https://api.cartridge.gg/x/shinai/madara" +# NEXT_PUBLIC_GRAPHQL_ENDPOINT="http://localhost:8080" +# NEXT_PUBLIC_ADMIN_ADDRESS="0x2" +# NEXT_PUBLIC_ADMIN_PRIVATE_KEY="0xc1cf1490de1352865301bb8705143f3ef938f97fdf892f1090dcb5ac7bcd1d" +# NEXT_PUBLIC_ACCOUNT_CLASS_HASH="0x006280083f8c2a2db9f737320d5e3029b380e0e820fe24b8d312a6a34fdba0cd" \ No newline at end of file diff --git a/web/src/constants.ts b/web/src/constants.ts index 5e55076be..caa9a830d 100644 --- a/web/src/constants.ts +++ b/web/src/constants.ts @@ -1,4 +1,4 @@ export const RYO_WORLD_ADDRESS = - "0x4b603970f151d639330fc35ddd591d7bac3c94505369270a969e54304a7e3f6"; + "0x3c3dfeb374720dfd73554dc2b9e0583cb9668efb3055d07d1533afa5d219fd5"; export const ETH_CONTRACT_ADDRESS = "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"; diff --git a/web/src/generated/graphql.ts b/web/src/generated/graphql.ts index cf30d2cb2..eeaf7c1f9 100644 --- a/web/src/generated/graphql.ts +++ b/web/src/generated/graphql.ts @@ -774,16 +774,18 @@ export type PlayerEntityQuery = { cursor: any; node?: { __typename?: "Entity"; + keys?: Array | null; components?: Array< | { __typename: "Drug"; quantity?: any | null } | { __typename: "Game" } | { __typename: "Market" } - | { __typename: "Name"; short_string?: any | null } + | { __typename: "Name" } | { __typename: "Player"; cash?: any | null; health?: any | null; turns_remaining?: any | null; + location_id?: any | null; } | { __typename: "Risks" } | null @@ -1018,19 +1020,18 @@ export const PlayerEntityDocument = ` totalCount edges { node { + keys components { __typename ... on Player { cash health turns_remaining + location_id } ... on Drug { quantity } - ... on Name { - short_string - } } } cursor diff --git a/web/src/graphql/entities.graphql b/web/src/graphql/entities.graphql index e506cc0af..240aeb284 100644 --- a/web/src/graphql/entities.graphql +++ b/web/src/graphql/entities.graphql @@ -19,19 +19,18 @@ query PlayerEntity($gameId: String!, $playerId: String!) { totalCount edges { node { + keys components { __typename ... on Player { cash health turns_remaining + location_id } ... on Drug { quantity } - ... on Name { - short_string - } } } cursor @@ -63,3 +62,46 @@ query LocationEntities($gameId: String!, $locationId: String!) { } } } + +# TODO: Query entire game state per gameId in one query +# query GameState($gameId: String!) { +# entities (first: 100, keys: [$gameId]]) { +# totalCount +# edges { +# node { +# componentNames +# components { +# __typename +# ... on Game { +# creator +# is_finished +# max_players +# max_turns +# num_players +# start_time +# } +# ... on Player { +# cash +# health +# turns_remaining +# } +# ... on Drug { +# quantity +# } +# ... on Market { +# drug_id +# location_id +# cash +# quantity +# } +# ... on Risks { +# arrested +# hurt +# mugged +# travel +# } +# } +# } +# } +# } +# } diff --git a/web/src/hooks/dojo/entities/useGameEntity.tsx b/web/src/hooks/dojo/entities/useGameEntity.tsx index c176be26f..1c2b8ef5c 100644 --- a/web/src/hooks/dojo/entities/useGameEntity.tsx +++ b/web/src/hooks/dojo/entities/useGameEntity.tsx @@ -57,16 +57,13 @@ export const useGameEntity = ({ { id: key }, { enabled: !!gameId, - refetchInterval: REFETCH_INTERVAL, }, ); useEffect(() => { if (gameId) { - console.log(gameId); const key_ = ec.starkCurve.poseidonHashMany([num.toBigInt(gameId)]); setKey(num.toHex(key_)); - console.log(num.toHex(key_)); } }, [gameId]); diff --git a/web/src/hooks/dojo/entities/useLocationEntity.tsx b/web/src/hooks/dojo/entities/useLocationEntity.tsx index dcfced593..bbddeda1e 100644 --- a/web/src/hooks/dojo/entities/useLocationEntity.tsx +++ b/web/src/hooks/dojo/entities/useLocationEntity.tsx @@ -10,14 +10,6 @@ import { useEffect, useState } from "react"; import { shortString } from "starknet"; import { REFETCH_INTERVAL, SCALING_FACTOR } from ".."; -interface LocationEntityData { - entities: [ - { - components: (Market | Risks)[]; - }, - ]; -} - export type DrugMarket = { name: string; // drug name price: number; @@ -41,14 +33,16 @@ export class LocationEntity { // we know both location and risk component uses key[1] as locationId const keys = edges[0].node?.keys || []; const locationId = keys[1]!; + console.log(edges); const risksComponent = edges.find((edge) => { - const components = edge.node?.components || []; - return components[0]!.__typename === "Risks"; - }) as Risks; + return edge.node?.components?.some( + (component) => component?.__typename === "Risks", + ); + })?.node?.components?.[0] as Risks; const drugMarketEntities = edges.filter((edge) => { - edge.node?.components?.find( + return edge.node?.components?.find( (component) => component?.__typename === "Market", ); }) as EntityEdge[]; diff --git a/web/src/hooks/dojo/entities/usePlayerEntity.tsx b/web/src/hooks/dojo/entities/usePlayerEntity.tsx index 018bb6194..238e5d186 100644 --- a/web/src/hooks/dojo/entities/usePlayerEntity.tsx +++ b/web/src/hooks/dojo/entities/usePlayerEntity.tsx @@ -1,7 +1,6 @@ import { Player, Drug as DrugType, - Name, usePlayerEntityQuery, EntityEdge, } from "@/generated/graphql"; @@ -32,16 +31,12 @@ export class PlayerEntity { static create(edges: EntityEdge[]): PlayerEntity | undefined { if (!edges || edges.length === 0) return undefined; - // player related entities - const playerEdges = edges.find((edge) => { - edge.node?.components?.find( + // player component + const playerComponent = edges.find((edge) => { + return edge.node?.components?.some( (component) => component?.__typename === "Player", ); - }); - - const playerComponent = playerEdges?.node?.components?.find( - (component) => component?.__typename === "Player", - ) as Player; + })?.node?.components?.[0] as Player; // drug entities const drugEdges = edges.filter((edge) => @@ -55,17 +50,15 @@ export class PlayerEntity { (component) => component?.__typename === "Drug", ) as DrugType; - const nameComponent = edge.node?.components?.find( - (component) => component?.__typename === "Name", - ) as Name; + // parse key fo drug_id which is name return { - name: shortString.decodeShortString(nameComponent.short_string), + name: "temp", quantity: drugComponent.quantity, }; }); - if (!playerEdges) return undefined; + if (!playerComponent) return undefined; return new PlayerEntity(playerComponent, drugs); } @@ -84,7 +77,6 @@ export const usePlayerEntity = ({ address?: string; }): PlayerInterface => { const [player, setPlayer] = useState(); - // TODO: remove leading zeros in address, maybe implemented in torii const { data, isFetched, refetch } = usePlayerEntityQuery( { gameId: gameId || "", playerId: address || "" }, @@ -93,6 +85,7 @@ export const usePlayerEntity = ({ refetchInterval: REFETCH_INTERVAL, // TODO: long polling, }, ); + useEffect(() => { const player_ = PlayerEntity.create(data?.entities?.edges as EntityEdge[]); if (player_) setPlayer(player_); diff --git a/web/src/hooks/dojo/systems/useSystems.tsx b/web/src/hooks/dojo/systems/useSystems.tsx index 9fff80bf7..7aaf8a30f 100644 --- a/web/src/hooks/dojo/systems/useSystems.tsx +++ b/web/src/hooks/dojo/systems/useSystems.tsx @@ -59,6 +59,7 @@ export const useSystems = (): SystemsInterface => { maxPlayers, maxTurns, ]); + // using joined event instead of created event to get initial location const event = parseEvent(receipt, RyoEvents.PlayerJoined); diff --git a/web/src/pages/[gameId]/[locationSlug]/index.tsx b/web/src/pages/[gameId]/[locationSlug]/index.tsx index b73caaee5..4c219175e 100644 --- a/web/src/pages/[gameId]/[locationSlug]/index.tsx +++ b/web/src/pages/[gameId]/[locationSlug]/index.tsx @@ -60,7 +60,7 @@ export default function Location() { return; } } - }, [locationId, playerEntity, router, gameId]); + }, [locationId, playerEntity, gameId]); if (!playerEntity || !locationEntity || !gameEntity) { return <>; diff --git a/web/src/pages/[gameId]/end.tsx b/web/src/pages/[gameId]/end.tsx index 02c3fa838..55a93e426 100644 --- a/web/src/pages/[gameId]/end.tsx +++ b/web/src/pages/[gameId]/end.tsx @@ -30,7 +30,7 @@ export default function End() { if (!name) return; await submitSetName(gameId, name); router.push("/"); - }, [name, router, submitSetName]); + }, [name, gameId, router, submitSetName]); return ( <> diff --git a/web/src/utils/event.ts b/web/src/utils/event.ts index 0d0ee1fa8..d0da1b372 100644 --- a/web/src/utils/event.ts +++ b/web/src/utils/event.ts @@ -1,12 +1,13 @@ import { InvokeTransactionReceiptResponse, num, shortString } from "starknet"; +// events are keyed by the hash of the event name export enum RyoEvents { - GameCreated = "GameCreated", - PlayerJoined = "PlayerJoined", - Traveled = "Traveled", - Bought = "Bought", - Sold = "Sold", - RandomEvent = "RandomEvent", + GameCreated = "0x230f942bb2087887c3b1dd964c716614bb6df172214f22409fefb734d96a4d2", + PlayerJoined = "0x214916ce0265d355fd91110809ffba7b5e672b108a8beea3dd235818431264b", + Traveled = "0x2c4d9d5da873550ed167876bf0bc2ae300ce1db2eeff67927a85693680a2328", + Bought = "0x20cb8131637de1953a75938db3477cc6b648e5ed255f5b3fe3f0fb9299f0afc", + Sold = "0x123e760cef925d0b4f685db5e1ac87aadaf1ad9f8069122a5bb03353444c386", + RandomEvent = "0x203b38ece4b4d98864bf85cb3f5261dad4c45aab6aa5d9228fbda95f7dd4f62", } export interface BaseEventData { @@ -50,9 +51,7 @@ export const parseEvent = ( receipt: InvokeTransactionReceiptResponse, eventType: RyoEvents, ): BaseEventData => { - const raw = receipt.events?.find( - (e) => shortString.decodeShortString(e.keys[0]) === eventType, - ); + const raw = receipt.events?.find((e) => e.keys[0] === eventType); if (!raw) { throw new Error(`event not found`);