-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
141 lines (123 loc) · 4.88 KB
/
Copy pathapp.js
File metadata and controls
141 lines (123 loc) · 4.88 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
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
//TODOS:
//Display countown after each score #1
//Splash Page
// - Join a new game
// move javascript to client.js
// move code to server to test multiple players
// have different colors for the paddles based on player position
var constants = { court: { width: 600, height: 600 },
paddle: { width: 50, height: 15, delta: 3 },
ball: { radius: 10, deltaLeft: 3, deltaTop: 2, interval: 30 }
};
var state = { paddles: {},
ball: { left: 0, top: 0 },
bottomPaddle: 0,
topPaddle: 0,
leftPaddle: 0,
rightPaddle: 0
};
var serverState = { intervalId: 0,
connections: 0
};
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs');
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function readfile_callback(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
}
);
};
function calculateBallPosition() {
var left = state.ball.left + constants.ball.deltaLeft;
var top = state.ball.top + constants.ball.deltaTop;
if (left >= constants.court.width) {
left = constants.court.width;
constants.ball.deltaLeft = -constants.ball.deltaLeft;
} else if (left <= 0) {
left = 0;
constants.ball.deltaLeft = -constants.ball.deltaLeft;
}
if (top + constants.ball.radius >= constants.court.height - constants.paddle.height) {
if (state.bottomPaddle &&
left > ( (state.paddles[state.bottomPaddle]/100) * constants.court.width - constants.paddle.width / 2) &&
(left < ( (state.paddles[state.bottomPaddle]/100) * constants.court.width + constants.paddle.width / 2) ) ) {
top = constants.court.height - constants.paddle.height - constants.ball.radius;
constants.ball.deltaTop = -constants.ball.deltaTop;
} else {
//TODO: #1
left = constants.court.width / 2;
top = constants.court.height / 2;
}
} else if (top <= 0) {
top = 0;
constants.ball.deltaTop = -constants.ball.deltaTop;
}
state.ball.left = left;
state.ball.top = top;
};
io.sockets.on('connection', function (socket) {
var paddleAdded = false;
if (!state.bottomPaddle) {
state.bottomPaddle = socket.id;
} else if (!state.topPaddle) {
state.topPaddle = socket.id;
} else if (!state.leftPaddle) {
state.leftPaddle = socket.id;
} else if (!state.rightPaddle) {
state.rightPaddle = socket.id;
} else {
// placeholder for fifth player
return;
}
state.paddles[socket.id] = 50;
socket.emit('environment', { court: { width: constants.court.width,
height: constants.court.height,
},
paddle: { width: constants.paddle.width,
height: constants.paddle.height,
delta: constants.paddle.delta
},
ball: { radius: constants.ball.radius },
player: { id: socket.id }
});
if ( !serverState.intervalId ) {
serverState.intervalId = setInterval( function(){
calculateBallPosition();
}, constants.ball.interval );
}
socket.intervalId = setInterval( function(){
socket.emit('ball', { position: { left: state.ball.left, top: state.ball.top } });
socket.emit('paddles', { positions: state.paddles, sides: {bottom: state.bottomPaddle, top: state.topPaddle, left: state.leftPaddle, right: state.rightPaddle }});
}, constants.ball.interval );
socket.on('paddle', function (data) {
state.paddles[socket.id] = data.left;
});
socket.on('disconnect', function () {
serverState.connections--;
clearInterval( socket.intervalId );
delete state.paddles[socket.id];
if (state.bottomPaddle == socket.id)
state.bottomPaddle = 0;
else if (state.topPaddle == socket.id)
state.topPaddle = 0;
else if (state.leftPaddle == socket.id)
state.leftPaddle = 0;
else if (state.rightPaddle == socket.id)
state.rightPaddle = 0;
if ( serverState.connections == 0 ) {
clearInterval( serverState.intervalId );
serverState.intervalId = 0;
}
console.log('player left');
});
console.log(serverState.connections);
serverState.connections++;
});