-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommands.js
95 lines (87 loc) · 2.32 KB
/
commands.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
var util = require('util');
function Command(name, f) {
this.name = name;
this.f = f;
}
Command.prototype.tryRun = function(line, g) {
var matches = new RegExp("^\s*"+this.name).test(line);
if(matches)
this.f(g);
return matches;
};
function PlayerCommand(name, f) {
Command.call(this, name, f);
}
util.inherits(PlayerCommand, Command);
PlayerCommand.prototype.tryRun = function(line, g) {
var matches = new RegExp("^\s*"+this.name + ' (.+)').exec(line);
if(matches) {
var player = g.players[matches[1]]
this.f(g, player);
}
return matches != null;
};
function NumberCommand(name, f) {
Command.call(this, name, f);
}
util.inherits(NumberCommand, Command);
NumberCommand.prototype.tryRun = function(line, g) {
var matches = new RegExp("^\s*"+this.name + ' (\\d+)').exec(line);
console.log(matches);
if(matches) {
var n = +matches[1];
this.f(g, n);
}
return matches != null;
};
var list = exports.list = [
new Command('players', function(g) {
util.log(Object.values(g.players).pluck('coloredName').join(', '));
}),
new Command('game', function(g) {
console.log(g);
}),
new Command('mass', function(g) {
console.log('Total mass of the universe: '+g.world.totalMass);
}),
new Command('score', function(g, cli) {
var width = cli ? cli.columns : 50;
var perMass = width / g.world.totalMass;
var bar = "";
var barLength = 0;
var scoreSoFar = 0;
Object.forEach(g.players, function(g, p) {
if(p.snake) {
var score = p.snake.mass;
scoreSoFar += score;
var thisBar = "";
while(barLength + thisBar.length < scoreSoFar * perMass)
thisBar += '█';
barLength += thisBar.length;
bar += thisBar.colored(p.color);
}
});
console.log(bar);
console.log(Object.values(g.players).pluck('coloredName').join(', '));
}),
new NumberCommand('balls', function(g, n) {
g.generateBalls(n);
}),
new PlayerCommand('kick', function(g, player) {
player && player.disconnect();
}),
new PlayerCommand('kill', function(g, player) {
player && player.kill();
}),
new PlayerCommand('spawn', function(g, player) {
player && !player.snake && player.spawnSnake(g.world);
}),
new PlayerCommand('help', function(g, player) {
player && player.snake && (player.snake.maxMass *= 2);
})
]
exports.tryRun = function(line, game, cli) {
return list.some(function(c) {
return c.tryRun(line, game, cli);
});
}