-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
242 lines (241 loc) · 8.03 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Dependencies
var express = require('express');
const cors = require('cors');
var http = require('http');
var https = require('https');
var path = require('path');
var fs = require('fs');
var app = express();
const httpApp = express();
const { EventEmitter } = require('events');
const { config } = require('dotenv');
config();
var key = fs.readFileSync('encryption/server.key') + '';
var cert = fs.readFileSync('encryption/www_collapsa_io.crt') + '';
var ca = fs.readFileSync('encryption/www_collapsa_io.ca-bundle') + '';
let httpsOptions = {
key: key,
cert: cert,
ca: ca,
};
var port = process.env.PORT || 3000; // Used by Heroku and http on localhost
process.env['PORT'] = process.env.PORT || 4000; // Used by https on localhost
let httpsServer;
let httpServer = http.createServer(app);
const { Collection } = require('discord.js');
const { MongoClient } = require('mongodb');
const { discorduserbaseUser, discordguildbaseGuild, collapsauserbaseUser } = require('./api/models/index.js');
let toLiteral = (obj) => JSON.parse(JSON.stringify(obj));
const { mongodbInteractor, collection } = require('./mongoDB');
const mongoDB = new mongodbInteractor('LogosKing', 'TBKCKD6B');
let usersRouter = require('./api/routes/users')(mongoDB);
// Run separate https server if on localhost
if (process.env.NODE_ENV == 'development') {
httpsServer = https.createServer(httpsOptions, app).listen(process.env.PORT, function () {
console.log('Express server listening with https on port %d in %s mode', this.address().port, app.settings.env);
});
}
if (process.env.NODE_ENV == 'development') {
app.use(function (req, res, next) {
res.setHeader('Strict-Transport-Security', 'max-age=8640000; includeSubDomains');
if (!req.secure) {
return res.redirect(301, 'https://' + req.hostname + ':' + process.env.PORT + req.url);
} else {
return next();
}
});
} else {
app.use(function (req, res, next) {
res.setHeader('Strict-Transport-Security', 'max-age=8640000; includeSubDomains');
if (req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'] === 'http') {
return res.redirect(301, 'https://' + req.hostname + req.url);
} else {
return next();
}
});
}
function dhm(t) {
var cd = 24 * 60 * 60 * 1000,
ch = 60 * 60 * 1000,
d = Math.floor(t / cd),
h = Math.floor((t - d * cd) / ch),
m = Math.floor((t - d * cd - h * ch) / 60000),
s = Math.round((t - d * cd - h * ch - m * 60000) / 1000),
pad = function (n) {
return n < 10 ? '0' + n : n;
};
console.log(cd, ch, d, h, m, pad);
if (s === 60) {
m++;
s = 0;
}
if (m === 60) {
h++;
m = 0;
}
if (h === 24) {
d++;
h = 0;
}
return (
(d ? d + ` Day${d > 1 ? 's' : ''} ` : '') +
(h ? h + ` Hour${h > 1 ? 's' : ''} ` : '') +
(m ? m + ` Minute${m > 1 ? 's' : ''} ` : '') +
s +
` Second${s != 1 ? 's' : ''}`
);
}
//var httpsServer = http.Server(app);
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt');
let dotenv = require('dotenv');
dotenv.config();
var socketIO = require('socket.io');
httpServer.listen(port, () => {
console.log('Your http server is listening on port ' + httpServer.address().port);
});
let io = httpsServer ? socketIO(httpsServer) : socketIO(httpServer);
io.on('connection', (socket) => {
console.log('New connection');
});
//var game = require('./Entity.js');
const Game = require('./Entity.js');
new Game(io.of('/usaeast1'), '/usaeast1', mongoDB);
//new game(io.of('/usaeast1'), '/usaeast1', mongoDB);
var favicon = require('serve-favicon');
let client = require('./collapsabot')(mongoDB);
/*
let webhookreq = https.request({
host:'discordapp.com',
path:'/api/webhooks/720406265997819994/ulw78QPg8HKXyr5nUHJOu8eLfGEuCjbXbId1TzhwPUg5KBCIngEdigUaQ0N6yTDeuYKs',
method:"POST",
headers: {
'content-type':'application/json'
}
})
.on('response', res => {
let buffer = []
res.on('data', d => buffer.push(d))
res.on('end', () => console.log(buffer.join('')))
})
webhookreq.write(JSON.stringify({
content:'@everyone',
embeds:[
{
title: "Fully functioning building system",
type: "rich",
description: "Fun fact! The building system is now completely up to date. Only thing left to add is a red part for unplaceable campfires, which can wait until another day",
timestamp: new Date().toISOString(),
color:parseInt('ff7d36', 16),
thumbnail:{
url:'http://www.collapsa.io/img/woodwall.png',
},
author:{
url:'http://www.collapsa.io/img/favicon.png',
name:'Logos King'
}
}
]
}))
webhookreq.end()*/
let collapsa;
/**
* @type {collection}
*/
let collapsauserbase;
/**
* @type {collection}
*/
let leaderboard;
process.reqCount = 0;
let formFormat = (data) =>
Object.keys(data)
.map((k) => `${k}=${encodeURIComponent(data[k])}`)
.join('&');
mongoDB.on('ready', () => {
console.log('Mlab interface loaded');
collapsa = mongoDB.databases.get('collapsa');
collapsauserbase = collapsa.collections.get('collapsauserbase');
});
const genSnowflake = require('./util/genSnowflake.js');
Math = require('./math.js');
let JSONtoString = (obj, currIndent = 1) => {
let ret = '{\r\n';
let makeLine = (obj, prop, indentTimes) => {
let str =
new Array(indentTimes).fill(' ').join('') +
`${prop}: ${typeof obj[prop] == 'object' ? JSONtoString(obj[prop], indentTimes + 1) : obj[prop]}\r\n`;
return str;
};
for (const prop in obj) {
ret += makeLine(obj, prop, currIndent);
}
return ret + new Array(currIndent - 1).fill(' ').join('') + '}';
};
app.use(bodyParser.json());
app.use(cors());
let apiRouter = express.Router();
let v1Router = express.Router();
apiRouter.use(function timeLog(req, res, next) {
process.reqCount++;
next();
});
v1Router.get('/', async (req, res) => {
res.send(
JSON.stringify({
availableEndpoints: 'users',
}),
);
});
v1Router.use('/users', usersRouter);
apiRouter.use('/v1', v1Router);
app.use('/api', apiRouter);
var Vector = require('./Vector.js');
const { strict } = require('assert');
const { stringify } = require('querystring');
const { nextTick } = require('process');
const { time } = require('console');
// Aliases
app.get('/.well-known/pki-validation', (req, res) => {
res.sendFile(path.join(__dirname, '/client/index.html'));
});
app.get('/', function (request, response) {
response.sendFile(path.join(__dirname, '/client/index.html'));
});
app.get('/404.js', function (request, response) {
response.sendFile(path.join(__dirname, '/404.js'));
});
app.get('/404.css', function (request, response) {
response.sendFile(path.join(__dirname, '/404.css'));
});
app.set('port', port);
app.use('/client', express.static(__dirname + '/client'));
app.use('/.well-known/pki-validation', express.static(__dirname + '/'));
app.use('/', express.static(__dirname + '/client'));
app.use(favicon(path.join(__dirname, '/client/favicon.ico')));
app.use(function (req, res, next) {
res.status(404).sendFile(__dirname + '/404.html');
});
var adminList = [];
if (process.env.NODE_ENV == 'production') {
let errorHandler = (e) => {
let errorList = mongoDB.databases.get('collapsa').collections.get('errorList');
errorList.addDocument(e);
};
process.on('uncaughtException', errorHandler);
process.on('unhandledRejection', errorHandler);
}
/*
let main = [[114, 137, 218], [59, 87, 157], [100, 65, 164]]
let dark = [[91, 110, 174], [47, 70, 126], [80, 52, 131]]
let light = [[142, 161, 225], [98, 121, 177], [131, 103, 182]]
let output = []
main.forEach((e, i) => {
let innerArray = []
e.forEach((ee, ie) => {
innerArray.push(dark[i][ie]/light[i][ie])
})
output.push(innerArray)
})
console.log(output)
console.log([176, 104, 49].map(e => e * 1.25 * 0.6))*/