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/src/systems/travel.cairo b/src/systems/travel.cairo index 949c4b068..2bcb4c33f 100644 --- a/src/systems/travel.cairo +++ b/src/systems/travel.cairo @@ -64,13 +64,6 @@ mod travel { }); } - // If arrested, player loses a turn and stays at same location - if result.arrested { - player.turns_remaining - 1; - set!(ctx.world, (player)); - return (true); - } - // If mugged, player loses half their cash if result.mugged { player.cash /= 2; @@ -78,6 +71,7 @@ mod travel { // update player player.location_id = next_location_id; + player.turns_remaining -= 1; set!(ctx.world, (player)); emit!(ctx.world, Traveled { 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/.eslintignore b/web/.eslintignore new file mode 100644 index 000000000..b94707787 --- /dev/null +++ b/web/.eslintignore @@ -0,0 +1,2 @@ +node_modules/ +dist/ diff --git a/web/src/components/Inventory.tsx b/web/src/components/Inventory.tsx index 13f24cc3e..89be57fd5 100644 --- a/web/src/components/Inventory.tsx +++ b/web/src/components/Inventory.tsx @@ -10,7 +10,7 @@ import { import React from "react"; import { usePlayerEntity } from "@/hooks/dojo/entities/usePlayerEntity"; import { useRouter } from "next/router"; -import { getDrugByName } from "@/hooks/ui"; +import { getDrugById } from "@/hooks/ui"; import { useDojo } from "@/hooks/dojo"; export const Inventory = ({ ...props }: StyleProps) => { @@ -50,7 +50,7 @@ export const Inventory = ({ ...props }: StyleProps) => { <> - {getDrugByName(drug.name).icon({ boxSize: "26" })} + {getDrugById(drug.id).icon({ boxSize: "26" })} {drug.quantity} {index < playerEntity.drugs.length - 1 && ( 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..44929704f 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: "Drug"; drug_id?: any | null; 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,19 @@ export const PlayerEntityDocument = ` totalCount edges { node { + keys components { __typename ... on Player { cash health turns_remaining + location_id } ... on Drug { + drug_id quantity } - ... on Name { - short_string - } } } cursor diff --git a/web/src/graphql/entities.graphql b/web/src/graphql/entities.graphql index e506cc0af..7198aa5b7 100644 --- a/web/src/graphql/entities.graphql +++ b/web/src/graphql/entities.graphql @@ -19,19 +19,19 @@ query PlayerEntity($gameId: String!, $playerId: String!) { totalCount edges { node { + keys components { __typename ... on Player { cash health turns_remaining + location_id } ... on Drug { + drug_id quantity } - ... on Name { - short_string - } } } cursor @@ -63,3 +63,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..ec20e8ad6 100644 --- a/web/src/hooks/dojo/entities/useLocationEntity.tsx +++ b/web/src/hooks/dojo/entities/useLocationEntity.tsx @@ -7,30 +7,22 @@ import { EntityEdge, } from "@/generated/graphql"; import { useEffect, useState } from "react"; -import { shortString } from "starknet"; +import { num, shortString } from "starknet"; import { REFETCH_INTERVAL, SCALING_FACTOR } from ".."; -interface LocationEntityData { - entities: [ - { - components: (Market | Risks)[]; - }, - ]; -} - export type DrugMarket = { - name: string; // drug name + id: string; // id is hex encoded drug name price: number; marketPool: Market; }; export class LocationEntity { - name: string; // location name same as id + id: string; // id is hex encoded location name risks: Risks; drugMarkets: DrugMarket[]; - constructor(name: string, risks: Risks, drugMarkets: DrugMarket[]) { - this.name = name; + constructor(id: string, risks: Risks, drugMarkets: DrugMarket[]) { + this.id = id; this.risks = risks; this.drugMarkets = drugMarkets; } @@ -43,12 +35,13 @@ export class LocationEntity { const locationId = keys[1]!; 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[]; @@ -59,28 +52,27 @@ export class LocationEntity { ) as Market; const keys = edge.node?.keys || []; - const drugName = keys[2]!; + const drugId = num.toHexString(keys[2]!); - const drugId = shortString.decodeShortString(drugName); const price = Number(marketComponent.cash) / Number(marketComponent.quantity) / SCALING_FACTOR; return { - name: drugId, + id: drugId, price: price, marketPool: marketComponent, }; }); - + console.log(drugMarkets); if (!risksComponent || drugMarkets.length === 0) return undefined; // sort by name - drugMarkets.sort((a, b) => a.name.localeCompare(b.name)); + drugMarkets.sort((a, b) => a.id.localeCompare(b.id)); return { - name: shortString.decodeShortString(locationId), + id: locationId, risks: risksComponent, drugMarkets: drugMarkets, }; @@ -104,7 +96,7 @@ export const useLocationEntity = ({ const { data, isFetched } = useLocationEntitiesQuery( { gameId: gameId || "", - locationId: shortString.encodeShortString(locationId || ""), + locationId: locationId || "", }, { enabled: !!gameId && !!locationId, diff --git a/web/src/hooks/dojo/entities/usePlayerEntity.tsx b/web/src/hooks/dojo/entities/usePlayerEntity.tsx index 018bb6194..bb17d4941 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"; @@ -10,7 +9,7 @@ import { shortString } from "starknet"; import { REFETCH_INTERVAL, SCALING_FACTOR } from ".."; type Drug = { - name: string; + id: string; quantity: number; }; @@ -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) => @@ -54,18 +49,14 @@ export class PlayerEntity { const drugComponent = edge.node?.components?.find( (component) => component?.__typename === "Drug", ) as DrugType; - - const nameComponent = edge.node?.components?.find( - (component) => component?.__typename === "Name", - ) as Name; - + console.log(drugComponent); return { - name: shortString.decodeShortString(nameComponent.short_string), + id: drugComponent.drug_id, quantity: drugComponent.quantity, }; }); - if (!playerEdges) return undefined; + if (!playerComponent) return undefined; return new PlayerEntity(playerComponent, drugs); } @@ -84,7 +75,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 +83,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/hooks/state.tsx b/web/src/hooks/state.tsx index 8157a9f16..2ae9392c4 100644 --- a/web/src/hooks/state.tsx +++ b/web/src/hooks/state.tsx @@ -1,6 +1,4 @@ -import { Account } from "starknet"; import { create } from "zustand"; -import { DrugProps, getDrugByName } from "./ui"; export enum Locations { Queens = "Queens", diff --git a/web/src/hooks/ui.tsx b/web/src/hooks/ui.tsx index 3950b6413..5398e4aa1 100644 --- a/web/src/hooks/ui.tsx +++ b/web/src/hooks/ui.tsx @@ -19,14 +19,13 @@ import { } from "@/components/icons/drugs"; import { Drugs, Locations, TravelEvents } from "./state"; - -import { Router, useRouter } from "next/router"; import React from "react"; export interface LocationProps { name: Locations; slug: string; icon: React.FC; + id: string; } export const locations: LocationProps[] = [ @@ -34,31 +33,37 @@ export const locations: LocationProps[] = [ name: Locations.Central, slug: "central", icon: CentralPark, + id: "0x43656e7472616c205061726b", }, { name: Locations.Queens, slug: "queens", icon: Queens, + id: "0x517565656e73", }, { name: Locations.Bronx, slug: "bronx", icon: Bronx, + id: "0x5468652042726f6e78", }, { name: Locations.Jersey, slug: "jersey", icon: Manhattan, + id: "0x4a65727365792043697479", }, { name: Locations.Coney, slug: "coney", icon: ConeyIsland, + id: "0x436f6e65792049736c616e64", }, { name: Locations.Brooklyn, slug: "brooklyn", icon: Brooklyn, + id: "0x42726f6f6b6c796e", }, ]; @@ -66,6 +71,7 @@ export interface DrugProps { name: Drugs; slug: string; icon: React.FC; + id: string; } const drugs: DrugProps[] = [ @@ -73,31 +79,37 @@ const drugs: DrugProps[] = [ name: Drugs.Ludes, slug: "ludes", icon: Ludes, + id: "0x4c75646573", }, { name: Drugs.Speed, slug: "speed", icon: Speed, + id: "0x5370656564", }, { name: Drugs.Weed, slug: "weed", icon: Weed, + id: "0x57656564", }, { name: Drugs.Acid, slug: "acid", icon: Acid, + id: "0x41636964", }, { name: Drugs.Heroin, slug: "heroin", icon: Heroin, + id: "0x4865726f696e", }, { name: Drugs.Cocaine, slug: "cocaine", icon: Cocaine, + id: "0x436f6361696e65", }, ]; @@ -129,9 +141,9 @@ export interface UiState { drugs: DrugProps[]; events: EventProps[]; getLocationBySlug: (slug: string) => LocationProps; - getLocationByName: (name: string) => LocationProps; + getLocationById: (id: string) => LocationProps; getDrugBySlug: (slug: string) => DrugProps; - getDrugByName: (name: string) => DrugProps; + getDrugById: (name: string) => DrugProps; getEventBySlug: (slug: string) => EventProps; getEventByName: (name: string) => EventProps; isBackButtonVisible: (pathname: string) => Boolean; @@ -150,9 +162,9 @@ export const getLocationBySlug = (slug: string): LocationProps => { return location || locations[0]; }; -export const getLocationByName = (name: string): LocationProps => { +export const getLocationById = (id: string): LocationProps => { const { locations } = useUiStore.getState(); - const location = locations.find((i) => i.name === name); + const location = locations.find((i) => i.id === id); return location || locations[0]; }; @@ -162,9 +174,9 @@ export const getDrugBySlug = (slug: string): DrugProps => { return drug || drugs[0]; }; -export const getDrugByName = (name: string): DrugProps => { +export const getDrugById = (id: string): DrugProps => { const { drugs } = useUiStore.getState(); - const drug = drugs.find((i) => i.name === name); + const drug = drugs.find((i) => i.id === id); return drug || drugs[0]; }; @@ -186,9 +198,9 @@ export const useUiStore = create(() => ({ drugs, events, getLocationBySlug, - getLocationByName, + getLocationById, getDrugBySlug, - getDrugByName, + getDrugById, getEventBySlug, getEventByName, isBackButtonVisible, diff --git a/web/src/pages/[gameId]/[locationSlug]/[drugSlug].tsx b/web/src/pages/[gameId]/[locationSlug]/[drugSlug].tsx index f4520f3e4..f9e3328d3 100644 --- a/web/src/pages/[gameId]/[locationSlug]/[drugSlug].tsx +++ b/web/src/pages/[gameId]/[locationSlug]/[drugSlug].tsx @@ -60,7 +60,7 @@ export default function Market() { const { location: locationEntity } = useLocationEntity({ gameId, - locationId: location.name, + locationId: location.id, }); const { player: playerEntity } = usePlayerEntity({ gameId, @@ -73,10 +73,10 @@ export default function Market() { useEffect(() => { if (!locationEntity || !playerEntity) return; - const market = locationEntity.drugMarkets.find((d) => d.name === drug.name); + const market = locationEntity.drugMarkets.find((d) => d.id === drug.id); if (!market) return; - const playerDrug = playerEntity.drugs.find((d) => d.name === drug.name); + const playerDrug = playerEntity.drugs.find((d) => d.id === drug.id); if (playerDrug) { setCanSell(playerDrug.quantity > 0); } @@ -256,7 +256,7 @@ const QuantitySelector = ({ setMax(calculateMaxQuantity(market.marketPool, player.cash)); } else if (type === TradeDirection.Sell) { const playerQuantity = player.drugs.find( - (d) => d.name === drug.name, + (d) => d.id === drug.id, )?.quantity; setMax(playerQuantity || 0); } diff --git a/web/src/pages/[gameId]/[locationSlug]/index.tsx b/web/src/pages/[gameId]/[locationSlug]/index.tsx index b73caaee5..992fe71fa 100644 --- a/web/src/pages/[gameId]/[locationSlug]/index.tsx +++ b/web/src/pages/[gameId]/[locationSlug]/index.tsx @@ -14,11 +14,7 @@ import { } from "@chakra-ui/react"; import Layout from "@/components/Layout"; import { useRouter } from "next/router"; -import { - getDrugByName, - getLocationByName, - getLocationBySlug, -} from "@/hooks/ui"; +import { getDrugById, getLocationById, getLocationBySlug } from "@/hooks/ui"; import { Cart } from "@/components/icons"; import { Footer } from "@/components/Footer"; import { Sounds, playSound } from "@/hooks/sound"; @@ -28,14 +24,12 @@ import { formatQuantity, formatCash } from "@/utils/ui"; import { Inventory } from "@/components/Inventory"; import { useGameEntity } from "@/hooks/dojo/entities/useGameEntity"; import { useDojo } from "@/hooks/dojo"; +import { shortString } from "starknet"; export default function Location() { const router = useRouter(); const gameId = router.query.gameId as string; - const locationId = getLocationBySlug( - router.query.locationSlug as string, - ).name; - + const locationId = getLocationBySlug(router.query.locationSlug as string).id; const { account } = useDojo(); const { location: locationEntity } = useLocationEntity({ @@ -55,7 +49,7 @@ export default function Location() { // check if player at right location if (locationId !== playerEntity.locationId) { router.replace( - `/${gameId}/${getLocationByName(playerEntity.locationId).slug}`, + `/${gameId}/${getLocationById(playerEntity.locationId).slug}`, ); return; } @@ -75,10 +69,10 @@ export default function Location() { return ( @@ -105,9 +99,7 @@ export default function Location() { cursor="pointer" onClick={() => { playSound(Sounds.HoverClick, 0.3, false); - router.push( - `${router.asPath}/${getDrugByName(drug.name).slug}`, - ); + router.push(`${router.asPath}/${getDrugById(drug.id).slug}`); }} > - {drug.name} + {getDrugById(drug.id).name} - {getDrugByName(drug.name).icon({})} + {getDrugById(drug.id).icon({})} 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/pages/[gameId]/travel.tsx b/web/src/pages/[gameId]/travel.tsx index 512ce046e..2d6c87292 100644 --- a/web/src/pages/[gameId]/travel.tsx +++ b/web/src/pages/[gameId]/travel.tsx @@ -19,7 +19,7 @@ import { motion } from "framer-motion"; import { LocationProps, useUiStore, - getLocationByName, + getLocationById, getEventBySlug, } from "@/hooks/ui"; import { useSystems } from "@/hooks/dojo/systems/useSystems"; @@ -46,7 +46,7 @@ export default function Travel() { useEffect(() => { if (playerEntity) { - const location = getLocationByName(playerEntity.locationId).name; + const location = getLocationById(playerEntity.locationId).name; setCurrentLocation(location); setTarget(location); } diff --git a/web/src/pages/[gameId]/turn.tsx b/web/src/pages/[gameId]/turn.tsx index 6b838ed50..62cca40a1 100644 --- a/web/src/pages/[gameId]/turn.tsx +++ b/web/src/pages/[gameId]/turn.tsx @@ -6,7 +6,7 @@ import { useGameEntity } from "@/hooks/dojo/entities/useGameEntity"; import { usePlayerEntity } from "@/hooks/dojo/entities/usePlayerEntity"; import { TradeDirection, usePlayerState } from "@/hooks/state"; -import { getDrugByName, getEventByName, getLocationByName } from "@/hooks/ui"; +import { getDrugById, getEventByName, getLocationById } from "@/hooks/ui"; import { Box, Button, @@ -59,7 +59,7 @@ export default function Turn() { return ( - {getLocationByName(playerEntity.locationId).icon({})} - {playerEntity.locationId} + {getLocationById(playerEntity.locationId).icon({})} + {getLocationById(playerEntity.locationId).name} {events.map((event, index) => ( @@ -103,9 +103,7 @@ export default function Turn() { onClick={() => { clearState(); router.push( - `/${gameId}/${ - getLocationByName(playerEntity.locationId).slug - })}`, + `/${gameId}/${getLocationById(playerEntity.locationId).slug})}`, ); }} > diff --git a/web/src/pages/index.tsx b/web/src/pages/index.tsx index 35f5402ed..854f97156 100644 --- a/web/src/pages/index.tsx +++ b/web/src/pages/index.tsx @@ -17,7 +17,7 @@ import BorderImage from "@/components/icons/BorderImage"; import Link from "next/link"; import Leaderboard from "@/components/Leaderboard"; import { useSystems } from "@/hooks/dojo/systems/useSystems"; -import { getLocationByName } from "@/hooks/ui"; +import { getLocationById } from "@/hooks/ui"; import { JoinedEventData } from "@/utils/event"; import { useGlobalScores } from "@/hooks/dojo/components/useGlobalScores"; import { useToast } from "@/hooks/toast"; @@ -73,12 +73,11 @@ export default function Home() { MAX_PLAYERS, NUM_TURNS, ); - const { gameId, locationName } = event as JoinedEventData; + + const { gameId, locationId } = event as JoinedEventData; toast("Created Game", Alert, `http://amazing_explorer/${hash}`); - router.push( - `/${gameId}/${getLocationByName(locationName).slug}`, - ); + router.push(`/${gameId}/${getLocationById(locationId).slug}`); }} > Hustle diff --git a/web/src/utils/event.ts b/web/src/utils/event.ts index 0d0ee1fa8..b3a2167f5 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 { @@ -29,7 +30,7 @@ export interface CreateEventData extends BaseEventData { export interface JoinedEventData extends BaseEventData { playerId: string; - locationName: string; + locationId: string; } export interface BoughtEventData extends 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`); @@ -81,7 +80,7 @@ export const parseEvent = ( return { gameId: num.toHexString(raw.data[0]), playerId: num.toHexString(raw.data[1]), - locationName: shortString.decodeShortString(raw.data[2]), + locationId: num.toHexString(raw.data[2]), } as JoinedEventData; case RyoEvents.Traveled: