app.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var app = angular.module('catsvsdogs', []);
  2. var socket = io.connect({transports:['polling']});
  3. var bg1 = document.getElementById('background-stats-1');
  4. var bg2 = document.getElementById('background-stats-2');
  5. app.controller('statsCtrl', function($scope){
  6. $scope.aPercent = 50;
  7. $scope.bPercent = 50;
  8. var updateScores = function(){
  9. socket.on('scores', function (json) {
  10. data = JSON.parse(json);
  11. var a = parseInt(data.a || 0);
  12. var b = parseInt(data.b || 0);
  13. var percentages = getPercentages(a, b);
  14. bg1.style.width = percentages.a + "%";
  15. bg2.style.width = percentages.b + "%";
  16. $scope.$apply(function () {
  17. $scope.aPercent = percentages.a;
  18. $scope.bPercent = percentages.b;
  19. $scope.total = a + b;
  20. });
  21. });
  22. };
  23. var init = function(){
  24. document.body.style.opacity=1;
  25. updateScores();
  26. };
  27. socket.on('message',function(data){
  28. init();
  29. });
  30. });
  31. function getPercentages(a, b) {
  32. var result = {};
  33. if (a + b > 0) {
  34. result.a = Math.round(a / (a + b) * 100);
  35. result.b = 100 - result.a;
  36. } else {
  37. result.a = result.b = 50;
  38. }
  39. return result;
  40. }