-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (70 loc) · 2.34 KB
/
index.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Wallet_1 = require("./lib/Wallet");
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const _ = require('lodash');
const connected = [];
const rooms = [];
app.use(express.static(__dirname));
app.get('/play/:username', (req, res) => {
res.sendFile(__dirname + '/client/play.html');
});
app.get('/admin', (req, res) => {
res.sendFile(__dirname + '/client/admin.html');
});
//waiting for connections on the play route
let insert = {};
io.on('connection', (socket) => {
const wallet = new Wallet_1.Wallet(socket.id);
insert = { id: socket.id, name: 'player', info: wallet };
connected.push(insert);
//checking for the rooms
if (rooms.length === 0) {
socket.join('room' + rooms.length);
rooms.push({ id: 'room' + rooms.length, full: false, users: [insert] });
}
else if (rooms[rooms.length - 1].full) {
socket.join('room' + rooms.length);
rooms.push({ id: 'room' + rooms.length, full: false, users: [insert] });
}
else {
rooms[rooms.length - 1].full = true;
socket.join('room' + (rooms.length - 1));
}
io.emit("info", insert);
console.log(rooms);
//waiting for pay event
socket.on('pay', (payeeAdd) => {
console.log(payeeAdd);
});
//waiting for socket disconnect event
socket.on('disconnect', () => {
let b = _.findIndex(connected, function (el) { return el.id == socket.id; });
connected.splice(b, 1);
});
socket.on("disconnecting", () => {
let socketId = Array.from(socket.rooms)[1];
//leaving the room
if (socketId) {
//finding the room
let a = _.findIndex(rooms, function (el) { return el.id == socketId; });
rooms[a].full = false;
//finding the user
let b = _.findIndex(rooms[a].users, function (el) { return el.id == socket.id; });
rooms[a].users.splice(b, 1);
socket.leave(socketId);
console.log(rooms);
}
});
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/client/index.html');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});