Skip to content

Commit

Permalink
query all game infos
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Apr 30, 2024
1 parent adb32de commit c764424
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 48 deletions.
39 changes: 31 additions & 8 deletions web/src/dojo/hooks/useGamesByPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { Game, GameEdge, useGamesByPlayerQuery } from "@/generated/graphql";
import {
Game,
GameEdge,
GameStorePacked,
Maybe,
World__Entity,
World__EntityEdge,
useGamesByPlayerQuery,
} from "@/generated/graphql";
import { useMemo } from "react";
import { GameClass } from "../class/Game";
import { useDojoContext } from "./useDojoContext";

export interface GamesByPlayerInterface {
games: Game[];
onGoingGames: Game[];
endedGames: Game[];
games: GameClass[];
onGoingGames: GameClass[];
endedGames: GameClass[];
isFetched: boolean;
}

Expand All @@ -13,17 +23,30 @@ export const useGamesByPlayer = (playerId: string): GamesByPlayerInterface => {
playerId,
});

const { configStore } = useDojoContext();

const games = useMemo(() => {
const edges = data?.gameModels?.edges as GameEdge[];
return (edges || []).map((i) => i.node as Game);
const edges = data?.entities?.edges as World__EntityEdge[];
const nodes = (edges || []).map((i) => i.node);

const games = nodes.flatMap((i) => {
const game = (i!.models || []).find((i) => i?.__typename === "Game") as Game;
const gamePacked = (i!.models || []).find((i) => i?.__typename === "GameStorePacked") as GameStorePacked;

if (!game || !gamePacked) return [];

return [new GameClass(configStore, game, gamePacked)];
});

return games;
}, [data]);

const onGoingGames = useMemo(() => {
return games.filter((i: Game) => !i.game_over);
return games.filter((i: GameClass) => !i.gameInfos.game_over);
}, [games]);

const endedGames = useMemo(() => {
return games.filter((i: Game) => i.game_over);
return games.filter((i: GameClass) => i.gameInfos.game_over);
}, [games]);

