-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
73 lines (64 loc) · 1.6 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
var BoxScore = require('./box_score.js');
var cheerio = require('cheerio');
// Constructor
function Game(gid) {
this.gid = gid;
this.boxscore = null;
}
Game.prototype.get_num_innings = function() {
return new Promise((resolve, reject) => {
this.set_boxscore().then(function() {
if (this.boxscore.linescore) {
resolve(this.boxscore.linescore.innings.length);
}
else {
resolve(0);
}
});
});
};
// Returns a BoxScore object representing the boxscore for this game
Game.prototype.set_boxscore = function() {
return new Promise((resolve, reject) => {
if (!this.boxscore) {
var box = new BoxScore();
box.load_from_game(this).then(function() {
this.boxscore = box;
resolve();
});
}
else {
resolve();
}
});
};
// export the class
module.exports = {
Game,
// Returns an array of Game objects for each game for the specified day
find_by_date : function(year, month, day, gamesPageData) {
// begin
var games = [];
//games_page = GamedayFetcher.fetch_games_page(year, month, day)
if (gamesPageData) {
$ = cheerio.load(gamesPageData);
var a = $('ul').html();
$ = cheerio.load(a);
$('a').each(function(i, elem) {
if ($(this).html().includes('gid')) {
var str = $(this).html();
var gid = str.substring(5, str.length-1);
game = new Game(gid);
games.push(game);
}
});
}
else {
console.log("Could not read games page for " + year + ", " + month + ", " + day + ".");
}
return games;
/* rescue
puts "No games data found for #{year}, #{month}, #{day}."
end*/
}
}