-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
92 lines (73 loc) · 2.55 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
const express = require('express');
const http = require('http');
const path = require('path');
const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server);
app.set('port', 3000);
// Secara default, nodejs tidak mengijinkan
// untuk mengakses file sebelum didaftarkan
app.use('/build',express.static(path.join(__dirname, 'build')));
app.use('/css',express.static(path.join(__dirname, 'css')));
app.use('/img',express.static(path.join(__dirname, 'img')));
app.use('/js',express.static(path.join(__dirname, 'js')));
app.use('/sound',express.static(path.join(__dirname, 'sound')));
app.use('/fonts',express.static(path.join(__dirname, 'fonts')));
app.use('/particle',express.static(path.join(__dirname, 'particle')));
server.listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
//==================
//-----Routing------
//==================
app.get('/', function (req, res) {
res.sendFile(__dirname + '/views' + '/awal.html');
});
app.get('/game', function (req, res) {
res.sendFile(__dirname + '/views' + '/index.html');
});
app.get('/game-multiplayer', function (req, res) {
res.sendFile(__dirname + '/views' + '/indexmultiplayer.html');
});
app.get('/start', function (req, res) {
res.sendFile(__dirname + '/views' + '/awal.html');
});
app.get('/instruksi', function (req, res) {
res.sendFile(__dirname + '/views' + '/instruksi.html');
});
app.get('/master', function(req, res) {
res.sendFile(__dirname + '/views' + '/master.html');
});
app.get('/player', function(req, res) {
res.sendFile(__dirname + '/views' + '/player.html');
});
app.get('/multiplayer', function(req, res) {
res.sendFile(__dirname + '/views' + '/form.html');
});
var players = [];
//=================
//-----Socket------
//=================
io.on('connection', function(socket) {
socket.on('join', function(room) {
socket.join(room);
console.log(socket.id);
if(players.length == 0 && socket.conn.remoteAddress != "::1") players.push(socket.id);
for(let i = 0;i < players.length; i++) {
if(socket.id != players[i] && socket.conn.remoteAddress != "::1") {
players.push(socket.id);
break;
}
}
io.sockets.connected[socket.id].emit('init', players.indexOf(socket.id) + 1);
if(players.length >= 2) {
io.sockets.in(room).emit('gameplay', true);
socket.on('sync', function(data) {
io.sockets.in(room).emit('sync', data);
});
}
});
socket.on('disconnect', function() {
console.log('A user disconnected');
});
});