-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLobbyMutator.js
72 lines (69 loc) · 1.84 KB
/
LobbyMutator.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
var superagent = require('superagent');
var LobbyStore = require('./LobbyStore');
// cb is (err, body)
var callLobbyApi = function(lobby_id, method, params, cb) {
superagent.post('/api/lobby/' + lobby_id + '/' + method)
.send(params)
.set('Accept', 'application/json')
.end(function(res) {
var error = null;
if (!res.ok) {
console.log('Error adding calling lobby ' + method + ': ', res.text);
error = res.text;
}
cb(error, res.body);
});
};
var LobbyMutator = function (session, lobby_id) {
this.session_ = session;
this.lobbyID_ = lobby_id;
};
// cb(err)
LobbyMutator.prototype.setGod = function(god_name, cb) {
cb = cb || function() {};
callLobbyApi(this.lobbyID_, 'set_god', {god_name: god_name}, function(err, body) {
if (err) {
cb(err);
return;
}
this.session_.LobbyStore().syncLobby(body.lobbyID, body.sequenceID, function(err) {
cb(err);
});
}.bind(this));
};
// cb(err)
LobbyMutator.prototype.addBot = function(cb) {
cb = cb || function() {};
callLobbyApi(this.lobbyID_, 'add_bot', {}, function(err, body) {
if (err) {
cb(err);
return;
}
this.session_.LobbyStore().syncLobby(body.lobbyID, body.sequenceID, function(err) {
cb(err);
});
}.bind(this));
};
// cb(err, gameID)
LobbyMutator.prototype.startGame = function(cb) {
cb = cb || function() {};
callLobbyApi(this.lobbyID_, 'start', {}, function(err, body) {
cb(err, body && body.gameID);
});
};
// cb(err)
LobbyMutator.prototype.joinGame = function(cb) {
cb = cb || function() {};
callLobbyApi(
this.lobbyID_,
'join',
{user_id: this.session_.getUser().id},
function(err, body) {
if (err) {
cb(err);
return;
}
this.session_.LobbyStore().syncLobby(body.lobbyID, body.sequenceID, cb);
}.bind(this));
};
module.exports = LobbyMutator;