Merge pull request #19 from bfirsh/vote-refactoring
Various pieces of vote refactoring
This commit is contained in:
commit
64839f70dc
@ -45,17 +45,24 @@ function getVotes(client) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
console.error("Error performing query: " + err);
|
console.error("Error performing query: " + err);
|
||||||
} else {
|
} else {
|
||||||
var data = result.rows.reduce(function(obj, row) {
|
var votes = collectVotesFromResult(result);
|
||||||
obj[row.vote] = row.count;
|
io.sockets.emit("scores", JSON.stringify(votes));
|
||||||
return obj;
|
|
||||||
}, {});
|
|
||||||
io.sockets.emit("scores", JSON.stringify(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(function() {getVotes(client) }, 1000);
|
setTimeout(function() {getVotes(client) }, 1000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function collectVotesFromResult(result) {
|
||||||
|
var votes = {a: 0, b: 0};
|
||||||
|
|
||||||
|
result.rows.forEach(function (row) {
|
||||||
|
votes[row.vote] = parseInt(row.count);
|
||||||
|
});
|
||||||
|
|
||||||
|
return votes;
|
||||||
|
}
|
||||||
|
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
app.use(bodyParser());
|
app.use(bodyParser());
|
||||||
app.use(methodOverride('X-HTTP-Method-Override'));
|
app.use(methodOverride('X-HTTP-Method-Override'));
|
||||||
|
@ -5,15 +5,6 @@ var bg1 = document.getElementById('background-stats-1');
|
|||||||
var bg2 = document.getElementById('background-stats-2');
|
var bg2 = document.getElementById('background-stats-2');
|
||||||
|
|
||||||
app.controller('statsCtrl', function($scope){
|
app.controller('statsCtrl', function($scope){
|
||||||
var animateStats = function(a,b){
|
|
||||||
if(a+b>0){
|
|
||||||
var percentA = a/(a+b)*100;
|
|
||||||
var percentB = 100-percentA;
|
|
||||||
bg1.style.width= percentA+"%";
|
|
||||||
bg2.style.width = percentB+"%";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
$scope.aPercent = 50;
|
$scope.aPercent = 50;
|
||||||
$scope.bPercent = 50;
|
$scope.bPercent = 50;
|
||||||
|
|
||||||
@ -23,15 +14,16 @@ app.controller('statsCtrl', function($scope){
|
|||||||
var a = parseInt(data.a || 0);
|
var a = parseInt(data.a || 0);
|
||||||
var b = parseInt(data.b || 0);
|
var b = parseInt(data.b || 0);
|
||||||
|
|
||||||
animateStats(a, b);
|
var percentages = getPercentages(a, b);
|
||||||
|
|
||||||
$scope.$apply(function() {
|
bg1.style.width = percentages.a + "%";
|
||||||
if(a + b > 0){
|
bg2.style.width = percentages.b + "%";
|
||||||
$scope.aPercent = a/(a+b) * 100;
|
|
||||||
$scope.bPercent = b/(a+b) * 100;
|
$scope.$apply(function () {
|
||||||
$scope.total = a + b
|
$scope.aPercent = percentages.a;
|
||||||
}
|
$scope.bPercent = percentages.b;
|
||||||
});
|
$scope.total = a + b;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -43,3 +35,16 @@ app.controller('statsCtrl', function($scope){
|
|||||||
init();
|
init();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function getPercentages(a, b) {
|
||||||
|
var result = {};
|
||||||
|
|
||||||
|
if (a + b > 0) {
|
||||||
|
result.a = Math.round(a / (a + b) * 100);
|
||||||
|
result.b = 100 - result.a;
|
||||||
|
} else {
|
||||||
|
result.a = result.b = 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
@ -31,8 +31,10 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="result" ng-if="total > 100">
|
<div id="result">
|
||||||
{{total}} votes
|
<span ng-if="total == 0">No votes yet</span>
|
||||||
|
<span ng-if="total == 1">{{total}} vote</span>
|
||||||
|
<span ng-if="total >= 2">{{total}} votes</span>
|
||||||
</div>
|
</div>
|
||||||
<script src="socket.io.js"></script>
|
<script src="socket.io.js"></script>
|
||||||
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
|
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
|
||||||
|
@ -47,6 +47,7 @@ body{
|
|||||||
vertical-align:middle;
|
vertical-align:middle;
|
||||||
}
|
}
|
||||||
#result{
|
#result{
|
||||||
|
z-index: 3;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
bottom: 40px;
|
bottom: 40px;
|
||||||
right: 20px;
|
right: 20px;
|
||||||
|
Loading…
Reference in New Issue
Block a user