-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
132 lines (105 loc) · 3.41 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
// Server stuff goes here
var express = require('express');
var app = express();
var server = require('http').Server(app);
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
// Store all players
var players = {};
var highScore = 0;
var stars = []
class Star {
constructor(id, x, y, display) {
this.id = id;
this.x = x;
this.y = y;
this.display = display;
}
}
stars.push(new Star(1, 24, 176, true));
stars.push(new Star(2, 73, 38, true));
stars.push(new Star(3, 185, 73, true));
stars.push(new Star(4, 301, 103, true));
stars.push(new Star(5, 375, 41, true));
stars.push(new Star(6, 419, 208, true));
stars.push(new Star(7, 603, 63, true));
stars.push(new Star(8, 721, 142, true));
stars.push(new Star(9, 37, 349, true));
stars.push(new Star(10, 148, 452, true));
stars.push(new Star(11, 277, 377, true));
stars.push(new Star(12, 359, 484, true));
stars.push(new Star(13, 484, 447, true));
stars.push(new Star(14, 601, 496, true));
stars.push(new Star(15, 738, 465, true));
stars.push(new Star(16, 564, 327, true));
stars.push(new Star(17, 722, 286, true));
var starCount = stars.length;
// Socket.io
var io = require('socket.io')(server, {
wsEngine: 'ws'
});
// Sockets logic goes here
io.on('connection', function (socket) {
console.log('a user connected');
// create a new player and add it to our players object
players[socket.id] = {
x: Math.floor(Math.random() * 700) + 50,
y: Math.floor(Math.random() * 500) + 50,
playerId: socket.id,
};
// update all other players of the new player
socket.broadcast.emit('newPlayer', players[socket.id]);
// send the players object to the new player
socket.emit('currentPlayers', players);
// Send leaderboard
io.emit('leaderScore', highScore);
// send the star object to the new player
socket.emit('starLocation', stars);
socket.on('disconnect', function () {
console.log('user disconnected');
// 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].keydown = movementData.keydown;
// emit a message to all players about the player that moved
socket.broadcast.emit('playerMoved', players[socket.id]);
});
// When a star is collected
socket.on('starCollected', function (id, score) {
if (stars[id].display == true) {
stars[id].display = false;
io.emit('removeStar', id);
starCount--;
}
if(score > highScore)
{
highScore = score;
io.emit('leaderScore', highScore);
}
if(starCount == 0)
{
stars.forEach(element => {
element.display=true;
});
starCount = stars.length;
setTimeout(ReDrawStars, 2000);
;
}
});
});
function ReDrawStars()
{
io.emit('replenishStars');
}
var port = process.env.PORT || 1337;
server.listen(port, function () {
console.log(`Listening on ${server.address().port}`);
});