Merge branch 'blockly-functions'

This commit is contained in:
Delucse
2020-09-17 13:24:38 +02:00
6 changed files with 26 additions and 6 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ import { withRouter } from 'react-router-dom';
import Compile from '../Compile';
import tutorials from './tutorials.json';
import { checkXml } from './compareXml';
import { checkXml } from '../../helpers/compareXml';
import { withStyles } from '@material-ui/core/styles';
import IconButton from '@material-ui/core/IconButton';
+3 -1
View File
@@ -11,6 +11,8 @@ import Instruction from './Instruction';
import Assessment from './Assessment';
import NotFound from '../NotFound';
import { detectWhitespacesAndReturnReadableResult } from '../../helpers/whitespace';
import tutorials from './tutorials.json';
import Card from '@material-ui/core/Card';
@@ -38,7 +40,7 @@ class Tutorial extends Component {
var tutorial = tutorials.filter(tutorial => tutorial.id === currentTutorialId)[0];
var steps = tutorial ? tutorial.steps : null;
var step = steps ? steps[this.props.activeStep] : null;
var name = step ? `${tutorial.title.replace(/\s/g,'')}_${step.headline.replace(/\s/g,'')}` : null;
var name = step ? `${detectWhitespacesAndReturnReadableResult(tutorial.title)}_${detectWhitespacesAndReturnReadableResult(step.headline)}` : null;
return (
!Number.isInteger(currentTutorialId) || currentTutorialId < 1 || currentTutorialId > tutorials.length ?
<NotFound button={{title: 'Zurück zur Tutorials-Übersicht', link: '/tutorial'}}/>
-81
View File
@@ -1,81 +0,0 @@
export const checkXml = (originalXmlString, userXmlString) => {
var originalXml = parseXml(originalXmlString);
var userXml = parseXml(userXmlString);
return compareXml(originalXml, userXml);
};
const parseXml = (xmlString) => {
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlString, "text/xml");
return xmlDoc;
};
const compareNumberOfBlocks = (originalBlocks, userBlocks) => {
if(originalBlocks.length !== userBlocks.length){
var blocks;
if(originalBlocks.length > userBlocks.length){
blocks = originalBlocks.length-userBlocks.length;
return {text: `Es wurde${blocks === 1 ? '' : 'n'} ${blocks} Bl${blocks === 1 ? 'ock' : 'öcke'} zu wenig verwendet.`, type: 'error'};
}
else {
blocks = userBlocks.length-originalBlocks.length;
return {text: `Es wurde${blocks === 1 ? '' : 'n'} ${blocks} Bl${blocks === 1 ? 'ock' : 'öcke'} zu viel verwendet.`, type: 'error'};
}
}
};
const compareBlockType = (originalBlock, userBlock, index) => {
if(originalBlock.attributes['type'].value !== userBlock.attributes['type'].value){
return {text: `Es wurde ein falscher Blocktyp an Position ${index+1} verwendet`, type: 'error'};
}
};
const compareParentBlock = (originalBlock, userBlock, index) => {
// using parentNode instead of parenElement
// see https://stackoverflow.com/questions/8685739/difference-between-dom-parentnode-and-parentelement
if(originalBlock.parentNode.attributes['name']){
if(userBlock.parentNode.attributes['name']){
// do the blocks have the same name-properties?
if(originalBlock.parentNode.attributes['name'].value !== userBlock.parentNode.attributes['name'].value){
if(userBlock.parentNode.attributes['name'].value === 'LOOP_FUNC' || userBlock.parentNode.attributes['name'].value === 'SETUP_FUNC'){
return {text: `Der Block mit dem Typen '${userBlock.attributes['type'].value}' wurde irrtümlicherweise in die ${userBlock.parentNode.attributes['name'].value === 'SETUP_FUNC' ? 'Setup' : 'Endlosschleifen'}-Funktion geschrieben.
Verschiebe den gesamten Block (und alle dazugehörigen Blöcke) in die ${userBlock.parentNode.attributes['name'].value !== 'SETUP_FUNC' ? 'Setup' : 'Endlosschleifen'}-Funktion.`, type: 'error'};
}
// TODO: has a block two name-properties?
return {text: `Der Block mit dem Typen '${userBlock.attributes['type'].value}' hat ein falsches 'name'-Attribut`, type: 'error'};
}
}
// user-block has not a name-attribute
else {
// do the user-block has a xmlns-attribute -> user-block is not connected
if(userBlock.parentNode.attributes['xmlns']){
return {text: `Der Block mit dem Typen '${userBlock.attributes['type'].value}' hat keine Verbindung zu einem anderen Block.`, type: 'error'};
}
// user-block has not a xmlns- AND name-attribute
else {
return {text: `Der Block an Position ${index+1} ist falsch eingeordnet. Tipp: Block an Position ${index+1} einem vorherigen Block unterordnen.`, type: 'error'};
}
}
}
};
const compareXml = (originalXml, userXml) => {
var originalItemList = originalXml.getElementsByTagName("block");
var userItemList = userXml.getElementsByTagName("block");
// compare number of blocks
var number = compareNumberOfBlocks(originalItemList, userItemList);
if(number){return number;}
for(var i=0; i < originalItemList.length; i++){
// compare type
var type = compareBlockType(originalItemList[i], userItemList[i], i);
if(type){return type;}
// compare name
var parent = compareParentBlock(originalItemList[i], userItemList[i], i);
if(parent){return parent;}
}
return {text: 'Super, alles richtig! Kompiliere nun die benutzen Blöcke, um eine BIN-Datei zu erhalten und damit das Programm auf die senseBox zu spielen und ausführen zu können.', type: 'success'};
};