-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
109 lines (94 loc) · 3.21 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
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
104
105
106
107
108
109
var express = require('express');
var port = process.env.PORT || 5050;
var server = express()
.use(express.static(__dirname + '/public'))
.use(express.static(__dirname))
.set('views', __dirname + '/public')
.engine('.html', require('ejs').renderFile)
.get('/', function(req, res) {
res.render('index.html');
})
.listen(port, function() { console.log("Listening on port " + port); });
var io = require('socket.io')(server);
'use strict';
const fs = require('fs');
var json;
fs.readFile('data.json', (err, data) => {
json = JSON.parse(data);
});
io.on('connection', function(socket) {
var g;
var user;
console.log('connection');
socket.on('init', function(data){
maybeg = json.all.filter(e => e.game == data.game);
if (maybeg.length == 0){
g = {game:data.game, draw: [], width:data.w, height:data.h, users: []};
console.log(g.width,g.height);
json.all.push(g);
}else{
g = maybeg[0];
}
fs.writeFileSync('data.json', JSON.stringify(json));
});
socket.on('showAllGames',function(){
socket.emit('allGames', json.all);
});
socket.on('start', function(data){
user = {name:data.name, col:data.col};
g.users.push(user);
io.emit('userUpdate',{game:g.game, users: g.users});
socket.emit('join', {width:g.width,height:g.height});
g.draw.forEach(function(e){
socket.emit('other_draw',e);
});
fs.writeFileSync('data.json', JSON.stringify(json));
});
socket.on('left',function(data){
if(g){
g.users = g.users.filter(e => e != user);
io.emit('userUpdate',{game:g.game, users: g.users});
}
fs.writeFileSync('data.json', JSON.stringify(json));
});
socket.on('draw', function(data) {
if(json.all.some(e => e.game == data.game)){
g.draw.push(data);
socket.broadcast.emit('other_draw', data);
}
fs.writeFileSync('data.json', JSON.stringify(json));
});
socket.on('disconnect', function(){
console.log('user disconnected');
if(g){
g.users = g.users.filter(e => e != user);
io.emit('userUpdate',{game:g.game, users: g.users});
if(g.users.length == 0 && g.draw.length == 0){
json.all = json.all.filter(e => e.game != g.game);
}
}
fs.writeFileSync('data.json', JSON.stringify(json));
});
socket.on('discon', function(){
console.log('user changed game');
if(g){
g.users = g.users.filter(e => e != user);
io.emit('userUpdate',{game:g.game, users: g.users});
if(g.users.length == 0 && g.draw.length == 0){
json.all = json.all.filter(e => e.game != g.game);
}
}
fs.writeFileSync('data.json', JSON.stringify(json));
});
socket.on('clear',function(data){
if(json.all.some(e => e.game == data.game)){
g.draw = [];
io.emit('clr',data);
}
fs.writeFileSync('data.json', JSON.stringify(json));
});
socket.on('clearall',function(){
json.all = [];
fs.writeFileSync('data.json', JSON.stringify(json));
});
});