From c2975bedc9be6d535d8dfcd8e176e1ad2c1e200a Mon Sep 17 00:00:00 2001 From: MaciejkaG Date: Sat, 6 Jan 2024 21:09:41 +0100 Subject: [PATCH] Multiple cool changes, added own SPA framework --- index.js | 15 +-- package.json | 3 +- public/assets/css/board.css | 1 + public/assets/css/main.css | 133 +++++++++++++++++++++++ public/assets/js/matchmaking.js | 0 public/assets/js/socket.js | 2 + public/assets/js/spa_lib.js | 44 ++++++++ views/game.handlebars | 47 -------- views/index-old.html | 16 --- views/index.handlebars | 183 ++++++++++++++++++++++---------- views/layouts/main.handlebars | 22 +++- views/pvp/create.handlebars | 83 --------------- views/pvp/index.handlebars | 70 ------------ views/pvp/join.handlebars | 86 --------------- 14 files changed, 332 insertions(+), 373 deletions(-) create mode 100644 public/assets/css/main.css delete mode 100644 public/assets/js/matchmaking.js create mode 100644 public/assets/js/socket.js create mode 100644 public/assets/js/spa_lib.js delete mode 100644 views/game.handlebars delete mode 100644 views/index-old.html delete mode 100644 views/pvp/create.handlebars delete mode 100644 views/pvp/index.handlebars delete mode 100644 views/pvp/join.handlebars diff --git a/index.js b/index.js index a20fe7f..58bf8e8 100644 --- a/index.js +++ b/index.js @@ -34,22 +34,15 @@ app.get("/", (req, res) => { res.render('index'); }); -app.get("/pvp", (req, res) => { - res.render('pvp/index'); +app.get("/*", (req, res) => { + res.redirect("/?path=" + req.originalUrl); }); -app.get("/pvp/join", (req, res) => { - res.render('pvp/join'); -}); - -app.get("/pvp/create", (req, res) => { - res.render('pvp/create'); -}); - - io.on('connection', (socket) => { const session = socket.request.session; + + socket.on('chat message', (msg) => { console.log('message: ' + msg); }); diff --git a/package.json b/package.json index af654e4..5fa506b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.js", "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node ." }, "repository": { "type": "git", diff --git a/public/assets/css/board.css b/public/assets/css/board.css index 551dda4..c91be99 100644 --- a/public/assets/css/board.css +++ b/public/assets/css/board.css @@ -93,6 +93,7 @@ h1,h2,h3,h4,h5,h6 { display: flex; justify-content: center; text-align: center; + transition: 0.15s opacity; } .shapes h2, .shapes h3 { diff --git a/public/assets/css/main.css b/public/assets/css/main.css new file mode 100644 index 0000000..769df43 --- /dev/null +++ b/public/assets/css/main.css @@ -0,0 +1,133 @@ +.container { + display: none; +} + +/* index */ +#mainMenuView .modes { + display: flex; + gap: 2rem; +} + +#mainMenuView .modes div { + height: 10rem; + width: 10rem; + background: black; + border: solid 1px white; + border-radius: 15px; + user-select: none; + cursor: pointer; + transition: all 0.3s; +} + +#mainMenuView .modes div:hover { + background: white; + color: black; +} + +/* PvP/index */ +#pvpMenuView .modes { + display: flex; + gap: 2rem; +} + +#pvpMenuView .modes div { + height: 10rem; + width: 10rem; + background: black; + border: solid 1px white; + border-radius: 15px; + user-select: none; + cursor: pointer; + transition: all 0.3s; +} + +#pvpMenuView .modes div:hover { + background: white; + color: black; +} + +/* PvP/create */ +#pvpCreateView .modes { + display: flex; + gap: 2rem; +} + +#pvpCreateView .modes div { + height: 17rem; + width: 15rem; + background: black; + border: solid 1px white; + border-radius: 15px; + user-select: none; + padding: 1rem 3rem; + display: flex; + flex-direction: column; + gap: 0.5rem; +} + +#pvpCreateView input, #pvpCreateView button { + background: black; + color: white; + border-radius: 15px; + border: 1px solid white; + font-size: 1.5rem; + text-align: center; + padding: 0.5rem 2rem; + outline: none; + transition: opacity 0.2s; +} + +#pvpCreateView button { + cursor: pointer; + font-size: 1rem; + transition: all 0.3s; +} + +#pvpCreateView button:hover { + background: white; + color: black; +} + +/* PvP/join */ +#pvpJoinView .modes { + display: flex; + gap: 2rem; +} + +#pvpJoinView .modes div { + height: 7rem; + width: 15rem; + background: black; + border: solid 1px white; + border-radius: 15px; + user-select: none; + padding: 2rem 3rem; +} + +#pvpJoinView form { + display: flex; + flex-direction: column; + gap: 2rem; +} + +#pvpJoinView form input { + background: black; + color: white; + border-radius: 15px; + border: 1px solid white; + font-size: 1.5rem; + text-align: center; + padding: 0.5rem 2rem; + outline: none; +} + +#pvpJoinView form input[type=submit] { + cursor: pointer; + font-size: 1rem; + transition: all 0.3s; +} + +#pvpJoinView form input[type=submit]:hover { + background: white; + color: black; +} \ No newline at end of file diff --git a/public/assets/js/matchmaking.js b/public/assets/js/matchmaking.js deleted file mode 100644 index e69de29..0000000 diff --git a/public/assets/js/socket.js b/public/assets/js/socket.js new file mode 100644 index 0000000..6442a00 --- /dev/null +++ b/public/assets/js/socket.js @@ -0,0 +1,2 @@ +const socket = io(); + diff --git a/public/assets/js/spa_lib.js b/public/assets/js/spa_lib.js new file mode 100644 index 0000000..251d1e8 --- /dev/null +++ b/public/assets/js/spa_lib.js @@ -0,0 +1,44 @@ +var activeView; + +function switchView(viewContainerId, useReplaceState=false) { + $(`.container`).css("opacity", 0); + setTimeout(() => { + $(`.container`).css("display", "none"); + $(`.container#${viewContainerId}`).css({"display": "flex", "opacity": "1"}); + let path = $(`.container#${viewContainerId}`).data("path"); + let title = $(`.container#${viewContainerId}`).data("title"); + if (useReplaceState) { + history.replaceState(null, "", path ? path : "/"); + } else { + history.pushState(null, title ? title : "Statki", path ? path : "/"); + } + + activeView = viewContainerId; + }, 150); +} + +const initialURLParams = new URLSearchParams(window.location.search); +const initialPath = initialURLParams.get('path'); + +window.addEventListener("load", () => { + if (initialPath != null) { + let elem = document.querySelector(`.container[data-path="${initialPath}"]`); + + if (elem != null) { + switchView(elem.id, true); + activeView = elem.id; + + } + } else { + switchView("mainMenuView"); + activeView = "mainMenuView"; + } +}); + +addEventListener("popstate", (event) => { + event.preventDefault(); + let elem = document.querySelector(`.container[data-path="${window.location.pathname}"]`); + if (elem != null) { + switchView(elem.id, true); + } +}); \ No newline at end of file diff --git a/views/game.handlebars b/views/game.handlebars deleted file mode 100644 index 2fcc63b..0000000 --- a/views/game.handlebars +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - Statki - - - - -
-
-

Statki

-
-
-
-

Wybrany statek

-

Jednomasztowiec

-

Dostępne: 1

-
- -

Sterowanie

-

S Zmiana statku

-

R Obrót statku

-

B Zamiana planszy

- -

Ruch: Przeciwnik

-

-
-
-
-
-
-
-
- - Designed by Maciejka -
-
- - - - - - \ No newline at end of file diff --git a/views/index-old.html b/views/index-old.html deleted file mode 100644 index 4e87a62..0000000 --- a/views/index-old.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - Document - - - -

Test

- - - \ No newline at end of file diff --git a/views/index.handlebars b/views/index.handlebars index 9d8e787..821ff59 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -1,63 +1,130 @@ - - - - - - - - Statki - - - - - - -
-
-

Statki

-

Wybierz tryb gry

-
-
-

PvP

-

Graj przeciwko innemu graczowi

-
-
-

Vs. AI

-

Graj przeciwko komputerowi

-
+ + +
+
+

Statki

+

PvP

+
+
+

Stwórz

+

Stwórz własny pokój

+
+
+

Dołącz

+

Dołącz do czyjegoś pokoju poprzez kod

+
+
+
+
+ +
+
+

Statki

+

PvP / Stwórz

+
+
+

Kod pokoju:

+ +

Oczekiwanie na gracza...

+ +
+
+
+
+ +
+
+

Statki

+

PvP / Dołącz

+
+
+
+ + +
+
+
+
+
+ +
+
+

Statki

+
+
+
+

Wybrany statek

+

Jednomasztowiec

+

Dostępne: 1

+
+ +

Sterowanie

+

S Zmiana statku

+

R Obrót statku

+

B Zamiana planszy

+ +

Ruch: Przeciwnik

+

+
+
+
+
+
+
+
+
+ + + +
+ + + + \ No newline at end of file diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars index 4839ad4..d43ce5f 100644 --- a/views/layouts/main.handlebars +++ b/views/layouts/main.handlebars @@ -1 +1,21 @@ -{{{body}}} \ No newline at end of file + + + + + + + + Statki + + + + + + + + Designed by Maciejka + + {{{body}}} + + + \ No newline at end of file diff --git a/views/pvp/create.handlebars b/views/pvp/create.handlebars deleted file mode 100644 index ca4531d..0000000 --- a/views/pvp/create.handlebars +++ /dev/null @@ -1,83 +0,0 @@ - - - - - - - - Statki - - - - - - -
-
-

Statki

-

PvP / Stwórz

-
-
-

Kod pokoju:

- -

Oczekiwanie na gracza...

- -
-
- Designed by Maciejka - -
-
- - \ No newline at end of file diff --git a/views/pvp/index.handlebars b/views/pvp/index.handlebars deleted file mode 100644 index 8ec5b02..0000000 --- a/views/pvp/index.handlebars +++ /dev/null @@ -1,70 +0,0 @@ - - - - - - - - Statki - - - - - - -
-
-

Statki

-

PvP

-
-
-

Stwórz

-

Stwórz własny pokój

-
-
-

Dołącz

-

Dołącz do czyjegoś pokoju poprzez kod

-
-
- - Designed by Maciejka - -
-
- - \ No newline at end of file diff --git a/views/pvp/join.handlebars b/views/pvp/join.handlebars deleted file mode 100644 index 019aa11..0000000 --- a/views/pvp/join.handlebars +++ /dev/null @@ -1,86 +0,0 @@ - - - - - - - - Statki - - - - - - -
-
-

Statki

-

PvP / Dołącz

-
-
-
- - -
-
-
- Designed by Maciejka - -
-
- - \ No newline at end of file