-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathserver.js
132 lines (121 loc) · 4.56 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
var express = require('express')
var socket = require('socket.io');
var Game = require('./models/game.js');
var Player = require('./models/player.js');
var Table = require('./models/table.js');
var Utility = require('./models/utility.js');
var Room = require('./models/room.js');
var io = socket.listen(8080);
var utility = new Utility();
var room = new Room("Test Room");
room.tables = utility.createSampleTables(10);
// <Socket.IO Section>
io.sockets.on('connection', function (socket) {
socket.emit('userOnline');
socket.on('connectToServer',function(data){
//Add player to the room
var player = new Player(socket.id);
player.setName(data.username);
player.status = "available";
room.addPlayer(player);
//Send Other Players that new player has connected
utility.sendEventToAllPlayersButPlayer('newUserOnline',
{message:"Player is online",username:data.username},io,room.players,player);
utility.sendEventToAllPlayers('tableList',
{tableList: room.getTableMessage(),playerCount:room.players.length},io,room.players);
});
socket.on('connectToTable',function(data){
var player = room.getPlayer(socket.id);
var table = room.getTable(data.tableID);
if(table.isTableAvailable() && table.addPlayer(player)){
player.tableID = table.id;
player.status = 'inTable';
utility.sendEventToTable('userConnectedToTable',
{message:"Player is in Table"},io,table);
utility.sendEventToAllPlayers('tableList',
{tableList: room.getTableMessage(),playerCount:room.players.length},io,room.players);
if(table.isPlaying()){
//Now table starts playing
utility.sendEventToTable('gameStarted',
{tableList: room.getTableMessage()},io,table);
table.gameObj.startGame(utility,io,table);
}
}
else{
socket.emit('errorEvent',{errorNo:100, message: "The table is full!!!"});
}
});
socket.on('userLeaveFromTable',function(data){
//Check if the user is in table
var player = room.getPlayer(socket.id);
if(player.tableID != ""){
var table = room.getTable(player.tableID);
table.removePlayer(player);
utility.sendEventToTable('userDisconnectedFromTable',
{username:player.name},io,table);
utility.sendEventToAllFreePlayersButPlayer('userDisconnectedFromTable',{
username:player.name},io,room.players,player);
socket.emit('playerDisconnectedFromTable',
{username:player.name});
utility.sendEventToAllPlayers('tableList',
{tableList: room.getTableMessage(),playerCount: room.players.length},io,room.players);
}
else{
socket.emit('errorEvent',{errorNo:101, message: "You are not connected to table!!!"});
}
});
socket.on('sendChatMessage',function(data){
var player = room.getPlayer(socket.id);
if(player.tableID == ""){
//Send Message to the Available Players
utility.sendEventToAllFreePlayers('userSendChatMessage',
{username:player.name,message:data.message},io,room.players);
}
else
{
//Send Message to the same Table Players
var table = room.getTable(player.tableID);
utility.sendEventToTable('userSendChatMessage',
{username:player.name},io,table);
}
});
socket.on('sendChatMessageToUser',function(data){
var player = room.getPlayer(socket.id);
var toPlayer = room.getPlayer(data.playerID);
utility.sendEventToSpecificPlayer('userSentChatMessageToUser',
{username:player.name},io,toPlayer);
});
socket.on('disconnect', function(){
//Check player status whether she is in table or game
var player = room.getPlayer(socket.id);
if(player.status != "available"){
//Remove from table
var table = room.getTable(player.tableID);
table.removePlayer(player);
}
//Remove from room
room.removePlayer(player);
utility.sendEventToAllPlayersButPlayer('userDisconnectedFromGame',
{message:"Player is disconnected",username:player.name},io,room.players,player);
utility.sendEventToAllPlayers('tableList',
{tableList: room.getTableMessage(),playerCount: room.players.length},io,room.players);
});
});
//</Socket.IO Section>
// <Express Section>
var app = express.createServer();
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
});
app.get('/', function(req, res){
res.render('index', {
message : 'De groeten'
});
});
app.listen(3000);
// </Express Section>