| 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396 |
1×
1×
1×
219×
238×
238×
1×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
238×
32×
32×
26×
1×
238×
8×
238×
32×
32×
1×
8×
8×
2×
8×
1×
32×
32×
2×
32×
1×
238×
238×
22×
216×
172×
238×
1×
238×
238×
186×
186×
1×
238×
1×
238×
238×
238×
238×
238×
280×
280×
280×
280×
267×
256×
256×
267×
267×
267×
280×
280×
280×
17×
238×
1×
952×
952×
1120×
1120×
1120×
1120×
1120×
1068×
1068×
1068×
1120×
560×
1×
238×
280×
267×
238×
238×
280×
280×
213×
1×
280×
1×
267×
11×
256×
1×
238×
238×
1×
547×
46×
46×
36×
238×
280×
280×
280×
5×
267×
280×
238×
1×
238×
238×
238×
280×
267×
267×
267×
267×
1×
238×
22×
22×
216×
216×
1×
520×
420×
1×
44×
44×
44×
1×
238×
238×
235×
238×
191×
32×
159×
46×
191×
30×
191×
2×
191×
6×
191×
2×
191×
9×
238×
1×
238×
476×
1×
476×
156×
1×
476×
156×
320×
1×
238×
235×
1×
238×
235×
3×
1×
32×
26×
6×
1×
26×
26×
11×
8×
4×
4×
3×
15×
26×
1×
29×
58×
1×
11×
14×
| /**
* @ngdoc service
* @name app.scores.scoreboardPrep
* @description
* Enhances the scores object with additional properties that
* are helpful for rendering the scores table. For example,
* titles are added to each set, so that it is
* easy to display a title next to each set.
*/
(function () {
'use strict';
angular
.module('app.scores')
.factory('scoreboardPrep', factory);
/** @ngInject */
function factory(shortenName) {
return function (sb) {
prepare(sb, shortenName);
return sb;
};
}
// Add various properties to the scoreboard object in order to make it easier to write client
// code and markup to display and alter the score.
function prepare(sb, shortenName) {
var tieBreak7Title = '7';
var tieBreak10Title = '10';
var setsTitles = [
'1st',
'2nd',
'3rd',
'4th',
'5th'
];
var context = actionContext();
// Add placeholder for a new set and new game to sets and games arrays
insertNew();
var setsAndGames = collectSetAndGames();
// Add various properties
sb.matchFlags = {};
sb.opponents = opponents();
sb.server = server();
insertScores(sb.opponents);
insertFirsts();
insertTitles();
insertShortNames();
sb.startingSetOrMatch = context.startingSetOrMatch;
sb.currentGame = currentGame();
sb.previousGame = previousGame();
sb.currentSet = currentSet();
sb.previousSet = previousSet();
sb.hasCompletedGames = hasCompletedGames();
sb.firstServers = null;
if (sb.currentGame && sb.currentGame.newGame) {
var list = listFirstServers();
if (list) {
sb.firstServers =
{
list: list,
id: list[0].id
};
}
}
function insertNew() {
if (context.inNewSet || context.inNewMatchTiebreak) {
sb.sets.push(newSet());
}
if (context.inNewGame || context.inTiebreak) {
var lastSet = sb.sets[sb.sets.length - 1];
lastSet.games.push(newGame());
}
// Create newSet object if a set is starting
function newSet() {
var result = {
newSet: true,
games: []
};
if (context.inNewMatchTiebreak)
result.tiebreak = true;
return result;
}
// Create newGame object if a game is starting
function newGame() {
var result = {newGame: true};
if (context.inTiebreak)
result.tiebreak = true;
return result;
}
}
// Opponent pair
function opponents() {
// An opponent is either a player or a team
var opponents = [];
if (sb.doubles && sb.first_team)
opponents = [sb.first_team.id, sb.second_team.id];
else if (sb.first_player)
opponents = [sb.first_player.id, sb.second_player.id];
return opponents;
}
// Add server property
function server() {
var result = null;
if (sb.state == 'in_progress') {
var lastGame = setsAndGames.lastGame;
if (lastGame && !lastGame.winner && lastGame.server)
result = lastGame.server;
}
return result;
}
function collectSetAndGames() {
var result = {
lastGame: null,
lastGameOrTiebreak: null,
prevGameOrTiebreak: null,
lastSet: null,
prevSet: null,
lastGames: []
};
var lastGame = null;
var lastGameInSet;
var winner;
enumerateSetsAndGames(
function (set) {
result.prevSet = result.lastSet;
result.lastSet = set;
lastGameInSet = null;
winner = false;
},
function (game) {
if (!game.tiebreak) {
lastGame = game;
lastGameInSet = game;
}
result.prevGameOrTiebreak = result.lastGameOrTiebreak;
result.lastGameOrTiebreak = game;
winner = winner || game.winner;
},
function (set) {
result.lastGame = lastGame;
result.lastGames.push(lastGameInSet);
if (winner)
result.lastSetWithWin = set;
}
);
return result;
}
// Call functions for each game and set
function enumerateSetsAndGames(onSet, onGame, onAfterGames) {
var setOrdinal = 1;
angular.forEach(sb.sets, function (set) {
Eif (onSet)
onSet(set, setOrdinal);
setOrdinal++;
var gameOrdinal = 1;
angular.forEach(set.games, function (game) {
Eif (onGame)
onGame(game, gameOrdinal, set);
gameOrdinal++;
});
if (onAfterGames) {
onAfterGames(set, setOrdinal);
}
});
}
// Add properties to be used in views
function insertTitles() {
enumerateSetsAndGames(
function (set, ordinal) {
insertSetTitle(set, ordinal);
},
function (game, ordinal, set) {
insertGameTitle(game, ordinal, set);
}
);
var lastGames = setsAndGames.lastGames;
for (var i = 0; i < lastGames.length; i++) {
var game = lastGames[i];
if (game)
game.verboseTitle = true;
}
function insertSetTitle(set, ordinal) {
// if (set.tiebreak)
// set.title = tieBreakTitle;
// else
set.title = setsTitles[ordinal - 1];
}
function insertGameTitle(game, ordinal, set) {
if (game.tiebreak)
game.title = (set.tiebreak) ? tieBreak10Title : tieBreak7Title;
else {
game.title = ordinal.toString();
}
}
}
// Add properties to be used in views
function insertScores(_opponents) {
var matchScorePair = [0, 0];
var setScorePair;
function incScore(scoresPair, winner) {
if (winner) {
var i = _opponents.indexOf(winner);
if (i >= 0)
scoresPair[i] += 1;
}
}
enumerateSetsAndGames(
function (set) {
setScorePair = [0, 0];
incScore(matchScorePair, set.winner);
if (set.scoring == 'ten_point')
set.tiebreak = true;
},
function (game) {
incScore(setScorePair, game.winner);
},
function (set) {
set.scores = setScorePair;
}
);
sb.scores = matchScorePair;
}
function insertFirsts() {
var firstOfMatch = true;
var firstOfSet;
enumerateSetsAndGames(
function () {
firstOfSet = true;
},
function (game) {
game.firstOfSet = firstOfSet;
game.firstOfMatch = firstOfMatch;
firstOfSet = false;
firstOfMatch = false;
}
);
}
function insertShortNames() {
if (sb.doubles) {
insertNames(sb.first_team);
insertNames(sb.second_team)
} else {
insertName(sb.first_player);
insertName(sb.second_player);
}
function insertName(player) {
if (player)
player.shortName = shortenName(player.name);
}
function insertNames(team) {
Eif (team) {
insertName(team.first_player);
insertName(team.second_player);
}
}
}
function actionContext() {
var result = {};
if (sb.state != 'not_started' && sb.state != 'complete')
result.playingMatch = true;
if (sb.actions) {
if (sb.actions.start_game || sb.actions.start_tiebreak)
result.startingGame = true;
else if (sb.actions.win_game || sb.actions.win_tiebreak || sb.actions.win_match_tiebreak)
result.playingGame = true;
if (sb.actions.start_game)
result.inNewGame = true;
if (sb.actions.start_tiebreak)
result.inTiebreak = true;
if (sb.actions.start_set)
result.inNewSet = true;
if (sb.actions.start_match_tiebreak)
result.inNewMatchTiebreak = true;
if (sb.actions.start_play || sb.actions.start_set || sb.actions.start_match_tiebreak)
result.startingSetOrMatch = true;
}
return result;
}
function hasCompletedGames() {
return [currentGame(), previousGame()].reduce(
function (last, game) {
return last || (game && game.winner);
}, false);
}
function currentGame() {
if (context.playingGame || context.startingGame)
return setsAndGames.lastGameOrTiebreak;
}
function previousGame() {
if (context.playingGame || context.startingGame)
return setsAndGames.prevGameOrTiebreak;
else
return setsAndGames.lastGameOrTiebreak;
}
function currentSet() {
if (context.playingMatch)
return setsAndGames.lastSet;
}
function previousSet() {
if (context.playingMatch)
return setsAndGames.prevSet;
else
return setsAndGames.lastSet;
}
// List players to serve next game
function listFirstServers() {
if ((sb.doubles && sb.servers.length <= 1) || (!sb.doubles && sb.servers.length === 0))
return listPlayers();
else
return null; // No more first servers
function listPlayers() {
var list = [];
if (sb.doubles) {
if (sb.servers.length == 1) {
if ([sb.first_team.first_player.id,
sb.first_team.second_player.id].indexOf(sb.servers[0]) >= 0) {
addTeamPlayers([sb.second_team]);
}
else
addTeamPlayers([sb.first_team]);
}
else
addTeamPlayers([sb.first_team, sb.second_team]);
}
else
addPlayers([sb.first_player, sb.second_player]);
return list;
function addPlayers(players) {
angular.forEach(players, function (player) {
list.push(
{
id: player.id,
name: player.name
});
});
}
function addTeamPlayers(teams) {
angular.forEach(teams, function (team) {
addPlayers([team.first_player, team.second_player]);
});
}
}
}
}
})();
|