From 9c464327fa0c026d638efa82af03710b5b6973b1 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 22 Oct 2022 18:38:28 +0200 Subject: Began writing an express backend --- static/.eslintrc.json | 36 +++++ static/index.css | 92 +++++++++++ static/index.js | 416 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 544 insertions(+) create mode 100644 static/.eslintrc.json create mode 100644 static/index.css create mode 100644 static/index.js (limited to 'static') diff --git a/static/.eslintrc.json b/static/.eslintrc.json new file mode 100644 index 0000000..4097d70 --- /dev/null +++ b/static/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "env": { + "browser": true, + "es2021": true + }, + "extends": "eslint:recommended", + "overrides": [ + { + "files": ["**/*.js"], + "globals": { + "CWDATA": "writable" + } + } + ], + "parserOptions": { + "ecmaVersion": "latest" + }, + "rules": { + "indent": [ + "error", + "tab" + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "double" + ], + "semi": [ + "error", + "always" + ] + } +} diff --git a/static/index.css b/static/index.css new file mode 100644 index 0000000..ab00f8f --- /dev/null +++ b/static/index.css @@ -0,0 +1,92 @@ +* { + box-sizing: border-box; + scrollbar-gutter: stable both-edges; +} + +aside { + float: right; +} + +body { + background: #126623; + display: grid; + grid-template-rows: auto 1fr auto; + font-family: -apple-system, BlinkMacSystemFont, + "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", + "Fira Sans", "Droid Sans", "Helvetica Neue", + sans-serif; + font-feature-settings: "kern" 1; + font-kerning: normal; +} + +button { + box-shadow: rgb(202, 239, 171) 0px 1px 0px 0px inset; + border-radius: 3px; + border: 1px solid rgb(38, 138, 22); + cursor: pointer; + font-weight: bold; + max-height: 3em; + padding: 6px; + background: linear-gradient(rgb(92, 184, 17) 5%, rgb(119, 212, 42) 100%) rgb(92, 184, 17); +} + +button:disabled, button[disabled] { + cursor: auto; +} + +main { + margin-left: auto; + margin-right: auto; +} + +td { + background: #47b75e; + border: 1px solid #d4fcdb; + min-width: 27px; + height: 27px; + text-align: center; +} + +#board { + border-collapse: collapse; + font-weight: bold; + max-width: 405px; +} + +#rack { + display:inline-block; + font-weight: bold; +} + +#remaining_letters { + max-width: 405px; +} + +.tw { + background:#E60000; +} +.dw { + background:#F86969; +} +.tl { + background:#3675FA; +} +.dl { + background:#22ACD8; +} +.start { + background:#ff5500; +} + +.letter { + background:#f3e797; +} +.placed { + color:red; +} + +.points { + display: inline; + font-weight: normal; + font-size: 8pt; +} diff --git a/static/index.js b/static/index.js new file mode 100644 index 0000000..0d10935 --- /dev/null +++ b/static/index.js @@ -0,0 +1,416 @@ +"use strict"; + +let CW = function(){ + // 102 letter tiles + // 0 point : blanches × 2. + // 1 point : E ×15, A ×9, I ×8, N ×6, O ×6, R ×6, S ×6, T ×6, U ×6, L ×5 + // 2 points : D ×3, G ×2, M ×3 + // 3 points : B ×2, C ×2, P ×2 + // 4 points : F ×2, H ×2, V ×2 + // 8 points : J ×1, Q ×1 + // 10 points : K ×1, W ×1, X ×1, Y ×1, Z ×1 + let total_remaining_letters = 102; + let letters = { + JOKER:{count:2, points:0 }, + E:{count:15, points:1}, A:{count:9, points:1}, I:{count:9, points:1}, N:{count:6, points:1}, O:{count:6, points:1}, R:{count:6, points:1}, S:{count:6, points:1}, T:{count:6, points:1}, U:{count:6, points:1}, L:{count:5, points:1}, + D:{count:3, points:2}, G:{count:2, points:2}, M:{count:3, points:2}, + B:{count:2, points:3}, C:{count:2, points:3}, P:{count:2, points:3}, + F:{count:2, points:4}, H:{count:2, points:4}, V:{count:2, points:4}, + J:{count:1, points:8}, Q:{count:1, points:8}, + K:{count:1, points:10}, W:{count:1, points:10}, X:{count:1, points:10}, Y:{count:1, points:10}, Z:{count:1, points:10}, + }; + let cursor = undefined; + let placed = []; // a sorted array of letters to place + + function makeBoardTileOnCLick(x, y) { + return function() { + let me = document.getElementById(["s", y, "_", x].join("")); + if (me.innerHTML === "") { + moveCursor(x, y); + } else if (me.classList.contains("placed")) { + moveFromBoardToRack(x, y, me); + } else if (cursor !== undefined && cursor.x == x && cursor.y == y) { + // if we just clicked the cursor, swap its direction + if (cursor.direction === "right") { + cursor.direction = "down"; + me.innerHTML = "▼"; + } else { + cursor.direction = "right"; + me.innerHTML = "▶"; + } + } + }; + } + function makeRackTileOnClick(i) { + return function() { + if (cursor !== undefined) { + let me = document.getElementById(["l", i].join("")); + if (me.innerHTML != "") { + moveFromRackToBoard(me, cursor.x, cursor.y); + } + } + }; + } + + function moveCursor(x, y) { + if (cursor === undefined) { + // No previous cursor, just set the new position + cursor = { x: x, y: y}; + } else { + // Remove the cursor from its former position + let src = document.getElementById(["s", cursor.y, "_", cursor.x].join("")); + src.innerHTML = ""; + cursor.x = x; + cursor.y = y; + } + // try to guess word direction if several letters are already placed + let dst = document.getElementById(["s", y, "_", x].join("")); + if (placed.length >= 2 && placed[0].x === placed[1].x) { + cursor.direction = "down"; + dst.innerHTML = "▼"; + } else { + cursor.direction = "right"; + dst.innerHTML = "▶"; + } + } + function moveCursorForwardIfPossible() { + while(cursor.x < 15 && cursor.y < 15) { + if (cursor.direction === "right") { + cursor.x++; + } else { + cursor.y++; + } + if (cursor.x >= 15 || cursor.y >= 15) { + cursor = undefined; + return; + } + let dst = document.getElementById(["s", cursor.y, "_", cursor.x].join("")); + if (dst.innerHTML === "") { + if (cursor.direction === "right") { + dst.innerHTML = "▶"; + } else { + dst.innerHTML = "▼"; + } + return; + } + } + } + + function moveFromBoardToRack(x, y, src) { + CWDATA.board[y][x] = ""; + // Find the letter in the placed array + let n = undefined; + for(n=placed.length-1; n>=0; n--) { + if (placed[n].x === x && placed[n].y === y) { + break; + } + } + // Find a free spot on the rack + let i = 0; + let dst = undefined; + do { + dst = document.getElementById(["l", i].join("")); + if (dst.innerHTML == "") + break; + i++; + } while (i<7); + // Move the board letter to the rack + if (src.innerHTML.charAt(21) === "&") { + dst.innerHTML = " "; + } else { + dst.innerHTML = src.innerHTML; + } + dst.classList.add("letter"); + src.classList.remove("letter"); + src.classList.remove("placed"); + // Place the cursor in the freed spot + moveCursor(x, y); + // Remove the letter from the placed array + placed.splice(n, 1); + // update the validate button + updateUI(); + } + + function moveFromRackToBoard(src, x, y) { + let dst = document.getElementById(["s", y, "_", x].join("")); + if (src.innerHTML === " ") { + let letter = prompt("Entrez la lettre à utiliser", "A"); + if (letter === null || !/^[A-Za-z]$/.test(letter)) { + return; + } + letter = letter.toUpperCase(); + dst.innerHTML = [letter, "