Skip to content
Snippets Groups Projects
Commit 7767c770 authored by Mamadu-lamarana Bah's avatar Mamadu-lamarana Bah :speech_balloon:
Browse files

gestion de la fin de l'enchère

parent b90269f3
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,7 @@
<div class="item">Prix actuel : <span class="prix"></span> </div>
</div>
<div class="price-options">
<div id="buttons" class="price-options">
<p>Enchères possibles :</p>
<button class="price-button">20€</button>
<button class="price-button">50€</button>
......
......@@ -5,7 +5,11 @@ const socket = io();
const inputName = document.getElementById("name");
const inputValue = document.getElementById("value");
const buttonStart = document.getElementById("start");
const buttonSale = document.getElementById("sale");
buttonSale.addEventListener("click", saleProduct);
socket.emit("auctioneer");
const prixActuel = document.getElementById("prixactuel");
prixActuel.textContent = inputValue.value+"";
......@@ -29,8 +33,14 @@ function startEnchere() {
buttonSale.disabled = false;
ident.textContent = `Debut de l'enchère pour ${inputName.value} à ${inputValue.value}€`;
socket.emit("infos", inputName.value, inputValue.value);
socket.on("prixActuel", (nouveauPrix, sock) => updatePrix(nouveauPrix, sock));
waitForNewEnchere();
socket.on("changePrix", (nouveauPrix, sock) => updatePrix(nouveauPrix, sock));
waitForNewEnchere(true);
}
function saleProduct() {
ident.textContent = `Fin de l'enchère. Un ${inputName.value} à ${prixActuel.textContent}`;
socket.emit("adjuge");
waitForNewEnchere(false);
}
function updatePrix(nouveauPrix, sock) {
......@@ -38,10 +48,10 @@ function updatePrix(nouveauPrix, sock) {
ident.textContent = `Nouvelle enchère réçu de ${sock}`;
}
function waitForNewEnchere() {
inputName.disabled = true;
inputValue.disabled = true;
buttonStart.disabled = true;
function waitForNewEnchere(disabled) {
inputName.disabled = disabled;
inputValue.disabled = disabled;
buttonStart.disabled = disabled;
}
......@@ -6,31 +6,50 @@ const ident = document.getElementById("ident-bid");
const description = document.getElementsByClassName("description");
const prix = document.getElementsByClassName("prix");
displayButton("none");
const buttons = document.querySelectorAll("button");
for(const button of buttons) {
button.addEventListener("click", selectPrice);
}
function displayButton(display) {
const divButtons = document.getElementById("buttons");
divButtons.style.display = display;
}
let prixActuel = 0;
socket.on("infos", (name, value) => {
ident.textContent = `Une nouvelle enchère commence`;
enchereBegin(name, value);
displayButton("block");
socket.on("changePrix", (nouveauPrix) => {changePrix(nouveauPrix)});
});
function changePrix(nouveauPrix) {
prix[0].textContent = nouveauPrix+"";
prixActuel = nouveauPrix;
}
function enchereBegin(name, value) {
description[0].textContent = name;
prix[0].textContent = value+"";
prixActuel += parseInt(prix[0].textContent);
prixActuel = parseInt(prix[0].textContent);
}
const buttons = document.querySelectorAll("button");
for(const button of buttons) {
button.addEventListener("click", selectPrice);
}
function selectPrice(event) {
const selectedPrix = parseInt(event.target.textContent);
prixActuel += selectedPrix;
prix[0].textContent = prixActuel;
socket.emit("prixActuel", prixActuel);
socket.on("prixActuel", (nouveauPrix) => {prix[0].textContent = nouveauPrix});
socket.on("winner", () => win());
ident.textContent = `Vous avez fait une enchère de +${selectedPrix}€`;
}
function win() {
console.log("ici win");
ident.textContent = `Enchères terminé, vous avez remporté l'enchère`;
displayButton("none");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment