-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMatch.js
86 lines (76 loc) · 1.88 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
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
/**
* @file
* Match objects.
*/
var EventEmitter = require('events').EventEmitter;
var util = require('util');
/**
* Match constructor.
*
* Represents a match in progress.
*
* @param String language
* The prefered language for team names.
*/
var Match = function (language) {
EventEmitter.call(this);
this.language = language;
this.initialized = false;
};
util.inherits(Match, EventEmitter);
/**
* Extracts important data from the match data and updates the match.
*
* @param Object matchData
* Match object parsed from the FIFA api.
*/
Match.prototype.update = function (matchData) {
var score = matchData.c_Score;
var live = matchData.b_Live;
this.data = matchData;
if (!this.initialized) {
this.homeTeam = this.data['c_HomeTeam_' + this.language];
this.awayTeam = this.data['c_AwayTeam_' + this.language];
this.url = this.data['c_ShareURL_' + this.language];
console.log(this.url);
this.score = '';
this.live = false;
this.initialized = true;
}
if (live != this.live) {
this[(live ? 'start' : 'end') + 'Match']();
} else if (score != this.score) {
this.updateScore(score);
}
};
/**
* Handles the match starting.
*
* Sets live to true and emits the startMatch event.
*/
Match.prototype.startMatch = function () {
console.log("start");
this.live = true;
this.emit('startMatch', this);
};
/**
* Handles the match ending.
*
* Sets live to false and emits the endMatch event.
*/
Match.prototype.endMatch = function () {
console.log("end");
this.live = false;
this.emit('endMatch', this);
};
/**
* Handles score changes.
*
* Updates the internal score and emits the scoreChange event.
*/
Match.prototype.updateScore = function (score) {
console.log("score");
this.score = score;
this.emit('updateScore', this);
};
module.exports = Match;