From 7767c77038596c8909baaa54ccdba9bbfe4eb721 Mon Sep 17 00:00:00 2001
From: Mamadu Lamarana Bah <mamadulamarana.bah.etu@univ-lille.fr>
Date: Thu, 15 Feb 2024 18:03:29 +0100
Subject: [PATCH] =?UTF-8?q?gestion=20de=20la=20fin=20de=20l'ench=C3=A8re?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 encheres/client/src/html/bidder.html      |  2 +-
 encheres/client/src/scripts/auctioneer.js | 22 ++++++++++-----
 encheres/client/src/scripts/bidder.js     | 33 ++++++++++++++++++-----
 3 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/encheres/client/src/html/bidder.html b/encheres/client/src/html/bidder.html
index 1a8d26d..45fa212 100644
--- a/encheres/client/src/html/bidder.html
+++ b/encheres/client/src/html/bidder.html
@@ -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>
diff --git a/encheres/client/src/scripts/auctioneer.js b/encheres/client/src/scripts/auctioneer.js
index f57fcb1..0f9fb0a 100644
--- a/encheres/client/src/scripts/auctioneer.js
+++ b/encheres/client/src/scripts/auctioneer.js
@@ -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;
 }
 
 
diff --git a/encheres/client/src/scripts/bidder.js b/encheres/client/src/scripts/bidder.js
index 6a45373..b4f8bc2 100644
--- a/encheres/client/src/scripts/bidder.js
+++ b/encheres/client/src/scripts/bidder.js
@@ -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");
+}
+
 
 
-- 
GitLab