From 16dbc5f885386e39a31c7b579f4dfb77734404d9 Mon Sep 17 00:00:00 2001 From: Elijah Hamovitz Date: Mon, 26 Feb 2018 10:21:20 -0800 Subject: [PATCH 1/3] Update Bee API methods to go directly through executionInfo Rather than calling a subtype method which THEN queues an action which then calls a subtype method, this PR untwitsts things to make the flow of execution more linear --- apps/src/maze/api.js | 4 ++-- apps/src/maze/bee.js | 57 ++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/apps/src/maze/api.js b/apps/src/maze/api.js index 2fac3b34d53e4..417336f3b9de1 100644 --- a/apps/src/maze/api.js +++ b/apps/src/maze/api.js @@ -258,11 +258,11 @@ exports.loopHighlight = API_FUNCTION(function (id) { * whether or not we're a Bee level */ exports.getNectar = API_FUNCTION(function (id) { - Maze.controller.subtype.getNectar(id); + Maze.executionInfo.queueAction('nectar', id); }); exports.makeHoney = API_FUNCTION(function (id) { - Maze.controller.subtype.makeHoney(id); + Maze.executionInfo.queueAction('honey', id); }); exports.atFlower = API_FUNCTION(function (id) { diff --git a/apps/src/maze/bee.js b/apps/src/maze/bee.js index 19d34bacc69ed..6e1473dc6204f 100644 --- a/apps/src/maze/bee.js +++ b/apps/src/maze/bee.js @@ -276,41 +276,53 @@ export default class Bee extends Gatherer { // API - getNectar(id) { + /** + * Attempt to harvest nectar from the current location; terminate the + * execution if this is not a valid place at which to get nectar. + * + * @return {boolean} whether or not the attempt was successful + */ + getNectar() { const col = this.maze_.pegmanX; const row = this.maze_.pegmanY; // Make sure we're at a flower. if (!this.isFlower(row, col)) { this.maze_.executionInfo.terminateWithValue(TerminationValue.NOT_AT_FLOWER); - return; + return false; } // Nectar is positive. Make sure we have it. if (this.flowerRemainingCapacity(row, col) === 0) { this.maze_.executionInfo.terminateWithValue(TerminationValue.FLOWER_EMPTY); - return; + return false; } - this.maze_.executionInfo.queueAction('nectar', id); this.gotNectarAt(row, col); + return true; } - // Note that this deliberately does not check whether bee has gathered nectar. - makeHoney(id) { + /** + * Attempt to make honey at the current location; terminate the execution if + * this is not a valid place at which to make honey. + * Note that this deliberately does not check whether bee has gathered nectar. + * + * @return {boolean} whether or not the attempt was successful + */ + makeHoney() { const col = this.maze_.pegmanX; const row = this.maze_.pegmanY; if (!this.isHive(row, col)) { this.maze_.executionInfo.terminateWithValue(TerminationValue.NOT_AT_HONEYCOMB); - return; + return false; } if (this.hiveRemainingCapacity(row, col) === 0) { this.maze_.executionInfo.terminateWithValue(TerminationValue.HONEYCOMB_FULL); - return; + return false; } - this.maze_.executionInfo.queueAction('honey', id); this.madeHoneyAt(row, col); + return true; } nectarRemaining(userCheck=false) { @@ -335,33 +347,22 @@ export default class Bee extends Gatherer { const col = this.maze_.pegmanX; const row = this.maze_.pegmanY; - if (this.getValue(row, col) <= 0) { - throw new Error("Shouldn't be able to end up with a nectar animation if " + - "there was no nectar to be had"); + if (this.getNectar()) { + this.playAudio_(NECTAR_SOUND); + this.drawer.updateItemImage(row, col, true); + this.drawer.updateNectarCounter(this.nectars_); } - - this.playAudio_(NECTAR_SOUND); - this.gotNectarAt(row, col); - - this.drawer.updateItemImage(row, col, true); - this.drawer.updateNectarCounter(this.nectars_); } animateMakeHoney() { const col = this.maze_.pegmanX; const row = this.maze_.pegmanY; - if (!this.isHive(row, col)) { - throw new Error("Shouldn't be able to end up with a honey animation if " + - "we arent at a hive or dont have nectar"); + if (this.makeHoney()) { + this.playAudio_(HONEY_SOUND); + this.drawer.updateItemImage(row, col, true); + this.drawer.updateHoneyCounter(this.honey_); } - - this.playAudio_(HONEY_SOUND); - this.madeHoneyAt(row, col); - - this.drawer.updateItemImage(row, col, true); - - this.drawer.updateHoneyCounter(this.honey_); } /** From 79fb2e0f8effa8b978282b6f6b2eae7dae272f49 Mon Sep 17 00:00:00 2001 From: Elijah Hamovitz Date: Mon, 26 Feb 2018 13:17:57 -0800 Subject: [PATCH 2/3] separate headless and animation versions entirely within subtype code, make the api responsible for conditionally connecting them --- apps/src/maze/api.js | 8 +++++-- apps/src/maze/bee.js | 55 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/apps/src/maze/api.js b/apps/src/maze/api.js index 417336f3b9de1..d9204ad3b44f2 100644 --- a/apps/src/maze/api.js +++ b/apps/src/maze/api.js @@ -258,11 +258,15 @@ exports.loopHighlight = API_FUNCTION(function (id) { * whether or not we're a Bee level */ exports.getNectar = API_FUNCTION(function (id) { - Maze.executionInfo.queueAction('nectar', id); + if (Maze.controller.subtype.getNectar()) { + Maze.executionInfo.queueAction("nectar", id); + } }); exports.makeHoney = API_FUNCTION(function (id) { - Maze.executionInfo.queueAction('honey', id); + if (Maze.controller.subtype.makeHoney()) { + Maze.executionInfo.queueAction("honey", id); + } }); exports.atFlower = API_FUNCTION(function (id) { diff --git a/apps/src/maze/bee.js b/apps/src/maze/bee.js index 6e1473dc6204f..e107745d27fe1 100644 --- a/apps/src/maze/bee.js +++ b/apps/src/maze/bee.js @@ -280,7 +280,10 @@ export default class Bee extends Gatherer { * Attempt to harvest nectar from the current location; terminate the * execution if this is not a valid place at which to get nectar. * - * @return {boolean} whether or not the attempt was successful + * This method is preferred over animateGetNectar for "headless" operation (ie + * when validating quantum levels) + * + * @return {boolean} whether or not this attempt was successful */ getNectar() { const col = this.maze_.pegmanX; @@ -306,7 +309,10 @@ export default class Bee extends Gatherer { * this is not a valid place at which to make honey. * Note that this deliberately does not check whether bee has gathered nectar. * - * @return {boolean} whether or not the attempt was successful + * This method is preferred over animateGetHoney for "headless" operation (ie + * when validating quantum levels) + * + * @return {boolean} whether or not this attempt was successful */ makeHoney() { const col = this.maze_.pegmanX; @@ -343,26 +349,55 @@ export default class Bee extends Gatherer { return this.hiveRemainingCapacity(row, col); } + /** + * Display the harvesting of nectar from the current location; raise a runtime + * error if the current location is not a valid spot from which to gather + * nectar. + * + * This method is preferred over getNectar for live operation (ie when + * actually displaying something to the user) + * + * @throws Will throw an error if the current cell has no nectar. + */ animateGetNectar() { const col = this.maze_.pegmanX; const row = this.maze_.pegmanY; - if (this.getNectar()) { - this.playAudio_(NECTAR_SOUND); - this.drawer.updateItemImage(row, col, true); - this.drawer.updateNectarCounter(this.nectars_); + if (this.getValue(row, col) <= 0) { + throw new Error("Shouldn't be able to end up with a nectar animation if " + + "there was no nectar to be had"); } + + this.playAudio_(NECTAR_SOUND); + this.gotNectarAt(row, col); + + this.drawer.updateItemImage(row, col, true); + this.drawer.updateNectarCounter(this.nectars_); } + /** + * Display the making of honey from the current location; raise a runtime + * error if the current location is not a valid spot at which to make honey. + * + * This method is preferred over getHoney for live operation (ie when + * actually displaying something to the user) + * + * @throws Will throw an error if the current cell is not a hive. + */ animateMakeHoney() { const col = this.maze_.pegmanX; const row = this.maze_.pegmanY; - if (this.makeHoney()) { - this.playAudio_(HONEY_SOUND); - this.drawer.updateItemImage(row, col, true); - this.drawer.updateHoneyCounter(this.honey_); + if (!this.isHive(row, col)) { + throw new Error("Shouldn't be able to end up with a honey animation if " + + "we arent at a hive or dont have nectar"); } + + this.playAudio_(HONEY_SOUND); + this.madeHoneyAt(row, col); + + this.drawer.updateItemImage(row, col, true); + this.drawer.updateHoneyCounter(this.honey_); } /** From 8c75ba1e0f0272044b945c9e3c0dd0b0ffd4a0eb Mon Sep 17 00:00:00 2001 From: Elijah Hamovitz Date: Wed, 28 Feb 2018 14:35:31 -0800 Subject: [PATCH 3/3] rename getNectar/makeHoney to tryGetNectar/MakeHoney to better represent their return values --- apps/src/maze/api.js | 4 ++-- apps/src/maze/bee.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/src/maze/api.js b/apps/src/maze/api.js index d9204ad3b44f2..d21011120ca5e 100644 --- a/apps/src/maze/api.js +++ b/apps/src/maze/api.js @@ -258,13 +258,13 @@ exports.loopHighlight = API_FUNCTION(function (id) { * whether or not we're a Bee level */ exports.getNectar = API_FUNCTION(function (id) { - if (Maze.controller.subtype.getNectar()) { + if (Maze.controller.subtype.tryGetNectar()) { Maze.executionInfo.queueAction("nectar", id); } }); exports.makeHoney = API_FUNCTION(function (id) { - if (Maze.controller.subtype.makeHoney()) { + if (Maze.controller.subtype.tryMakeHoney()) { Maze.executionInfo.queueAction("honey", id); } }); diff --git a/apps/src/maze/bee.js b/apps/src/maze/bee.js index e107745d27fe1..dea35b765ff57 100644 --- a/apps/src/maze/bee.js +++ b/apps/src/maze/bee.js @@ -285,7 +285,7 @@ export default class Bee extends Gatherer { * * @return {boolean} whether or not this attempt was successful */ - getNectar() { + tryGetNectar() { const col = this.maze_.pegmanX; const row = this.maze_.pegmanY; @@ -314,7 +314,7 @@ export default class Bee extends Gatherer { * * @return {boolean} whether or not this attempt was successful */ - makeHoney() { + tryMakeHoney() { const col = this.maze_.pegmanX; const row = this.maze_.pegmanY; @@ -354,7 +354,7 @@ export default class Bee extends Gatherer { * error if the current location is not a valid spot from which to gather * nectar. * - * This method is preferred over getNectar for live operation (ie when + * This method is preferred over tryGetNectar for live operation (ie when * actually displaying something to the user) * * @throws Will throw an error if the current cell has no nectar. @@ -379,7 +379,7 @@ export default class Bee extends Gatherer { * Display the making of honey from the current location; raise a runtime * error if the current location is not a valid spot at which to make honey. * - * This method is preferred over getHoney for live operation (ie when + * This method is preferred over tryMakeHoney for live operation (ie when * actually displaying something to the user) * * @throws Will throw an error if the current cell is not a hive.