-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
186 lines (165 loc) · 5.81 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import WebSocket from 'ws';
import { send, fillZeros } from './src/utils';
import { GameMap } from './src/map';
import { TILES } from './src/tiles'
import { findStartingCoordinates, createMap } from './src/map-gen';
// Thank you https://nameberry.com/list/45/Unisex-Baby-Names?all=1
const names = ['London', 'Zion', 'Murphy', 'Salem', 'Taylen', 'Angel', 'Brett', 'Kylar', 'Jaziah', 'Lake', 'Reilly', 'Emery', 'Campbell', 'Avery', 'Scout', 'Ramsey', 'Rory', 'Micah', 'Alexis', 'River', 'Armani', 'Dakotah', 'Kennedy', 'Azariah', 'Sam', 'Phoenix', 'Jess', 'Brighton', 'Justice', 'Jaidyn', 'Parker', 'Charlie', 'Valentine', 'Rio', 'Robin', 'Casey', 'Dylan', 'Kylin', 'True', 'Keegan', 'Royal', 'Kendall', 'Sasha', 'Hayden', 'Dakota', 'Gentry', 'Austen', 'Perry', 'Drew', 'Riley', 'Quinn', 'Eastyn', 'Jael', 'Toby', 'Dominique', 'Lane', 'Reagan', 'Timber', 'Lennon', 'Brady', 'Jackie', 'Gray', 'Denver', 'Payson', 'Skyler', 'Honor', 'Payton', 'Morgan', 'Paxton', 'Dana', 'Sage', 'Cypress', 'Alex', 'Timber', 'Indiana', 'Ellery', 'Landry', 'Sky', 'Clarke', 'Harper', 'Sidney', 'Jordan', 'Teagan', 'Jaden', 'Reese', 'Storm', 'Amen', 'Frankie', 'Oakley', 'Marlo', 'Finley', 'Ocean', 'Sawyer', 'Rowan', 'Jazz', 'Bailey', 'Emerson', 'Samar', 'Harley', 'Devon', 'Ryley', 'Flynn', 'Yael', 'Jalen', 'Nikita', 'Jules', 'Cameron', 'Ellington', 'Taylor', 'Hollis'];
const getName = i => names[i % names.length];
let index = 0;
const players = {};
const MAP_BASE = 12;
const MAP_ITER = 3;
const MAP_SIZE = MAP_BASE * Math.pow(2, MAP_ITER);
const MAP_LAND_PROB = 0.3;
const MAP_SMOOTHNESS = 5;
const T_SIZE = 16;
const D_SIZE = 64;
const MAP_OBJECTS = {
'apple_tree_bottom': {
'rules': {
'apple_tree_top': [-1, 0], // y, x
},
'prob': 0.05,
},
'tree_bottom': {
'rules': {
'tree_top': [-1, 0], // y, x
},
'prob': 0.25,
},
'yellow_flower': {
'prob': 0.1,
},
'red_flower': {
'prob': 0.03,
},
'white_flower': {
'prob': 0.02,
},
'stump': {
'prob': 0.01,
},
};
const COLLIDER_OBJECTS = [
'tree_bottom', 'apple_tree_bottom', 'stump'
];
const [layers, islands, numIslands] = createMap('land', 'water', MAP_BASE, MAP_LAND_PROB, MAP_ITER, MAP_SMOOTHNESS, MAP_OBJECTS);
const map = new GameMap(MAP_SIZE, MAP_SIZE, T_SIZE, D_SIZE, layers, islands, numIslands);
function spawnTrees(map) {
const treeProbability = 0.005;
const rows = map.rows;
const cols = map.cols;
let layer1 = fillZeros(map.cols,map.rows);
let layer2 = fillZeros(map.cols, map.rows);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (map.getTile(0,j,i) === TILES['land'] && map.getTile(1, j, i) === 0) {
if (Math.random() < treeProbability) {
layer1[i][j] = 1;
layer2[i][j] = 1;
map.setTile(1, j, i, TILES['tree_bottom']);
if (i > 0) {
map.setTile(2, j, i-1, TILES['tree_top']);
}
}
}
}
}
return {layer1, layer2};
}
const wss = new WebSocket.Server({ port: 5000 });
function noop() {}
function heartbeat() {
this.isAlive = true;
}
// Broadcast to all.
wss.broadcast = (type, data) => {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
send(client, type, data);
}
});
};
// Broadcast to everyone else.
wss.broadcastOthers = (socket, type, data) => {
wss.clients.forEach(client => {
if (client !== socket && client.readyState === WebSocket.OPEN) {
send(client, type, data);
}
});
}
wss.on('connection', socket => {
socket.isAlive = true;
socket.on('pong', heartbeat);
socket.id = index++;
const name = getName(socket.id);
send(socket, 'map', map);
send(socket, 'players', players);
const spawnLoc = findStartingCoordinates(layers, MAP_SIZE, 'land', COLLIDER_OBJECTS);
send(socket, 'self', { id: socket.id, name, pos: spawnLoc });
wss.broadcastOthers(socket, 'info', `${name} joined!`);
socket.on('message', message => {
const { type, data } = JSON.parse(message);
switch (type) {
case 'newPlayer': {
const player = data;
players[socket.id] = player;
wss.broadcastOthers(socket, 'newPlayer', { id: socket.id, player: player });
break;
}
case 'playerMoved': {
const { x, y, dir, moving, dirOffset } = data;
const player = players[socket.id];
player.x = x;
player.y = y;
player.dir = dir;
player.moving = moving;
player.dirOffset = dirOffset;
wss.broadcastOthers(socket, 'playerMoved', { id: socket.id, x, y, dir, moving, dirOffset });
break;
}
case 'playerPet': {
const { pet } = data;
const player = players[socket.id];
player.pet = pet;
wss.broadcastOthers(socket, 'playerPet', { id: socket.id, pet });
break;
}
case 'tileUpdate': {
const { layer, col, row, type } = data;
map.setTile(layer, col, row, type);
wss.broadcastOthers(socket, 'tileUpdate', data);
break;
}
case 'chatMessage': {
wss.broadcastOthers(socket, 'chatMessage', { id: socket.id, text: data });
break;
}
}
});
socket.on('close', () => {
delete players[socket.id];
wss.broadcastOthers(socket, 'deletePlayer', socket.id);
wss.broadcastOthers(socket, 'info', `${name} disconnected.`);
});
});
// Periodically spawn trees on the map
setInterval(() => {
// Only spawn trees if there are connected players
if (wss.clients.size > 0) {
// spawnTrees() returns two 2D arrays containing the object layers of the map
const layers = spawnTrees(map);
wss.broadcast('spawnTrees', layers);
}
}, 90000);
// Detect and close broken connections
setInterval(() => {
wss.clients.forEach(client => {
if (client.isAlive === false) {
return client.terminate();
}
client.isAlive = false;
client.ping(noop);
});
}, 30000);