Skip to content

Commit

Permalink
Readded code that was lost due to poor rebasing. Applied formatting f…
Browse files Browse the repository at this point in the history
…ixes and ran eslint.
  • Loading branch information
eviterin committed Mar 16, 2024
1 parent d943959 commit d112e58
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 476 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/src/Game.sol
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ contract Game {
uint256[] storage cards = gdata.cards;
pdata.deckStart = uint8(cards.length);
inventory.checkDeck(msg.sender, deckID);
uint256[] memory deck = inventory.getDeck(msg.sender, deckID);
uint256[] memory deck = inventory.getDeckCards(msg.sender, deckID);

for (uint256 i = 0; i < deck.length; i++) {
cards.push(deck[i]);
Expand Down
18 changes: 10 additions & 8 deletions packages/contracts/src/Inventory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ contract Inventory is Ownable {
// ---------------------------------------------------------------------------------------------

// Returns the list of cards in the given deck of the given player.
function getDeck(address player, uint8 deckID)
function getDeckCards(address player, uint8 deckID)
external
view
exists(player, deckID)
Expand All @@ -342,16 +342,18 @@ contract Inventory is Ownable {

// ---------------------------------------------------------------------------------------------

// Returns the decks of a given player.
function getAllDecks(address player) external view returns (Deck[] memory) {
return decks[player];
}

// ---------------------------------------------------------------------------------------------

// Returns the list of cards in the given deck of the given player.
function getDeckReal(address player, uint8 deckID)
external
view
exists(player, deckID)
returns (Deck memory)
{
function getDeck(address player, uint8 deckID) external view exists(player, deckID) returns (Deck memory) {
return decks[player][deckID];
}

// ---------------------------------------------------------------------------------------------

// Returns the decks of a given player.
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/src/test/Integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ contract Integration is Test {
game.createGame(2);

// IDs don't start at 1 because the deploy script currently airdrops some addresses, might change.
inventory.getDeck(player2, 0); // player1 has card id of 49-72 inclusive
inventory.getDeck(player2, 0); // player2 has card id of 73-96 inclusive
inventory.getDeckCards(player2, 0); // player1 has card id of 49-72 inclusive
inventory.getDeckCards(player2, 0); // player2 has card id of 73-96 inclusive

vm.startPrank(player1);
game.joinGame(gameID, DECK_ID, SALT_HASH, JOIN_DATA);
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/test/Inventory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract InventoryTest is Test {
// expect revert if player's deck contains a card with more than `MAX_CARD_COPY` copies.
function testCheckDeckExceedsMaxCopy() public {
uint8 deckId = 0;
uint256 randomCard = inventory.getDeck(player1, deckId)[2];
uint256 randomCard = inventory.getDeckCards(player1, deckId)[2];

// increase card `randomCard` copies to 4
vm.startPrank(player1);
Expand Down
316 changes: 158 additions & 158 deletions packages/webapp/src/actions/getDeck.ts
Original file line number Diff line number Diff line change
@@ -1,158 +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 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<any> {
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<any> {
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<any> {
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<any> {
try {
return await getDeckNamesImpl(args)
} catch (err) {
defaultErrorHandling("getDeckNames", err)
return false
}
}

// -------------------------------------------------------------------------------------------------

async function getAllDecksImpl(args: GetDeckArgs): Promise<any> {
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<any> {
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<any> {
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<any> {
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
}
}

// =================================================================================================
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<any> {
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<any> {
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<any> {
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<any> {
try {
return await getDeckNamesImpl(args)
} catch (err) {
defaultErrorHandling("getDeckNames", err)
return false
}
}

// -------------------------------------------------------------------------------------------------

async function getAllDecksImpl(args: GetDeckArgs): Promise<any> {
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<any> {
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<any> {
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<any> {
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
}
}

// =================================================================================================
Loading

0 comments on commit d112e58

Please sign in to comment.