This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
123 lines (87 loc) · 3.13 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
// do something to end server and game after some fixed score, most probably less than 1000 and declare the winner color
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
var players = {};
nplayer=0;
var star = {
x: Math.floor(Math.random() * 1380) +20,
y: Math.floor(Math.random() * 780) +20
};
var scores = {
blue: 0,
red: 0
};
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
nplayer++;
console.log('a user connected');
// create a new player and add it to our players object
players[socket.id] = {
rotation: 0,
x: Math.floor(Math.random() * 700) + 50,
y: Math.floor(Math.random() * 500) + 50,
playerId: socket.id,
team: (nplayer == 1) ? 'red' : 'blue'
};
// send the players object to the new player
socket.emit('currentPlayers', players);
// send the star object to the new player
socket.emit('starLocation', star);
// send the current scores
socket.emit('scoreUpdate', scores);
// update all other players of the new player
socket.broadcast.emit('newPlayer', players[socket.id]);
socket.on('disconnect', function () {
console.log('user disconnected');
nplayer--;
// remove this player from our players object
delete players[socket.id];
// emit a message to all players to remove this player
io.emit('disconnect', socket.id);
});
// when a player moves, update the player data
socket.on('playerMovement', function (movementData) {
players[socket.id].x = movementData.x;
players[socket.id].y = movementData.y;
players[socket.id].rotation = movementData.rotation;
// emit a message to all players about the player that moved
socket.broadcast.emit('playerMoved', players[socket.id]);
});
socket.on('starCollected', function () {
if (players[socket.id].team === 'red') {
scores.red += 10;
} else {
scores.blue += 10;
}
//star.x = 16 ;
//star.y = 16 ;
use=0;
do
{
star.x = Math.floor(Math.random() * 1410);
star.y = Math.floor(Math.random() * 810) ;
use=0;
while(star.y<=16&&(star.x<=25||star.x>=1240)) // Here out score text is currently printed at these locations, x between 0-25 or x between 1240-1280 and y between 0-16
{
star.x = Math.floor(Math.random() * 1410); // So keep changing star's location until it stops coming in text's location
star.y = Math.floor(Math.random() * 810) ;
}
Object.keys(players).forEach(function (id)
{
if (players[id].x === star.x||players[id].y === star.y)
use++;
});
}
while(use!=0);
io.emit('starLocation', star);
io.emit('scoreUpdate', scores);
});
});
server.listen(process.env.PORT || 8081, function () {
console.log(`Listening on ${server.address().port}`);
});