Skip to content

Commit

Permalink
Merge pull request #20882 from code-dot-org/subtype-results
Browse files Browse the repository at this point in the history
Split maze Subtypes into Subtypes and ResultsHandlers
  • Loading branch information
Hamms committed Mar 1, 2018
2 parents d64830c + 7e4c397 commit e631a01
Show file tree
Hide file tree
Showing 20 changed files with 782 additions and 602 deletions.
2 changes: 1 addition & 1 deletion apps/src/maze/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var move = function (direction, id) {
if (Maze.controller.subtype.isWordSearch()) {
Maze.controller.subtype.markTileVisited(Maze.controller.pegmanY, Maze.controller.pegmanX, false);
}
if (Maze.controller.shouldCheckSuccessOnMove()) {
if (Maze.shouldCheckSuccessOnMove()) {
Maze.checkSuccess();
}
};
Expand Down
169 changes: 36 additions & 133 deletions apps/src/maze/bee.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { randomValue } from '../utils';
import Gatherer from './gatherer';
import mazeMsg from './locale';
import BeeCell from './beeCell';
import BeeItemDrawer from './beeItemDrawer';
import {
TestResults,
BeeTerminationValue as TerminationValue
} from '../constants.js';

Expand All @@ -28,14 +26,13 @@ export default class Bee extends Gatherer {
throw new Error(`bad flowerType for Bee: ${config.level.flowerType}`);
}

this.nectarGoal_ = config.level.nectarGoal || 0;
this.honeyGoal_ = config.level.honeyGoal || 0;

// at each location, tracks whether user checked to see if it was a flower or
// honeycomb using an if block
this.userChecks_ = [];

this.overrideStepSpeed = 2;
this.honey_ = undefined;
this.nectars_ = undefined;
}

/**
Expand Down Expand Up @@ -95,139 +92,17 @@ export default class Bee extends Gatherer {
}

/**
* Did we reach our total nectar/honey goals?
* @return {boolean}
* @override
*/
succeeded() {
// nectar/honey goals
if (this.honey_ < this.honeyGoal_ || this.nectars_.length < this.nectarGoal_) {
return false;
}

if (!this.checkedAllClouded() || !this.checkedAllPurple()) {
return false;
}

return super.succeeded();
}

/**
* @override
*/
collectedEverything() {
// quantum maps implicity require "collect everything", non-quantum
// maps don't really care
if (!this.maze_.map.hasMultiplePossibleGrids()) {
return true;
}

return super.collectedEverything();
}

/**
* @override
*/
terminateWithAppSpecificValue() {
const executionInfo = this.maze_.executionInfo;

if (this.nectars_.length < this.nectarGoal_) {
executionInfo.terminateWithValue(TerminationValue.INSUFFICIENT_NECTAR);
} else if (this.honey_ < this.honeyGoal_) {
executionInfo.terminateWithValue(TerminationValue.INSUFFICIENT_HONEY);
} else if (!this.checkedAllClouded()) {
executionInfo.terminateWithValue(TerminationValue.UNCHECKED_CLOUD);
} else if (!this.checkedAllPurple()) {
executionInfo.terminateWithValue(TerminationValue.UNCHECKED_PURPLE);
} else if (!this.collectedEverything()) {
executionInfo.terminateWithValue(TerminationValue.DID_NOT_COLLECT_EVERYTHING);
}
}

/**
* Did we check every flower/honey that was covered by a cloud?
*/
checkedAllClouded() {
for (let row = 0; row < this.maze_.map.currentStaticGrid.length; row++) {
for (let col = 0; col < this.maze_.map.currentStaticGrid[row].length; col++) {
if (this.shouldCheckCloud(row, col) && !this.checkedCloud(row, col)) {
return false;
}
}
}
return true;
}

