-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·247 lines (219 loc) · 24.3 KB
/
server.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var http = require("http");
var express = require("express");
var bodyParser = require("body-parser");
// var app = express();
// app.use(bodyParser.json()); // for parsing application/json
// app.use(bodyParser.urlencoded({ extended: true })); // for parsing
var alreadyReportedScores = [];
var MLBhost = 'gd2.mlb.com';
var thisTeamCode = 'det';
var composedNotificationsURL = '';
var scoreBoardJSONResult = '';
var parentResponseObj = {};
//Set up listener which starts the first HTTP request to gd2.mlb.com when it gets a request from the client/page
http.createServer(function(request, response) {
console.log("GET Request on port 8888: " + request.url);
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
response.writeHead(200, {"Content-Type": "text/plain"});
if (request.url === '/getTodaysScores') {
//step 1: find the current game to get details for
console.log("1: parentResponse is:" + response);
parentResponseObj = response;
setupAndLaunchSBRequest(response);
}
else if (request.url === '/runUnitTests') {
console.log("run the unit tests");
parentResponseObj = response;
setupStaticTestRequest(response);
}
}).listen(8888);
//step 1: setup for the first http request to get the scoreboard
function setupAndLaunchSBRequest(parentResponse) {
var today = new Date();
var todayMonth = (today.getMonth() + 1);
if (todayMonth<10) { todayMonth = '0' + todayMonth};
var todayDate = today.getDate();
if (todayDate<10) { todayDate = '0' + todayDate};
baseURL = '/components/game/mlb/';
todayPath = 'year_' + today.getFullYear() + '/month_' + todayMonth + '/day_' + todayDate + '/';
//todayPath = 'year_' + '2016' + '/month_' + '07' + '/day_' + '02' + '/';
console.log('todayPath=' + todayPath);
console.log("2: parentResponse is:" + parentResponse);
gamePath = requestSBforGameFilePath(parentResponse, baseURL + todayPath + 'miniscoreboard.json');
}
//step 1a: setup for a specific day's request, for integration testing
function setupStaticTestRequest(parentResponse) {
baseURL = "/components/game/mlb/";
todayPath = 'year_' + '2016' + '/month_' + '07' + '/day_' + '02' + '/';
gamePath = requestSBforGameFilePath(parentResponse, baseURL + todayPath + 'miniscoreboard.json');
}
//step 3: parse the result from the scoreboard, to get the directory of the right game
var parseScoreBoardForGameSubDir = function parseScoreBoardForGameSubDir(parentResponse) {
//console.log(scoreBoardJSONResult.toString());
console.log(typeof(scoreBoardJSONResult));
var scoreBoardJSONResultObject = JSON.parse(scoreBoardJSONResult);
console.log(typeof(scoreBoardJSONResultObject));
//console.log(scoreBoardJSONResultObject.subject);
//console.log(scoreBoardJSONResultObject.data);
console.log(scoreBoardJSONResultObject.data.games.game.length);
//do the actual parsing of the JSON text here, return the subdir name to be used as 'gamePath' later on
for (var gameNumber = 0; gameNumber < scoreBoardJSONResultObject.data.games.game.length; gameNumber++) {
console.log("checking game " + gameNumber + " in: " + scoreBoardJSONResultObject.data.games.game[gameNumber].location);
if ((scoreBoardJSONResultObject.data.games.game[gameNumber].home_code === thisTeamCode) ||
(scoreBoardJSONResultObject.data.games.game[gameNumber].away_code === thisTeamCode)) {
composedNotificationsURL = scoreBoardJSONResultObject.data.games.game[gameNumber].game_data_directory;
composedNotificationsURL += "/notifications/notifications_full.xml";
console.log("found matching game, path is: " + composedNotificationsURL);
break;
}
};
scoreBoardJSONResult = '';
//fire off step 4, next http request for the actual data
if (composedNotificationsURL.length > 0) {
console.log("4: parentResponse is:" + parentResponse);
requestNotificationsForGame(parentResponse, composedNotificationsURL);
}
else {
parentResponseObj.write("No game found for the Tigers today");
parentResponseObj.end();
}
}
//step 2: initiate the first http request to get the scoreboard
function requestSBforGameFilePath(parentResponse, pathToSB) {
var scoreBoardRequest = {
options: {
host: MLBhost,
path: pathToSB //looking for miniscoreboard.json in this directory
},
callback: function(response) {
response.on('data', function(chunk) {
scoreBoardJSONResult += chunk;
//console.log(chunk);
});
response.on('end', parseScoreBoardForGameSubDir);
},
parentResponse: {}
}
scoreBoardRequest.parentResponse = parentResponse;
console.log("3: parentResponse is:" + parentResponse);
http.request(scoreBoardRequest.options, scoreBoardRequest.callback).end();
}
//step 4: do the request for the actual notification file for this game
function requestNotificationsForGame(parentResponse, composedNotificationsURL) {
var notificationsRequest = {
options: {
host: MLBhost,
path: ''
},
callback: function(response) {
response.on('data', function(chunk) {
fullNotificationsXML += chunk;
});
response.on('end', function() {
fullNotificationsXML = fullNotificationsXML.toString().replace('undefined', '');
//var xmlSample ="<!--Copyright 2016 MLB Advanced Media, L.P. Use of any content on this page acknowledges agreement to the terms posted here http://gdx.mlb.com/components/copyright.txt--><notifications modified_date=\"2016-05-08T14:45:44Z\"><game id=\"2016/05/07/texmlb-detmlb-1\" gameday=\"2016_05_07_texmlb_detmlb_1\"><notification inning=\"1\" top=\"Y\" ab=\"1\" pitch=\"0\" seq=\"1\" batter=\"596059\" pitcher=\"460059\" pbp=\"\" uid=\"-1845147899\"><type category=\"lineups\" away=\"140\" home=\"116\" start_time=\"2016/05/07 05:10 PM\"/></notification><notification inning=\"1\" top=\"Y\" ab=\"1\" pitch=\"0\" seq=\"2\" batter=\"596059\" pitcher=\"460059\" pbp=\"\" uid=\"-1845147868\"><player id=\"456167\"/><player id=\"460059\"/><player id=\"596059\"/><player id=\"608577\"/><player id=\"134181\"/><type category=\"game start\" home_pitcher=\"460059\" away_pitcher=\"456167\" batter=\"596059\" batter_avg=\".287\" on_deck=\"608577\" on_deck_avg=\".318\" in_hole=\"134181\" in_hole_avg=\".268\"/></notification><notification inning=\"3\" top=\"N\" ab=\"28\" pitch=\"0\" seq=\"4\" away_team_runs=\"2\" home_team_runs=\"3\" outs=\"3\" batter=\"425902\" pitcher=\"460059\" uid=\"-1057043260\" pbp=\"\"><player id=\"460059\"/><player id=\"519168\"/><player id=\"425902\"/><player id=\"435622\"/><player id=\"519048\"/><type category=\"end inning\" home_pitcher=\"460059\" away_pitcher=\"519168\" batter=\"425902\" batter_avg=\".205\" on_deck=\"435622\" on_deck_avg=\".236\" in_hole=\"519048\" in_hole_avg=\".250\"/></notification><notification inning=\"5\" top=\"N\" ab=\"45\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"5\" outs=\"3\" batter=\"596059\" pitcher=\"460059\" uid=\"1333141134\" pbp=\"\"><player id=\"460059\"/><player id=\"519168\"/><player id=\"596059\"/><player id=\"596059\"/><player id=\"596059\"/><type category=\"end inning\" home_pitcher=\"460059\" away_pitcher=\"519168\" batter=\"596059\" batter_avg=\".287\" on_deck=\"596059\" on_deck_avg=\".287\" in_hole=\"596059\" in_hole_avg=\".287\"/></notification><notification inning=\"6\" top=\"N\" ab=\"56\" pitch=\"0\" seq=\"1\" away_team_runs=\"7\" home_team_runs=\"5\" outs=\"3\" batter=\"592261\" pitcher=\"543935\" uid=\"452322607\" pbp=\"\"><player id=\"543935\"/><player id=\"452666\"/><player id=\"592261\"/><player id=\"596059\"/><player id=\"596059\"/><type category=\"end inning\" home_pitcher=\"543935\" away_pitcher=\"452666\" batter=\"592261\" batter_avg=\".226\" on_deck=\"596059\" on_deck_avg=\".287\" in_hole=\"596059\" in_hole_avg=\".287\"/></notification><notification inning=\"9\" top=\"N\" ab=\"82\" pitch=\"0\" seq=\"2\" away_team_runs=\"10\" home_team_runs=\"5\" outs=\"3\" pbp=\"\" uid=\"1904430296\"><player id=\"519168\"/><player id=\"594986\"/><type category=\"end game\" winning_team=\"140\" losing_team=\"116\" winning_pitcher=\"519168\" winning_pitcher_win=\"1\" winning_pitcher_loss=\"0\" losing_pitcher=\"594986\" losing_pitcher_win=\"0\" losing_pitcher_loss=\"1\"/></notification></game><team id=\"140\" code=\"tex\"><notification inning=\"1\" top=\"Y\" ab=\"1\" pitch=\"0\" seq=\"1\" away_team_runs=\"1\" home_team_runs=\"0\" outs=\"0\" batter=\"608577\" pitcher=\"460059\" pbp=\"Rougned Odor homers (6) on a line drive to left center field. \" uid=\"1990180997\"><player id=\"596059\"/><type category=\"lead change\" lead_type=\"lead\" lead_team=\"140\" trail_team=\"116\"/><type category=\"score\"/><type category=\"homerun\" batter=\"596059\"/></notification><notification inning=\"1\" top=\"Y\" ab=\"2\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"0\" outs=\"0\" batter=\"134181\" pitcher=\"460059\" pbp=\"Nomar Mazara homers (4) on a line drive to right center field. \" uid=\"-864525940\"><player id=\"608577\"/><type category=\"score\"/><type category=\"homerun\" batter=\"608577\"/></notification><notification inning=\"3\" top=\"N\" ab=\"28\" pitch=\"1\" seq=\"3\" away_team_runs=\"2\" home_team_runs=\"3\" outs=\"2\" batter=\"457454\" pitcher=\"519168\" pbp=\"Pitching Change: Anthony Ranaudo replaces A. J. Griffin. \" uid=\"723039986\"><player id=\"519168\"/><player id=\"456167\"/><type category=\"pitching change\" relief_pitcher=\"519168\" era=\"-\" win=\"0\" loss=\"0\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"456167\"/></notification><notification inning=\"6\" top=\"Y\" ab=\"47\" pitch=\"0\" seq=\"1\" away_team_runs=\"3\" home_team_runs=\"5\" outs=\"0\" batter=\"134181\" pitcher=\"460059\" pbp=\"Nomar Mazara doubles (3) on a sharp line drive to left fielder Justin Upton. Rougned Odor scores. \" uid=\"359693750\"><player id=\"596059\"/><type category=\"score\"/></notification><notification inning=\"6\" top=\"Y\" ab=\"48\" pitch=\"0\" seq=\"2\" away_team_runs=\"3\" home_team_runs=\"5\" outs=\"0\" batter=\"134181\" pitcher=\"460059\" pbp=\"\" uid=\"1214914646\"><player id=\"134181\"/><type category=\"runner in scoring position\" batter=\"134181\" avg=\".276\"/></notification><notification inning=\"6\" top=\"Y\" ab=\"48\" pitch=\"0\" seq=\"1\" away_team_runs=\"5\" home_team_runs=\"5\" outs=\"0\" batter=\"425902\" pitcher=\"594986\" pbp=\"Adrian Beltre homers (4) on a fly ball to right field. Nomar Mazara scores. \" uid=\"503808544\"><player id=\"608577\"/><player id=\"134181\"/><type category=\"lead change\" lead_type=\"tie\" lead_team=\"140\" trail_team=\"116\"/><type category=\"score\"/><type category=\"homerun\" batter=\"134181\"/></notification><notification inning=\"6\" top=\"Y\" ab=\"51\" pitch=\"0\" seq=\"1\" away_team_runs=\"7\" home_team_runs=\"5\" outs=\"1\" batter=\"462101\" pitcher=\"543935\" pbp=\"Mitch Moreland homers (4) on a fly ball to left center field. Ian Desmond scores. \" uid=\"102893055\"><player id=\"435622\"/><player id=\"519048\"/><type category=\"lead change\" lead_type=\"lead\" lead_team=\"140\" trail_team=\"116\"/><type category=\"score\"/><type category=\"homerun\" batter=\"519048\"/></notification><notification inning=\"6\" top=\"N\" ab=\"54\" pitch=\"0\" seq=\"1\" away_team_runs=\"7\" home_team_runs=\"5\" outs=\"0\" batter=\"435079\" pitcher=\"452666\" pbp=\"Pitching Change: Tom Wilhelmsen replaces Anthony Ranaudo. \" uid=\"256388429\"><player id=\"452666\"/><player id=\"519168\"/><type category=\"pitching change\" relief_pitcher=\"452666\" era=\"9.00\" win=\"1\" loss=\"2\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"519168\"/></notification><notification inning=\"7\" top=\"Y\" ab=\"60\" pitch=\"0\" seq=\"1\" away_team_runs=\"7\" home_team_runs=\"5\" outs=\"2\" batter=\"134181\" pitcher=\"458677\" pbp=\"\" uid=\"963937410\"><player id=\"134181\"/><type category=\"runner in scoring position\" batter=\"134181\" avg=\".276\"/></notification><notification inning=\"7\" top=\"Y\" ab=\"60\" pitch=\"0\" seq=\"1\" away_team_runs=\"9\" home_team_runs=\"5\" outs=\"2\" batter=\"425902\" pitcher=\"458677\" pbp=\"Adrian Beltre homers (5) on a fly ball to left field. Delino DeShields scores. \" uid=\"-302718464\"><player id=\"592261\"/><player id=\"134181\"/><type category=\"score\"/><type category=\"homerun\" batter=\"134181\"/></notification><notification inning=\"7\" top=\"N\" ab=\"62\" pitch=\"0\" seq=\"2\" away_team_runs=\"9\" home_team_runs=\"5\" outs=\"0\" batter=\"400121\" pitcher=\"518617\" pbp=\"Pitching Change: Jake Diekman replaces Tom Wilhelmsen. \" uid=\"-1666862245\"><player id=\"518617\"/><player id=\"452666\"/><type category=\"pitching change\" relief_pitcher=\"518617\" era=\"2.53\" win=\"0\" loss=\"1\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"452666\"/></notification><notification inning=\"8\" top=\"N\" ab=\"69\" pitch=\"0\" seq=\"1\" away_team_runs=\"9\" home_team_runs=\"5\" outs=\"0\" batter=\"543238\" pitcher=\"501817\" pbp=\"Pitching Change: Tony Barnette replaces Jake Diekman. \" uid=\"1682529853\"><player id=\"501817\"/><player id=\"518617\"/><type category=\"pitching change\" relief_pitcher=\"501817\" era=\"3.65\" win=\"1\" loss=\"2\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"518617\"/></notification><notification inning=\"9\" top=\"Y\" ab=\"74\" pitch=\"0\" seq=\"1\" away_team_runs=\"9\" home_team_runs=\"5\" outs=\"0\" batter=\"592261\" pitcher=\"450275\" pbp=\"\" uid=\"-1627945023\"><player id=\"592261\"/><type category=\"runner in scoring position\" batter=\"592261\" avg=\".221\"/></notification><notification inning=\"9\" top=\"Y\" ab=\"74\" pitch=\"0\" seq=\"1\" away_team_runs=\"10\" home_team_runs=\"5\" outs=\"1\" batter=\"596059\" pitcher=\"450275\" pbp=\"Delino DeShields out on a sacrifice fly to right fielder J. Martinez. Bryan Holaday scores. \" uid=\"-133557836\"><player id=\"592407\"/><type category=\"score\"/></notification><notification inning=\"9\" top=\"N\" ab=\"82\" pitch=\"0\" seq=\"2\" away_team_runs=\"10\" home_team_runs=\"5\" outs=\"2\" batter=\"543238\" pitcher=\"474521\" pbp=\"Pitching Change: Shawn Tolleson replaces Tony Barnette. \" uid=\"174643387\"><player id=\"474521\"/><player id=\"501817\"/><type category=\"pitching change\" relief_pitcher=\"474521\" era=\"5.73\" win=\"0\" loss=\"1\" save=\"9\" bat_order=\"0\" leaving_pitcher=\"501817\"/></notification></team><team id=\"116\" code=\"det\"><notification inning=\"2\" top=\"N\" ab=\"17\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"1\" outs=\"2\" batter=\"578428\" pitcher=\"456167\" pbp=\"Anthony Gose triples (2) on a sharp line drive to center fielder Delino DeShields. Jarrod Saltalamacchia scores. \" uid=\"906164227\"><player id=\"457454\"/><type category=\"score\"/></notification><notification inning=\"2\" top=\"N\" ab=\"18\" pitch=\"0\" seq=\"2\" away_team_runs=\"2\" home_team_runs=\"1\" outs=\"2\" batter=\"578428\" pitcher=\"456167\" pbp=\"\" uid=\"-147595644\"><player id=\"578428\"/><type category=\"runner in scoring position\" batter=\"578428\" avg=\".250\"/></notification><notification inning=\"3\" top=\"N\" ab=\"26\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"1\" outs=\"2\" batter=\"457708\" pitcher=\"456167\" pbp=\"\" uid=\"-1114301655\"><player id=\"457708\"/><type category=\"runner in scoring position\" batter=\"457708\" avg=\".239\"/></notification><notification inning=\"3\" top=\"N\" ab=\"27\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"1\" outs=\"2\" batter=\"592206\" pitcher=\"456167\" pbp=\"\" uid=\"-1085672504\"><player id=\"592206\"/><type category=\"runner in scoring position\" batter=\"592206\" avg=\".376\"/><type category=\"bases loaded\" batter=\"592206\" avg=\".376\"/></notification><notification inning=\"3\" top=\"N\" ab=\"27\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"3\" outs=\"2\" batter=\"457454\" pitcher=\"456167\" pbp=\"Nick Castellanos singles on a line drive to center fielder Delino DeShields. Miguel Cabrera scores. Victor Martinez scores. Justin Upton to 3rd. \" uid=\"1888966427\"><player id=\"408234\"/><player id=\"400121\"/><type category=\"lead change\" lead_type=\"lead\" lead_team=\"116\" trail_team=\"140\"/><type category=\"score\"/></notification><notification inning=\"3\" top=\"N\" ab=\"28\" pitch=\"0\" seq=\"2\" away_team_runs=\"2\" home_team_runs=\"3\" outs=\"2\" batter=\"457454\" pitcher=\"456167\" pbp=\"\" uid=\"-1057043322\"><player id=\"457454\"/><type category=\"runner in scoring position\" batter=\"457454\" avg=\".217\"/></notification><notification inning=\"4\" top=\"N\" ab=\"34\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"3\" outs=\"1\" batter=\"435079\" pitcher=\"519168\" pbp=\"\" uid=\"-2081007635\"><player id=\"435079\"/><type category=\"runner in scoring position\" batter=\"435079\" avg=\".308\"/></notification><notification inning=\"4\" top=\"N\" ab=\"34\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"5\" outs=\"1\" batter=\"502110\" pitcher=\"519168\" pbp=\"Ian Kinsler homers (6) on a fly ball to left center field. Anthony Gose scores. \" uid=\"1392426331\"><player id=\"543238\"/><player id=\"435079\"/><type category=\"score\"/><type category=\"homerun\" batter=\"435079\"/></notification><notification inning=\"5\" top=\"N\" ab=\"43\" pitch=\"0\" seq=\"1\" away_team_runs=\"2\" home_team_runs=\"5\" outs=\"0\" batter=\"457454\" pitcher=\"519168\" pbp=\"\" uid=\"1275882832\"><player id=\"457454\"/><type category=\"runner in scoring position\" batter=\"457454\" avg=\".217\"/></notification><notification inning=\"6\" top=\"Y\" ab=\"49\" pitch=\"0\" seq=\"3\" away_team_runs=\"5\" home_team_runs=\"5\" outs=\"0\" batter=\"425902\" pitcher=\"594986\" pbp=\"Pitching Change: Kyle Ryan replaces Mike Pelfrey. \" uid=\"-909903181\"><player id=\"594986\"/><player id=\"460059\"/><type category=\"pitching change\" relief_pitcher=\"594986\" era=\"1.98\" win=\"0\" loss=\"0\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"460059\"/></notification><notification inning=\"6\" top=\"Y\" ab=\"52\" pitch=\"0\" seq=\"1\" away_team_runs=\"7\" home_team_runs=\"5\" outs=\"1\" batter=\"462101\" pitcher=\"543935\" pbp=\"Pitching Change: Alex Wilson replaces Kyle Ryan. \" uid=\"530001990\"><player id=\"543935\"/><player id=\"594986\"/><type category=\"pitching change\" relief_pitcher=\"543935\" era=\"2.25\" win=\"0\" loss=\"0\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"594986\"/></notification><notification inning=\"7\" top=\"Y\" ab=\"58\" pitch=\"0\" seq=\"1\" away_team_runs=\"7\" home_team_runs=\"5\" outs=\"0\" batter=\"596059\" pitcher=\"458677\" pbp=\"Pitching Change: Justin Wilson replaces Alex Wilson. \" uid=\"-750423967\"><player id=\"458677\"/><player id=\"543935\"/><type category=\"pitching change\" relief_pitcher=\"458677\" era=\"1.50\" win=\"0\" loss=\"0\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"543935\"/></notification><notification inning=\"8\" top=\"Y\" ab=\"66\" pitch=\"0\" seq=\"1\" away_team_runs=\"9\" home_team_runs=\"5\" outs=\"0\" batter=\"435622\" pitcher=\"572403\" pbp=\"Pitching Change: Drew VerHagen replaces Justin Wilson. \" uid=\"729962917\"><player id=\"572403\"/><player id=\"458677\"/><type category=\"pitching change\" relief_pitcher=\"572403\" era=\"6.23\" win=\"1\" loss=\"0\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"458677\"/></notification><notification inning=\"8\" top=\"N\" ab=\"72\" pitch=\"0\" seq=\"1\" away_team_runs=\"9\" home_team_runs=\"5\" outs=\"1\" batter=\"502110\" pitcher=\"501817\" pbp=\"\" uid=\"-1481089353\"><player id=\"502110\"/><type category=\"runner in scoring position\" batter=\"502110\" avg=\".239\"/></notification><notification inning=\"9\" top=\"Y\" ab=\"73\" pitch=\"0\" seq=\"1\" away_team_runs=\"9\" home_team_runs=\"5\" outs=\"0\" batter=\"592407\" pitcher=\"450275\" pbp=\"Pitching Change: Mark Lowe replaces Drew VerHagen. \" uid=\"1005538989\"><player id=\"450275\"/><player id=\"572403\"/><type category=\"pitching change\" relief_pitcher=\"450275\" era=\"2.79\" win=\"1\" loss=\"0\" save=\"0\" bat_order=\"0\" leaving_pitcher=\"572403\"/></notification><notification inning=\"9\" top=\"N\" ab=\"81\" pitch=\"0\" seq=\"1\" away_team_runs=\"10\" home_team_runs=\"5\" outs=\"2\" batter=\"457454\" pitcher=\"501817\" pbp=\"\" uid=\"1875801114\"><player id=\"457454\"/><type category=\"runner in scoring position\" batter=\"457454\" avg=\".217\"/></notification><notification inning=\"9\" top=\"N\" ab=\"82\" pitch=\"0\" seq=\"1\" away_team_runs=\"10\" home_team_runs=\"5\" outs=\"2\" batter=\"543238\" pitcher=\"474521\" pbp=\"\" uid=\"1904430265\"><player id=\"543238\"/><type category=\"runner in scoring position\" batter=\"543238\" avg=\".218\"/><type category=\"bases loaded\" batter=\"543238\" avg=\".218\"/></notification></team></notifications>";
//console.log(fullNotificationsXML);
var parseString = require('xml2js').parseString;
parseString(fullNotificationsXML, function (err, fullNotificationsObj) {
//console.log(require('util').inspect(fullNotificationsObj, false, null));
//console.log('about to call getNextScore, prevScoreUID=' + prevScoreUID);
var scoreText = getNextScore(fullNotificationsObj)
//console.log('outside the scope of getNextScore, prevScoreUID=' + prevScoreUID);
console.log(scoreText);
parentResponseObj.write(scoreText)
});
parentResponseObj.end();
});
},
parentResponse: {}
}
notificationsRequest.options.path = composedNotificationsURL;
notificationsRequest.parentResponse = parentResponse;
console.log("5: parentResponse is:" + parentResponse);
var fullNotificationsXML;
console.log("options before notifications request: " + notificationsRequest.options.host + notificationsRequest.options.path);
http.request(notificationsRequest.options, notificationsRequest.callback).end();
}
//step 4.1: parse the notifications XML for the next notification chunk
function getNextScore(fullNotificationsObj) {
//get the right team's notification list
var teamNodes = [];
var teamNode;
var myTeamIsAway;
teamNodes = fullNotificationsObj.notifications.team;
if (teamNodes[0].$.code == thisTeamCode) {
teamNode = teamNodes[0];
myTeamIsAway = true;
otherTeamCode = teamNodes[1].$.code;
} else {
teamNode = teamNodes[1];
myTeamIsAway = false;
otherTeamCode = teamNodes[0].$.code;
}
//console.log('length of teamNode.notification: ' + teamNode.notification.length);
//loop through that notification list to see if there is a score we haven't yet reported
for (var i = 0; i < teamNode.notification.length; i++) {
var thisNotification = teamNode.notification[i];
//console.log('thisNotification: ' + thisNotification);
if (isThisNotificationAScore(thisNotification)) {
console.log('loop iteration ' + i + ', alreadyReportedScores =' + alreadyReportedScores);
console.log('index of this uid in the array=' + alreadyReportedScores.indexOf(thisNotification.$.uid));
if (alreadyReportedScores.indexOf(thisNotification.$.uid) == -1) {
alreadyReportedScores.push(thisNotification.$.uid);
//going to return a score to the client
var boxScore;
var inning = ordinal_suffix_of(thisNotification.$.inning);
if (myTeamIsAway) {
boxScore = ' [' + inning + ': ' + thisTeamCode.toUpperCase() + ' ' + thisNotification.$.away_team_runs +
', ' + otherTeamCode.toUpperCase() + ' ' + thisNotification.$.home_team_runs + ']';
}
else {
boxScore = ' [' + inning + ': ' + otherTeamCode.toUpperCase() + ' ' + thisNotification.$.away_team_runs +
', ' + thisTeamCode.toUpperCase() + ' ' + thisNotification.$.home_team_runs + ']';
}
return thisNotification.$.pbp + boxScore;
}
}
}
return '';
}
/*
*PMR 5/16/16 attribution: http://stackoverflow.com/questions/13627308/add-st-nd-rd-and-th-ordinal-suffix-to-a-number
by Salman A from Stack Overflow
*/
function ordinal_suffix_of(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
//checks the notification object to see if it includes a score
function isThisNotificationAScore(thisNotification) {
for (var i = 0; i < thisNotification.type.length; i++) {
if (thisNotification.type[i].$.category == 'score') {
return true;
}
}
return false;
}
// app.get('/getResponder',function(request, response) {
// console.log("GET Request on port 8000");
// var body = request.body;
// console.log(body);
// console.log(request);
// response.setHeader("Access-Control-Allow-Origin", "*");
// response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
// response.writeHead(200, {"Content-Type": "text/plain"});
// response.write('response from server: ' + request.query.clientInput);
// response.end();
// });
// app.listen(8000);