Skip to content

Commit

Permalink
add successfully retrieving sacrificed familiar
Browse files Browse the repository at this point in the history
  • Loading branch information
willemolding committed Dec 29, 2023
1 parent 7f057c3 commit 931e483
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 17 deletions.
33 changes: 27 additions & 6 deletions client/src/game/gameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,22 @@ export type SpellcrafterGame = {
familiar: Familiar | null,
}

export async function gameStateFromGameValuesQuery({ valueingameModels, familiarModels }: GetGameDataQuery): SpellcrafterGame {
export async function gameStateFromGameValuesQuery({ valueingameModels, familiarModels, ownerModels }: GetGameDataQuery): SpellcrafterGame {

const gameValues = new Map<number, number>();
let gameOwner = null;

ownerModels?.edges?.forEach((entity) => {
entity?.node?.entity?.models?.forEach((model) => {
switch (model?.__typename) {
case "Owner":
gameOwner = model?.address;
break;
default:
break;
}
})
})

valueingameModels?.edges?.forEach((entity) => {
entity?.node?.entity?.models?.forEach((model) => {
Expand All @@ -58,16 +71,24 @@ export async function gameStateFromGameValuesQuery({ valueingameModels, familiar
}).filter(([_, count]) => count)

const familiar = familiarModels?.edges?.map((entity) => {
const familiar: Familiar = {};
let familiar: Familiar | null = {};
entity?.node?.entity?.models?.forEach((model) => {
switch (model?.__typename) {
case "Familiar":
familiar.id = parseInt(model?.entity_id);
familiar.familiarType = model?.familiar_type;
familiar.id = parseInt(model?.entity_id);
familiar.familiarType = model?.familiar_type;
break;
case "Occupied":
familiar.busyUntil = model?.until || 0;
familiar.hasItem = model?.reaped == false && model?.doing !== "None" && model?.until <= time;
familiar.busyUntil = model?.until || 0;
familiar.hasItem = model?.reaped == false && model?.doing !== "None" && model?.until <= time;
break;
case "Owner":
console.log(model?.address, gameOwner)
if (model?.address != gameOwner) {
// the familiar has been sacrificed!
familiar = null;
return;
}
break;
default:
break;
Expand Down
22 changes: 21 additions & 1 deletion client/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ export type GetGameDataQueryVariables = Exact<{
}>;


export type GetGameDataQuery = { __typename?: 'World__Query', valueingameModels?: { __typename?: 'ValueingameConnection', edges?: Array<{ __typename?: 'ValueingameEdge', node?: { __typename?: 'Valueingame', entity?: { __typename?: 'World__Entity', keys?: Array<string | null> | null, models?: Array<{ __typename: 'Familiar' } | { __typename: 'Occupied' } | { __typename: 'Owner' } | { __typename: 'Valueingame', entity_id?: any | null, value?: any | null } | null> | null } | null } | null } | null> | null } | null, familiarModels?: { __typename?: 'FamiliarConnection', edges?: Array<{ __typename?: 'FamiliarEdge', node?: { __typename?: 'Familiar', entity?: { __typename?: 'World__Entity', keys?: Array<string | null> | null, models?: Array<{ __typename: 'Familiar', entity_id?: any | null, game_id?: any | null, familiar_type?: any | null } | { __typename: 'Occupied', entity_id?: any | null, until?: any | null, doing?: any | null, reaped?: any | null } | { __typename: 'Owner' } | { __typename: 'Valueingame' } | null> | null } | null } | null } | null> | null } | null };
export type GetGameDataQuery = { __typename?: 'World__Query', valueingameModels?: { __typename?: 'ValueingameConnection', edges?: Array<{ __typename?: 'ValueingameEdge', node?: { __typename?: 'Valueingame', entity?: { __typename?: 'World__Entity', keys?: Array<string | null> | null, models?: Array<{ __typename: 'Familiar' } | { __typename: 'Occupied' } | { __typename: 'Owner' } | { __typename: 'Valueingame', entity_id?: any | null, value?: any | null } | null> | null } | null } | null } | null> | null } | null, familiarModels?: { __typename?: 'FamiliarConnection', edges?: Array<{ __typename?: 'FamiliarEdge', node?: { __typename?: 'Familiar', entity?: { __typename?: 'World__Entity', keys?: Array<string | null> | null, models?: Array<{ __typename: 'Familiar', entity_id?: any | null, game_id?: any | null, familiar_type?: any | null } | { __typename: 'Occupied', entity_id?: any | null, until?: any | null, doing?: any | null, reaped?: any | null } | { __typename: 'Owner', entity_id?: any | null, address?: any | null } | { __typename: 'Valueingame' } | null> | null } | null } | null } | null> | null } | null, ownerModels?: { __typename?: 'OwnerConnection', edges?: Array<{ __typename?: 'OwnerEdge', node?: { __typename?: 'Owner', entity?: { __typename?: 'World__Entity', keys?: Array<string | null> | null, models?: Array<{ __typename: 'Familiar' } | { __typename: 'Occupied' } | { __typename: 'Owner', entity_id?: any | null, address?: any | null } | { __typename: 'Valueingame' } | null> | null } | null } | null } | null> | null } | null };

export type GetPlayersGamesQueryVariables = Exact<{
address: Scalars['ContractAddress']['input'];
Expand Down Expand Up @@ -583,6 +583,26 @@ export const GetGameDataDocument = gql`
doing
reaped
}
... on Owner {
entity_id
address
}
}
}
}
}
}
ownerModels(where: {entity_id: $game_id}) {
edges {
node {
entity {
keys
models {
__typename
... on Owner {
entity_id
address
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions client/src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,32 @@ query getGameData($game_id: u128!) {
doing
reaped
}
... on Owner {
entity_id
address
}
}
}
}
}
}

ownerModels(where: {entity_id: $game_id}) {
edges {
node {
entity {
keys
models {
__typename
... on Owner {
entity_id
address
}
}
}
}
}
}
}

query getPlayersGames($address: ContractAddress!) {
Expand Down
35 changes: 27 additions & 8 deletions client/src/ui/pages/gamePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const GamePage: React.FC = () => {
const {
account: { account },
networkLayer: {
systemCalls: { forage, interact, summon, send },
systemCalls: { forage, interact, summon, send, reapAction, sacrifice },
network: { graphSdk }
},
} = useDojo();
Expand Down Expand Up @@ -54,18 +54,29 @@ export const GamePage: React.FC = () => {
}, 2000)
}

const doReapAction = async (account: Account, familiar_id: number) => {
if (!currentGameId) return;
await reapAction(account, parseInt(currentGameId), familiar_id);
setTimeout(() => {
fetchGameData(currentGameId)
}, 2000)
}

const doSacrifice = async (account: Account, familiar_id: number) => {
if (!currentGameId) return;
await sacrifice(account, parseInt(currentGameId), familiar_id);
setTimeout(() => {
fetchGameData(currentGameId)
}, 2000)
}

const fetchGameData = async (currentGameId: string | null) => {
if (!currentGameId) return;
const game_id = padHex(currentGameId);
const { data: valueData } = await graphSdk().getGameData({ game_id });

const gameState = await gameStateFromGameValuesQuery(valueData);

// if there is a familiar then fetch the occupied data

// const { data: occupiedData } = await graphSdk().getOccupied({ entity_id: padHex(currentGameId) });


setGameState(gameState);
}

Expand All @@ -91,12 +102,20 @@ export const GamePage: React.FC = () => {
</div>

<div className="global-button-style" style={{ fontSize: "2.4cqw", padding: "5px 10px", fontFamily: "OL", fontWeight: "100" }} onClick={() => { doSummon(account, 0) }}>
Summon
Summon Familiar
</div>


<div className="global-button-style" style={{ fontSize: "2.4cqw", padding: "5px 10px", fontFamily: "OL", fontWeight: "100" }} onClick={() => { doSend(account, gameState?.familiar?.id) }}>
Send
Send Familiar
</div>

<div className="global-button-style" style={{ fontSize: "2.4cqw", padding: "5px 10px", fontFamily: "OL", fontWeight: "100" }} onClick={() => { doReapAction(account, gameState?.familiar?.id) }}>
Reap
</div>

<div className="global-button-style" style={{ fontSize: "2.4cqw", padding: "5px 10px", fontFamily: "OL", fontWeight: "100" }} onClick={() => { doSacrifice(account, gameState?.familiar?.id) }}>
Sacrifice Familiar
</div>

{/* <div className="global-button-style" style={{ fontSize: "2.4cqw", padding: "5px 10px", fontFamily: "OL", fontWeight: "100" }} onClick={() => { doForage(account)}}>
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod spellcrafter_system {
use core::zeroable::Zeroable;
use spellcrafter::constants::{
INITIAL_BARRIERS, BARRIERS_STAT, HOTCOLD_STAT, LIGHTDARK_STAT, POLAR_STAT_MIDPOINT,
CHAOS_STAT, ITEMS_HELD, CHAOS_PER_FORAGE, ITEM_LIMIT, FAMILIAR_LIMIT, FAMILIARS_HELD,
CHAOS_STAT, ITEMS_HELD, TICKS_PER_FORAGE, ITEM_LIMIT, FAMILIAR_LIMIT, FAMILIARS_HELD,
TICKS_PER_SUMMON, BARRIERS_LIMIT, TICKS, TICKS_PER_SEND,
};
use spellcrafter::types::{Region, FamiliarType, FamiliarTypeTrait, Action};
Expand Down Expand Up @@ -61,7 +61,7 @@ mod spellcrafter_system {
let card_id = draw_from_region(world, game_id, region);

// increase chaos by a fixed amount. In the future this will be a function of time
increase_stat(world, game_id, CHAOS_STAT, CHAOS_PER_FORAGE);
tick(world, game_id, TICKS_PER_FORAGE);

return card_id;
}
Expand Down

0 comments on commit 931e483

Please sign in to comment.