-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.js
154 lines (123 loc) · 3.4 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
const express = require('express')
const app = express();
const socketio = require('socket.io');
const expressServer = app.listen(process.env.PORT || 3000);
const io = socketio(expressServer);
const PlayerShape = require('./common/PlayerShape');
let players = [];
let meats = [];
const day = {
number: 120,
period: 'morning'
};
meats = meatCreate();
app.use('/', express.static('client/dist'))
app.get('/players', function (req, res) {
res.send(JSON.stringify(players));
})
io.sockets.on('connection', (socket) => {
const { id } = socket;
socket.on('start', (data, callback) => {
const { x, y, username } = data;
const player = new PlayerShape(id, username, x, y);
callback({
status: true,
players: players,
player: player
});
console.log("Connected Player: ", player, "\n");
players.push(player);
});
socket.on('update', (data, callback) => {
const player = players.find((player) => data.id === player.id);
if (!!player) {
const {x, y} = data;
player.x = x;
player.y = y;
callback({
health: player.health,
food: player.food
});
}
});
socket.on('hit', (data) => {
const player = players.find((player) => data.id === player.id);
if (!!player) {
player.health -= data.damage;
}
});
socket.on('meatEating', (data) => {
meats = meats.filter(m => m.x !== data.x && m.y !== data.y);
const player = players.find((player) => data.id === player.id);
if (player) {
player.food = data.food;
}
});
socket.on('foodSpending', (data) => {
const player = players.find((player) => data.id === player.id);
if (player) {
player.food = data.food;
player.health = data.health;
}
})
socket.on('healthExtract', (data) => {
const player = players.find((player) => data.id === player.id);
if (player) {
player.health = data.health;
}
})
socket.on('disconnect', () => {
const disconnectedPlayer = players.find((player) => socket.id === player.id);
players = players.filter(p => p.id !== socket.id);
if (disconnectedPlayer) {
const newMeats = [];
const meatCount = disconnectedPlayer.food;
for (let i = 0; i < meatCount; i++) {
let dx = (Math.random() * 10);
dx *= Math.floor(Math.random() * 2) === 1 ? 1 : -1;
let dy = (Math.random() * 10);
dy *= Math.floor(Math.random() * 2) === 1 ? 1 : -1;
const x = disconnectedPlayer.x + dx;
const y = disconnectedPlayer.y + dy;
newMeats.push({ x, y });
}
meats.push(...newMeats);
socket.broadcast.emit('new-meat', newMeats);
}
});
socket.on('forceDisconnect', () => {
socket.disconnect();
});
socket.on('message', ({x, y, message}) => {
socket.broadcast.emit('message',
{
id: socket.id,
x, y, message
});
});
});
setInterval(() => {
if (day.number == 0) {
if (day.period == 'morning') {
day.period = 'night';
day.number = 60;
} else {
day.period = 'morning';
day.number = 120;
}
} else {
day.number--;
}
io.sockets.emit('dayCycle', day);
}, 1000);
setInterval(() => {
io.sockets.emit('heartbeat', { players, meats });
}, 33);
function meatCreate() {
for (let i=0; i < 150; i++) {
const x = Math.floor(Math.random() * 2000);
const y = Math.floor(Math.random() * 2000);
meats.push({ x, y });
}
return meats;
}