-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (72 loc) · 2.39 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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require('mongoose');
const cors = require('cors');
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
require('dotenv').config();
const { queueUpdate, queueInit, queueDelete } = require('./functions/socketQueue')
const { matchInit, matchUpdate } = require('./functions/socketMatch')
app.use(cors());
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
const PORT = process.env.PORT || 8081;
// ####################################################### ROUTES
const matchRoute = require('./routes/match');
app.use('/match', matchRoute);
const userRoute = require('./routes/user');
app.use('/user', userRoute);
const queueRoute = require('./routes/queue');
app.use('/queue', queueRoute);
// ####################################################### APP USES
app.use(bodyParser.urlencoded({ extended: false })); // NÃO/SIM ACEITA dados complexos* = FALSE/TRUE
app.use(bodyParser.json()); //Aceita apenas json como formato
app.use(express.static("public"));
app.get('/', function (req, res) {
// res.redirect("/match")
res.status(200).send({
messagem: "OK Rota GET ROOT"
});
});
app.post('/', function (req, res) {
const userLogin = req.body;
res.send({
menssagem: "OK FOI",
result: userLogin
})
});
io.on('connection', async (socket) => {
const queue = await queueInit();
io.emit('queueInit', queue);
const match = await matchInit();
io.emit('matchInit', match);
socket.on('queueDelete', async(playerId) => {
const queue = await queueDelete(playerId);
io.emit('queueUpdate', queue);
})
socket.on('queueUpdate', async (player) => {
try {
const { count, queue } = await queueUpdate(player);
io.emit('queueUpdate', queue);
if(await count >= 6){
const { queueRes, matchRes, reportRes } = await matchUpdate();
io.emit('queueUpdate', queueRes);
io.emit('matchUpdate', matchRes);
io.emit('reportUpdate', reportRes);
}
return;
} catch (error) {
console.log(error);
}
})
// socket.on('match', async (event) => {
// socket.join("some room");
// io.to("some room").emit("hi");
// })
})
server.listen(PORT, function () {
console.log("Server started on port " + PORT);
});