-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
96 lines (81 loc) · 2.85 KB
/
game.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
var games = {};
var game_types = require('./game_types.js').game_types;
var io = require('./server.js').io;
function gid() {
return 'xxxxxx'.replace(/[xy]/g, function(c) {
var v = Math.random()*16|0;
return v.toString(16);
});
}
function verify_settings(settings) {
var game_type_info = game_types[settings.id].info;
if( game_type_info.players.length === undefined ) {
if( 'players_count' in settings )
return 'invalid_player_count'; //return 'Player count provided while not allowed';
} else {
if( game_type_info.players.indexOf(settings.players_count|0) == -1 )
return 'invalid_player_count'; //return 'Illegal player count';
}
for( var i = 0; i < game_type_info.settings.length; i++ ) {
var setting = game_type_info.settings[i];
if( ! (setting.name in settings) )
return 'Setting `'+setting.name+'` required';
var value = settings[setting.name];
if( 'options' in setting ) {
if( setting.options.indexOf(value) == -1 )
return 'Setting `'+setting.name+'` has illegal value';
} else if( setting.value == 'int' ) {
if( isNaN(value) )
return 'Setting `'+setting.name+'` has illegal value';
value |= 0;
if( setting.value_ex ) {
if( setting.value_ex.min && setting.value_ex.min > value )
return 'Setting `'+setting.name+'` is too small';
if( setting.value_ex.max && setting.value_ex.max < value )
return 'Setting `'+setting.name+'` is too big';
}
}
}
}
function Game(settings, owner) {
do {
this.id = gid();
} while( this.id in games );
this.type = settings.id;
this.owner = owner;
this.settings = settings;
this.status = 'lobby';
this.players = [];
var players_count = settings.players_count || game_types[this.type].info.players;
for( var i = 0; i < players_count; i++ )
this.players[i] = null;
games[this.id] = this;
}
Game.prototype.serializeToLobby = function() {
return {
id: this.id,
type: this.type,
status: this.status,
players: this.players.map( function(u) { return u ? u.username : null; } )
};
};
Game.prototype.serializeToPregame = function() {
return {
players: this.players.map( function(u) { return u ? u.username : null; } )
};
}
Game.prototype.room = function() { return 'game:'+this.id; }
Game.prototype.game_type = function() { return game_types[this.type]; }
Game.prototype.changeStatus = function(status) {
this.status = status;
io.to('index').emit('Index.update', [this.serializeToLobby()] );
}
Game.prototype.end = function() {
this.changeStatus('ended');
io.to(this.room()).emit('Game.ended');
}
module.exports = {
Game: Game,
games: games,
verify_settings: verify_settings
};