-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (56 loc) · 1.7 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
const express = require("express");
var cors = require('cors');
const app = express();
const server = require('http').createServer(app);
const { Server } = require('socket.io');
const io = new Server(server, {});
const teamsRouter = require('./routes/teams.js');
const stocksRouter = require('./routes/stocks.js');
const tradeRouter = require('./routes/trade.js');
const gameRouter = require('./routes/game.js');
const { swaggerUi, specs } = require('./swagger/swagger');
const buyingLogic = require('./modules/buyingLogic');
const updateTurn = require('./modules/updateTurn');
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
io.on('connection', (socket, data) => {
let turn;
socket.on('gameStart', (data, fn) => {
socket.broadcast.emit('gameStart', data);
});
socket.on('nextTurn', (data, fn) => {
async function next() {
turn = await new Promise((resolve, result) => {
resolve(updateTurn());
});
socket.broadcast.emit('nextTurn',turn);
}
next();
});
socket.on('endPurchase', (data, fn) => {
async function Purchase() {
await new Promise((resolve, result) => {
resolve(buyingLogic());
})
}
Purchase();
});
socket.on('gameStop', (data, fn) => {
socket.broadcast.emit('gameStop', data);
})
socket.on('sendAnswer', (data) => {
socket.broadcast.emit('answer', data);
})
});
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
app.use('/teams', teamsRouter);
app.use('/stocks', stocksRouter);
app.use('/trade', tradeRouter);
app.use('/game', gameRouter);
app.get('/', (req, res) => {
res.send("Postock api server");
})
server.listen(8080, ()=>{
console.log("8080");
})