forked from rakeshok/gameserver
-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.js
63 lines (50 loc) · 1.71 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
var async = require('async');
var assert = require('assert');
var constants = require('constants');
var fs = require('fs');
var path = require('path');
var config = require('./server/config');
var socket = require('./server/socket');
var database = require('./server/database');
var Game = require('./server/game');
var GameHistory = require('./server/game_history');
var _ = require('lodash');
var server;
if (config.USE_HTTPS) {
var options = {
key: fs.readFileSync(config.HTTPS_KEY),
cert: fs.readFileSync(config.HTTPS_CERT),
secureProtocol: 'SSLv23_method',
secureOptions: constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_SSLv2
};
if (config.HTTPS_CA) {
options.ca = fs.readFileSync(config.HTTPS_CA);
}
server = require('https').createServer(options).listen(config.PORT, function() {
console.log('Listening on port ', config.PORT, ' on HTTPS!');
});
} else {
server = require('http').createServer().listen(config.PORT, function() {
console.log('Listening on port ', config.PORT, ' with http');
});
}
async.parallel([
database.getGameHistory,
database.getLastGameInfo,
database.getBankroll
], function(err, results) {
if (err) {
console.error('[INTERNAL_ERROR] got error: ', err,
'Unable to get table history');
throw err;
}
var gameHistory = new GameHistory(results[0]);
var info = results[1];
var bankroll = results[2];
console.log('Have a bankroll of: ', bankroll/1e8, ' btc');
var lastGameId = info.id;
var lastHash = info.hash;
assert(typeof lastGameId === 'number');
var game = new Game(lastGameId, lastHash, bankroll, gameHistory);
socket(server, game);
});