aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaran Jayachandra <karan.jayachandra@nxp.com>2025-11-11 16:23:12 +0100
committerKaran Jayachandra <karan.jayachandra@nxp.com>2025-11-11 16:23:12 +0100
commit5dcf65616f82cb9b652f6913c93e26ff50983fdf (patch)
treed5e04b77772b5845d4bd3106ad968322d41739a9
parentdd0c6a458ef5229a5fddf0e7cae25fb3a5d495f7 (diff)
Data load added
-rw-r--r--index.html4
-rw-r--r--index.js18
-rw-r--r--player.mjs20
-rw-r--r--utils.mjs19
4 files changed, 52 insertions, 9 deletions
diff --git a/index.html b/index.html
index e58a3da..9cefa80 100644
--- a/index.html
+++ b/index.html
@@ -49,9 +49,9 @@
</section>
<section class="section">
<h2 class="title">Player Update</h3>
- <div id="file-js" class="file is-danger has-name is-boxed">
+ <div id="player-load" class="file is-danger has-name is-boxed">
<label class="file-label">
- <input class="file-input" type="file" name="resume" />
+ <input class="file-input" type="file" name="players" accept=".csv"/>
<span class="file-cta">
<span class="file-icon">
<i class="fas fa-cloud-upload-alt"></i>
diff --git a/index.js b/index.js
index 98236a4..8b146a8 100644
--- a/index.js
+++ b/index.js
@@ -1,22 +1,30 @@
import { Round } from "./game.mjs";
import { generateCourts } from "./court.mjs";
-import { generatePlayers } from "./player.mjs";
+import { csvToPlayers, generatePlayers } from "./player.mjs";
import { addToggleBehaviour } from "./utils.mjs";
// Court handling
let courts = generateCourts();
addToggleBehaviour(courts, "court_");
+
// Regulars handling
let regulars = generatePlayers();
addToggleBehaviour(regulars, "regular_");
-const fileInput = document.querySelector("#file-js input[type=file]");
+const fileInput = document.querySelector("#player-load input[type=file]");
if (fileInput !== null) {
fileInput.onchange = () => {
if (fileInput.files.length > 0) {
- const fileName = document.querySelector("#file-js .file-name");
- fileName.textContent = fileInput.files[0].name;
- console.log(fileInput.files[0]);
+ const file = fileInput.files[0];
+ if (file) {
+ const reader = new FileReader();
+ reader.onload = function(e) {
+ const content = e.target.result;
+ regulars = csvToPlayers(content);
+ addToggleBehaviour(regulars, "regular_");
+ };
+ reader.readAsText(file);
+ }
}
}
};
diff --git a/player.mjs b/player.mjs
index 2982df0..75d31d8 100644
--- a/player.mjs
+++ b/player.mjs
@@ -34,4 +34,24 @@ export function generatePlayers() {
players.push(new Player(i, "Player " + i, randomLevel(), randomStatus()));
}
return players;
+}
+
+function parseLineToPlayer(data) {
+ var info = data.split(",");
+ var id = Number(info[0].trim());
+ var name = info[1].trim() + " " + info[2].trim();
+ var level = Number(info[3].trim());
+ return new Player(id, name, level, false);
+}
+
+export function csvToPlayers(csv) {
+ var lines = csv.split("\n");
+ var players = [];
+ for (var i = 1; i < lines.length; i++) {
+ if(lines[i] == undefined || lines[i].trim() == "") {continue;}
+ players.push(parseLineToPlayer(lines[i]));
+ }
+ console.log("Loaded " + players.length + " Players");
+ for (var i=0; i<players.length; i++) {players[i].print();}
+ return players;
} \ No newline at end of file
diff --git a/utils.mjs b/utils.mjs
index e639916..968731c 100644
--- a/utils.mjs
+++ b/utils.mjs
@@ -3,11 +3,26 @@ export function addToggleBehaviour(list, description) {
let listId = description + 'list';
document.getElementById(listId).innerHTML = renderedHTML;
for (let index = 0; index < list.length; ++index) {
- const p = document.getElementById(description + index);
+ const p = document.getElementById(description + list[index].id);
p.addEventListener("click", () => {
list[index].toggle();
p.classList.toggle('is-success');
p.classList.toggle('is-danger');
});
}
-} \ No newline at end of file
+}
+
+// document.querySelector("#player-load input[type=file]").onchange = () => {
+// if (fileInput.files.length > 0) {
+// const file = fileInput.files[0];
+// if (file) {
+// const reader = new FileReader();
+// reader.onload = function(e) {
+// const content = e.target.result;
+// regulars = csvToPlayers(content);
+// addToggleBehaviour(regulars, "regular_");
+// };
+// reader.readAsText(file);
+// }
+// }
+// } \ No newline at end of file