| 701 | } |
| 702 | |
| 703 | const createBlock = function ({ owner, prop, generator, codeLanguage, codeFormat, include, level, superBasicLevels, propNames }) { |
| 704 | const propName = prop.name ? prop.name.replace(/"/g, '') : undefined |
| 705 | const returnsValue = (prop.returns != null) || (prop.userShouldCaptureReturn != null) || (!['function', 'snippet'].includes(prop.type)) |
| 706 | const name = `Hero_${propName}` |
| 707 | let args = prop.args || [] |
| 708 | if (superBasicLevels?.includes(level?.get('slug')) && (['moveDown', 'moveLeft', 'moveRight', 'moveUp'].includes(propName))) { |
| 709 | // Don't include steps argument yet |
| 710 | args = [] |
| 711 | } |
| 712 | |
| 713 | generator.forBlock[name] = function (block) { |
| 714 | const parts = [] |
| 715 | if (propName && ['this', 'self', 'hero'].includes(prop.owner)) { |
| 716 | if (level?.get('product') === 'codecombat-junior') { |
| 717 | parts.push(propName) // Functional |
| 718 | } else { |
| 719 | parts.push(`hero.${propName}`) // Object-oriented |
| 720 | } |
| 721 | } else if (prop.type === 'function' || (prop.type === 'snippet' && args.length)) { |
| 722 | parts.push(propName.replace(/\(.*/, '')) |
| 723 | } else if (propName) { |
| 724 | parts.push(propName) |
| 725 | } else { |
| 726 | parts.push(owner) |
| 727 | } |
| 728 | |
| 729 | if (prop.type === 'function' || (prop.type === 'snippet' && args.length)) { |
| 730 | parts.push('(') |
| 731 | Object.keys(args).forEach((idx) => { |
| 732 | if (idx > 0) parts.push(', ') |
| 733 | const arg = args[idx] |
| 734 | // let code = generator.valueToCode(block, arg.name, generator.ORDER_ATOMIC) |
| 735 | let code = generator.valueToCode(block, arg.name, generator.ORDER_CONDITIONAL) |
| 736 | if (!code && arg.name === 'to') { |
| 737 | code = `'${block.getFieldValue(arg.name)}'` |
| 738 | } |
| 739 | if (!code && ['steps', 'squares'].includes(arg.name)) { |
| 740 | code = `${block.getFieldValue(arg.name)}` |
| 741 | } |
| 742 | if (!code && arg.default) { |
| 743 | if (/move(Up|Left|Right|Down)/.test(propName)) { |
| 744 | // Don't add the default value |
| 745 | } else { |
| 746 | code = arg.default |
| 747 | } |
| 748 | } |
| 749 | switch (codeLanguage) { |
| 750 | case 'javascript': |
| 751 | parts.push(code ?? `undefined /* ${arg.name} */`) |
| 752 | break |
| 753 | case 'python': |
| 754 | parts.push(code ?? 'None') |
| 755 | break |
| 756 | case 'lua': |
| 757 | parts.push(code ?? 'nil') |
| 758 | break |
| 759 | } |
| 760 | }) |