-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.mjs
147 lines (122 loc) · 4.52 KB
/
server.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Cheese Finder server by Rob Miles October 2022
// Version 1.1
// If you move the server to a different location you will
// have to update the base path string to reflect the new location
// Rob Miles www.robmiles.com
import http from 'http';
import fs from 'fs';
import path from 'path';
import url from 'url';
import { setupGame, grid, noOfCheeses } from './game.mjs';
const basePath = "./";
var gridWidth = 10;
var gridHeight = 10;
let absoluteHour = 0;
function getAbsoluteHour(date) {
let result = (date.getUTCFullYear() * 372 * 24) +
(date.getUTCMonth() * 31 * 24) +
(date.getUTCDate() * 24) +
date.getUTCHours();
return result;
}
function handlePageRequest(request, response) {
if(request.url == '/'){
request.url = '/index.html';
}
console.log("Page request for:" + request.url);
let filePath = basePath + request.url;
let fileTypeDecode = {
html: "text/HTML",
css: "text/css",
ico: "image/x-icon",
mjs: "text/javascript",
js: "text/javascript",
jpg: "image/jpeg",
jpeg: "image/jpeg",
png: "image/png",
tiff: "image/tiff"
}
if (fs.existsSync(filePath)) {
// If it is a file - return it
console.log(" found file OK");
response.statusCode = 200;
let extension = path.extname(filePath);
extension = extension.slice(1);
extension = extension.toLowerCase();
let contentType = fileTypeDecode[extension];
if (contentType == undefined) {
console.log(" invalid content type")
response.statusCode = 415;
response.setHeader('Content-Type', 'text/plain');
response.write("Unspported media type: " + extension);
response.end();
}
else {
response.setHeader('Content-Type', contentType);
let readStream = fs.createReadStream(filePath);
readStream.pipe(response);
}
}
else {
// If it is not a file it might be a command
// get the date
let date = new Date();
// get the absolute hour for this date
let newAbsoluteHour = getAbsoluteHour(date);
if (newAbsoluteHour != absoluteHour) {
// Set up the game grid
// this is what we want
let gameRequest = {
width: gridWidth,
height: gridHeight,
colorStyles: ["white", "red", "orange", "yellow", "yellowGreen", "lightGreen", "cyan",
"lightBlue", "blue", "purple", "magenta", "darkGray"],
minCheeses: 1,
maxCheeses: 6,
startValue: newAbsoluteHour,
randMult: 8121,
randAdd: 28413,
randModulus: 134456789
}
// set up the game
setupGame(gameRequest);
// update the absoluteHour value
absoluteHour = newAbsoluteHour;
}
var parsedUrl = url.parse(request.url, true);
let json;
console.log(" local path:" + parsedUrl.pathname);
switch (parsedUrl.pathname) {
case '/getstart.json':
response.statusCode = 200;
response.setHeader('Content-Type', 'text/json');
let answer = { width: gridWidth, height: gridHeight, noOfCheeses: noOfCheeses, hour: absoluteHour };
json = JSON.stringify(answer);
response.write(json);
response.end();
break;
case '/getstyle.json':
let x = Number(parsedUrl.query.x);
let y = Number(parsedUrl.query.y);
response.statusCode = 200;
response.setHeader('Content-Type', 'text/json');
console.log("Got: (" + x + "," + y + ")");
let styleText = grid[x][y].style;
let styleObject = { style: styleText, hour: absoluteHour };
let styleJSON = JSON.stringify(styleObject);
response.write(styleJSON);
response.end();
break;
default:
console.log(" file not found")
response.statusCode = 404;
response.setHeader('Content-Type', 'text/plain');
response.write("Cant find file at: " + filePath);
response.end();
}
}
}
let server = http.createServer(handlePageRequest);
console.log("Server running");
const port = process.env.PORT || 8080
server.listen(port);