-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
185 lines (163 loc) · 5.96 KB
/
server.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// The Entry Point for our game from the Package.json file will be the server:
'use strict';
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const _ = require('lodash');
const fs = require('fs');
const PORT = process.env.PORT || 8080;
// Experimental Micro-Database (for storing user submitted info from the game):
// We'll store semi-imaginary game data here (like save files and progress):
let dataBase = [];
// We'll store all the username/password combos here so people can log in:
let users = {
username: 'password',
Dan: '',
};
// The user will need to log in to play, and this field will keep track of their name:
let currentUser = '';
// End of database area :)
const handleTest1 = (req, res) => {
let reply = 'Fellow';
res.send(JSON.stringify(reply));
};
const handleTest2 = (req, res) => {
let theTime = new Date();
let timeString = `${theTime.getHours()}:${theTime.getMinutes()}:${theTime.getSeconds()}`;
res.send(JSON.stringify(timeString));
};
const handleWorldChange = (req, res) => {
let data = req.body;
const { time, progressReport } = data;
const dataTuple = [progressReport, time];
dataBase.push(dataTuple);
res.send(JSON.stringify(`${dataTuple[0]} reached at ${dataTuple[1]}.`));
};
const handleUserId = (req, res) => {
// if the user is signed in, return their name when the game page asks for it; else return null to redirect them to the signin page:
let reply = '';
if (currentUser.length != 0) {
reply = JSON.stringify(currentUser);
} else {
reply = JSON.stringify(null);
}
res.send(reply);
};
// This function verifies if you are an existing user, and if so, whether you've entered the correct password...
// If these criteria are both true, then you can go ahead and play the game:
const handleLogin = (req, res) => {
const { username, password } = req.body;
let responseBody = {
status: 'success',
error: null,
username: username,
info: '',
};
// if name is on the list and the pw is good:
if (Object.keys(users).includes(username) && users[username] == password) {
currentUser = username;
res.status(200);
res.send(JSON.stringify(responseBody));
// if the name is good but the pw isn't:
} else if (Object.keys(users).includes(username)) {
responseBody.status = 'error';
responseBody.error = 403;
responseBody.info = 'Password incorrect.';
res.send(JSON.stringify(responseBody));
// if nothing matches, ask the user to sign up a new account:
} else {
responseBody.status = 'error';
responseBody.error = 402;
responseBody.info = 'Username not found in server database.';
res.send(JSON.stringify(responseBody));
}
};
// If someone is logging in for the first time, they will be asked to send a name/password to the server;
// The server will check if the name is available, and if so,
// it will add this name/pw combo to the users list, and make the given username the current User...
// if the name is already taken it will send a message to that effect:
const handleNewUser = (req, res) => {
const { username, password } = req.body;
let responseBody = {
status: 'success',
error: null,
username: username,
info: '',
};
// Check if name exists in users database:
if (Object.keys(users).includes(username)) {
console.log('name taken');
(responseBody.status = 'error'),
(responseBody.error = 406),
(responseBody.info =
'Sorry, that name is already taken. If you are a returning user, please sign in with your password, otherwise please choose a different username and try again.');
// if the name is original then add it to the users database and set it as the current user:
} else {
res.status(200);
users[username] = password;
currentUser = username;
console.log(users);
}
// Then send back the response body:
res.send(JSON.stringify(responseBody));
};
// Logging Out:
const handleLogout = (req, res) => {
currentUser = '';
res.send(JSON.stringify('logout successful.'));
};
// Launching the Map Editor:
const handleMapEditor = (req, res) => {
res.status(200);
res.send(JSON.stringify('Map Editor Launched.'));
}
const handleReadFile = (req, res) => {
const { filename } = req.params;
fs.readFile(`public/Map-editor/input-samples/${filename}`, 'utf-8', (err, data) => {
if (err) throw err;
res.send(JSON.stringify(data));
})
};
const handleWriteFile = (req, res) => {
const { mapData } = req.body;
// const filePath = `public/Map-editor/outputs/${filename}.js`;
// Rather than creating an entirely new file, append new biomes to an existing file for easier importing/testing:
fs.appendFile('public/assets/libraries/basic_biomes.js', mapData, (err) => {
if (err) {
console.log(err);
} else {
res.send(JSON.stringify('File creation complete.'));
}
})
};
express()
.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
})
.use(morgan('dev'))
.use(express.static('public'))
.use(express.urlencoded({ extended: false }))
.use(bodyParser.json())
// Endpoints:
.get('/game', handleTest1) // Game is on a fixed page, which will request some data from the server as a test.
.get('/time', handleTest2)
.get('/userid', handleUserId) // First real server function here: Check if the player is signed in and send their username if so.
.post('/post-world', handleWorldChange)
.post('/user-login', handleLogin)
.post('/create-user', handleNewUser)
.get('/logout', handleLogout)
.get('/map-editor', handleMapEditor)
.get('/readfile/:filename', handleReadFile)
.post('/writefile', handleWriteFile)
// Four-oh-Four page:
.get('*', (req, res) => {
res.status(404);
res.send('Oh mah Christ, how did we get here?!');
})
// listen on the port, and report this fact:
.listen(PORT, () => console.log(`listening on port ${PORT}`));