| 889 | }) |
| 890 | |
| 891 | function generateRandomCodeForApis (apis) { |
| 892 | let indent = 0 |
| 893 | const solutionCodeLines = [] |
| 894 | while (solutionCodeLines.length < 3 + Math.random() * 10) { |
| 895 | const api = _.sample(apis) |
| 896 | let prefix = '' |
| 897 | for (let i = 0; i < indent * 4; ++i) { |
| 898 | prefix += ' ' |
| 899 | } |
| 900 | if (api === 'for-loop') { |
| 901 | solutionCodeLines.push(prefix + `for (let i = 0; i < ${Math.ceil(Math.random() * 5)}; ++i {`) |
| 902 | ++indent |
| 903 | } else if (api === 'go') { |
| 904 | solutionCodeLines.push(prefix + `${api}('${_.sample(directionNames)}', ${Math.ceil(Math.random() * 4)})`) |
| 905 | } else if (['hit', 'zap'].includes(api)) { |
| 906 | solutionCodeLines.push(prefix + `${api}('${_.sample(directionNames)}')`) |
| 907 | } else if (['spin'].includes(api)) { |
| 908 | solutionCodeLines.push(prefix + `${api}()`) |
| 909 | // } else if (['look'].includes(api)) { |
| 910 | // solutionCodeLines.push(prefix + `${api}()`) |
| 911 | } else { |
| 912 | solutionCodeLines.push(prefix + `${api}()`) |
| 913 | } |
| 914 | } |
| 915 | while (indent > 0) { |
| 916 | let prefix = '' |
| 917 | for (let i = 0; i < indent * 4; ++i) { |
| 918 | prefix += ' ' |
| 919 | } |
| 920 | solutionCodeLines.push(prefix + '}') |
| 921 | --indent |
| 922 | } |
| 923 | const solutionCode = solutionCodeLines.join('\n') |
| 924 | const starterCode = solutionCodeLines.slice(0, 1 + Math.floor(Math.random() * 3)).join('\n') |
| 925 | return { solutionCode, starterCode } |
| 926 | } |
| 927 | |
| 928 | function updateProgrammableConfig ({ thangs, solutionCode, starterCode, apis }) { |
| 929 | const hero = _.find(thangs, (thang) => thang.id === 'Hero Placeholder') |