aad-assignment-1/index.html

138 lines
4.6 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>DETI Coin Miner - WebAssembly</title>
<style>
body {
font-family: monospace;
padding: 20px;
}
button {
padding: 10px 20px;
margin: 5px;
font-size: 16px;
}
#stats {
margin-top: 20px;
padding: 10px;
background: #f0f0f0;
border-radius: 5px;
}
.control-group {
margin: 10px 0;
}
label {
display: inline-block;
width: 200px;
}
</style>
</head>
<body>
<h1>DETI Coin Miner (WebAssembly)</h1>
<div class="control-group">
<label>Iterations per batch:</label>
<input type="number" id="batchSize" value="1000000" step="100000">
</div>
<div class="control-group">
<label>Update interval (ms):</label>
<input type="number" id="updateInterval" value="100" step="50">
</div>
<button id="start">Start Mining</button>
<button id="stop">Stop Mining</button>
<button id="reset">Reset</button>
<div id="stats">
Waiting to start...
</div>
<script src="aad_coin_miner_wasm.js"></script>
<script>
let mining = false;
let Module;
let miningInterval;
let updateInterval;
CoinMinerModule().then(mod => {
Module = mod;
console.log('WebAssembly module loaded');
document.getElementById('start').onclick = () => {
if (!mining) {
mining = true;
console.log('Starting mining...');
startMining();
}
};
document.getElementById('stop').onclick = () => {
mining = false;
Module._stop_mining();
clearInterval(miningInterval);
clearInterval(updateInterval);
console.log('Mining stopped');
};
document.getElementById('reset').onclick = () => {
Module._reset_mining();
mining = false;
clearInterval(miningInterval);
clearInterval(updateInterval);
document.getElementById('stats').innerHTML = 'Reset complete. Click Start to begin.';
console.log('Mining reset');
};
function updateStats() {
const attemptsPtr = Module._malloc(8);
const coinsPtr = Module._malloc(4);
const hashRatePtr = Module._malloc(8);
const elapsedPtr = Module._malloc(8);
Module._get_statistics(attemptsPtr, coinsPtr, hashRatePtr, elapsedPtr);
const attempts = Module.getValue(attemptsPtr, 'i64');
const coins = Module.getValue(coinsPtr, 'i32');
const hashRate = Module.getValue(hashRatePtr, 'double');
const elapsed = Module.getValue(elapsedPtr, 'double');
Module._free(attemptsPtr);
Module._free(coinsPtr);
Module._free(hashRatePtr);
Module._free(elapsedPtr);
document.getElementById('stats').innerHTML = `
<strong>Mining Statistics:</strong><br>
Attempts: ${attempts.toString()}<br>
Coins Found: ${coins}<br>
Hash Rate: ${(hashRate / 1e6).toFixed(2)} MH/s<br>
Elapsed Time: ${elapsed.toFixed(2)} seconds<br>
`;
const coinsFound = Module._get_found_coins_count();
if (coinsFound > 0) {
console.log(`Total coins found: ${coinsFound}`);
}
}
function startMining() {
const batchSize = parseInt(document.getElementById('batchSize').value);
const updateMs = parseInt(document.getElementById('updateInterval').value);
// Mine continuously using setInterval for larger batches
miningInterval = setInterval(() => {
if (!mining) {
clearInterval(miningInterval);
return;
}
Module._mine_coins_wasm(batchSize);
}, 0);
// Update stats periodically
updateInterval = setInterval(updateStats, updateMs);
}
}).catch(err => {
console.error('Failed to load WebAssembly module:', err);
document.getElementById('stats').innerHTML = 'Error loading module: ' + err.message;
});
</script>
</body>
</html>