-
Notifications
You must be signed in to change notification settings - Fork 4
/
match.js
58 lines (49 loc) · 1.49 KB
/
match.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
const _ = require('lodash');
const announcers = require('./announcers');
class Match {
constructor() {
this.matchData = {
home_team: {goals: 0},
away_team: {goals: 0},
home_team_events: [],
away_team_events: []
};
this.live = false;
this.announce = announcers;
}
async update(matchData) {
const live = !['future', 'completed'].includes(matchData.status);
if (live !== this.live) {
if (live) {
this.live = true;
await this.announce.start(matchData);
} else {
this.live = false;
await this.announce.end(matchData);
}
}
if (matchData.home_team_events.length !== this.matchData.home_team_events.length ||
matchData.away_team_events.length !== this.matchData.away_team_events.length) {
await this.reportEvents(matchData);
}
if (matchData.home_team.goals !== this.matchData.home_team.goals ||
matchData.away_team.goals !== this.matchData.away_team.goals) {
await this.announce.vsScore(matchData);
}
this.matchData = matchData;
}
reportEvents(matchData) {
_.orderBy(
_.differenceBy(
_.get(matchData, 'home_team_events', []),
_.get(this, 'matchData.home_team_events', []),
'time'), ['time'])
.forEach(event => this.announce.event(event, _.get(matchData, 'home_team'), matchData));
_.orderBy(
_.get(matchData, 'away_team_events', []),
_.get(this, 'matchData.away_team_events', []),
'time', ['time'])
.forEach(event => this.announce.event(event, _.get(matchData, 'away_team'), matchData));
}
}
module.exports = Match;