I am in the process of developing a multiplayer game, and I would like a specific function to be triggered once the first player updates an "isStarted" field in the collection.
Within my controller code:
angular.module('mcitygame').directive('gcaseGame', function() {
return {
restrict: 'E',
templateUrl: 'client/gcases/gcase-game/gcase-game.html',
controllerAs: 'gcaseGame',
controller: function($scope, $stateParams, $reactive, $window, $element) {
$reactive(this).attach($scope);
this.subscribe('gcases'); // Keep track of changes in the collection related to the "isStarted" field
this.helpers({
gcase: () => {
return GCases.findOne({_id: 'dynamicID'}) //For illustration purposes, I have used 'dynamicID' instead of $stateParams.gcaseId obtained from the router.
});
this.startGame = function(option){
if(option == 1) {
console.log(" Users clicked me from the interface!");
GCases.update({_id :'dynamicID'}, {
$set: {isStarted: true, updatedAt: Date.now()}
});
} else {
console.log("I am here because someone called me from a different client!");
}
};
////////////////////////// I want to implement something like this
this.autorun(() => {
if(this.gcase.isStarted){
console.log(" isStartred = " + this.gcase.isStarted);
this.startGame();
}
});
////////////////////////// or this
this.gcase.observeChanges({
changed: function (id, gcase) {
console.log(" isStartred = " + this.gcase.isStarted);
this.startGame();
}
});
//////////////////////////
}
}})
Furthermore, outside the controller:
GCases = new Mongo.Collection("gcases");
Any assistance on this matter would be greatly appreciated.