-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.js
101 lines (96 loc) · 3.96 KB
/
admin.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
const express = require('express');
const bodyparser = require('body-parser');
const mongo = require('mongoose');
const auth_db = require('./src_server/auth_db');
const game_db = require('./game_server/db');
const auth_helper = require('./src_server/auth_helper');
const dev_tools = require('./src_server/tools');
const game_config = require('./game_server/config');
let app = express();
app.listen(82);
app.get('/', landing_page);
app.use('/src_client', express.static('src_client')); /* for abstract css/js/img */
app.use('/game_client', express.static('game_client')); /* for game-specific css/js/img */
let urlencodedparser = bodyparser.urlencoded({extended:false});
mongo.connect(`mongodb://localhost/${game_config.db}`, function(){}).then(() => {})
.catch(err => {
console.error(err.stack);
process.exit(1);
});
mongo.Promise = global.Promise;
app.post('/start_game',urlencodedparser,function(request, response){
auth_helper.session_handshake(request.body.session_key,function(user_session_doc){
console.log("session_key is valid:");
console.info(`user_session_doc`,user_session_doc);
response.send(user_session_doc);
});
});
app.post('/view_users',urlencodedparser,function(request, response){
auth_db.users.find()
.then((resp) => {response.send(resp);})
.catch((resp) => {response.send(resp);});
});
app.post('/view_sessions',urlencodedparser,function(request, response){
auth_db.sessions.find()
.then((resp) => {response.send(resp);})
.catch((resp) => {response.send(resp);});
});
app.post('/create_user',urlencodedparser,function(request, response){
console.info(`request.body`,request.body);
let obj = JSON.parse(JSON.stringify(request.body));
new auth_db.users(obj).save()
.then((resp) => {response.send(resp);})
.catch((resp) => {response.send(resp);});
});
app.post('/submit_login',urlencodedparser,function(request, response){
auth_db.users.find({
username: request.body.username,
password: request.body.password
})
.then((resp) => {
if(resp.length === 1){
const session_key = auth_helper.generate_key();
const session_doc = {
username: request.body.username,
key: session_key,
json: `{}`
};
console.info(`auth_db.sessions`,auth_db.sessions);
new auth_db.sessions(session_doc).save().then(console.log).catch(e=>console.log(e))
response.send(session_doc);
} else {
response.send("bad login");
}
})
.catch((resp) => {response.send(resp);});
});
let api_test_arr = [
['/view_users', ''],
['/view_sessions', ''],
['/start_game', 'foobar=1'],
['/create_user', 'username=test123&password=test456'],
['/submit_login', 'username=test123&password=test456'],
['/submit_login', 'username=test123&password=the_wrong_password'],
['/update_user', '_id=asdf'],
];
function landing_page(request, response) {
console.info(`request`,request);
response.send(`
<html>
<head>
<title>${game_config.name}</title>
<script src='https://code.jquery.com/jquery-3.2.1.min.js'></script>
<link rel="stylesheet" href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'></link>
<link rel="stylesheet" href='https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css'></link>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js'></script>
<link rel="stylesheet" type="text/css" href="src_client/style.css"></link>
<script src="src_client/main.js"></script>
<script src="src_client/gui.js"></script>
<script src="src_client/tools.js"></script>
</head>
<body>
<div id="game">${dev_tools.api_test_form(api_test_arr)}</div>
</body>
</html>
`);
}