return {
Expand Down
34 changes: 23 additions & 11 deletions web/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1732,11 +1732,11 @@ export type GameByIdQueryVariables = Exact<{
export type GameByIdQuery = { __typename?: 'World__Query', gameModels?: { __typename?: 'GameConnection', edges?: Array<{ __typename?: 'GameEdge', node?: { __typename?: 'Game', game_id?: any | null, game_mode?: any | null, max_turns?: any | null, max_wanted_shopping?: any | null, hustler_id?: any | null, player_name?: any | null, player_id?: any | null, game_over?: any | null } | null } | null> | null } | null };

export type GamesByPlayerQueryVariables = Exact<{
playerId?: InputMaybe<Scalars['ContractAddress']>;
playerId?: InputMaybe<Scalars['String']>;
}>;


export type GamesByPlayerQuery = { __typename?: 'World__Query', gameModels?: { __typename?: 'GameConnection', edges?: Array<{ __typename?: 'GameEdge', node?: { __typename?: 'Game', game_id?: any | null, player_id?: any | null, leaderboard_version?: any | null, game_mode?: any | null, hustler_id?: any | null, player_name?: any | null, game_over?: any | null } | null } | null> | null } | null };
export type GamesByPlayerQuery = { __typename?: 'World__Query', entities?: { __typename?: 'World__EntityConnection', edges?: Array<{ __typename?: 'World__EntityEdge', node?: { __typename?: 'World__Entity', id?: string | null, keys?: Array<string | null> | null, models?: Array<{ __typename: 'DrugConfig' } | { __typename: 'ERC20AllowanceModel' } | { __typename: 'ERC20BalanceModel' } | { __typename: 'ERC20MetadataModel' } | { __typename: 'EncounterConfig' } | { __typename: 'Game', game_id?: any | null, player_id?: any | null, leaderboard_version?: any | null, game_mode?: any | null, hustler_id?: any | null, player_name?: any | null, game_over?: any | null } | { __typename: 'GameConfig' } | { __typename: 'GameStorePacked', game_id?: any | null, player_id?: any | null, packed?: any | null } | { __typename: 'HustlerItemBaseConfig' } | { __typename: 'HustlerItemTiersConfig' } | { __typename: 'InitializableModel' } | { __typename: 'Leaderboard' } | { __typename: 'LocationConfig' } | { __typename: 'RyoAddress' } | { __typename: 'RyoConfig' } | null> | null } | null } | null> | null } | null };

export type GameStorePackedQueryVariables = Exact<{
gameId: Scalars['String'];
Expand Down Expand Up @@ -2009,17 +2009,29 @@ useInfiniteGameByIdQuery.getKey = (variables?: GameByIdQueryVariables) => variab
;

export const GamesByPlayerDocument = `
query GamesByPlayer($playerId: ContractAddress) {
gameModels(where: {player_id: $playerId}) {
query GamesByPlayer($playerId: String) {
entities(keys: ["*", $playerId]) {
edges {
node {
game_id
player_id
leaderboard_version
game_mode
hustler_id
player_name
game_over
id
keys
models {
__typename
... on Game {
game_id
player_id
leaderboard_version
game_mode
hustler_id
player_name
game_over
}
... on GameStorePacked {
game_id
player_id
packed
}
}
}
}
}
Expand Down
32 changes: 23 additions & 9 deletions web/src/graphql/game.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,31 @@ query GameById($gameId: u32) {
}
}

query GamesByPlayer($playerId: ContractAddress) {
gameModels(where: { player_id: $playerId }) {

query GamesByPlayer($playerId: String) {
entities(keys: ["*", $playerId]) {
edges {
node {
game_id
player_id
leaderboard_version
game_mode
hustler_id
player_name
game_over
id
keys
models {
__typename
... on Game {
game_id
player_id
leaderboard_version
game_mode
hustler_id
player_name
game_over
}
... on GameStorePacked {
game_id
player_id
packed

}
}
}
}
}
Expand Down
67 changes: 47 additions & 20 deletions web/src/pages/game/history.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Button } from "@/components/common";
import { HustlerIcon, Hustlers } from "@/components/hustlers";
import { Alert, Clock, User } from "@/components/icons";
import { Layout } from "@/components/layout";
import { HomeLeftPanel, Leaderboard, Tutorial } from "@/components/pages/home";
import { HallOfFame } from "@/components/pages/home/HallOfFame";
import { GameClass } from "@/dojo/class/Game";
import { useDojoContext, useRouterContext } from "@/dojo/hooks";
import { useGamesByPlayer } from "@/dojo/hooks/useGamesByPlayer";
import { Game } from "@/generated/graphql";
import { play } from "@/hooks/media";
import { Sounds, playSound } from "@/hooks/sound";
import { useToast } from "@/hooks/toast";
import { formatCashHeader } from "@/utils/ui";
import {
Card,
Divider,
Expand Down Expand Up @@ -82,14 +85,14 @@ export default function History() {
);
}

const GameList = ({ games }: { games?: Game[] }) => {
const GameList = ({ games }: { games?: GameClass[] }) => {
const { router } = useRouterContext();

const onClick = (game: Game) => {
if (!game.game_over) {
router.push(`/0x${game.game_id.toString(16)}`);
const onClick = (game: GameClass) => {
if (!game.gameInfos.game_over) {
router.push(`/0x${game.gameInfos.game_id.toString(16)}`);
} else {
router.push(`/0x${game.game_id.toString(16)}/logs`);
router.push(`/0x${game.gameInfos.game_id.toString(16)}/logs`);
}
};

Expand All @@ -98,37 +101,61 @@ const GameList = ({ games }: { games?: Game[] }) => {
<UnorderedList boxSize="full" variant="dotted" h="auto" fontSize={"12px"}>
<ListItem key={"OxO"}>
<HStack mr={3} whiteSpace="nowrap">
<Text w={"70px"} flexShrink={0}>
Saison
<Text w={"40px"} flexShrink={0}></Text>
<Text w={"80px"} flexShrink={0}>
Identity
</Text>
<Text w={"70px"} flexShrink={0}>
Game id
<Text w={"70px"} flexShrink={0} align="right">
Turn
</Text>
<Text w={"200px"} flexShrink={0}>
Identity
<Text w={"120px"} flexShrink={0} align="right">
Location
</Text>
<Text w={"70px"} flexShrink={0} align="right">
Health
</Text>
<Text w={"100px"} flexShrink={0} align="right">
Cash
</Text>
</HStack>
</ListItem>

{games.map((game: Game, index: number) => {
const playerName = shortString.decodeShortString(game.player_name);
{games.map((game: GameClass, index: number) => {
const playerName = shortString.decodeShortString(game.gameInfos.player_name);

return (
<ListItem key={game.game_id} cursor="pointer" onClick={() => onClick(game)} h="30px">
<ListItem key={game.gameInfos.game_id} cursor="pointer" onClick={() => onClick(game)} h="30px">
<HStack mr={3} whiteSpace="nowrap">
<Text w={"70px"} flexShrink={0}>
{game.leaderboard_version}
</Text>
<Text w={"70px"} flexShrink={0}>
{game.game_id}
<Text w={"40px"} flexShrink={0}>
<HustlerIcon hustler={game.gameInfos.hustler_id as Hustlers} />
</Text>
<Text w={"200px"} flexShrink={0}>

<Text w={"80px"} flexShrink={0}>
{playerName}
</Text>
<Text w={"70px"} flexShrink={0} align="right">
{game.player.turn}
</Text>
<Text w={"120px"} flexShrink={0} align="right">
{game.player.location.name}
</Text>
<Text w={"70px"} flexShrink={0} align="right">
{game.player.health}
</Text>
<Text w={"100px"} flexShrink={0} align="right">
{formatCashHeader(game.player.cash)}
</Text>
</HStack>
</ListItem>
);
})}
</UnorderedList>
);
};

// <Text w={"70px"} flexShrink={0}>
// {game.gameInfos.leaderboard_version}
// </Text>
// <Text w={"70px"} flexShrink={0}>
// {game.gameInfos.game_id}
// </Text>

0 comments on commit c764424

Please sign in to comment.