MCPcopy Create free account
hub / github.com/codecombat/codecombat / getSolutionSrc

Function getSolutionSrc

app/core/store/modules/game.js:236–283  ·  view source on GitHub ↗
(state, getters, rootState)

Source from the content-addressed store, hash-verified

234 hasPlayedGame (state) { return state.hasPlayedGame },
235 levelSolution (state) { return state.levelSolution },
236 getSolutionSrc (state, getters, rootState) {
237 return function (codeLanguage) {
238 let component, hero, jsSource, source
239 if (!(hero = _.find((rootState.game?.level?.thangs || []), { id: 'Hero Placeholder' }))) {
240 return undefined
241 }
242
243 if (!(component = _.find((hero.components || []), c => c?.config?.programmableMethods?.plan))) {
244 return undefined
245 }
246
247 const {
248 plan
249 } = component.config.programmableMethods
250
251 const solutions = _.filter((plan?.solutions || []), s => !s.testOnly && s.succeeds)
252 if (rootState.game?.level?.type === 'web-dev') {
253 const htmlSource = (_.find(solutions, { language: 'html' }))?.source
254 if (htmlSource) {
255 if (/<playercode>/.test(htmlSource)) {
256 return utils.extractPlayerCodeTag(htmlSource)
257 }
258 return htmlSource
259 }
260 }
261 let rawSource = (_.find(solutions, { language: codeLanguage }))?.source
262 if (!rawSource && (jsSource = (_.find(solutions, { language: 'javascript' })?.source))) {
263 // If there is no target language solution yet, generate one from JavaScript.
264 rawSource = translateUtils.translateJS(jsSource, codeLanguage)
265 }
266
267 if (!rawSource) {
268 return undefined
269 }
270
271 try {
272 source = _.template(rawSource)(utils.i18n(plan, 'context'))
273 } catch (e) {
274 console.error(`Cannot auto fill solution: ${e.message}`)
275 }
276
277 if (_.isEmpty(source)) {
278 return undefined
279 }
280
281 return source
282 }
283 }
284 }
285}
286

Callers

nothing calls this directly

Calls 3

filterMethod · 0.80
templateMethod · 0.80
errorMethod · 0.45

Tested by

no test coverage detected