Skip to content

Instantly share code, notes, and snippets.

@AnthonyClink
Created May 5, 2013 23:38
Show Gist options
  • Save AnthonyClink/5522595 to your computer and use it in GitHub Desktop.
Save AnthonyClink/5522595 to your computer and use it in GitHub Desktop.
(function( ng, app ) {
"use strict";
// I provide a repository for the quests.
app.service(
"questService",
function( $q, $resource, $http, _, categoryService ) {
var QuestbookAPI = $resource( 'http://174.126.249.6\\:8080/worldcraft/quests' , {}, { query: {method: 'GET', isArray: true} });
var quests = [];
QuestbookAPI.query(function(newQuests){
console.log("I ran !!!!! WOOTZORS . I am the questbook api query.");
quests = newQuests;
_.each(quests, function(quest){
quest.id = quest.questID.id;
quest.categoryID = quest.questCategory.id;
});
});
// I get the quest with the given ID.
function getQuestByID( id ) {
var deferred = $q.defer();
var quest = _.findWithProperty( quests, "id", id );
if ( quest ) {
deferred.resolve( ng.copy( quest ) );
} else {
deferred.reject();
}
return( deferred.promise );
}
// I get the quests in the given category.
function getQuestsByCategoryID( categoryID ) {
console.log("I ran !!!!! WOOTZORS . I am the CategoryID lookup query.");
var deferred = $q.defer();
var filteredQuests = _.filterWithProperty( quests, "categoryID", categoryID );
if ( filteredQuests ) {
deferred.resolve( ng.copy( filteredQuests ) );
} else {
deferred.reject();
}
return( deferred.promise );
}
// I get a random quest in the given category, less the given quest.
function getRandomQuestExcluding( categoryID, excludeQuestID ) {
var deferred = $q.defer();
var questsInCategory = _.filterWithProperty( quests, "categoryID", categoryID );
if ( questsInCategory ) {
var index = _.random( 0, ( questsInCategory.length - 1 ) );
while ( questsInCategory[ index ].questID.id === excludeQuestID ) {
if( questsInCategory.length === 1 ){
//there is only one accessable quest. break out of the while loop
break;
}
index = _.random( 0, ( questsInCategory.length - 1 ) );
}
deferred.resolve( ng.copy( questsInCategory[ index ] ) );
} else {
deferred.reject();
}
return( deferred.promise );
}
// ---------------------------------------------- //
// ---------------------------------------------- //
// Set up a collection of constants for our quest categories. Normally, this would be managed
// by the server-side database; but, since we're using static data, this will just make the code
// easier to read.
var questType = categoryService.getQuestTypes();
var difficulty = {HIGH:"high", MEDIUM:"medium", LOW:"low", TRIVIAL:"Trivial"};
// ---------------------------------------------- //
// ---------------------------------------------- //
// Return the public API.
return({
getQuestByID: getQuestByID,
getQuestsByCategoryID: getQuestsByCategoryID,
getRandomQuestExcluding: getRandomQuestExcluding,
});
}
);
})( angular, Worldcraft );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment