This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
app.js
198 lines (163 loc) · 6.08 KB
/
app.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
/**
* Main file
* Pokemon Showdown - http://pokemonshowdown.com/
*
* This is the main Pokemon Showdown app, and the file you should be
* running to start Pokemon Showdown if you're using it normally.
*
* This file sets up our SockJS server, which handles communication
* between users and your server, and also sets up globals. You can
* see details in their corresponding files, but here's an overview:
*
* Users - from users.js
*
* Most of the communication with users happens in users.js, we just
* forward messages between the sockets.js and users.js.
*
* Rooms - from rooms.js
*
* Every chat room and battle is a room, and what they do is done in
* rooms.js. There's also a global room which every user is in, and
* handles miscellaneous things like welcoming the user.
*
* Dex - from sim/dex.js
*
* Handles getting data about Pokemon, items, etc.
*
* Ladders - from ladders.js and ladders-remote.js
*
* Handles Elo rating tracking for players.
*
* Chat - from chat.js
*
* Handles chat and parses chat commands like /me and /ban
*
* Sockets - from sockets.js
*
* Used to abstract out network connections. sockets.js handles
* the actual server and connection set-up.
*
* @license MIT
*/
'use strict';
// NOTE: This file intentionally doesn't use too many modern JavaScript
// features, so that it doesn't crash old versions of Node.js, so we
// can successfully print the "We require Node.js 8+" message.
// Check for version and dependencies
try {
// I've gotten enough reports by people who don't use the launch
// script that this is worth repeating here
eval('{ let a = async () => {}; }');
} catch (e) {
throw new Error("We require Node.js version 8 or later; you're using " + process.version);
}
try {
require.resolve('sockjs');
} catch (e) {
throw new Error("Dependencies are unmet; run node pokemon-showdown before launching Pokemon Showdown again.");
}
const FS = require('./lib/fs');
/*********************************************************
* Load configuration
*********************************************************/
try {
// @ts-ignore This file doesn't exist on the repository, so Travis checks fail if this isn't ignored
require.resolve('./config/config');
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err; // should never happen
throw new Error('config.js does not exist; run node pokemon-showdown to set up the default config file before launching Pokemon Showdown again.');
}
// @ts-ignore This file doesn't exist on the repository, so Travis checks fail if this isn't ignored
global.Config = require('./config/config');
global.Monitor = require('./monitor');
if (Config.watchconfig) {
let configPath = require.resolve('./config/config');
FS(configPath).onModify(() => {
try {
delete require.cache[configPath];
global.Config = require('./config/config');
if (global.Users) Users.cacheGroupData();
Monitor.notice('Reloaded config/config.js');
} catch (e) {
Monitor.adminlog("Error reloading config/config.js: " + e.stack);
}
});
}
/*********************************************************
* Set up most of our globals
*********************************************************/
global.Server = {};
global.Dex = require('./sim/dex');
global.toId = Dex.getId;
global.LoginServer = require('./loginserver');
global.Ladders = require('./ladders');
global.Users = require('./users');
global.Punishments = require('./punishments');
global.Console = require('./console.js');
global.Chat = require('./chat');
global.Rooms = require('./rooms');
global.Tells = require('./tells');
global.Ontime = {};
global.Db = require('nef')(require('nef-fs')('config/db'));
delete process.send; // in case we're a child process
global.Verifier = require('./verifier');
Verifier.PM.spawn();
global.Tournaments = require('./tournaments');
global.Dnsbl = require('./dnsbl');
Dnsbl.loadDatacenters();
if (Config.crashguard) {
// graceful crash - allow current battles to finish before restarting
process.on('uncaughtException', err => {
let crashType = require('./lib/crashlogger')(err, 'The main process');
if (crashType === 'lockdown') {
Rooms.global.startLockdown(err);
} else {
Rooms.global.reportCrash(err);
}
});
process.on('unhandledRejection', err => {
let crashType = require('./lib/crashlogger')(err, 'A main process Promise');
if (crashType === 'lockdown') {
Rooms.global.startLockdown(err);
} else {
Rooms.global.reportCrash(err);
}
});
}
/*********************************************************
* Start networking processes to be connected to
*********************************************************/
global.Sockets = require('./sockets');
exports.listen = function (port, bindAddress, workerCount) {
Sockets.listen(port, bindAddress, workerCount);
};
if (require.main === module) {
// Launch the server directly when app.js is the main module. Otherwise,
// in the case of app.js being imported as a module (e.g. unit tests),
// postpone launching until app.listen() is called.
let port;
if (process.argv[2]) port = parseInt(process.argv[2]);
Sockets.listen(port);
}
/*********************************************************
* Set up our last global
*********************************************************/
global.TeamValidatorAsync = require('./team-validator-async');
TeamValidatorAsync.PM.spawn();
// uptime recording
FS('./logs/uptime.txt').readIfExistsSync(function (err, uptime) {
if (!err) global.uptimeRecord = parseInt(uptime, 10); // eslint-disable-line radix
global.uptimeRecordInterval = setInterval(function () {
if (global.uptimeRecord && process.uptime() <= global.uptimeRecord) return;
global.uptimeRecord = process.uptime();
FS('./logs/uptime.txt').write(global.uptimeRecord.toFixed(0));
}, 1 * 60 * 60 * 1000);
});
/*********************************************************
* Start up the githubhook server
********************************************************/
require('./github');
/*********************************************************
* Start up the REPL server
*********************************************************/
require('./lib/repl').start('app', cmd => eval(cmd));