Skip to content
This repository has been archived by the owner on Jun 16, 2018. It is now read-only.

Commit

Permalink
ng-scores: Refactor scoreboard handling to scores service, create gen…
Browse files Browse the repository at this point in the history
…eric scores mock.
  • Loading branch information
poelstra committed Jul 13, 2014
1 parent 47d2d36 commit 41627c8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 90 deletions.
9 changes: 9 additions & 0 deletions spec/mocks/scoresMock.js
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())
};
2 changes: 1 addition & 1 deletion spec/views/rankingSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('ranking', function() {
$scope = $rootScope.$new();
controller = $controller('rankingCtrl', {
'$scope': $scope,
'$scores': {}
'$scores': scoresMock,
'$stages': stagesMock,
});
});
Expand Down
16 changes: 4 additions & 12 deletions spec/views/scoresSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ describe('ranking', function() {
});

var $scope, controller;
var dummyScores = {
scores: [{
score: 1
},{
score: 2
}],
remove: jasmine.createSpy('scoreRemoveSpy')
};

beforeEach(function() {
angular.mock.module(module.name);
angular.mock.inject(function($controller, $rootScope) {
$scope = $rootScope.$new();
controller = $controller('scoresCtrl', {
'$scope': $scope,
'$scores': dummyScores
'$scores': scoresMock
});
});
});
Expand All @@ -29,17 +21,17 @@ describe('ranking', function() {
it('should initialize', function() {
expect($scope.sort).toEqual('index');
expect($scope.rev).toEqual(true);
expect($scope.scores).toEqual(dummyScores.scores);
expect($scope.scores).toEqual(scoresMock.scores);
});
});

describe('amending scores',function() {
it('should remove a score',function() {
$scope.removeScore(1);
expect(dummyScores.remove).toHaveBeenCalledWith(1);
expect(scoresMock.remove).toHaveBeenCalledWith(1);
});

it('should edit a score',function() {
xit('should edit a score',function() {
var newScore = {score:4};
$scope.editScore(1,newScore);
expect($scope.scores[1]).toEqual(newScore);
Expand Down
101 changes: 88 additions & 13 deletions src/js/services/ng-scores.js
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
};
}]);
});
65 changes: 1 addition & 64 deletions src/js/views/ranking.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,5 @@
"use strict";

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)
// - "DSQ" Team Disqualified (i.e. were not allowed to start, or removed during round)
// - "DNC" Team Did Not Compete (i.e. didn't show up when they had to)
// - 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: "bla",
}
],
"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;
}

define('views/ranking',[
'services/log',
'services/ng-scores',
Expand All @@ -75,15 +14,13 @@ define('views/ranking',[
$scope.sort = 'rank';
$scope.rev = false;

var scoreboard = getDummyScoreboard();

$scope.doSort = function(col,defaultSort) {
$scope.rev = ($scope.sort === col)? !$scope.rev : defaultSort;
$scope.sort = col;
};

$scope.stages = $stages.stages;
$scope.scoreboard = getDummyScoreboard();
$scope.scoreboard = $scores.getScoreboard();
}
]);
});

0 comments on commit 41627c8

Please sign in to comment.