Skip to content

Commit

Permalink
Unique Team Code
Browse files Browse the repository at this point in the history
Concerns #28
Load & Get unique code works with Honorary Member
UI to be made better.
  • Loading branch information
Gulix committed Jan 10, 2017
1 parent 45c26b0 commit 54069ab
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 25 deletions.
7 changes: 6 additions & 1 deletion builder/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

<!-- ko if: isAffiliationDisplayed -->
<div class="affiliation-selection global-container">
<h2>Enter a Code</h2>
<div class="enter-code">
<input type="text" class="team-code" data-bind="value: teamCodeInput"></input>
<div class="button" data-bind="click: enterTeamCode">Enter Code</div>
</div>
<h2>Choose your faction / alignment</h2>
<div class="step-back" data-bind="click: goBack, visible: goBackIsVisible"></div>
<div class="affiliation-boxes" data-bind="foreach: affiliations">
Expand Down Expand Up @@ -55,7 +60,7 @@ <h2>My Team</h2>
<div class="card-title"><span class="icon-cards"></span><span data-bind="text: nbLeaderCards"></span> Leader Cards</div>
<div class="card-action" data-bind="click: showLeaderCardSelection"><span class="icon-add-card card-action-icon"></span>Select one ...</div>
<div class="card-honorary-member" data-bind="visible: isHonoraryMemberSelected"><span class="icon-honorary-member"></span>Honorary Member</div>
<div class="card-honorary-member-name" data-bind="visible: isHonoraryMemberSelected, text: honoraryMemberName"></div>
<div class="card-honorary-member-name" data-bind="visible: isHonoraryMemberSelected, text: honoraryMemberName"></div>
</div>
<!-- ko foreach: rosterSupremes -->
<supreme-box params="supreme: $data"></supreme-box>
Expand Down
82 changes: 76 additions & 6 deletions builder/js/scripts/teamCode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
define([], function() {
define(['viewmodels/affiliationVM',
'viewmodels/supremeVM'],
function(AffiliationVM,
SupremeVM)
{

function getTeamCode(teamVM)
{
Expand All @@ -13,20 +17,86 @@ function getTeamCode(teamVM)
}
// Next characters (before .) are the length of the id of the Supremes (max length)
var maxSupreme = _.maxBy(teamVM.rosterSupremes(), function(s) { return s.jsonData.id.toString(16).length; });
var iMax = maxSupreme.jsonData.id.toString(16).length;
uniqueCode += iMax.toString() + ".";
var iCharactersUsedForSupremeCode = maxSupreme.jsonData.id.toString(16).length;
uniqueCode += iCharactersUsedForSupremeCode.toString() + ".";

// Then we create the string with all the Supremes ID with equal numbers of digits
var supremesIdString = _.join(
_.map(teamVM.rosterSupremes(), function(s) { return _.padStart(s.jsonData.id.toString(16), iMax, '0'); })
_.map(teamVM.rosterSupremes(), function(s) { return getSupremeCode(s, iCharactersUsedForSupremeCode); })
, '');

uniqueCode += supremesIdString;

// If the Team includes an Honorary Member, we include it in the code
if (teamVM.isHonoraryMemberSelected()) {
uniqueCode += ".HM";
// First the code of the Leader / Field Commander, then the HonoraryMember
if (teamVM.supremeGrantingHonorary() != null) {
uniqueCode += getSupremeCode(teamVM.supremeGrantingHonorary(), iCharactersUsedForSupremeCode);
}
var hm = teamVM.honoraryMember();
if (hm != null) {
uniqueCode += getSupremeCode(hm, iCharactersUsedForSupremeCode);
}
}
}
return uniqueCode;
}


function loadTeamFromCode(teamBuilder, code) {
// TODO : check if code is in correct format

// First Step : Get the correct Affiliation
var affiliation = AffiliationVM.getByCode(code.substring(0,2));
if (affiliation != null)
{
teamBuilder.loadTeamForAffiliation(affiliation);

var codeSplit = code.split(".");
// Second Step : Add the Supremes
var iCharactersForSupremeCode = parseInt(codeSplit[0].substring(2), 10);
for(var iIndex = 0; iIndex < codeSplit[1].length; iIndex += iCharactersForSupremeCode) {
var supremeCode = parseInt(codeSplit[1].substring(iIndex, iIndex + iCharactersForSupremeCode), 16);
var supremeVM = SupremeVM.getByCode(supremeCode, teamBuilder.recruitSupreme, teamBuilder.dismissSupreme);
supremeVM.isRecruited(true);
teamBuilder.recruitSupreme(supremeVM);
}

// Third Step : Manage the Honorary Member
var optionalCodes = _.slice(codeSplit, 2);
if (codeSplit.length >= 3) {
var hmCode = _.find(optionalCodes, function(c) { return c.startsWith('HM'); });
if (hmCode != null) {
// First characters are the code of the Leader / Field Commander
var supremeCode = parseInt(hmCode.substring(2, 2 + iCharactersForSupremeCode), 16);
var grantingHM = _.find(teamBuilder.team().rosterSupremes(), function(s) { return s.jsonData.id == supremeCode; });
if (grantingHM != null) {
teamBuilder.team().supremeGrantingHonorary(grantingHM);
}
// Then it's the Honorary Member
supremeCode = parseInt(hmCode.substring(2 + iCharactersForSupremeCode, 2 + 2*iCharactersForSupremeCode), 16);
var honoraryMember = _.find(teamBuilder.team().rosterSupremes(), function(s) { return s.jsonData.id == supremeCode; });
if (honoraryMember != null) {
teamBuilder.team().isHonoraryMemberSelected(true);
honoraryMember.dismissFromTeam = teamBuilder.team().dismissHonoraryMember;
honoraryMember.isHonoraryMember(true);
}
}
}
}

}

function getSupremeCode(supremeVM, iPaddingNumber)
{
if (supremeVM != null) {
return _.padStart(supremeVM.jsonData.id.toString(16), iPaddingNumber, '0');
}
return '';
}

return {
getTeamCodeFromRoster: function(roster) { return getTeamCode(roster) }
getTeamCodeFromRoster: function(roster) { return getTeamCode(roster); },
loadFromCode: function(teamBuilder, code) { loadTeamFromCode(teamBuilder, code); }
}
});
30 changes: 21 additions & 9 deletions builder/js/viewmodels/affiliationVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,7 @@ function affiliationVM(jsonFaction, isHeroes, isVillains, jsonOrigin, isIndepend
if (self.factionVM != null) {
return self.factionVM.id();
} else {
var code = self.isHeroes ? "H" : "V";
if ((self.originVM != null) && !self.originVM.isAllOrigins()) {
code += self.originVM.origin_key.substring(0,1);
}
code = self.isIndependent ? code.toLowerCase() : code.toUpperCase();
if ((self.originVM == null) || self.originVM.isAllOrigins()) {
code += '_';
}
return code;
return self.isHeroes ? "H_" : "V_";
}
}