/**
* Did we check every purple flower
*/
checkedAllPurple() {
for (let row = 0; row < this.maze_.map.currentStaticGrid.length; row++) {
for (let col = 0; col < this.maze_.map.currentStaticGrid[row].length; col++) {
if (this.shouldCheckPurple(row, col) && !this.userChecks_[row][col].checkedForNectar) {
return false;
}
}
}
return true;
}

/**
* @override
* Get the total count of all honey collected
*/
getTestResults(terminationValue) {
switch (terminationValue) {
case TerminationValue.NOT_AT_FLOWER:
case TerminationValue.FLOWER_EMPTY:
case TerminationValue.NOT_AT_HONEYCOMB:
case TerminationValue.HONEYCOMB_FULL:
return TestResults.APP_SPECIFIC_FAIL;

case TerminationValue.UNCHECKED_CLOUD:
case TerminationValue.UNCHECKED_PURPLE:
case TerminationValue.INSUFFICIENT_NECTAR:
case TerminationValue.INSUFFICIENT_HONEY:
case TerminationValue.DID_NOT_COLLECT_EVERYTHING:
var testResults = this.maze_.getTestResults(true);
// If we have a non-app specific failure, we want that to take precedence.
// Values over TOO_MANY_BLOCKS_FAIL are not true failures, but indicate
// a suboptimal solution, so in those cases we want to return our
// app specific fail. Same goes for BLOCK_LIMIT_FAIL.
if (testResults >= TestResults.TOO_MANY_BLOCKS_FAIL || testResults === TestResults.BLOCK_LIMIT_FAIL) {
testResults = TestResults.APP_SPECIFIC_FAIL;
}
return testResults;
}

return super.getTestResults(terminationValue);
getHoneyCount() {
return this.honey_;
}

/**
* @override
* Get the total count of all nectar collected
*/
getMessage(terminationValue) {
switch (terminationValue) {
case TerminationValue.NOT_AT_FLOWER:
return mazeMsg.notAtFlowerError();
case TerminationValue.FLOWER_EMPTY:
return mazeMsg.flowerEmptyError();
case TerminationValue.NOT_AT_HONEYCOMB:
return mazeMsg.notAtHoneycombError();
case TerminationValue.HONEYCOMB_FULL:
return mazeMsg.honeycombFullError();
case TerminationValue.UNCHECKED_CLOUD:
return mazeMsg.uncheckedCloudError();
case TerminationValue.UNCHECKED_PURPLE:
return mazeMsg.uncheckedPurpleError();
case TerminationValue.INSUFFICIENT_NECTAR:
return mazeMsg.insufficientNectar();
case TerminationValue.INSUFFICIENT_HONEY:
return mazeMsg.insufficientHoney();
case TerminationValue.DID_NOT_COLLECT_EVERYTHING:
return mazeMsg.didNotCollectEverything();
default:
return super.getMessage(terminationValue);
}
getNectarCount() {
return this.nectars_.length;
}

/**
Expand Down Expand Up @@ -285,6 +160,34 @@ export default class Bee extends Gatherer {
return this.userChecks_[row][col].checkedForFlower || this.userChecks_[row][col].checkedForHive;
}

/**
* Did we check every flower/honey that was covered by a cloud?
*/
checkedAllClouded() {
for (let row = 0; row < this.maze_.map.currentStaticGrid.length; row++) {
for (let col = 0; col < this.maze_.map.currentStaticGrid[row].length; col++) {
if (this.shouldCheckCloud(row, col) && !this.checkedCloud(row, col)) {
return false;
}
}
}
return true;
}

/**
* Did we check every purple flower
*/
checkedAllPurple() {
for (let row = 0; row < this.maze_.map.currentStaticGrid.length; row++) {
for (let col = 0; col < this.maze_.map.currentStaticGrid[row].length; col++) {
if (this.shouldCheckPurple(row, col) && !this.userChecks_[row][col].checkedForNectar) {
return false;
}
}
}
return true;
}

/**
* Flowers are either red or purple. This function returns true if a flower is red.
*/
Expand Down

0 comments on commit e631a01

Please sign in to comment.