-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
136 lines (117 loc) · 4.62 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, '/static')));
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var users = {};
var users_connections = {};
var openGames = {};
var activeGames = {};
var game_id = 0;
var user_count = 0;
app.get('/game', function(req, res) {
res.sendFile(__dirname + '/static/game.html');
});
io.on('connection', function(socket) {
user_count += 1;
console.log("user_count: "+user_count);
console.log('new connection ' + socket.id);
socket.on('message', function(msg) {
console.log('Got message from client: ' + msg);
});
socket.on('login',function(userId){
console.log(userId + ' is login');
socket.userId = userId;
if (!users[userId]) {
console.log('creating new user');
users[userId] = {userId: socket.userId, games:{}};
users_connections[userId] = socket;
} else {
console.log('user found!');
Object.keys(users[userId].games).forEach(function(gameId) {
console.log('gameid - ' + gameId);
});
}
socket.emit('login', {session: socket.id ,user_count: user_count});
});
socket.on('playnow',function(mode){
var open_new_game = true;
//FIND AVAILABLE GAME
if (Object.keys(openGames).length > 0){
for (var i=0;i<Object.keys(openGames).length;i++){
var game = openGames[i+1];
if (game.users.white == null) {
socket.gameId = game.id;
game.users.white = socket.userId;
users[game.users.white].games[game.id] = game.id;
socket.emit('startgame', {game: game, color: 'white'});
users_connections[game.users.black].emit('joingame',{game: game});
open_new_game = false;
} else if (game.users.black == null) {
game.users.black = socket.userId;
users[game.users.black].games[game.id] = game.id;
socket.emit('startgame', {game: game, color: 'black'});
users_connections[game.users.white].emit('joingame',{game: game});
open_new_game = false;
}
if (open_new_game == false) {
activeGames[game.id] = game;
break;
}
}
}
//NEW GAME
if (open_new_game) {
game_id ++;
var game = {
id: game_id,
board: null,
users: {white: null, black: null}
};
socket.gameId = game.id;
var user_color = Math.floor(Math.random() * 2) + 1;
if (user_color == 1) {
game.users.white = socket.userId;
users[game.users.white].games[game.id] = game.id;
socket.emit('startgame', {game: game, color: 'white'});
} else {
game.users.black = socket.userId;
users[game.users.black].games[game.id] = game.id;
socket.emit('startgame', {game: game, color: 'black'});
}
openGames[game.id] = game;
}
});
socket.on('move', function(move) {
//console.log('Got move from client: ' + move.move);
socket.broadcast.emit('move', move);
activeGames[move.gameId].board = move.board;
});
socket.on("resign", function(msg) {
console.log('resign '+msg.color);
socket.emit('resign', {color: msg.color});
if (activeGames[msg.gameId]==null){
//do nothing;
} else if ((msg.color == 'white')&&(activeGames[msg.gameId]!=null)){
var user_id = activeGames[msg.gameId].users.black;
users_connections[user_id].emit('resign', {color: msg.color});
delete activeGames[msg.gameId];
} else if ((msg.color == 'black')&&(activeGames[msg.gameId]!=null)){
var user_id = activeGames[msg.gameId].users.white;
users_connections[user_id].emit('resign', {color: msg.color});
delete activeGames[msg.gameId];
}
openGames[msg.gameId].users.white = 'game_end';
openGames[msg.gameId].users.black = 'game_end';
});
socket.on("disconnect", function(s) {
user_count -= 1;
console.log("Disconnected!!");
console.log("user_count: "+user_count);
});
});
http.listen(port, function() {
console.log('listening on *: ' + port);
});