-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathroom.js
More file actions
103 lines (103 loc) · 2.43 KB
/
Copy pathroom.js
File metadata and controls
103 lines (103 loc) · 2.43 KB
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
var Player = require('./Player');
var GameMain = require('./GameMain');
function Room(roomId) {
this.roomId = roomId;
this.playerList = [];
this.usernameList = [];
this.gameMain = null;
this.limitPlayerNum = 2;
};
Room.prototype.addPlayer = function (socket, username) {
var _player = new Player(socket, username);
this.playerList.push(_player);
this.usernameList.push(username);
};
Room.prototype.delPlayer = function (username) {
for (var index = 0; index < this.playerList.length; index++) {
var element = this.playerList[index];
if (element.username == username) {
this.playerList.splice(index, 1);
}
}
};
Room.prototype.resetGame = function(){
this.playerList.forEach(function(ele){
ele.ready = false;
});
this.gameMain = null;
};
Room.prototype.setPlayerReady = function (username) {
var result = false;
this.playerList.forEach(function (ele) {
if (ele.username == username) {
ele.ready = true;
result = true;
}
});
if (this.checkAbleStartGame() == true) {
console.log('check open game');
this.openGame();
}
return result;
};
Room.prototype.openGame = function () {
this.gameMain = new GameMain({
playerList: this.playerList,
roomId: this.roomId
});
this.gameMain.startGame();
};
Room.prototype.setPlayerNotReady = function (username) {
var result = false;
this.playerList.forEach(function (ele) {
if (ele.username == username) {
ele.ready = false;
result = true;
}
});
return result;
};
Room.prototype.checkAbleStartGame = function () {
var result = true;
if (this.playerList.length == this.limitPlayerNum) {
this.playerList.forEach(function (ele) {
if (ele.ready == false) {
result = false;
}
});
}else{
result = false;
}
return result;
};
Room.prototype.checkSameUsername = function (username) {
var _index = this.usernameList.indexOf(username);
if (_index == -1) {
return false;
} else {
return true;
}
};
Room.prototype.delPlayerBySocketId = function (socketId) {
var _index = this.hasPlayerBySocketId(socketId);
if (_index != -1) {
this.playerList.splice(_index, 1);
}
};
Room.prototype.getPlayerList = function () {
var arr = [];
this.playerList.forEach(function (ele) {
arr.push({ username: ele.username, ready: ele.ready });
})
return arr;
};
Room.prototype.hasPlayerBySocketId = function (socketId) {
var _index = -1;
this.playerList.forEach(function (ele, index) {
if (ele.id == socketId) {
_index = index;
}
});
return _index;
};
module.exports = Room;