/
adjudicate.js
90 lines (77 loc) 路 3.53 KB
/
adjudicate.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
'use strict';
module.exports = {
name: 'adjudicate',
process: function(job, done) {
var Promise = require('bluebird'),
db = require('./server/db'),
core = require('../cores/index'),
winston = require('winston'),
path = require('path'),
seekrits = require('nconf')
.file('custom', path.join(process.cwd(), 'server/config/local.env.json'))
.file('default', path.join(process.cwd(), 'server/config/local.env.sample.json')),
mailer = require('../mailer/mailer'),
gameID = job.data.gameID,
// judgePath = path.join(seekrits.get('judgePath'), 'diplomacy-godip'),
variant,
game,
phase;
winston.log('Starting adjudication job', { gameID: gameID });
db.bookshelf.transaction(function(t) {
core.game.getAsync(gameID, t) // Fetches the game in question.
.then(function(_game) {
game = _game;
variant = core.variant.get(game.get('variant'));
// Not everyone is ready. Handling this situation deserves its own block.
// FIXME: Drop 'false' when ignoreLateOrders is implemented.
if (false && !game.get('ignoreLateOrders') && !game.isEverybodyReady()) { // eslint-disable-line
handleLatePhase();
throw new Error('Not adjudicating: some players are not ready');
}
return core.phase.get(gameID, null, t);
})
.then(function(_phase) {
phase = _phase;
core.phase.adjudicatePhase(variant, game, phase, t);
})
// Notify participants.
.then(function(nextPhase) {
var sendOneAsync = Promise.promisify(mailer.sendOne);
return Promise.map(game.related('players'), function(junk, p) {
var player = game.related('players').at(p),
emailOptions = {
gameName: game.get('name'),
gameURL: path.join(seekrits.get('domain'), 'games', game.get('id')),
subject: '[' + game.get('name') + '] ' + phase.get('season') + ' ' + phase.get('year') + ' has been adjudicated',
deadline: phase.get('deadline'),
phase: phase.get('season'),
year: phase.get('year'),
nextPhase: nextPhase.getNextPhaseSeason(variant),
nextYear: nextPhase.getNextPhaseYear(variant)
};
core.user.get(player.pivot.get('user_id'))
.then(function(user) {
emailOptions.email = user.get('email');
return sendOneAsync('adjudication', emailOptions);
});
});
})
// Resets ready flag to false for all players.
.then(function() {
return core.game.resetAllReadyFlags(game);
})
.then(function() {
return core.game.getAsync(gameID, t);
})
.catch(function(err) {
winston.error(err);
return done(err);
});
})
.then(function() {
return done(null, { game: game.toJSON({ obfuscate: true }), phase: phase.toJSON({ obfuscate: false }) });
});
}
};
function handleLatePhase() {
}