-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
206 lines (183 loc) · 7.75 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');
var port = process.env.PORT || 8080;
var server = http.createServer(handleRequest);
var theWave = createWave();
server.listen(port);
console.log('Our app is running on http://localhost:' + port);
function handleRequest(req, res) {
// What did we request?
var pathname = req.url;
// If blank let's ask for index.html
if (pathname == '/') {
pathname = '/index.html';
}
var ext = path.extname(pathname);
var typeExt = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css'
};
// What is it? Default to plain text
var contentType = typeExt[ext] || 'text/plain';
// User file system module
fs.readFile(__dirname + pathname,
// Callback function for reading
function(err, data) {
// if there is an error
if (err) {
res.writeHead(500);
return res.end('Error loading ' + pathname);
}
// Otherwise, send the data, the contents of the file
res.writeHead(200, {
'Content-Type': contentType
});
res.end(data);
}
);
}
function createWave() {
var wave = {};
// Create the deepOne for the wave
wave.deepOne = createDeepOne();
wave.oldOnes = [];
wave.servitors = [];
wave.minions = [];
// Create the old ones of the wave 1-3
var oldOnesCount = Math.floor((Math.random() * 3) + 1);
for (var i = 0; i < oldOnesCount; i++) {
var newOldOne = createOldOne();
wave.oldOnes.push(newOldOne);
}
// Create the servitors of the wave 3-10
var servitorsCount = Math.floor((Math.random() * 10) + 3);
for (var i = 0; i < servitorsCount; i++) {
var newServitor = createServitor();
wave.servitors.push(newServitor);
}
// Create the minions of the wave 20-40
var minionsCount = Math.floor((Math.random() * 40) + 20);
for (var i = 0; i < minionsCount; i++) {
var newMinion = createMinion();
wave.minions.push(newMinion);
}
return wave;
}
function createDeepOne() {
// Create the name of the deep one
var mos = ['ch', 'chu', 'chuch', 'chucp', 'th', 'thu', 'thul', 'lh', 'lhu', 'co', 'chio', 'chor', 'cher', 'mhu', 'mu', 'klath', 'kloth', 'klotha', 'klotho', 'plothorc', 'bruthap'];
var vowels = ['a', 'e', 'i', 'o', 'u', 'á', 'é', 'í', 'ó', 'ú', 'ça', 'çe', 'çí', 'çó', 'çú', 'ä', 'ë', 'ï', 'ö', 'ü'];
var consonants = ['b', 'c', 'doé', 'có', '¨f', '`p', 'lo', 'ch', 'gl', 'flą', 'flatar', 'mol', 'tr', 'ad', 'dd'];
var nick = mos[Math.floor((Math.random() * (mos.length - 1)) + 0)] + vowels[Math.floor((Math.random() * (vowels.length - 1)) + 0)] + consonants[Math.floor((Math.random() * (consonants.length - 1)) + 0)]
var deepOne = {
nick: nick,
servitors: [],
minions: [],
lifePoints: Math.floor((Math.random() * 20000) + 10000)
};
// A deep one can invoque multiple servitors and minions
// Create the servitors of the wave 1-2
var servitorsCount = Math.floor((Math.random() * 2) + 1);
for (var i = 0; i < servitorsCount; i++) {
var newServitor = createServitor();
deepOne.servitors.push(newServitor);
}
// Create the minions of the deep one 10-20
var minionsCount = Math.floor((Math.random() * 20) + 10);
for (var i = 0; i < minionsCount; i++) {
var newMinion = createMinion();
deepOne.minions.push(newMinion);
}
return deepOne;
}
function createOldOne() {
var mos = ['mh', 'mhu', 'mhumh', 'mhump', 'th', 'thu', 'thul', 'lh', 'lhu', 'mo', 'mhio', 'mhor', 'mher', 'mhu', 'mu', 'klath', 'kloth', 'klotha', 'klotho', 'plothorm', 'brsuthap'];
var vowels = ['ae', 'ei', 'io', 'ou', 'ua', 'áe', 'éi', 'ío', 'óu', 'aú', 'çae', 'çei', 'çío', 'çóu', 'açú', 'iä', 'ië', 'iï', 'iö', 'üi'];
var consonants = ['b', 'c', 'doé', 'có', '¨f', '`p', 'lo', 'ch', 'gl', 'flą', 'flatar', 'mol', 'tr', 'ad', 'dd'];
var nick = mos[Math.floor((Math.random() * (mos.length - 1)) + 0)] + vowels[Math.floor((Math.random() * (vowels.length - 1)) + 0)] + consonants[Math.floor((Math.random() * (consonants.length - 1)) + 0)]
var oldOne = {
nick: nick,
minions: [],
lifePoints: Math.floor((Math.random() * 10000) + 5000)
};
// Create the minions of the old one 1-15
var minionsCount = Math.floor((Math.random() * 15) + 1);
for (var i = 0; i < minionsCount; i++) {
var newMinion = createMinion();
oldOne.minions.push(newMinion);
}
return oldOne;
}
function createServitor() {
var mos = ['h', 'hu', 'huh', 'hup', 'th', 'thu', 'thul', 'lh', 'lhu', 'o', 'hio', 'hor', 'her', 'hu', 'u', 'klath', 'kloth', 'klotha', 'klotho', 'plothor', 'brsuthap'];
var vowels = ['ae', 'ei', 'io', 'ou', 'ua', 'áe', 'éi', 'ío', 'óu', 'aú', 'çae', 'çei', 'çío', 'çóu', 'açú', 'iä', 'ië', 'iï', 'iö', 'üi'];
var consonants = ['b', 'c', 'doé', 'có', '¨f', '`p', 'lo', 'ch', 'gl', 'flą', 'flatar', 'ol', 'tr', 'ad', 'dd'];
var nick = mos[Math.floor((Math.random() * (mos.length - 1)) + 0)] + vowels[Math.floor((Math.random() * (vowels.length - 1)) + 0)] + consonants[Math.floor((Math.random() * (consonants.length - 1)) + 0)];
var servitor = {
nick: nick,
minions: [],
lifePoints: Math.floor((Math.random() * 2000) + 1000)
};
// Create the minions of the servitor one 1-15
var minionsCount = Math.floor((Math.random() * 15) + 1);
for (var i = 0; i < minionsCount; i++) {
var newMinion = createMinion();
servitor.minions.push(newMinion);
}
return servitor;
}
function createMinion() {
var mos = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];
var vowels = ['ae', 'ei', 'io', 'ou', 'ua', 'áe', 'éi', 'ío', 'óu', 'aú', 'çae', 'çei', 'çío', 'çóu', 'açú', 'iä', 'ië', 'iï', 'iö', 'üi'];
var nick = mos[Math.floor((Math.random() * (mos.length - 1)) + 0)] + vowels[Math.floor((Math.random() * (vowels.length - 1)) + 0)] + mos[Math.floor((Math.random() * (mos.length - 1)) + 0)];
var minion = {
nick: nick,
lifePoints: Math.floor((Math.random() * 150) + 50)
};
return minion;
}
// WebSocket Portion
// WebSockets work with the HTTP server
var io = require('socket.io').listen(server);
// Register a callback function to run when we have an individual connection
// This is run for each individual user that connects
io.sockets.on('connect',
// We are given a websocket object in our function
function(socket) {
console.log("We have a new client: " + socket.id);
socket.broadcast.emit('wave', {
wave: theWave
});
socket.on('waveData', function(){
io.emit('wave', {
wave: theWave
});
});
// When this user emits, client side: socket.emit('otherevent',some data);
socket.on('playerData',
function(data) {
// Send it to all other clients
socket.broadcast.emit('playerData', {
direction: data.direction,
shooting: data.shooting,
nick: data.nick,
death: data.death,
id: data.id,
position: data.position,
worldPosition: data.worldPosition,
socketId: socket.id,
lifePoints: data.lifePoints
});
}
);
socket.on('disconnect', function() {
console.log("Client " + socket.id + " has disconnected");
socket.broadcast.emit('disconnection', {
socketId: socket.id
});
});
}
);