Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/client/src/contexts/CharacterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const CharacterProvider = ({

const [isRefreshing, setIsRefreshing] = useState(false);

const getCharacterData = useCallback(async () => {
const refreshCharacterData = useCallback(async () => {
if (!(delegatorAddress && publicClient && worldContract)) return;
const characterComponent = Array.from(
runQuery([
Expand Down Expand Up @@ -174,18 +174,18 @@ export const CharacterProvider = ({
const refreshCharacter = useCallback(async () => {
setIsRefreshing(true);
try {
await getCharacterData();
await refreshCharacterData();
} catch (error) {
renderError('Error refreshing character');
} finally {
setIsRefreshing(false);
}
}, [getCharacterData, renderError]);
}, [refreshCharacterData, renderError]);

useEffect(() => {
if (!(delegatorAddress && publicClient && worldContract)) return;
getCharacterData();
}, [delegatorAddress, getCharacterData, publicClient, worldContract]);
refreshCharacterData();
}, [delegatorAddress, refreshCharacterData, publicClient, worldContract]);

return (
<CharacterContext.Provider
Expand Down
100 changes: 91 additions & 9 deletions packages/client/src/pages/CharacterCreation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ import { singletonEntity } from '@latticexyz/store-sync/recs';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { FaLock } from 'react-icons/fa';
import { useNavigate } from 'react-router-dom';
import { getContract } from 'viem';
import { useWalletClient } from 'wagmi';

import { useCharacter } from '../contexts/CharacterContext';
import { useMUD } from '../contexts/MUDContext';
import { useToast } from '../hooks/useToast';
import { useUploadFile } from '../hooks/useUploadFile';
import { API_URL } from '../utils/constants';
import { shortenAddress } from '../utils/helpers';
import { StatsClasses } from '../utils/types';
import {
fetchMetadataFromUri,
shortenAddress,
uriToHttp,
} from '../utils/helpers';
import { StatsClasses, Weapon } from '../utils/types';

const STARTER_WEAPON_IDS = [BigInt(1), BigInt(2), BigInt(3)];

export const CharacterCreation = (): JSX.Element => {
const navigate = useNavigate();
Expand All @@ -42,6 +49,7 @@ export const CharacterCreation = (): JSX.Element => {
components: { UltimateDominionConfig },
delegatorAddress,
isSynced,
network: { publicClient, worldContract },
systemCalls: { enterGame, mintCharacter, rollStats },
} = useMUD();
const { character, characterStats, isRefreshing, refreshCharacter } =
Expand All @@ -59,6 +67,7 @@ export const CharacterCreation = (): JSX.Element => {
const [characterClass, setCharacterClass] = useState<StatsClasses>(
StatsClasses.Warrior,
);
const [starterWeapons, setStarterWeapons] = useState<Weapon[] | null>(null);

const [isCreating, setIsCreating] = useState(false);
const [showError, setShowError] = useState(false);
Expand All @@ -75,6 +84,72 @@ export const CharacterCreation = (): JSX.Element => {
setShowError(false);
}, [avatar, description, name]);

const fetchStarterWeapons = useCallback(async () => {
try {
const _items: Weapon[] = await Promise.all(
STARTER_WEAPON_IDS.map(async itemId => {
const itemTemplateStats = await worldContract.read.UD__getWeaponStats(
[itemId],
);

const itemsContractAddress =
await worldContract.read.UD__getItemsContract();

const itemsToken = getContract({
address: itemsContractAddress,
abi: [
{
constant: true,
inputs: [
{
name: 'tokenId',
type: 'uint256',
},
],
name: 'uri',
outputs: [
{
name: '',
type: 'string',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
],
client: publicClient,
});

const metadataURI = await itemsToken.read.uri([itemId]);
const fetachedMetadata = await fetchMetadataFromUri(
uriToHttp(metadataURI)[0],
);

return {
agiModifier: itemTemplateStats.agiModifier.toString(),
classRestrictions: itemTemplateStats.classRestrictions,
hitPointModifier: itemTemplateStats.hitPointModifier.toString(),
intModifier: itemTemplateStats.intModifier.toString(),
maxDamage: itemTemplateStats.maxDamage.toString(),
minDamage: itemTemplateStats.minDamage.toString(),
minLevel: itemTemplateStats.minLevel.toString(),
strModifier: itemTemplateStats.strModifier.toString(),
...fetachedMetadata,
} as Weapon;
}),
);

setStarterWeapons(_items);
} catch (error) {
renderError(error, 'Error fetching starter item.');
}
}, [publicClient, renderError, worldContract]);

useEffect(() => {
fetchStarterWeapons();
}, [fetchStarterWeapons]);

const onUploadAvatar = useCallback(() => {
const input = document.getElementById('avatarInput');

Expand Down Expand Up @@ -527,13 +602,20 @@ export const CharacterCreation = (): JSX.Element => {
<Heading size="sm">Items</Heading>
<Text>1</Text>
</HStack>
<HStack border="1px solid" borderColor="grey400" w="100%">
<Box bgColor="grey400" h="50px" w="50px" />
<Box>
<Text size="xs">Rusty Dagger</Text>
<Text size="xs">STR+1 AGI+3 INT+4</Text>
</Box>
</HStack>
{starterWeapons && starterWeapons[characterClass] && (
<HStack border="1px solid" borderColor="grey400" w="100%">
<Box bgColor="grey400" h="50px" w="50px" />
<Box>
<Text size="xs">{starterWeapons[characterClass].name}</Text>
<Text size="xs">
STR+
{starterWeapons[characterClass].strModifier} AGI+
{starterWeapons[characterClass].agiModifier} INT+
{starterWeapons[characterClass].intModifier}
</Text>
</Box>
</HStack>
)}
<Link
alignSelf="end"
color="grey500"
Expand Down
11 changes: 11 additions & 0 deletions packages/client/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ export type Monster = Metadata & {
mobId: string;
monsterId: Entity;
};

export type Weapon = Metadata & {
agiModifier: string;
classRestrictions: StatsClasses[];
hitPointModifier: string;
intModifier: string;
maxDamage: string;
minDamage: string;
minLevel: string;
strModifier: string;
};
47 changes: 47 additions & 0 deletions packages/contracts/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"metadataUriPrefix": "ipfs://QmVUaqRpQJHyqugYd12Qf2iErNSoGvLF1cbeRHpmX8bChs/",
"items": [
{
"metadataUri": "1_rusty_sword.json",
"name": "Rusty Sword",
"stats": {
"agiModifier": 4,
"classRestrictions": [],
"hitPointModifier": 6,
"intModifier": 5,
"maxDamage": 2,
"minDamage": 1,
"minLevel": 1,
"strModifier": 3
}
},
{
"metadataUri": "2_cracked_dagger.json",
"name": "Cracked Dagger",
"stats": {
"agiModifier": 4,
"classRestrictions": [],
"hitPointModifier": 6,
"intModifier": 5,
"maxDamage": 2,
"minDamage": 1,
"minLevel": 1,
"strModifier": 3
}
},
{
"metadataUri": "3_cobbled_wand.json",
"name": "Cobbled Wand",
"stats": {
"agiModifier": 4,
"classRestrictions": [],
"hitPointModifier": 6,
"intModifier": 5,
"maxDamage": 2,
"minDamage": 1,
"minLevel": 1,
"strModifier": 3
}
}
]
}

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions packages/contracts/out/IWorld.sol/IWorld.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1293,42 +1293,42 @@
"internalType": "struct WeaponStats",
"components": [
{
"name": "minDamage",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "maxDamage",
"type": "uint256",
"internalType": "uint256"
"name": "agiModifier",
"type": "int256",
"internalType": "int256"
},
{
"name": "classRestrictions",
"type": "uint8[]",
"internalType": "uint8[]"
},
{
"name": "minLevel",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "strModifier",
"name": "hitPointModifier",
"type": "int256",
"internalType": "int256"
},
{
"name": "agiModifier",
"name": "intModifier",
"type": "int256",
"internalType": "int256"
},
{
"name": "intModifier",
"type": "int256",
"internalType": "int256"
"name": "maxDamage",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "hitPointModifier",
"name": "minDamage",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "minLevel",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "strModifier",
"type": "int256",
"internalType": "int256"
}
Expand Down
38 changes: 19 additions & 19 deletions packages/contracts/out/IWorld.sol/IWorld.abi.json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1293,42 +1293,42 @@ declare const abi: [
"internalType": "struct WeaponStats",
"components": [
{
"name": "minDamage",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "maxDamage",
"type": "uint256",
"internalType": "uint256"
"name": "agiModifier",
"type": "int256",
"internalType": "int256"
},
{
"name": "classRestrictions",
"type": "uint8[]",
"internalType": "uint8[]"
},
{
"name": "minLevel",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "strModifier",
"name": "hitPointModifier",
"type": "int256",
"internalType": "int256"
},
{
"name": "agiModifier",
"name": "intModifier",
"type": "int256",
"internalType": "int256"
},
{
"name": "intModifier",
"type": "int256",
"internalType": "int256"
"name": "maxDamage",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "hitPointModifier",
"name": "minDamage",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "minLevel",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "strModifier",
"type": "int256",
"internalType": "int256"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/out/IWorld.sol/IWorld.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/contracts/out/MapSystem.sol/MapSystem.json

Large diffs are not rendered by default.

Loading