`;
inputsHtml += `
Earnings Projection:
Reward Token: ${inputs.rewardTokenSymbol}
Earning Rate: ${p2e_formatNumber(inputs.earningRate,2)} tokens per ${inputs.earningPeriodUnit}
Assumed Avg. Token Price: ${p2e_formatUsd(inputs.avgTokenPriceUsd)}
Other Monthly Income: ${p2e_formatUsd(inputs.otherMonthlyIncomeUsd)}
Other Asset Sales (Total): ${p2e_formatUsd(inputs.otherAssetSalesUsd)}
`;
inputsHtml += `
Costs & Fees:
Token Sale Fee: ${p2e_formatPercentage(inputs.tokenSaleFeePercent)}
NFT Sale Fee: ${p2e_formatPercentage(inputs.nftSaleFeePercent)}
Total Gas Fees (Period): ${p2e_formatUsd(inputs.totalGasFeesUsd)}
Other Monthly Costs: ${p2e_formatUsd(inputs.otherMonthlyCostsUsd)}
`;
const resultsHtml = `
Financial Summary (Over ${inputs.analysisDurationMonths} Months):
Net from Token Rewards: ${p2e_formatUsd(results.netUsdFromTokens)}
Total Other Income: ${p2e_formatUsd(results.totalOtherIncome)}
Net from Other Asset Sales: ${p2e_formatUsd(results.netOtherAssetSales)}
Total Gross Revenue: ${p2e_formatUsd(results.totalGrossRevenue)}
Total Operating Costs: (${p2e_formatUsd(results.totalOperatingCosts)})
Net Operating Profit: ${p2e_formatUsd(results.netOperatingProfit)}
Net from Resale of Initial Assets: ${p2e_formatUsd(results.netResaleOfInitialAssets)}
Overall Net Profit / Loss: ${p2e_formatUsd(results.overallNetProfitLoss)}
Simple ROI: ${p2e_formatPercentage(results.roi)}
Annualized ROI (CAGR): ${p2e_formatPercentage(results.cagr)}
`;
const interpretationNote_pdf = document.querySelector('#p2eRoiAnalyzer .p2e-interpretation-note').innerText.replace("CRITICAL P2E RISKS & CONSIDERATIONS:", "").trim();
pdfContentEl.innerHTML = `
Play-to-Earn (P2E) Gaming ROI Analysis for ${inputs.gameName}
I. Input Parameters
${inputsHtml}
II. Projected Financial Summary
${resultsHtml}
CRITICAL P2E RISKS & CONSIDERATIONS:${interpretationNote_pdf.replace(/\n/g, "
").replace(/
`;
document.body.appendChild(pdfContentEl);
html2canvas(pdfContentEl, { scale: 2, useCORS: true, logging:true, windowWidth: pdfContentEl.scrollWidth, windowHeight: pdfContentEl.scrollHeight }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
let numPages = Math.ceil(canvas.height / ( (pdfHeight - 40) * (canvas.width / (pdfWidth - 40)) ) );
let pageCanvasHeight = (pdfHeight - 40) * (canvas.width / (pdfWidth - 40));
for (let i = 0; i < numPages; i++) {
if (i > 0) pdf.addPage();
let sourceY = i * pageCanvasHeight;
let sourceHeight = Math.min(pageCanvasHeight, canvas.height - sourceY);
const tempCanvas = document.createElement('canvas');
tempCanvas.width = canvas.width;
tempCanvas.height = sourceHeight;
const ctx = tempCanvas.getContext('2d');
ctx.drawImage(canvas, 0, sourceY, canvas.width, sourceHeight, 0, 0, canvas.width, sourceHeight);
const pageImgData = tempCanvas.toDataURL('image/png');
const pageImgHeight = (sourceHeight * (pdfWidth - 40)) / canvas.width;
pdf.addImage(pageImgData, 'PNG', 20, 20, pdfWidth - 40, pageImgHeight, undefined, 'FAST');
}
pdf.save(`${(inputs.gameName || "P2E").replace(/[^a-z0-9]/gi, '_')}_ROI_Analysis.pdf`);
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("P2E PDF Error:", err); alert("Error generating P2E ROI PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
p2e_openTab({}, 'p2e_tab_setup');
// Set some default values for quicker testing
const initialInvEl = document.getElementById('p2e_initialInvestmentUsd');
if (initialInvEl && !initialInvEl.value) initialInvEl.value = '500';
const durationEl = document.getElementById('p2e_analysisDurationMonths');
if (durationEl && !durationEl.value) durationEl.value = '6';
const earningRateEl = document.getElementById('p2e_earningRate');
if(earningRateEl && !earningRateEl.value) earningRateEl.value = '100';
const tokenPriceEl = document.getElementById('p2e_avgTokenPriceUsd');
if(tokenPriceEl && !tokenPriceEl.value) tokenPriceEl.value = '0.10';
if (!document.getElementById('p2e_initialInvestmentUsd')) {
console.error("Critical input 'p2e_initialInvestmentUsd' not found.");
}
});