Skip to content

Commit

Permalink
feature: perfection tracker for levels with best number of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Patolord committed Nov 18, 2024
1 parent 4cfaa0a commit 928577b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
19 changes: 18 additions & 1 deletion __tests__/LevelStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,29 @@ describe('this store', function() {

expect(LevelStore.isLevelSolved(firstLevel.id))
.toEqual(false);
LevelActions.setLevelSolved(firstLevel.id);
LevelActions.setLevelSolved(firstLevel.id, false);
expect(LevelStore.isLevelSolved(firstLevel.id))
.toEqual(true);
LevelActions.resetLevelsSolved();
expect(LevelStore.isLevelSolved(firstLevel.id))
.toEqual(false);
});

it('can solve a level with best status and then reset', function() {
var sequenceMap = LevelStore.getSequenceToLevels();
var firstLevel = sequenceMap[
Object.keys(sequenceMap)[0]
][0];

expect(LevelStore.isLevelBest(firstLevel.id))
.toEqual(false);
LevelActions.setLevelSolved(firstLevel.id, true);
expect(LevelStore.isLevelBest(firstLevel.id))
.toEqual(true);
LevelActions.resetLevelsSolved();
expect(LevelStore.isLevelBest(firstLevel.id))
.toEqual(false);
});


});
7 changes: 4 additions & 3 deletions src/js/actions/LevelActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ var ActionTypes = AppConstants.ActionTypes;

var LevelActions = {

setLevelSolved: function(levelID) {
setLevelSolved: function(levelID, best) {
AppDispatcher.handleViewAction({
type: ActionTypes.SET_LEVEL_SOLVED,
levelID: levelID
levelID: levelID,
best: best
});
},

Expand All @@ -29,4 +30,4 @@ var LevelActions = {

};

module.exports = LevelActions;
module.exports = LevelActions;
8 changes: 6 additions & 2 deletions src/js/level/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,15 @@ var Level = Sandbox.extend({

levelSolved: function(defer) {
this.solved = true;

if (!this.isShowingSolution) {
LevelActions.setLevelSolved(this.level.id);
var numCommands = this.gitCommandsIssued.length;
var best = this.getNumSolutionCommands();
var isBest = numCommands <= best;

LevelActions.setLevelSolved(this.level.id, isBest);
log.levelSolved(this.getEnglishName());
}

this.hideGoal();

var nextLevel = LevelStore.getNextLevel(this.level.id);
Expand Down
18 changes: 12 additions & 6 deletions src/js/stores/LevelStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,17 @@ AppConstants.StoreSubscribePrototype,
},

isLevelSolved: function(levelID) {
if (!_levelMap[levelID]) {
throw new Error('that level doesn\'t exist!');
}
return !!_solvedMap[levelID];
var levelData = _solvedMap[levelID];
return levelData ? levelData.solved === true : false;
},


isLevelBest: function(levelID) {
var levelData = _solvedMap[levelID];
return levelData ? levelData.best === true : false;
},



dispatchToken: AppDispatcher.register(function(payload) {
var action = payload.action;
Expand All @@ -202,8 +208,8 @@ AppConstants.StoreSubscribePrototype,
_syncToStorage();
shouldInform = true;
break;
case ActionTypes.SET_LEVEL_SOLVED:
_solvedMap[action.levelID] = true;
case ActionTypes.SET_LEVEL_SOLVED:
_solvedMap[action.levelID] = { solved: true, best: action.best || false };
_syncToStorage();
shouldInform = true;
break;
Expand Down
12 changes: 8 additions & 4 deletions src/js/views/levelDropdownView.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,16 @@ var SeriesView = BaseView.extend({
updateSolvedStatus: function() {
// this is a bit hacky, it really should be some nice model
// property changing but it's the 11th hour...
var toLoop = this.$('a.levelIcon').each(function(index, el) {
var id = $(el).attr('data-id');
$(el).toggleClass('solved', LevelStore.isLevelSolved(id));
this.$('a.levelIcon').each(function() {
var $el = $(this);
var id = $el.attr('data-id');
var isSolved = LevelStore.isLevelSolved(id);
var isBest = LevelStore.isLevelBest(id);
$el.toggleClass('solved', isSolved);
$el.toggleClass('best', isBest);
});
},

getEventID: function(ev) {
var element = ev.target;
return $(element).attr('data-id');
Expand Down
5 changes: 5 additions & 0 deletions src/style/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,11 @@ a.levelIcon.solved:active {
background: #5edb15;
}

a.levelIcon.best {
border-color: gold;
background: gold;
}

a.levelIcon div.index {
font-weight: 400;
text-shadow: 1px 1px 2px #CCC, 0 2px 0 #C9C9C9;
Expand Down

0 comments on commit 928577b

Please sign in to comment.