Update scoreboard module to have multiple players

This commit is contained in:
Ronan 2023-01-06 14:33:32 +01:00
parent 171fc24610
commit e2f47642de
4 changed files with 96 additions and 69 deletions

View file

@ -27,6 +27,12 @@
</nav> </nav>
<div class="container"> <div class="container">
<aside class="left-sidebar"> <aside class="left-sidebar">
<h2>Score</h2>
<div class="input-field">
<label for="name">Name</label>
<input type="text" id="name" name="name">
<button id="submit-name">Submit</button>
</div>
<table border="1"> <table border="1">
<thead> <thead>
<th>Name</th> <th>Name</th>
@ -34,18 +40,6 @@
</thead> </thead>
<tbody id="scoreTable"> <tbody id="scoreTable">
<tr>
<td>
Axel
</td>
<td>1</td>
</tr>
<tr>
<td>
Ronan
</td>
<td>2</td>
</tr>
</tbody> </tbody>
</table> </table>
</aside> </aside>

View file

@ -1,68 +1,81 @@
export class Gamer { export class Scoreboard {
constructor(Name, Score){ constructor() {
this.name = Name; this.currentGamer = new Gamer("user", 0);
this.score = Score; this.gamers = [];
this.gamers = [] this.gamers.push(this.currentGamer);
}
}
Scoreboard() updatedName(name) {
{ let previousGamer = this.currentGamer;
let alias = prompt("Entre ton nom", "User"); if(name != this.currentGamer.name) {
//let gamers = []; console.log("updated");
//self.gamers this.currentGamer = new Gamer(name, 0);
this.gamers.push( new Gamer(alias, 0)); this.addGamer(this.currentGamer);
//console.log(gamers); }
//let game = new Gamer ("test", 0);
//let table = document.getElementById("scoreTable"); if (previousGamer != this.currentGamer && previousGamer.score == 0) {
this.removeGamer(previousGamer);
console.log("removed");
console.log(this.gamers);
}
//console.log(table);
//console.log(tableau);
this.renderArray(); this.renderArray();
} }
addGamer() addGamer(gamer) {
{ this.gamers.push(gamer);
}
removeGamer(ev)
{
this.gamers.splice(ev.target.getAttribute("array-index"), 1);
renderArray();
} }
renderArray() removeGamer(gamer) {
{ this.gamers.splice(this.gamers.indexOf(gamer), 1);
let tableau= document.getElementById("scoreTable"); }
//console.log(tableau);
tableau.innerText = ""; renderArray() {
let table = document.getElementById("scoreTable");
table.innerHTML = "";
this.gamers.forEach((gamer, index) => { this.gamers.forEach((gamer, index) => {
let tableRow = document.createElement("tr"); let row = table.insertRow();
let nameTd = document.createElement("td"); let cell1 = row.insertCell();
let scoreTd = document.createElement("td"); let cell2 = row.insertCell();
let rmvTd = document.createElement("td"); cell1.innerHTML = gamer.name;
let rmvButton = document.createElement("button"); cell2.innerHTML = gamer.score;
rmvButton.innerText = "delete";
rmvButton.setAttribute("array-index", index);
rmvButton.addEventListener("click", gamer.removeGamer);
rmvTd.appendChild(rmvButton);
nameTd.innerText = gamer.name;
scoreTd.innerText = gamer.score;
tableRow.appendChild(nameTd);
tableRow.appendChild(scoreTd);
tableRow.appendChild(rmvTd);
tableau.appendChild(tableRow);
}); });
//console.log(tableau);
}
updateCurrentGamer(gamer) {
this.currentGamer = gamer;
}
getCurrentGamer() {
return this.currentGamer;
} }
UpdateScore() }
{
export class Gamer {
constructor(name, score) {
this.name = name;
this.score = score;
}
updateScore(score) {
this.score = score;
}
updateName(name) {
this.name = name;
} }
} getScore() {
return this.score;
}
getName() {
return this.name;
}
}

View file

@ -4,7 +4,7 @@ import { MoveDirection } from '/modules/enums.mjs'
import { fillLevelsSelection, selectLevel, LevelManager } from '/modules/levelSelection.mjs' import { fillLevelsSelection, selectLevel, LevelManager } from '/modules/levelSelection.mjs'
import { Timer } from '/modules/timer.mjs' import { Timer } from '/modules/timer.mjs'
import { TutorialControler } from '/modules/tutorialControler.mjs' import { TutorialControler } from '/modules/tutorialControler.mjs'
import { Gamer } from '/modules/scoreboard.mjs' import { Scoreboard, Gamer } from '/modules/scoreboard.mjs'
let canvas = document.getElementById('canvas'); let canvas = document.getElementById('canvas');
let difficultySlider = document.getElementById('difficulty-slider'); let difficultySlider = document.getElementById('difficulty-slider');
@ -79,8 +79,22 @@ window.addEventListener("keydown", (event) => {
}); });
//let table = document.getElementById("scoreTable"); //let table = document.getElementById("scoreTable");
let joueur = new Gamer("user", 0); // let joueur = new Gamer("user", 0);
joueur.Scoreboard(); // joueur.Scoreboard();
let scoreboard = new Scoreboard();
let inputName = document.getElementById("name");
inputName.addEventListener("input", (event) => {
console.log(event.target.value);
inputName.setAttribute('value', event.target.value);
});
let submitButton = document.getElementById("submit-name");
submitButton.addEventListener("click", () => {
let value = inputName.value;
scoreboard.updatedName(value);
});
window.gamestate = gameState; window.gamestate = gameState;
gameState.playground.draw(ctx); gameState.playground.draw(ctx);

View file

@ -26,6 +26,7 @@ main {
min-height: 40vh; min-height: 40vh;
} }
aside { aside {
margin: 0 auto; margin: 0 auto;
float: left; float: left;
@ -142,3 +143,8 @@ tr:nth-of-type(even) {
position: relative; position: relative;
text-align: justify; text-align: justify;
} }
.input-field {
display: flex;
flex-direction: row;
}