Skip to content

Instantly share code, notes, and snippets.

@sklink
Created September 14, 2015 05:20
Show Gist options
  • Save sklink/f41a45710d4e00ba30bf to your computer and use it in GitHub Desktop.
Save sklink/f41a45710d4e00ba30bf to your computer and use it in GitHub Desktop.
angular
.module('btg.dashboard')
.controller('HeroCardController', HeroCardController);
function HeroCardController(addHero, teams, $meteor, draftFactory) {
var vm = this;
var draftId;
angular.extend(vm, {
addToTeam: addToTeam,
addToEnemy: addToEnemy,
swapTeam: swapTeam,
});
/**
* Add hero to the users, or "heroTeam".
*
* @param {string} heroId Id of the hero to add to the team.
*/
function addToTeam(heroId) {
draftFactory.changeDraft(heroId, 'add-to-team');
startCharacterToAdd(heroId, teams.heroTeam);
}
/**
* Add hero to the opposing team, or "enemyTeam".
*
* @param {string} heroId Id of the hero to add to the enemy
*/
function addToEnemy(heroId) {
draftFactory.changeDraft(heroId, 'add-to-enemy');
startCharacterToAdd(heroId, teams.enemyTeam);
}
/**
* Move hero from it's current team to the opposite team.
*
* @param {string} heroId Id of the hero to switch teams.
*/
function swapTeam(heroId) {
if(teams.heroTeam.containsMember(heroId)) {
removeHero(teams.heroTeam, _.find(teams.heroTeam.getTeam(), {_id: heroId}));
addToEnemy(heroId);
} else if(teams.enemyTeam.containsMember(heroId)) {
removeHero(teams.enemyTeam, _.find(teams.enemyTeam.getTeam(), {_id: heroId}));
addToTeam(heroId);
}
}
/**
* Start the wizard-like experience of the hero modal, to allow
* a user to correctly add a new character to a choosen team.
*
* This will call the dashboard.lane state to start the modal.
*
* If the user completes the "modal", they will be moved back to
* the dashboard state.
*
* @param {string} id Id of the hero to add
* @param {Object} team Team to add the hero to
*/
function startCharacterToAdd(id, team) {
addHero.newHero(id, team);
addHero.completeHero();
}
function removeHero(team, hero) {
if(_.isUndefined(team)) {
team = teams.getTeamByName(vm.toEdit.teamName);
hero = vm.toEdit;
}
team.removeCharacterFromTeam(hero);
vm.toEdit = {};
vm.isEditing = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment