Skip to content

Commit

Permalink
Merge pull request #246 from P4-Games/feature/match-endpoint
Browse files Browse the repository at this point in the history
Feature/match endpoint
  • Loading branch information
dappsar authored Dec 13, 2023
2 parents a47473d + 19a32e4 commit 9820bd1
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/pages/api/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ export default async function handler(req, res) {
gammaCardsAbi.abi,
provider
)
const u1Cards = (await getCardsByUser(gammaCardsContractInstance, w1)).user
const u2Cards = (await getCardsByUser(gammaCardsContractInstance, w2)).user

// TODO: obtener repetidas
// TODO: ver las que ambos no tienen
// TODO: obtener match
// stamped = true && quantity > 1
const u1Cards = await getFilteredCards(gammaCardsContractInstance, w1)
const u2Cards = await getFilteredCards(gammaCardsContractInstance, w2)

// tiene user1 y no el user2 y viceversa
const u1NotInU2 = getNotPresentCards(u1Cards, u2Cards)
const u2NotInU1 = getNotPresentCards(u2Cards, u1Cards)

console.log(u1Cards, u2Cards, u1NotInU2, u2NotInU1)

res.setHeader('Content-Type', 'application/json')
res.status(200).json({
user1: u1Cards,
user2: u2Cards,
match: false
match: u1Cards.length > 0 && u2Cards.length > 0
})
} catch (error) {
console.error(error)
Expand All @@ -41,3 +45,27 @@ export default async function handler(req, res) {
})
}
}

async function getFilteredCards(contractInstance, wallet) {
const userCards = (await getCardsByUser(contractInstance, wallet)).user

const filteredCards = Object.keys(userCards).reduce((filtered, key) => {
const card = userCards[key]
if (card.quantity > 1 && card.stamped === true) {
filtered[key] = card
}
return filtered
}, {})

return filteredCards
}

function getNotPresentCards(userCards1, userCards2) {
const notPresentCards = {}
Object.keys(userCards1).forEach((key) => {
if (!userCards2[key]) {
notPresentCards[key] = userCards1[key]
}
})
return notPresentCards
}

0 comments on commit 9820bd1

Please sign in to comment.