This repository was archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
130 lines (109 loc) · 4.16 KB
/
server.js
File metadata and controls
130 lines (109 loc) · 4.16 KB
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
var express = require('express'),
app = express(),
http = require('http'),
socketIo = require('socket.io');
// start webserver on port 8080
var server = http.createServer(app);
var io = socketIo.listen(server);
server.listen(8080);
// add directory with our static files
app.use(express.static(__dirname + '/public'));
console.log("Server running on 127.0.0.1:8080");
// array of all lines drawn
// 2000 x 2000
// characters are 5 x 5
var characterHistory = {};
var specials = {};
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var leaderboard = [];
function updateLeaderboard() {
full_ranking = [];
for (id in characterHistory) {
full_ranking.push(characterHistory[id]);
}
full_ranking.sort(compareScore);
leaderboard = (full_ranking.length >= 10) ? full_ranking.slice(0, 10) : full_ranking;
io.emit('update_leaderboard', leaderboard);
}
function compareScore(firstChar, secondChar) {
return secondChar.score - firstChar.score;
}
// event-handler for new incoming connections
io.on('connection', function (socket) {
// first send the history to the new client
socket.emit('update_characters', characterHistory);
// add handler for message type "draw_line".
socket.on('move_character', function (data) {
// add received line to history
if (data.id in characterHistory) {
characterHistory[data.id].x = data.pos.x;
characterHistory[data.id].y = data.pos.y;
characterHistory[data.id].angle = data.pos.angle;
characterHistory[data.id].name = data.name;
// send line to all clients
io.emit('update_characters', characterHistory);
io.emit('update_specials', specials);
}
});
// {id:ID, attack:{x: X, y: Y}, type:'A'}
socket.on('attack', function(data) {
var dead = [];
var gotSpecials = [];
var attackRadius = 40;
if (data.type == 'B') {
console.log(characterHistory[data.id].name + " used a special!");
attackRadius = 80;
}
for (var key in characterHistory) {
if (characterHistory.hasOwnProperty(key)) {
var character = characterHistory[key];
if ((Math.abs(character.x - data.attack.x) <= attackRadius) && (Math.abs(character.y - data.attack.y) <= attackRadius) && (data.id != key)) {
dead.push(key);
}
}
}
for (var skey in specials) {
if (specials.hasOwnProperty(skey)) {
var special = specials[skey];
if ((Math.abs(special.x - data.attack.x) <= attackRadius) && (Math.abs(special.y - data.attack.y) <= attackRadius)) {
gotSpecials.push(skey);
}
}
}
for (var i=0; i < dead.length; i++) {
socket.broadcast.to(dead[i]).emit( 'character_died', '');
}
if (gotSpecials.length > 0) {
socket.emit('got_special','');
}
for (var j=0; j < gotSpecials.length; j++) {
delete specials[gotSpecials[j]];
}
characterHistory[data.id].score += dead.length;
console.log(characterHistory[data.id].name + " has attacked and killed " + dead.length.toString() + " turtles.");
updateLeaderboard();
});
characterHistory[socket.id] = {x: Math.floor((Math.random()*2350)+25), y: Math.floor((Math.random()*1150)+25), score:0, angle:0, special:false};
socket.emit('init_character', {id: socket.id, pos: characterHistory[socket.id]});
characterHistory[socket.id] = {x: Math.floor((Math.random()*2350)+25), y: Math.floor((Math.random()*1150)+25), score:0, angle:0, special:false};
socket.emit('init_character', {id: socket.id, pos: characterHistory[socket.id], name: 'Name ' + socket.id});
io.emit('update_leaderboard', leaderboard);
socket.on('disconnect', function() {
delete characterHistory[socket.id];
});
});
function createSpecial() {
var sid = makeid();
if (Object.keys(specials).length < 5) {
specials[sid] = {x:Math.floor((Math.random()*2350)+25) , y: Math.floor((Math.random()*1150)+25)};
}
setTimeout(createSpecial, 10000);
}
createSpecial();