From cde5752219d57029a7ae857e9b23a851b0d284c8 Mon Sep 17 00:00:00 2001 From: eviterin Date: Sat, 16 Mar 2024 16:11:04 +0100 Subject: [PATCH] Readded code that was lost due to poor rebasing. Applied formatting fixes and ran eslint. --- packages/contracts/src/Inventory.sol | 2 +- packages/webapp/src/actions/getDeck.ts | 278 +++++++++-------- packages/webapp/src/actions/setDeck.ts | 191 ++++++------ .../src/components/collection/deckList.tsx | 112 ++++--- .../src/components/collection/deckPanel.tsx | 13 +- packages/webapp/src/pages/collection.tsx | 290 +++++++++--------- 6 files changed, 481 insertions(+), 405 deletions(-) diff --git a/packages/contracts/src/Inventory.sol b/packages/contracts/src/Inventory.sol index 8225a42f..379bc239 100644 --- a/packages/contracts/src/Inventory.sol +++ b/packages/contracts/src/Inventory.sol @@ -339,7 +339,7 @@ contract Inventory is Ownable { { return decks[player][deckID].cards; } - + // --------------------------------------------------------------------------------------------- // Returns the decks of a given player. diff --git a/packages/webapp/src/actions/getDeck.ts b/packages/webapp/src/actions/getDeck.ts index 2f396879..0162501f 100644 --- a/packages/webapp/src/actions/getDeck.ts +++ b/packages/webapp/src/actions/getDeck.ts @@ -1,120 +1,158 @@ -import { defaultErrorHandling } from "src/actions/errors" -import { contractWriteThrowing } from "src/actions/libContractWrite" -import { Address } from "src/chain" -import { deployment } from "src/deployment" -import { inventoryABI } from "src/generated" - -// ================================================================================================= - -export type getAllDecksArgs = { - playerAddress: Address - onSuccess: () => void -} - -// ------------------------------------------------------------------------------------------------- - -/** - * Fetches all decks of the given player by sending the `getAllDecks` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function getAllDecks(args: getAllDecksArgs): Promise { - try { - return await getAllDecksImpl(args) - } catch (err) { - defaultErrorHandling("getAllDecks", err) - return false - } -} - -/** - * Fetches the deck of the given player of a given ID by sending the `getDeck` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function getDeck(args: GetDeckAtArgs): Promise { - try { - return await getDeckImpl(args) - } catch (err) { - defaultErrorHandling("getDeck", err) - return false - } -} - -// ------------------------------------------------------------------------------------------------- - -async function getAllDecksImpl(args: getAllDecksArgs): Promise { - try { - const result = await contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "getAllDecks", - args: [args.playerAddress], - }) - - args.onSuccess() - return result - } catch (error) { - console.error("Error fetching decks:", error) - return null - } - } - -// ------------------------------------------------------------------------------------------------- - -async function getDeckImpl(args: GetDeckAtArgs): Promise { - try { - const result = await contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "getDeck", - args: [args.playerAddress, args.index], - }) - - args.onSuccess() - return result - } catch (error) { - console.error("Error fetching deck:", error) - return null - } -} - -// ------------------------------------------------------------------------------------------------- - -async function getNumDecksImpl(args: GetDeckArgs): Promise { - try { - const result = await contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "getNumDecks", - args: [args.playerAddress], - }) - - args.onSuccess() - return result - } catch (error) { - console.error("Error fetching decks:", error) - return null - } -} - -// ------------------------------------------------------------------------------------------------- - -async function getDeckNamesImpl(args: GetDeckArgs): Promise { - try { - const result = await contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "getDeckNames", - args: [args.playerAddress], - }) - - args.onSuccess() - return result - } catch (error) { - console.error("Error fetching decks:", error) - return null - } -} - -// ================================================================================================= \ No newline at end of file +import { defaultErrorHandling } from "src/actions/errors" +import { contractWriteThrowing } from "src/actions/libContractWrite" +import { Address } from "src/chain" +import { deployment } from "src/deployment" +import { inventoryABI } from "src/generated" + +// ================================================================================================= + +export type GetDeckArgs = { + playerAddress: Address + onSuccess: () => void +} + +export type GetDeckAtArgs = { + playerAddress: Address + onSuccess: () => void + index: number +} + +// ------------------------------------------------------------------------------------------------- + +/** + * Fetches all decks of the given player by sending the `getAllDecks` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function getAllDecks(args: GetDeckArgs): Promise { + try { + return await getAllDecksImpl(args) + } catch (err) { + defaultErrorHandling("getAllDecks", err) + return false + } +} + +/** + * Fetches the deck of the given player of a given ID by sending the `getDeckReal` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function getDeck(args: GetDeckAtArgs): Promise { + try { + return await getDeckImpl(args) + } catch (err) { + defaultErrorHandling("getDeck", err) + return false + } +} + +// ------------------------------------------------------------------------------------------------- + +/** + * Fetches deck count of the given player by sending the `getNumDecks` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function getNumDecks(args: GetDeckArgs): Promise { + try { + return await getNumDecksImpl(args) + } catch (err) { + defaultErrorHandling("getNumDecks", err) + return false + } +} + +// ------------------------------------------------------------------------------------------------- + +/** + * Fetches deck count of the given player by sending the `getNumDecks` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function getDeckNames(args: GetDeckArgs): Promise { + try { + return await getDeckNamesImpl(args) + } catch (err) { + defaultErrorHandling("getDeckNames", err) + return false + } +} + +// ------------------------------------------------------------------------------------------------- + +async function getAllDecksImpl(args: GetDeckArgs): Promise { + try { + const result = await contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "getAllDecks", + args: [args.playerAddress], + }) + + args.onSuccess() + return result + } catch (error) { + console.error("Error fetching decks:", error) + return null + } +} + +// ------------------------------------------------------------------------------------------------- + +async function getDeckImpl(args: GetDeckAtArgs): Promise { + try { + const result = await contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "getDeckReal", + args: [args.playerAddress, args.index], + }) + + args.onSuccess() + return result + } catch (error) { + console.error("Error fetching deck:", error) + return null + } +} + +// ------------------------------------------------------------------------------------------------- + +async function getNumDecksImpl(args: GetDeckArgs): Promise { + try { + const result = await contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "getNumDecks", + args: [args.playerAddress], + }) + + args.onSuccess() + return result + } catch (error) { + console.error("Error fetching decks:", error) + return null + } +} + +// ------------------------------------------------------------------------------------------------- + +async function getDeckNamesImpl(args: GetDeckArgs): Promise { + try { + const result = await contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "getDeckNames", + args: [args.playerAddress], + }) + + args.onSuccess() + return result + } catch (error) { + console.error("Error fetching decks:", error) + return null + } +} + +// ================================================================================================= diff --git a/packages/webapp/src/actions/setDeck.ts b/packages/webapp/src/actions/setDeck.ts index 4e20d60c..c114aeae 100644 --- a/packages/webapp/src/actions/setDeck.ts +++ b/packages/webapp/src/actions/setDeck.ts @@ -1,101 +1,90 @@ -import { defaultErrorHandling } from "src/actions/errors" -import { contractWriteThrowing } from "src/actions/libContractWrite" -import { Address } from "src/chain" -import { deployment } from "src/deployment" -import { inventoryABI } from "src/generated" -import { checkFresh, freshWrap } from "src/store/checkFresh" -import { Deck } from "src/store/types" - -// ================================================================================================= - -export type SaveArgs = { - deck: Deck - playerAddress: Address - onSuccess: () => void -} - -export type ModifyArgs = { - deck: Deck - playerAddress: Address - index: number - onSuccess: () => void -} - - -// ------------------------------------------------------------------------------------------------- - -/** - * Saves a deck created by the player by sending the `saveDeck` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function save(args: SaveArgs): Promise { - try { - return await saveImpl(args) - } catch (err) { - return defaultErrorHandling("save", err) - } -} - -/** - * Modifies a deck owned by the player by sending the `modifyDeck` transaction. - * - * Returns `true` iff the transaction is successful. - */ -export async function modify(args: ModifyArgs): Promise { - try { - return await modifyImpl(args) - } catch (err) { - return defaultErrorHandling("modify", err) - } -} - -// ------------------------------------------------------------------------------------------------- - -async function saveImpl(args: SaveArgs): Promise { - const cardBigInts = args.deck.cards.map(card => card.id) - - checkFresh(await freshWrap( - contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "addDeck", - args: [ - args.playerAddress, - { name: args.deck.name, cards: cardBigInts } - ], - }))) - - args.onSuccess() - return true -} - -async function modifyImpl(args: ModifyArgs): Promise { -<<<<<<< HEAD -<<<<<<< HEAD -======= ->>>>>>> 35b5d7c (Modify deck support) - const cardBigInts = args.deck.cards.map(card => card.id) - console.log("INDEX: " + args.index) - checkFresh(await freshWrap( - contractWriteThrowing({ - contract: deployment.Inventory, - abi: inventoryABI, - functionName: "replaceDeck", - args: [ - args.playerAddress, - args.index, - { name: args.deck.name, cards: cardBigInts } - ], - }))) - - args.onSuccess() - return true -<<<<<<< HEAD -======= ->>>>>>> 3d86518 (Can now save deck onchain) -======= ->>>>>>> 35b5d7c (Modify deck support) -} - -// ================================================================================================= \ No newline at end of file +import { defaultErrorHandling } from "src/actions/errors" +import { contractWriteThrowing } from "src/actions/libContractWrite" +import { Address } from "src/chain" +import { deployment } from "src/deployment" +import { inventoryABI } from "src/generated" +import { checkFresh, freshWrap } from "src/store/checkFresh" +import { Deck } from "src/store/types" + +// ================================================================================================= + +export type SaveArgs = { + deck: Deck + playerAddress: Address + onSuccess: () => void +} + +export type ModifyArgs = { + deck: Deck + playerAddress: Address + index: number + onSuccess: () => void +} + +// ------------------------------------------------------------------------------------------------- + +/** + * Saves a deck created by the player by sending the `saveDeck` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function save(args: SaveArgs): Promise { + try { + return await saveImpl(args) + } catch (err) { + return defaultErrorHandling("save", err) + } +} + +/** + * Modifies a deck owned by the player by sending the `modifyDeck` transaction. + * + * Returns `true` iff the transaction is successful. + */ +export async function modify(args: ModifyArgs): Promise { + try { + return await modifyImpl(args) + } catch (err) { + return defaultErrorHandling("modify", err) + } +} + +// ------------------------------------------------------------------------------------------------- + +async function saveImpl(args: SaveArgs): Promise { + const cardBigInts = args.deck.cards.map((card) => card.id) + + checkFresh( + await freshWrap( + contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "addDeck", + args: [args.playerAddress, { name: args.deck.name, cards: cardBigInts }], + }) + ) + ) + + args.onSuccess() + return true +} + +async function modifyImpl(args: ModifyArgs): Promise { + const cardBigInts = args.deck.cards.map((card) => card.id) + console.log("INDEX: " + args.index) + checkFresh( + await freshWrap( + contractWriteThrowing({ + contract: deployment.Inventory, + abi: inventoryABI, + functionName: "replaceDeck", + args: [args.playerAddress, args.index, { name: args.deck.name, cards: cardBigInts }], + }) + ) + ) + + args.onSuccess() + return true +} + +// ================================================================================================= diff --git a/packages/webapp/src/components/collection/deckList.tsx b/packages/webapp/src/components/collection/deckList.tsx index 7e4ffd8e..770ecf34 100644 --- a/packages/webapp/src/components/collection/deckList.tsx +++ b/packages/webapp/src/components/collection/deckList.tsx @@ -1,47 +1,79 @@ -import React from 'react' +import React, { useCallback, useEffect, useState } from "react" + +import { getDeckNames } from "src/actions/getDeck" import Link from "src/components/link" -import { Deck } from 'src/store/types' +import { Button } from "src/components/ui/button" +import * as store from "src/store/hooks" interface DeckCollectionDisplayProps { - decks: Deck[] - onDeckSelect: (deckID: number) => void - isLoadingDecks: boolean + onDeckSelect: (deckID: number) => void } -const DeckCollectionDisplay: React.FC = ({ decks, onDeckSelect, isLoadingDecks }) => { - return ( -
- {/* New Deck Button */} - - New Deck → - - - {/* Loading Decks Button */} - {isLoadingDecks && ( - - )} - - {/* Deck Buttons */} - {decks.map((deck, deckID) => ( - - ))} - -
- ) +const DeckCollectionDisplay: React.FC = ({ onDeckSelect }) => { + const playerAddress = store.usePlayerAddress() + const [deckNames, setDeckNames] = useState([]) + const [isLoadingDecks, setIsLoadingDecks] = useState(false) + + const loadDeckNames = useCallback(() => { + if (playerAddress) { + setIsLoadingDecks(true) + getDeckNames({ + playerAddress: playerAddress, + onSuccess: () => {}, + }) + .then((response) => { + if (!response.simulatedResult) return + const receivedDecks = response.simulatedResult as string[] + setDeckNames(receivedDecks) + }) + .catch((error) => { + console.error("Error fetching decks:", error) + }) + .finally(() => { + setIsLoadingDecks(false) + }) + } + }, [playerAddress]) + + useEffect(() => { + loadDeckNames() + }, [loadDeckNames]) + + return ( +
+ {/* New Deck Button */} + + + {/* Loading Button */} + {isLoadingDecks && ( + + )} + + {/* Deck Buttons */} + {deckNames.map((deckname, deckID) => ( + + ))} +
+ ) } -export default DeckCollectionDisplay \ No newline at end of file +export default DeckCollectionDisplay diff --git a/packages/webapp/src/components/collection/deckPanel.tsx b/packages/webapp/src/components/collection/deckPanel.tsx index 46c23f45..9699d065 100644 --- a/packages/webapp/src/components/collection/deckPanel.tsx +++ b/packages/webapp/src/components/collection/deckPanel.tsx @@ -62,14 +62,15 @@ const DeckConstructionPanel: React.FC = ({ {/* Counter Row */}
-
-
+
+
-
- {selectedCards.length}/{MAX_CARDS} + {selectedCards.length}/{MAX_CARDS}
@@ -77,7 +78,7 @@ const DeckConstructionPanel: React.FC = ({