Last active
August 29, 2015 14:25
-
-
Save seemaullal/f347e925c8d2e0026da7 to your computer and use it in GitHub Desktop.
Angular Sample #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This is a factory used to manage a firebase reference that held data for each | |
room of a coding challenge instance. It was part of a web applications where | |
users could compete in coding challenges against each other in real time | |
The application can be viewed at http://www.coduels.com */ | |
'use strict'; | |
app.factory('RoomFactory', function($q) { | |
var factory = {}; | |
var roomsRef = new Firebase('http://dazzling-torch-169.firebaseio.com/rooms'); | |
factory.activeRooms = []; | |
factory.createRoom = function(exercise, user) { | |
var gameStartTime = new Date(); | |
gameStartTime = gameStartTime.setSeconds(gameStartTime.getSeconds() + 10); | |
var roomData = { | |
users: [user], | |
exerciseId: exercise._id, | |
exerciseName: exercise.name, | |
shortDescription: exercise.shortDescription, | |
longDescription: exercise.longDescription, | |
editorPrompt: exercise.editorPrompt, | |
testCode: exercise.testCode, | |
category: exercise.category, | |
difficulty: exercise.difficulty, | |
gameStartTime: gameStartTime, | |
winner: null, | |
isPractice: false | |
}; | |
var roomKey = roomsRef.push(roomData).key(); | |
return roomKey; | |
}; | |
factory.createPracticeRoom = function(exercise, user) { | |
var roomData = { | |
users: [user], | |
exerciseId: exercise._id, | |
exerciseName: exercise.name, | |
shortDescription: exercise.shortDescription, | |
longDescription: exercise.longDescription, | |
editorPrompt: exercise.editorPrompt, | |
testCode: exercise.testCode, | |
category: exercise.category, | |
difficulty: exercise.difficulty, | |
gameStartTime: Date.now(), | |
winner: null, | |
isPractice: true | |
}; | |
var roomKey = roomsRef.push(roomData).key(); | |
return roomKey; | |
}; | |
factory.deleteActiveRoom = function(roomKey) { | |
var ref = roomsRef.child(roomKey); | |
ref.remove(); | |
}; | |
factory.updateActiveRoomData = function () { | |
return $q(function(resolve, reject) { | |
var activeRoomData = []; | |
roomsRef.once('value', function (firebaseSnapshot){ | |
for (var key in firebaseSnapshot.val()){ | |
var roomData = firebaseSnapshot.val()[key]; | |
roomData.roomId = key; | |
if (roomData.gameStartTime > Date.now() ) | |
// don't put closed rooms (timed out) on scope for now | |
activeRoomData.push(roomData); | |
} | |
resolve(activeRoomData); | |
}); | |
}); | |
}; | |
factory.addUserToRoom = function (userObj, roomId) { | |
var ref = roomsRef.child(roomId); | |
var list = []; | |
ref.once('value', function (snap){ | |
list = snap.val().users; | |
list.push(userObj); | |
ref.child('users').set(list); | |
}); | |
}; | |
factory.removeUserFromRoom = function (userId, roomId) { | |
var ref = roomsRef.child(roomId); | |
var userlist = []; | |
ref.once('value', function (snap){ | |
userlist = snap.val().users; | |
if (userlist.length === 1){ | |
ref.remove(); | |
return; | |
} | |
userlist.forEach (function (user, index) { | |
if (userId === user._id){ | |
userlist.splice(index,1); | |
} | |
}); | |
ref.child('users').set(userlist); | |
}); | |
}; | |
return factory; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment