-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
91 lines (72 loc) · 2.7 KB
/
main.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
/////////////////////////////////////////////////////////////////// Módulos
// Servidor HTTP
const http = require('http');
const https = require('https');
const fs = require('fs');
// Express APP.
const express = require('express');
// Cors para controlar acesso.
const cors = require('cors');
// Banco de dados NoSQL.
const mongoose = require('mongoose');
// Colyseus, manuseio de partida.
const colyseus = require('colyseus');
const monitor = require("@colyseus/monitor").monitor;
// Função para carregar diversos scripts.
const requireDir = require('require-dir');
// Converter Req.Body para JSON.
const bodyParser = require('body-parser');
// Chave de acesso para requests.
const accessKey = 'aXmsaKsoaPw9192_CClsoKWmLx,lSkWPeo´qpLSOjpOMXpoaJspoJXpasmOPWempaSOdmPSOd__dsapiPWO())';
// Propriedades do manuseio de partida.
const matchMaker = colyseus.matchMaker;
///////////////////////////////////////////////////////////////////
// Iniciar APP.
const app = express();
// Porta
const port = 21238;
// Conectar ao NoSql
const mongooseHost = 'lowmanadatabase-7ki9x.gcp.mongodb.net/test?retryWrites=true&w=majority';
mongoose.connect('mongodb+srv://lowdev:a4d62af8@'+mongooseHost, {useUnifiedTopology: true, useNewUrlParser: true}, (err, db)=>{
if(!err) console.log('Conectado ao Banco de Dados...');
else console.log('Ocorreu um erro na conexão ao Banco de Dados.');
});
requireDir('./source/schemes'); // Importar 'Schemas'.
// Middlewares.
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.use("/colyseus", monitor());
app.use((req, res, next)=>{
if(req.method != 'GET'){
var header_ = req.body.accessKey;
if (header_ != accessKey){
return res.status(400).send('N/A');
} else next();
} else {
next();
}
});
app.use("/user", require('./source/_userRequests'));
app.use("/source/gameSource", express.static('source/gameSource'));
// Criar Servidor HTTP.
const server = http.createServer(app);
// const server = https.createServer({
// key: fs.readFileSync("key.pem"),
// cert: fs.readFileSync("cert.pem")
// }, app);
// Criar Servidor inGame.
const gameServer = new colyseus.Server({
server: server,
pingMaxRetries: 5,
pingInterval: 7000,
});
/////////////////////////////////////////////////////////////////// Definir salas do servidor do jogo.
// Definindo GameLobby
gameServer.define('GameLobby', require('./source/_gameLobbyModel').GameRoom);
gameServer.define('GameMatch', require('./source/gameData/_matchRoom'));
gameServer.define('Game', require('./source/gameData/_gameRoom'));
matchMaker.createRoom('GameLobby');
// Finalizar
gameServer.listen(port);
console.log(`Servidor 'inGame' ouvindo na porta: ${port}.`);