Skip to content

Instantly share code, notes, and snippets.

@rotemtam
Last active September 9, 2015 21:52
Show Gist options
  • Select an option

  • Save rotemtam/b14483efffe500c3c78f to your computer and use it in GitHub Desktop.

Select an option

Save rotemtam/b14483efffe500c3c78f to your computer and use it in GitHub Desktop.
Using $scope.$watch to respond to game state changes
$scope.$watch('game.data.state', function(newValue, oldValue) {
switch(newValue) {
case 'preQuestion':
$scope.countdown = 5;
$interval(function() {
$scope.countdown--;
},1000, $scope.countdown)
.then(function() {
Host.setGameState('question');
});
break;
case 'question':
$scope.currentQuestion = Host.getCurrentQuestion();
$scope.answers = Trivia.getPossibleAnswers($scope.currentQuestion);
$scope.game.data.possibleAnswers = $scope.answers;
$scope.countdown = 7;
$interval(function() {
$scope.countdown--;
},1000, $scope.countdown)
.then(function() {
Host.setGameState('postQuestion');
});
break;
case 'postQuestion':
$scope.correct = [];
$scope.currentQuestion = Host.getCurrentQuestion();
angular.forEach($scope.game.data.users, function(v,k) {
if(Trivia.checkAnswer(Host.getCurrentQuestion().q, v.answer)) {
v.currentPoints = (v.currentPoints || 0) + 100;
$scope.correct.push(v.screen_name);
}
});
Host.syncObject.$save();
break;
case 'leaderboard':
$scope.leaderboard = _.map($scope.game.data.users, function(user) {
return {
screen_name:user.screen_name,
current_points:user.currentPoints
}
});
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment