-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
206 lines (168 loc) · 6.68 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// requires
var http = require('http');
var socketio = require('socket.io');
var express = require('express');
var tools = require('./node_modules/threedox/tools');
var THREEDOX = require('./node_modules/threedox/THREEDOX.Classes');
//
var router = express();
var server = http.createServer(router);
var io = socketio.listen(server);
//
var manageRooms = new THREEDOX.ManageRooms();
var allUsers = {};
var userNumber = 1;
// route
router.use(express.static(__dirname + "/client"));
// on first client connection
io.sockets.on('connection', function(socket) {
var kk = {
WYBRANO: 1,
NIE_WYBRANO: 0,
WYBRALES: 2
};
//
var myNumber = userNumber++;
var myName = 'user' + myNumber;
allUsers['id_'+socket.id] = socket;
tools.showUserInfo(socket, 'User connected');
// on NewGame
socket.on('newGame', function(data) {
//make new player
var player = new THREEDOX.Player({
id: socket.id,
name: myName,
host: socket.handshake.headers.host,
socket: socket
});
manageRooms.giveMeRoom(player);
console.log(manageRooms.rooms);
var playerGame = manageRooms.getGameByPlayerId(player.id);
var playerFactObj = manageRooms.getPlayerById(player.id);
//sprawdzenie czy dla gry gracza o id jest wybrany znak
var signChosen = playerGame.signChosen;
if(signChosen) {
playerFactObj.setSign(playerGame.signLeft);
//signState
signState(socket, kk.WYBRANO, playerGame.signLeft, 'newGameBox');
} else {
//signState
signState(socket, kk.NIE_WYBRANO, null, 'newGameBox');
}
});
// sends info about signState
function signState(s, ok, takeSign, from, err) {
var obj = {
kk: ok,
takeSign: takeSign,
from: from,
err: err
};
s.emit('signState', obj);
}
// init
socket.emit('init', { text: "Witaj na serwerze 3DOX!" });
// on choose
socket.on('choose', function(data) {
var playerGame = manageRooms.getGameByPlayerId(socket.id);
var signChosen = playerGame.signChosen;
//znak nie zostal jeszcze wybrany w tej grze
if(!signChosen) {
tools.showUserInfo(socket, 'This user choosed ' + data.sign);
// set choose
var bool = manageRooms.getPlayerById(socket.id).setSign(data.sign);
if(bool) {
if(playerGame.setSignChoice(data.sign)) {
signState(socket, kk.WYBRALES, data.sign, 'choiceBox');
var friend = manageRooms.getPartnerByPlayerId(socket.id);
if(friend) {
friend.setSign(playerGame.signLeft);
signState(friend.socket, kk.WYBRANO, playerGame.signLeft, 'partnerChoiceBox');
}
}
} else {
signState(socket, kk.WYBRALES, null, 'choiceBox', 'Coś poszlo nie tak przy ustawianiu znaku.');
}
} else {
var signLeft = playerGame.signLeft;
manageRooms.getPlayerById(socket.id).setSign(signLeft);
signState(socket, kk.WYBRANO, signLeft, 'choiceBox');
}
});
socket.on('getGamerSign', function() {
var playerObj = manageRooms.getPlayerById(socket.id);
var sign = (playerObj)? playerObj.sign : null;
socket.emit('getGamerSign', { sign: sign });
});
socket.on('update', function(flag) {
emitUpdated(socket, flag);
});
function emitUpdated(s, flag) {
if(typeof flag === 'undefined') flag = '';
var playerObj = manageRooms.getPlayerById(s.id);
var friendObj = manageRooms.getPartnerByPlayerId(s.id);
var sign = (playerObj)? playerObj.sign : null;
var playerGame = manageRooms.getGameByPlayerId(s.id);
if(flag === 'start') {
playerGame.gameStarted = true;
emitUpdated(manageRooms.getPartnerByPlayerId(s.id).socket, '');
}
var gameStarted = (playerGame)? playerGame.gameStarted : false;
var gamerNumber = (playerObj)? playerObj.number : null;
var whoTurn = (playerGame)? playerGame.whoTurn : null;
var field = (playerGame)? playerGame.field : null;
var score = (playerGame)? playerGame.score : null;
var friendName = (friendObj)? friendObj.name : 'not connected';
var friendNum = (gamerNumber === 1)? 2 : 1;
var playersNames = {};
playersNames['player'+gamerNumber] = playerObj.name;
playersNames['player'+friendNum] = friendName;
var obj = {
gamerSign: sign,
gameStarted: gameStarted,
gamerNumber: gamerNumber,
whoTurn: whoTurn,
field: field,
score: score,
playersNames: playersNames
}
s.emit('updated', obj);
}
socket.on('deletePlayers', function() {
tools.showUserInfo(socket, 'User deleted from room');
manageRooms.deletePlayer(socket.id);
console.log(manageRooms.rooms);
});
socket.on('changeTurn', function() {
manageRooms.getGameByPlayerId(socket.id).changeTurn();
var friend = manageRooms.getPartnerByPlayerId(socket.id);
emitUpdated(friend.socket, '');
emitUpdated(socket, '');
});
socket.on('fillFieldCell', function(data) {
var game = manageRooms.getGameByPlayerId(socket.id);
game.fillFieldCell(data.gamerSign, data.i, data.j, data.k);
game.checkShots();
//emitUpdated(socket, 'fillFieldCell');
//emitUpdated(manageRooms.getPartnerByPlayerId(socket.id).socket, 'fillFieldCell');
});
// on user disconnect
socket.on('disconnect', function(){
tools.showUserInfo(socket, 'User disconnected');
var friendObj = manageRooms.getPartnerByPlayerId(socket.id);
if(friendObj) {
manageRooms.deletePlayer(friendObj.socket.id);
friendObj.socket.emit('friendDisconnected', 'You was deleted from the room because, your opponent has disconnected from the game. Sorry.');
}
manageRooms.deletePlayer(socket.id);
console.log(manageRooms.rooms);
allUsers['id_'+socket.id] = null;
});
})
var localhost = '127.0.0.1';
var homeNet = '192.168.0.13';
// run server
server.listen(8081, localhost, function(){
var addr = server.address();
console.log("Server listening at", addr.address + ":" + addr.port);
});