Expand Down Expand Up @@ -169,6 +161,26 @@ return {
}

return affiliations;
},

/* Returns the Affiliation corresponding to the code (see also getUniqueCode) */
getByCode: function(code) {
if (code == "H_") {
var heroesAffiliation = new affiliationVM(null, true, false, null, false, null);
heroesAffiliation.isOriginSelection = true;
return heroesAffiliation;
} else if (code == "V_") {
var villainsAffiliation = new affiliationVM(null, false, true, null, false, null);
villainsAffiliation.isOriginSelection = true;
return villainsAffiliation;
} else {
var factions = Factions.load();
var faction = _.find(factions, function(f) { return f.id == code; });
if (faction != null) {
return new affiliationVM(faction, null, null, null, null, null);
}
}
return null;
}
}
});
5 changes: 5 additions & 0 deletions builder/js/viewmodels/supremeVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ return {
}

return supremesReturned;
},
getByCode: function(code, recruitAction, dismissAction) {
var supremes = Supremes.load();
var supreme = _.find(supremes, function(s) { return s.id == code; });
return new supremeVM(supreme, recruitAction, dismissAction);
}
}
});
24 changes: 19 additions & 5 deletions builder/js/viewmodels/teamBuilderVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ define(['knockout',
'viewmodels/affiliationVM',
'viewmodels/supremeVM',
'viewmodels/teamVM',
'viewmodels/supremeFilter'],
'viewmodels/supremeFilter',
'scripts/teamCode'],
function(ko,
TinyColor,
_,
AffiliationVM,
SupremeVM,
TeamVM,
SupremeFilter) {
SupremeFilter,
TeamCode) {

function teamBuilderVM()
{
Expand All @@ -25,6 +27,7 @@ function teamBuilderVM()
self.team = ko.observable(null);
self.supremeFilter = ko.observable(null);
self.goBackIsVisible = ko.observable(false);
self.teamCodeInput = ko.observable('');

/**********************************/
/* Accessors & Computed Variables */
Expand Down Expand Up @@ -78,9 +81,7 @@ function teamBuilderVM()
self.selectAffiliation = function(selectedAffiliation) {
// Final affiliation ?
if (selectedAffiliation.isFinal()) {
self.affiliations([]);
self.team(TeamVM.newTeamVM(selectedAffiliation));
self.supremesPool(SupremeVM.loadForAffiliation(selectedAffiliation, self.recruitSupreme, self.dismissSupreme));
self.loadTeamForAffiliation(selectedAffiliation);
} else { // Affiliation that leads to other affiliations
self.affiliations(selectedAffiliation.nextAffiliations());
self.team(null);
Expand All @@ -89,6 +90,12 @@ function teamBuilderVM()
}
}

self.loadTeamForAffiliation = function(affiliationVM) {
self.affiliations([]);
self.team(TeamVM.newTeamVM(affiliationVM));
self.supremesPool(SupremeVM.loadForAffiliation(affiliationVM, self.recruitSupreme, self.dismissSupreme));
}

/*----- Recruitment of Supremes -----*/
self.recruitSupreme = function(supremeVM) {
self.team().recruitSupreme(supremeVM);
Expand All @@ -106,6 +113,13 @@ function teamBuilderVM()
self.supremesPool([]);
}

/*------- Team Code -------*/
self.enterTeamCode = function() {
if ((self.teamCodeInput() != null) && (self.teamCodeInput().length > 0)) {
TeamCode.loadFromCode(self, self.teamCodeInput());
}
}

/*************************/
/* Object Initialization */
/*************************/
Expand Down
13 changes: 9 additions & 4 deletions builder/js/viewmodels/teamVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,20 @@ function teamVM(affiliation)
return _.sortBy(supremes, [function(o) { return o.jsonData.name; }]);
})

self.honoraryMember = ko.pureComputed(function() {
var honoraryMember = _.find(self.rosterSupremes(), function(s) { return s.isHonoraryMember(); });
return honoraryMember;
});
self.honoraryMemberName = ko.pureComputed(function() {
if (self.isHonoraryMemberSelected())
var hm = self.honoraryMember();
if (hm != null)
{
var honoraryMember = _.find(self.rosterSupremes(), function(s) { return s.isHonoraryMember(); });
if (honoraryMember != null) return honoraryMember.jsonData.name;
return hm.jsonData.name;
}
return '';
});


/*************/
/* Functions */
/*************/
Expand Down Expand Up @@ -230,7 +235,7 @@ function teamVM(affiliation)
}
}
self.recruitSupreme(supremeVM);
self.endHonoraryMemberSelection();
self.isHonoraryMembersSelectable(false);
self.hideLeaderCardSelection();
self.isHonoraryMemberSelected(true);
}
Expand Down

0 comments on commit 54069ab

Please sign in to comment.