import express from 'express';
import { HandCashMinter } from '@handcash/handcash-connect';
const app = express();
app.get('/api/open-pack', async (req, res) => {
const { authToken, itemOrigin } = req.query;
// Create a new HandCashMinter instance using the auth token from the query
const handCashMinter = HandCashMinter.fromAppCredentials({
appId: process.env.HANDCASH_APP_ID,
authToken: authToken as string, // Use the auth token from the query
appSecret: process.env.HANDCASH_APP_SECRET
});
try {
// Generate 5 random cards (this is where your game logic would go)
const newCards = generateRandomCards(5);
// Burn the pack and create new cards
const burnAndCreateResult = await handCashMinter.burnAndCreateItemsOrder({
burn: {
origins: [itemOrigin as string],
},
issue: {
items: newCards,
collectionId: "your-collection-id",
}
});
console.log("Pack opened, new cards created:", burnAndCreateResult);
// Redirect the user to their inventory or a "pack opened" page
res.redirect('/pack-opened?result=success');
} catch (error) {
console.error("Error opening pack:", error);
res.status(500).send("Error opening pack");
}
});
function generateRandomCards(count: number) {
// This is a placeholder for your game's logic to generate random cards
return Array(count).fill(null).map((_, index) => ({
name: `Random Card ${index + 1}`,
description: "A card from the Mystery Pack",
rarity: ["Common", "Uncommon", "Rare", "Epic", "Legendary"][Math.floor(Math.random() * 5)],
mediaDetails: {
image: {
url: `https://yourgame.com/images/card-${index + 1}.webp`,
contentType: "image/webp"
}
},
quantity: 1,
}));
}
app.listen(3000, () => console.log('Server running on port 3000'));