-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
159 lines (141 loc) · 5.33 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
//wss://project-one-mmwar.herokuapp.com:443/socket.io/?EIO=4&transport=websocket
//Access the functionality that socket.io provides & if we dont have a port set, use the given port
var io = require('socket.io')(process.env.PORT || 8080);
//The id of the player represented as a Hex number
var shortid = require('shortid');
//The postgres database
var pg = require('pg');
pg.defaults.ssl = true;
console.log('heroku server started');
//The list of all the players on the server
var players = [];
//The current match id
var thisMatchId = 0;
//The current match room
var match;
//The list of currently played games
var matches = [];
//This counter is used to determine if the player is the first or the second
var counter = 0;
//Database URL
var connectionString = "postgres://srgvuycakcyozi:6f209485fdb080e2ed9f8fda6226b3e5484a29bb23e24960bfffe5d708b760d8@ec2-107-20-250-195.compute-1.amazonaws.com:5432/d8fcrmkf51oman";
//Callback for the 1st connection
io.on('connection', function (socket) {
//Player id
var thisPlayerId;
//You are the first player, create the match
if (counter == 0) {
thisMatchId = shortid.generate();
match = thisMatchId.toString();
matches[thisMatchId] = match;
}
socket.room = match;
socket.join(match);
//Tell the player that he connected and ask him for his Id
socket.emit('register', { match: thisMatchId, camp: counter });
//The client recieve the register callback and sends his id
socket.on('playerId', function(playerData) {
if (playerData.id) {//There is a previous id
thisPlayerId = playerData.id;
console.log('there is a previously existing id: ', playerData.id);
pg.connect (connectionString, function(err, client) {
if (err) {
return console.error(err);
}
console.log('connected to postgres for selecting');
const select = {
text: 'SELECT * FROM user_db WHERE id = $1;',
values: [thisPlayerId],
};
client
.query(select)
.on('row', function (row) {
console.log(JSON.stringify(row));
});
});
} else {
thisPlayerId = shortid.generate();
console.log('new id: ', thisPlayerId);
pg.connect(connectionString, function(err, client) {
if (err) {
return console.error(err);
}
console.log('connected to postgres for inserting');
const insert = {
text: 'INSERT INTO user_db(id, played, won) VALUES($1, $2, $3);',
values: [thisPlayerId, 0, 0],
};
client
.query(insert)
.on('row', function (row) {
console.log(JSON.stringify(row));
});
});
socket.emit('myId', { id: thisPlayerId });
}
pg.end();
});
//You are the second player, echo the start command
if (counter != 0) {
io.in(socket.room).emit('ready', { match: thisMatchId });
}
counter++;
counter = counter % 2;
//Broadcast to both players the attack
socket.on('attack', function (data) {
console.log('attack ', JSON.stringify(data));
socket.broadcast.to(socket.room).emit('attack', data);
});
//Update losing case
socket.on('lostMatch', function() {
pg.connect(connectionString, function(err, client) {
if (err) {
return console.error(err);
}
console.log('connected to postgres for updating.. lost');
const updateLost = {
text: 'UPDATE user_db SET played = played + 1 WHERE id = $1;',
values: [thisPlayerId],
};
client
.query(updateLost)
.on('row', function (row) {
console.log(JSON.stringify(row));
});
});
pg.end();
});
//Update winning case
socket.on('wonMatch', function() {
pg.connect(connectionString, function(err, client) {
if (err) {
return console.error(err);
}
console.log('connected to postgres for updating.. won');
const updateWon = {
text: 'UPDATE user_db SET played = played + 1, won = won + 1 WHERE id = $1;',
values: [thisPlayerId],
};
client
.query(updateWon)
.on('row', function (row) {
console.log(JSON.stringify(row));
});
});
pg.end();
});
//To delete the disconnected player from the server
socket.on('disconnect', function () {
console.log('removing client: ' + thisPlayerId);
//To remove the player and the match from the server list
//delete players[thisPlayerId];
//To tell all the clients to delete the players character from the scene
socket.broadcast.to(socket.room).emit('disconnected', { id: thisPlayerId });
// leave the current room
socket.leave(socket.room);
});
});
//Update the camps of all connected players
setInterval(function(){
io.emit('update');
}, 4000);