This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ng-scores: Refactor scoreboard handling to scores service, create gen…
…eric scores mock.
- Loading branch information
Showing
5 changed files
with
103 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var scoresMock = { | ||
scores: [{ | ||
score: 1 | ||
},{ | ||
score: 2 | ||
}], | ||
remove: jasmine.createSpy('scoreRemoveSpy'), | ||
getScoreboard: jasmine.createSpy('getScoreboardSpy').andReturn(getDummyScoreboard()) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,128 @@ | ||
function getDummyScoreboard() { | ||
// Scoreboard contains scores for each stage of the tournament. | ||
// For each of these stages, there's a list of teams who have played | ||
// in this round, each recording all invidual round scores and an overal | ||
// highest score for that stage. | ||
// Teams are sorted by rank, but note that some teams may have the | ||
// same rank, if they have the exact same ordered list of scores. | ||
// | ||
// Possible score values and their ordering from high to low: | ||
// - <number> Integer score (can be negative) | ||
// - "dnc" Team Did Not Compete (i.e. didn't show up when they had to) | ||
// - "dsq" Team Disqualified (i.e. were not allowed to start, or removed during round) | ||
// - null Team did not play yet for this round | ||
var scoreboard = { | ||
"practice": [ | ||
/* teams... */ | ||
], | ||
"qualifying": [ | ||
{ | ||
team: { number: "4", name: "Volta" }, | ||
scores: [280, 100, 300], | ||
rank: 1, | ||
highest: 300, | ||
}, | ||
{ | ||
team: { number: "23", name: "Superteam" }, | ||
scores: [300, 100, 280], | ||
rank: 1, | ||
highest: 300, | ||
}, | ||
{ | ||
team: { number: "1", name: "NXT Generation" }, | ||
scores: [120, 200, null], | ||
rank: 2, | ||
highest: 200, | ||
}, | ||
{ | ||
team: { number: "20", name: "Utter Failure" }, | ||
scores: ["dnc", "dsq", "dnc"], | ||
rank: 3, | ||
highest: "dnc", | ||
} | ||
], | ||
"final": [ | ||
{ | ||
team: { number: "4", name: "Volta" }, | ||
scores: [250], | ||
rank: 2, | ||
highest: 250, | ||
}, | ||
{ | ||
team: { number: "23", name: "Superteam" }, | ||
scores: [280], | ||
rank: 1, | ||
highest: 280, | ||
} | ||
] | ||
}; | ||
return scoreboard; | ||
} | ||
|
||
/** | ||
* service handling all access to scores | ||
* Service handling all access to scores, including scoreboard | ||
* and ranking computation. | ||
*/ | ||
define([ | ||
'services/ng-services', | ||
'services/log', | ||
'services/ng-fs' | ||
'services/ng-fs', | ||
'services/ng-stages' | ||
],function(module,log) { | ||
return module.factory('$scores',['$fs',function($fs) { | ||
return module.factory('$scores',['$fs', '$stages', function($fs, $stages) { | ||
var scores = []; | ||
var stages = $stages.stages; | ||
|
||
function clear() { | ||
scores.splice(0, scores.length); | ||
} | ||
function save() { | ||
return $fs.write('scores.json',scores).then(function() { | ||
log('scores saved'); | ||
},function() { | ||
log('scores write error'); | ||
}, function(err) { | ||
log('scores write error', err); | ||
}); | ||
} | ||
function load() { | ||
return $fs.read('scores.json').then(function(res) { | ||
//remove everything | ||
// Replace contents of array (without creating a new object, so any | ||
// existing references to it remain valid) | ||
res.unshift(scores.length); | ||
res.unshift(0); | ||
scores.splice.apply(scores,res); | ||
},function() { | ||
}, function(err) { | ||
//error | ||
log('scores read error'); | ||
log('scores read error', err); | ||
}); | ||
} | ||
function remove(index) { | ||
var rem = scores.splice(index,1); | ||
return $fs.remove(rem[0].file).then(function() { | ||
// First remove the scoresheet, then re-save scores | ||
var score = scores.splice(index, 1); | ||
return $fs.remove(score.file).then(function() { | ||
return save(); | ||
},function() { | ||
log('error removing score'); | ||
}, function(err) { | ||
log('error removing score', err); | ||
}); | ||
} | ||
function add(data) { | ||
scores.push(data); | ||
return save(); | ||
} | ||
|
||
function getScoreboard() { | ||
return getDummyScoreboard(); | ||
} | ||
|
||
load(); | ||
|
||
return { | ||
scores: scores, | ||
clear: clear, | ||
load: load, | ||
save: save, | ||
remove: remove, | ||
add: add | ||
add: add, | ||
getScoreboard: getScoreboard | ||
}; | ||
}]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters