Skip to content

Commit

Permalink
Teacher tool: fixed successful blocks for blocks exist new blocks exi…
Browse files Browse the repository at this point in the history
…st schema (#9908)

* fixed successful blocks for blocks exist new blocks exist schema

* changed successful blocks to be a map, iterating through all block counts for blocksexist
  • Loading branch information
srietkerk authored Mar 7, 2024
1 parent c93a681 commit 80c84d3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions pxteditor/code-validation/runValidatorPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ function runBlocksExistValidation(usedBlocks: Blockly.Block[], inputs: pxt.block
const blockResults = validateBlocksExist({ usedBlocks, requiredBlockCounts });
let successfulBlocks: Blockly.Block[] = [];
if (blockResults.passed) {
const blockIdsFromValidator = Object.keys(inputs.blockCounts)
const blockId = blockIdsFromValidator?.[0];
successfulBlocks = blockResults.successfulBlocks.length ? blockResults.successfulBlocks[0][blockId] : [];
for (const blockCount of inputs.blockCounts) {
const blockId = blockCount.blockId;
successfulBlocks.push(...blockResults.successfulBlocks[blockId]);
}
}
return [successfulBlocks, blockResults.passed];
}
Expand Down
8 changes: 4 additions & 4 deletions pxteditor/code-validation/validateBlocksExist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ export function validateBlocksExist({ usedBlocks, requiredBlockCounts }: {
missingBlocks: string[],
disabledBlocks: string[],
insufficientBlocks: string[],
successfulBlocks: pxt.Map<Blockly.Block[]>[],
successfulBlocks: pxt.Map<Blockly.Block[]>,
passed: boolean
} {
let missingBlocks: string[] = [];
let disabledBlocks: string[] = [];
let insufficientBlocks: string[] = [];
let successfulBlocks: pxt.Map<Blockly.Block[]>[] = [];
let successfulBlocks: pxt.Map<Blockly.Block[]> = {};
const userBlocksEnabledByType = usedBlocks?.reduce((acc: pxt.Map<number>, block) => {
acc[block.type] = (acc[block.type] || 0) + (block.isEnabled() ? 1 : 0);
return acc;
}, {});

for (const [requiredBlockId, requiredCount] of Object.entries(requiredBlockCounts || {})) {
const countForBlock = userBlocksEnabledByType[requiredBlockId];
const passedBlocks = usedBlocks.filter((block) => block.type === requiredBlockId);
const passedBlocks = usedBlocks.filter((block) => block.isEnabled() && block.type === requiredBlockId);
if (passedBlocks.length > 0) {
successfulBlocks.push({ [requiredBlockId]: passedBlocks})
successfulBlocks[requiredBlockId] = passedBlocks;
}

if (countForBlock === undefined) {
Expand Down

0 comments on commit 80c84d3

Please sign in to comment.