Skip to content

Commit

Permalink
Merge pull request #42243 from code-dot-org/aug28-validation
Browse files Browse the repository at this point in the history
[PoemBot][HOC] Validation Code
  • Loading branch information
ajpal committed Aug 31, 2021
2 parents 18f0ec9 + 521fd96 commit 4544a05
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
6 changes: 5 additions & 1 deletion apps/i18n/spritelab/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,9 @@
"thirdClickButNoSpeech": "You clicked on more than two sprites but one of your clicks didn't cause a sprite to say anything. Add a `say` block under each `when clicked` event. <xml><block type=\"gamelab_spriteSay\"><title name=\"SPEECH\">Hello, world!</title><value name=\"SPRITE\"><block type=\"gamelab_allSpritesWithAnimation\"><title name=\"ANIMATION\">\"default\"</title></block></value></block></xml>",
"changeOrSetSize": "None of your sprites changed size.",
"changeOrSetCostume": "None of your sprites changed costumes.",
"changeBackground": "The background was not changed."
"changeBackground": "The background was not changed.",
"poemBotMakeBackground": "You need to add a background effect.",
"poemBotMakeForeground": "You need to add a foreground effect.",
"poemBotRemovedSprite": "It looks like you removed a sprite. You can add it back from the **Sprites** toolbox.",
"poemBotSetSize": "Use a `set size` block to change the size of your sprite. Use a number other than 100."
}
35 changes: 35 additions & 0 deletions apps/src/p5lab/spritelab/libraries/PoemBotLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const POEM_DURATION = 500;
export default class PoemBotLibrary extends CoreLibrary {
constructor(p5) {
super(p5);
// Extra information for validation code to be able to inspect the program state
this.validationInfo = {
endTime: POEM_DURATION
};
this.poemState = {
title: '',
author: '',
Expand Down Expand Up @@ -136,6 +140,37 @@ export default class PoemBotLibrary extends CoreLibrary {
this.lineEvents[lineNum].push(callback);
},

getValidationInfo() {
return this.validationInfo;
},

setSuccessFrame() {
if (!this.validationInfo.successFrame) {
// Only set the success frame if it hasn't already been set (the first
// frame at which we know the student will pass the level).
this.validationInfo.successFrame = this.p5.frameCount;
}
},

drawProgressBar() {
this.p5.push();
this.p5.noStroke();
if (this.validationInfo.successFrame) {
// The student will pass the level
this.p5.fill(this.p5.rgb(0, 173, 188));
} else {
// The student will not pass the level (yet);
this.p5.fill(this.p5.rgb(118, 102, 160));
}
this.p5.rect(
0,
390,
(this.p5.frameCount / POEM_DURATION) * PLAYSPACE_SIZE,
10
);
this.p5.pop();
},

...backgroundEffects,
...foregroundEffects
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {PALETTES} from '../constants';
export const commands = {
// TODO: would it be possible to re-use the background/foreground effect code from dance party?
setBackgroundEffect(effectName, palette) {
this.validationInfo.backgroundEffect = effectName;
switch (effectName) {
case 'colors': {
let amount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as utils from './utils';

export const commands = {
setForegroundEffect(effectName) {
this.validationInfo.foregroundEffect = effectName;
switch (effectName) {
case 'rain': {
const drops = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"preload_asset_list": null,
"encrypted_examples": [

]
],
"validation_code": "var validationInfo = getValidationInfo();\r\nif (validationInfo.backgroundEffect) {\r\n setSuccessFrame();\r\n}\r\ndrawProgressBar();\r\nif (World.frameCount == validationInfo.endTime) {\r\n if (validationInfo.successFrame) {\r\n levelFailure(0, 'genericSuccess');\r\n } else {\r\n levelFailure(3, 'poemBotMakeBackground');\r\n }\r\n}"
},
"game_id": 64,
"published": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"preload_asset_list": null,
"encrypted_examples": [

]
],
"validation_code": "var validationInfo = getValidationInfo();\r\nif (validationInfo.foregroundEffect) {\r\n setSuccessFrame();\r\n}\r\ndrawProgressBar();\r\nif (World.frameCount == validationInfo.endTime) {\r\n if (validationInfo.successFrame) {\r\n levelFailure(0, 'genericSuccess');\r\n } else {\r\n levelFailure(3, 'poemBotMakeForeground');\r\n }\r\n}"
},
"game_id": 64,
"published": true,
Expand Down
1 change: 1 addition & 0 deletions dashboard/config/scripts/levels/poetryHoC_addSprites.level
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"callout_json": "[]",
"parent_level_id": 29613,
"video_key": "hoc_dance_properties",
"validation_code": "var validationInfo = getValidationInfo();\r\nvar spriteIds = getSpriteIdsInUse();\r\n\r\nif (spriteIds.length > 0) {\r\n setSuccessFrame();\r\n}\r\ndrawProgressBar();\r\nif (World.frameCount == validationInfo.endTime) {\r\n if (validationInfo.successFrame) {\r\n levelFailure(0, 'genericSuccess');\r\n } else {\r\n levelFailure(3, 'noSprites');\r\n }\r\n}",
"preload_asset_list": null
},
"user_id": 1196,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"preload_asset_list": null,
"encrypted_examples": [

]
],
"validation_code": "var validationInfo = getValidationInfo();\r\nvar spriteIds = getSpriteIdsInUse();\r\n\r\nfor (var i = 0; i < spriteIds.length; i++) {\r\n if (getProp({id: spriteIds[i]}, \"scale\") !== 100) {\r\n setSuccessFrame();\r\n }\r\n}\r\n\r\ndrawProgressBar();\r\nif (World.frameCount == validationInfo.endTime) {\r\n if (validationInfo.successFrame) {\r\n levelFailure(0, 'genericSuccess');\r\n } else if (spriteIds.length == 0) {\r\n levelFailure(3, 'poemBotRemovedSprite');\r\n } else {\r\n levelFailure(3, 'poemBotSetSize');\r\n }\r\n}"
},
"game_id": 64,
"published": true,
Expand Down

0 comments on commit 4544a05

Please sign in to comment.