aboutsummaryrefslogtreecommitdiffstats
path: root/rogue-server.js
blob: 59b6c21692c365120acd481aa8405257b8aa7c1b (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Server for rogue.js
 *
 * Matt Kohls
 * 2019
 * GPL3
 */

/** General game bits **/

function getBonus(value) {
	if(value % 2 == 1) {
		--value;
	}
	return (value - 10) / 2;
}

function genStat() {
	var value = 0;
	for(var i = 0; i < 3; i++) {
		value += Math.floor(Math.random() * 6);
	}
	return value;
}

class Location {
	constructor(x, y, floor) {
		this.x = x;
		this.y = y;
		this.floor = floor;
	}

	get x() { return this.x; }
	set x(value) { this.x = value; }
	get y() { return this.y; }
	set y(value) { this.y = value; }
	get floor() { return this.floor; }
	set floor(value) { this.floor = value; }
}

class Item {
	constructor(bonus, type) {
		this.bonus = bonus;
		this.type = type;
	}

	get name() {
		var name = this.type + " ";
		if(this.bonus > 0) {
			name = name + "+" + this.bonus;
		} else if(this.bonus < 0) {
			name = name + "-" +this. bonus;
		}
		return name;
	}

	get bonus() {
		return this.bonus;
	}

	get type() {
		return this.type;
	}
}

class Action {
	constructor(type, message) {
		this.type = type;
		this.message = message;
	}

	get type() {
		return this.type;
	}

	get message() {
		return this.message;
	}
}
	

class Mob {
	constuctor(strength, dexterity, constitution, intelligence, wisdom, level, location) {
		this.strength = strength;
		this.dexterity = dexterity;
		this.constitution = constitution;
		this.intelligence = intelligence;
		this.wisdom = wisdom;
		this.level = level;
		this.location = location;
		this.xp = 0;
		this.hp = level * getBonus(constitution) + Math.floor(Math.random() * 6) * level;
		this.hpMax = this.hp;
		this.potions = 0;
		this.armor = null;
		this.weapon = null;
		this.staff = null;
	}

	addXP(amount) {
		this.xp += amount;
		if(this.xp >= 100) {
			this.xp = 0;
			this.level += 1;
			this.hpMax += getBonus(this.constitution) + Math.floor(Math.random() * 6);
		}
	}

	drinkPotion() {
		if(this.potions > 0) {
			this.potions -= 1;
			var gained = Math.floor(Math.random() * 6) * this.level;
			if(this.hp + gained > this.hpMax) {
				this.hp = this.hpMax;
				return "You feel brand new";
			} else {
				this.hp += gained;
				return "That patched you up a bit";
			}
		} else {
			return "No potions to drink";
		}
	}

	get inititive() {
		return getBonus(this.dexterity) + this.level + 10;
	}

	get ac() {
		if(armor != null) {
			return 10 + getBonus(this.dexterity) + this.armor.bonus;
		}
		return 10 + getBonus(this.dexterity);
	}

	get spellResist() {
		if(staff != null) {
			return 10 + getBonus(this.wisdom) + this.staff.bonus;
		}
		return 10 + getBonus(this.wisdom);
	}

	get hp() {
		return this.hp;
	}

	set hp(value) {
		this.hp = value;
	}

	get location() {
		return this.location;
	}

	set location(value) {
		this.location = value;
	}

	set action(value) {
		this.action = value;
	}

	get action() {
		return this.action;
	}

	attack(type) {
		if(type == "weapon") {
			if(this.weapon != null) {
				return getBonus(this.strength) + this.weapon.bonus + Math.floor(Math.random() * 20);
			}
			return getBonus(this.strength) + Math.floor(Math.random() * 20);
		} else if(type == "staff") {
			if(this.staff != null) {
				return getBonus(this.intelligence) + this.staff.bonus + Math.floor(Math.random() * 20);
			}
			return getBonus(this.intelligence) + Math.floor(Math.random() * 20);
		}
	}

	damage(type) {
		if(type == "weapon") {
			if(this.weapon != null) {
				return getBonus(this.strength) + this.weapon.bonus + Math.floor(Math.random() * 6);
			}
			return getBonus(this.strength) + Math.floor(Math.random() * 6);
		} else if(type == "staff") {
			if(this.staff != null) {
				return getBonus(this.intelligence) + this.staff.bonus + Math.floor(Math.random() * 6);
			}
			return getBonus(this.intelligence) + Math.floor(Math.random() * 6);
		}
	}

	drop(item) {
		if(item.type == "weapon") {
			var temp = this.weapon;
			this.weapon = null;
			return temp;
		} else if(item.type == "staff") {
			var temp = this.staff;
			this.staff = null;
			return temp;
		} else if(item.type == "armor") {
			var temp = this.armor;
			this.armor = null;
			return temp;
		}
	}

	pickUp(item) {
		if(item.type == "potion") {
			this.potions += 1;
			return null;
		} else if(item.type == "weapon") {
			this.drop(item);
			this.weapon = item;
			return "Aquired: " + item.name;
		} else if(item.type == "staff") {
			this.drop(item);
			this.staff = item;
			return "Aquired: " + item.name;
		} else if(item.type == "armor") {
			this.drop(item);
			this.armor = item;
			return "Aquired: " + item.name;
		}
	}
}

/** Turn generation **/

var mobs = [];
var dead = [];
const floors = require('./floors.json');

/**
 * Generates the turn order by placing the index of the mob into an array.
 * 
 */
function getInititive() {
	var init = new Array();
	for(var i = 0; i < 20; i++) {
		init[i] = new Array();
	}
	for(var i = 0; i < mobs.length; i++) {
		var place = mobs[i].inititive;
		if(place > 19) {
			place = 19;
		} else if(place < 0) {
			place = 0;
		}
		init[place].push(i);
	}
	return init
}

function attack(mobA, mobB, type) {
	var attack = mobs[mobA].attack(type);
	var defence;
	if(type == "staff") {
		defence = mobs[mobB].spellResist;
	} else if(type == "weapon") {
		defence = mobs[mobB].ac;
	}
	if(attack >= defence) {
		mobs[mobB].hp = mobs[mobB].hp - mobs[mobA].damage(type);
		var xp = 1;
		if(mobs[mobB].hp < 0 && dead.includes(mobB) == false) {
			dead.push(mobB);
			xp = 10;
		}
		mobs[mobA].addXP(xp);
	}
}

function move(mob, dir) {
	var location = mobs[mob].location;
	switch(dir) {
	case 'w':
		location.x = location.x - 1;
		break;
	case 'a':
		location.y = location.y - 1;
		break;
	case 's':
		location.y = location.y + 1;
		break;
	case 'd':
		location.x = location.x + 1;
		break;
	case '.':
		var symbol = floors[location.floor].charAt(location.y * 40 + location.x);
		if(symbol == '>') {
			location.floor = location.floor + 1;
			if(location.floor > floors.length - 1) {
				return "Already at bottom floor";
			}
			mobs[mob].location = location;
			return "Go down the stairs";
		} else if(symbol == '<') {
			location.floor = location.floor - 1;
			if(location.floor < 0) {
				return "Already at top floor";
			}
			mobs[mob].location = location;
			return "Go up the stairs";
		}
		break;
	default:
		break;
	}
	if(location.x < 0 || location.x > 99 || location.y < 0 || location.y > 39) {
		return "Hit a wall";
	}
	if(floors[location.floor].charAt(location.y * 40 + location.x) == '*') {
		return "Hit a wall";
	}
	for(var i = 0; i < mobs.length; i++) {
		if(mobs[i].location.floor == location.floor) {
			if(mobs[i].location.x == location.x && mobs[i].location.y == location.y) {
				return attack(mob, i, "weapon");
			}
		}
	}
	mobs[mob].location = location;
}

function cast(mob, dir) {

}

function performActions(init) {
	for(var i = init.length - 1; i > -1; i--) {
		for(var j = 0; j < init[i].length; j++) {
			var mob = init[i][j];
			var message = "";
			if(mobs[mob].hp > 0) {
				switch(mobs[mob].action.type) {
				case 'wait':
					message = "You sit";
					break;
				case 'move':
					move(mob, mobs[mob].action.message);
					break;
				case 'cast':
					cast(mob, mobs[mob].action.message);
					break;
				case 'drink':
					message = mobs[mob].drinkPotion();
					break;
				case 'pick':
					//message = mobs[init[i][j]].pickUp();
					break;
				case 'drop':
					message = "Item dropped";
					mobs[mob].drop(mobs[mob].action.message);
				default:
					break;
				}
			}
			mobs[mob].action = new Action('done', message);
		}
	}
}

function sendResults() {

}


function genTurn() {
	dead = new Array();

	var init = getInititive();
	performActions(init);
	sendResults();

}


/** Websocket stuff **/

const http = require('http');
const WebSocket = require('ws');
const uuid = require('uuid/v4');

// Used to keep track of all the players and their connection
const players = new Map();

const httpServer = http.createServer();
const wsServer = new WebSocket.Server({ noServer: true });

wsServer.on('connection', function connection(ws, request) {
	players.set(ws, uuid());
	console.log(new Date().toUTCString() + ' | ' + players.get(ws) + ' joins');

	ws.on('open', function join() {
		// Do things on an open, doesn't seem to get hit though
	});
	
	ws.on('message', function message(msg) {
		// Do thing with message from client
		console.log(new Date().toUTCString() + ' | ' + players.get(ws) + ' says \"' + msg + '\"');
	});

	ws.on('close', function leave() {
		console.log(new Date().toUTCString() + ' | ' + players.get(ws) + ' leaves');
		clients.delete(ws);
	});
});

httpServer.on('upgrade', function upgrade(request, socket, head) {
	wsServer.handleUpgrade(request, socket, head, function done(ws) {
		wsServer.emit('connection', ws, request);
	});
});

httpServer.listen(8080);