Skip to content

Commit b577420

Browse files
committed
minor refactoring and added server.js (I'm learning to use nodeJS)
1 parent 8711f10 commit b577420

File tree

6 files changed

+79
-22
lines changed

6 files changed

+79
-22
lines changed

style.css renamed to assets/style.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/* GENERAL */
22
body {
3-
--pauseFilter: grayscale(100%) brightness(130%) contrast(60%);
4-
53
font: 90% Trebuchet MS;
64
text-align: center;
75
color: black;
86
transition: filter 0.5s ease-in-out;
97
user-select: none;
108
}
9+
[data-paused] {
10+
filter: grayscale(100%) brightness(130%) contrast(60%);
11+
}
1112
.background {
1213
height: 100%;
1314
width: 100%;
File renamed without changes.

index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name='description' content='An original game based on the snake game.' />
77
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
88
<meta charset='UTF-8' />
9-
<link rel='icon' href='assets/favicon.png' type='image/png'/>
9+
<link rel='icon' href='favicon.ico' type='image/png'/>
1010

1111
<script src='source/sounds.js'> </script>
1212
<script src='source/options_menu.js'> </script>
@@ -19,7 +19,7 @@
1919
<!-- Main Formatting: -->
2020
<link rel='stylesheet'
2121
type='text/css'
22-
href='style.css'>
22+
href='assets/style.css'>
2323
</style>
2424

2525
<!-- List of language filenames: -->
@@ -73,6 +73,14 @@
7373

7474
<!-- Run Game Code: -->
7575
<script>
76+
// Disable scroll-down via spacebar:
77+
window.addEventListener('keydown', (event) => {
78+
if (event.keyCode == 32 && event.target == document.body) {
79+
event.preventDefault();
80+
return false;
81+
}
82+
});
83+
7684
const GAME = new Game();
7785
</script>
7886
</body>

server.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
const PORT_NUM = 8080;
4+
5+
const HTTP = require('http');
6+
const URL = require('url');
7+
const FS = require('fs');
8+
9+
10+
11+
HTTP.createServer((req, res) => {
12+
13+
// Decode the url:
14+
const url = URL.parse(req.url, true)
15+
let path = url.pathname.slice(1);
16+
if (path.length == 0) { path = 'index.html'; }
17+
FS.access(path, (err) => {
18+
if (err || path.includes('..')) {
19+
path = 'index.html';
20+
}
21+
});
22+
const ctype = contentTypes[path.split('.').pop().toLowerCase()];
23+
const query = url.query;
24+
console.log(url, path, ctype);
25+
26+
// Respond to a request:
27+
if (req.method == 'GET') {
28+
29+
// Clean the path argument:
30+
FS.readFile(path, (err, data) => {
31+
if (err) {
32+
console.error(err);
33+
res.statusCode = 400;
34+
} else {
35+
res.writeHead(200, {'Content-Type': ctype});
36+
res.write(data);
37+
}
38+
res.end();
39+
});
40+
41+
} else {
42+
res.statusCode = 404;
43+
res.end();
44+
}
45+
46+
}).listen(PORT_NUM);
47+
48+
49+
50+
const contentTypes = {
51+
'html': 'text/html',
52+
'js': 'text/javascript',
53+
'css': 'text/css',
54+
55+
'png': 'image/png',
56+
'ico': 'image/png',
57+
'wav': 'audio/wav',
58+
'mp3': 'audio/mpeg',
59+
}

source/game.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,10 @@ function weightedChoice(weights) {
7575
*
7676
*
7777
* TODO:
78-
* change populations implementation to be separate class:
79-
* array of arrays of keys. outer array holds pool of keys
80-
* that have been spawned <outer-index> times. Initially all in
81-
* outer entry #zero, which is shuffled once on start. When spawned,
82-
* shuffled into next outer-entry at random spot. When spawning,
83-
* go through each key in order until one is found that can spawn there.
78+
* somehow move away from balancing populations on shuffle
79+
* with such space&computationally costly methods.
8480
*
8581
* fix the broken progress/difficulty bar (broken because of cs selectors)
86-
* fix bugs with the new backtracking.
8782
*
8883
* make game runner_catch and gameover sounds.
8984
*/
@@ -98,14 +93,6 @@ class Game {
9893
const dGrid = document.getElementById('grid');
9994
dGrid.style.setProperty('--width-in-tiles', width);
10095

101-
// Disable scroll-down via spacebar:
102-
window.addEventListener('keydown', (event) => {
103-
if (event.keyCode == 32 && event.target == document.body) {
104-
event.preventDefault();
105-
return false;
106-
}
107-
});
108-
10996
// Initialize Table display:
11097
for (let y = 0; y < width; y++) {
11198
const row = dGrid.insertRow();
@@ -274,14 +261,14 @@ class Game {
274261
// The player pressed the pause button:
275262
if (!this.paused) {
276263
this.backgroundMusic.pause();
277-
document.body.style.filter = 'var(--pauseFilter)';
264+
document.body.dataset.paused = 'this attribute exists.';
278265

279266
// The user pressed the un-pause button:
280267
} else {
281268
if (!this.muted) {
282269
this.backgroundMusic.play();
283270
}
284-
document.body.style.filter = '';
271+
delete document.body.dataset.paused;
285272
this.chaserCancel = setTimeout(that.moveChaser.bind(that), 1000);
286273
this.nommerCancel = setTimeout(that.moveNommer.bind(that), 1000);
287274
this.runnerCancel = setTimeout(that.moveRunner.bind(that), 1000);

source/player.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ class Player {
109109
for (let i = 0; i < game.targets.length; i++) {
110110
if (dest.equals(game.targets[i])) {
111111
this.score += 1;
112-
if (!this.game.muted) SoundEffects.playEffectFrom(Player.eatSounds);
112+
if (!this.game.muted) {
113+
SoundEffects.playEffectFrom(Player.eatSounds);
114+
}
113115
game.heat = game.numTargets * Math.sqrt(
114116
game.heat / game.numTargets + 1);
115117

0 commit comments

Comments
 (0)