Skip to content

Instantly share code, notes, and snippets.

angular.module('kahootCloneApp')
.controller('MainCtrl', function ($scope, $location, _, fbutil) {
$scope.newGame = function() {
$scope.creatingGame = true;
// Generate random 6 digit pincode for the game
var PIN = _.random(100000,999999),
// Connect to Firebase
yo angularfire:route Host
yo angularfire:service Host
// we store data about games as nodes of the /games node
{
"games" : {
"123456" : { // Game identifiers will be 6 digit PIN codes
// Gamestate, key-value pairs
},
"234567" : {
// ...
}
}
angular.module('kahootCloneApp')
.controller('HostCtrl', function ($scope, Host, $routeParams,fbutil) {
Host.init($routeParams.PIN)
.then(function() {
Host.syncObject.$bindTo($scope, 'game');
});
$scope.startGame = function() {
$scope.game.data.state = 'preQuestion'
<div class="row" ng-if="game.data.state == 'waitingForPlayers'">
<div class="jumbotron">
<h1>Join our game!</h1>
<h2>Go to http://someaddress.com on your smartphone</h2>
<h2>Our PIN code is {{game.$id}}</h2>
<p><button class="btn btn-success" ng-click="startGame()">Let's begin!</button></p>
</div>
<div class="col-md-12">
<div class="well">
<h3>Who's here:</h3>
yo angularfire:route Player
yo angularfire:service Player
angular.module('kahootCloneApp')
.service('Host', function (fbutil) {
var self = this;
self.init = function(PIN) {
self.syncObject = fbutil.syncObject('games/' + PIN);
return self.syncObject.$loaded();
};
});
<div class="row" ng-if="game.data.state=='joinGame'">
<div class="col-md-6 col-md-offset-3">
<form>
<div class="form-group">
<label for="PIN">Game PIN #</label>
<input ng-model="$parent.PIN" type="text" class="form-control" id="PIN" placeholder="Enter 6 digit number availible on main screen">
</div>
<div class="form-group">
<label for="screenName">Your name</label>
<input ng-model="$parent.screenName" type="text" class="form-control" id="screenName" placeholder="Choose a screen name">
angular.module('kahootCloneApp')
.controller('PlayerCtrl', function ($scope, Player, $location,$routeParams) {
if(! $routeParams.hasOwnProperty('PIN')) {
$scope.game = {
data : {
state: 'joinGame'
}
}
} else {
angular.module('kahootCloneApp')
.service('Player', function (fbutil, _, $cookieStore) {
// AngularJS will instantiate a singleton by calling "new" on this function
var self = this, _so;
self.getUniqId = function() {
// generate a unique idenftifier for the player and save it in a cookie to allow refreshes
if($cookieStore.get('playerId')) {
return self._id = $cookieStore.get('playerId');
} else {