const CCTF_STARTED_EVENT = 'CCTFStarted'; const PLAYER_REGISTERED_EVENT = 'PlayerRegistered'; const FLAG_ADDED_EVENT = 'FlagAdded'; const FLAG_SOLVED_EVENT = 'FlagSolved'; const FLAG_REMOVED_EVENT = 'FlagRemoved'; var FILTER_OPTS = { filter: { value: [], }, fromBlock: 0 }; var web3; var web3Listener; const contractAddress = '0xf51B62641D4c472E6EC0DB7Ea50382aE66Ff092e'; var contract; var contractAbi; var currentBlock = 0; function loadRegisteredPlayers(events){ registerContractEventListeners(); } function appendRow(table, registrationEvent){ let returnValues = registrationEvent['returnValues']; let address = returnValues['playerAddress']; let row = document.createElement("tr"); let addressCol = document.createElement("td"); let statusCol = document.createElement("td"); let pointsCol = document.createElement("td"); row.id = address; addressCol.id = `${address}.addressCol` addressCol.innerText = String(address); statusCol.id = `${address}.statusCol` getPlayerStatus(address).then(result => updatePlayerStatus(address, result)); pointsCol.id = `${address}.pointsCol` getPlayerPoints(address).then(result => updatePlayerPoints(address, result)); row.appendChild(addressCol); row.appendChild(statusCol); row.appendChild(pointsCol); table.appendChild(row); } function updatePlayerStatus(address, statusEnum){ let statusString = ""; switch(statusEnum){ case '0': statusString = "Unverified"; break; case '1': statusString = "Verified"; break; case '2': statusString = "Banned"; break; } let statusCol = document.getElementById(`${address}.statusCol`); statusCol.innerText = statusString; } function updatePlayerPoints(address, points){ let pointsCol = document.getElementById(`${address}.pointsCol`); pointsCol.innerText = points; } async function getPlayerStatus(address){ return await contract.methods.getPlayerStatus(address).call(); } async function getPlayerPoints(address){ return await contract.methods.getPlayerPoints(address).call(); } async function getPastEvents(eventName, callback) { await contract.getPastEvents(eventName, { fromBlock: 0, toBlock: 'latest', }, (err, events) => { callback(events); }); }; window.addEventListener("load", function () { if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); web3Listener = new Web3('wss://eth.toldi.eu/ws'); window.fetch('./abi.json').then((response) => response.json()).then((data) => { contractAbi = data; contract = new web3Listener.eth.Contract(contractAbi, contractAddress); getPastEvents(PLAYER_REGISTERED_EVENT, loadRegisteredPlayers); }); } else { console.log("No web3? You should consider trying MetaMask!"); } }); function registerContractEventListeners(){ contract.events.FlagSolved({ fromBlock: 0 }, function(error, event){ console.log(event); }) .on('data', function(event){ let address = event['returnValues']['solver']; getPlayerPoints(address).then(result => updatePlayerPoints(address, result)); console.log('data ' + event) }) .on('changed', function(event){ console.log('changed ' + event) }) .on('connected', function(event){ console.log('connected ' + event) }) .on('error', console.error); contract.events.PlayerRegistered({ fromBlock: 0 }, function(error, event){ console.log(event); }) .on('data', function(event){ let table = document.querySelector('tbody'); appendRow(table, event); console.log('data ' + event) }) .on('changed', function(event){ console.log('changed ' + event) }) .on('connected', function(event){ console.log('connected ' + event) }) .on('error', console.error); }