-
Notifications
You must be signed in to change notification settings - Fork 1
/
importer.js
178 lines (146 loc) · 5.07 KB
/
importer.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var assert = require('assert');
var async = require('async');
var db = require('./db');
var parseMatches = require('./parser');
// Constants
var POINTS_WIN = 3;
var POINTS_DRAW = 1;
var POINTS_LOSS = 0;
exports.leagues = function (files, callback) {
var self = this;
assert.ok(Array.isArray(files), 'files should be an array');
assert.ok(typeof(callback) === 'function', 'callback should be a function');
var conn = db.connect(function (error) {
if (error) { return callback(error); }
async.each(files, function (file, callback) {
parseMatches(file, function (error, games) {
if (error) { return callback(error); }
var season = games[0].season;
var gamesCollection = conn.db.collection('games');
gamesCollection.insert(games, function (error) {
if (error) { return callback(error); }
self.tables(season, function (error) {
callback(error);
});
});
});
}, function (error) {
callback(error);
});
});
};
exports.tables = function (season, callback) {
assert.ok(season, 'season is required');
assert.ok(typeof(callback) === 'function', 'callback should be a function');
var splitSeason = season.split('-');
var startYear = splitSeason[0];
var endYear = splitSeason[1];
var startOfSeason = new Date(startYear + '-07-01');
var midOfSeason = new Date(startYear + '-12-31');
var endOfSeason = new Date(endYear + '-06-30');
var conn = db.connect(function (error) {
if (error) { return callback(error); }
var gamesCollection = conn.db.collection('games');
var tablesCollection = conn.db.collection('tables');
// Save mid-season snapshot of the table
saveTable(gamesCollection, tablesCollection, startOfSeason, midOfSeason, function (error) {
if (error) { return callback(error); }
// Save end-of-season snapshot of the table
saveTable(gamesCollection, tablesCollection, startOfSeason, endOfSeason, function (error) {
callback(error);
});
});
});
};
var saveTable = function (gamesCollection, tablesCollection, start, end, callback) {
gamesCollection.find({
date: { $gt: start, $lte: end },
}).toArray(function (error, games) {
if (error) { return callback(error); }
var table = generateLeagueTable(games);
tablesCollection.insert(table, function (error) {
callback(error);
});
});
};
// Calculate league table
var generateLeagueTable = function (games) {
var teams = {};
games.forEach(function (game) {
// Initialize teams if they don't exist
[ game.homeTeam, game.awayTeam ].forEach(function (team) {
if (!teams[team]) {
teams[team] = {
season: game.season,
name: team,
games: {
played: 0,
won: 0,
drawn: 0,
lost: 0,
points: 0,
},
goals: {
scored: 0,
conceded: 0,
diff: 0,
},
};
}
});
var homeWon = game.score.fullTime.home > game.score.fullTime.away;
var draw = game.score.fullTime.home === game.score.fullTime.away;
// Increment both teams' play count
teams[game.homeTeam].games.played += 1;
teams[game.awayTeam].games.played += 1;
// Set game result
if (homeWon) {
teams[game.homeTeam].games.won += 1;
teams[game.homeTeam].games.points += POINTS_WIN;
teams[game.awayTeam].games.lost += 1;
teams[game.awayTeam].games.points += POINTS_LOSS;
} else if (draw) {
teams[game.homeTeam].games.drawn += 1;
teams[game.homeTeam].games.points += POINTS_DRAW;
teams[game.awayTeam].games.drawn += 1;
teams[game.awayTeam].games.points += POINTS_DRAW;
} else {
teams[game.homeTeam].games.lost += 1;
teams[game.homeTeam].games.points += POINTS_LOSS;
teams[game.awayTeam].games.won += 1;
teams[game.awayTeam].games.points += POINTS_WIN;
}
// Set Goal Stats
teams[game.homeTeam].goals.scored += game.score.fullTime.home;
teams[game.homeTeam].goals.conceded += game.score.fullTime.away;
teams[game.homeTeam].goals.diff = teams[game.homeTeam].goals.scored - teams[game.homeTeam].goals.conceded;
teams[game.awayTeam].goals.scored += game.score.fullTime.away;
teams[game.awayTeam].goals.conceded += game.score.fullTime.home;
teams[game.awayTeam].goals.diff = teams[game.awayTeam].goals.scored - teams[game.awayTeam].goals.conceded;
});
// Sort teams in desired order
var table = Object.keys(teams).map(function(k) { return teams[k] });
// Teritary sort by alphabetical order of team name
table.sort(function (t1, t2) {
if (t2.name > t1.name) {
return -1;
} else if (t2.name < t1.name) {
return 1
} else {
return 0;
}
});
// Secondary sort by goal diff
table.sort(function (t1, t2) {
return t1.goals.diff - t2.goals.diff;
});
// Primary sort by points
table.sort(function (t1, t2) {
return t2.games.points - t1.games.points;
});
table = table.map(function (row, index) {
row.position = index + 1;
return row;
});
return table;
};