var balance;
var ids;
var onSale;
var toDisplay;
$(document).ready( async () => {//when page is loaded, get latest instance of blockchain
await connectWallet();
await onlyOwnerAccess();
balance = await returnBalance();
displayBalance(balance);
ids = await getBirdsOfOwner();
onSale = await getBirdsOnSale();
if (onSale == "") {
toDisplay = ids
;
} else {
toDisplay = ids
.filter(x => !onSale.includes(x));//all birds of this user not on sale
}
await buildCatalog(toDisplay);
activateMouseListeners();
});
function displayBalance(balance) {
$('#fundsAvailable').html("You have " + balance + " ETH available from bird sales. ");
};
function appendBirdToCatalog(dna, id) {
catalogBox(id); //class is defined and set in rendering.js
renderBird(`#BirdBox${id}`, birdDna(dna), id);
};
function catalogBox(id) {
var boxDiv = `
ID:
` + id + `
GEN:
MUM:
DAD:
DNA:
`
$('.row').append(boxDiv);
};
//Listener for withdraw button
$('#withdrawButton').click(async ()=>{
if(balance > 0) {
$('#withdrawButton').hide();
await withdraw();
location.reload();
};
});
//Listeners for offer buttons
function activateCatalogEventListeners() {
$(`[id^='birdPrice']`).keypress(async function(e) {
if ( e.which == 13 ) {//both enter buttons have '13'.
await initializeMarketplace();//allow Marketplace contract to handle offers.
var id = $(this).attr("id").substring(9);//extract id from HTML.
var price = $(this).val();//get price of the bird with the same id as the button
if (isNaN(price)) {
alert("Please enter a number!")
} else if (price <= 0) {
alert("Please enter a positive number!")
} else{
$(`#birdPrice${id}`).hide();
$(`#offerButton${id}`).hide();
await sellBird(price, id);
$(`#BirdBox${id}`).remove();//remove bird from DOM
};
};
});
$(`[id^='offerButton']`).on("click", async function() {
await initializeMarketplace();//allow Marketplace contract to handle offers.
var id = $(this).attr("id").substring(11);//extract id from HTML.
var price = $(`#birdPrice${id}`).val();//get price of the bird with the same id as the button
if (isNaN(price)) {
alert("Please enter a number!")
} else if (price <= 0) {
alert("Please enter a positive number!")
} else{
$(`#birdPrice${id}`).hide();
$(`#offerButton${id}`).hide();
await sellBird(price, id);
$(`#BirdBox${id}`).remove();//remove bird from DOM
};
});
};
//Listener for eye animation
function activateMouseListeners() {
$('.CatalogBox').on("mousemove", () => {
var eyeballs = $('.eyesFollow');
document.onmousemove = function(event) {
var x = event.clientX * 65 / window.innerWidth + "%";
var y = event.clientY * 65 / window.innerHeight + "%";
for (let i = 0; i < eyeballs.length; i++) {
eyeballs[i].style.left = x;
eyeballs[i].style.top = y;
};
};
});
};