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

Function autoFillSolution

app/core/store/modules/game.js:173–227  ·  view source on GitHub ↗
({ commit, getters, rootState }, codeLanguage)

Source from the content-addressed store, hash-verified

171 return commit('setHasPlayedGame', hasPlayed)
172 },
173 autoFillSolution ({ commit, getters, rootState }, codeLanguage) {
174 let source
175 if (!utils.showOzaria()) {
176 if (codeLanguage == null) {
177 let left
178 codeLanguage = (left = utils.getQueryVariable('codeLanguage')) != null ? left : 'javascript'
179 } // Belongs in Vuex eventually
180 const noSolution = function () {
181 const text = `No ${codeLanguage} solution available for ${rootState.game.level.name}.`
182 noty({ text, timeout: 3000 })
183 return console.error(text)
184 }
185
186 source = getters.getSolutionSrc(codeLanguage)
187
188 if (source == null) { noSolution() }
189
190 return commit('setLevelSolution', {
191 autoFillCount: rootState.game.levelSolution.autoFillCount + 1,
192 source
193 })
194 } else { // Ozaria
195 try {
196 let jsSource
197 const hero = _.find(((rootState.game.level != null ? rootState.game.level.thangs : undefined) != null ? (rootState.game.level != null ? rootState.game.level.thangs : undefined) : []), { id: 'Hero Placeholder' })
198 const component = _.find(hero.components != null ? hero.components : [], x => __guard__(__guard__(x != null ? x.config : undefined, x2 => x2.programmableMethods), x1 => x1.plan))
199 const plan = __guard__(component.config != null ? component.config.programmableMethods : undefined, x => x.plan)
200 const solutions = _.filter(((plan != null ? plan.solutions : undefined) != null ? (plan != null ? plan.solutions : undefined) : []), s => !s.testOnly && s.succeeds)
201 let rawSource = __guard__(_.find(solutions, { language: codeLanguage }), x1 => x1.source)
202 if (!rawSource && (jsSource = __guard__(_.find(solutions, { language: 'javascript' }), x2 => x2.source))) {
203 // If there is no target language solution yet, generate one from JavaScript.
204 rawSource = translateUtils.translateJS(jsSource, codeLanguage)
205 }
206 // eslint-disable-next-line camelcase
207 const external_ch1_avatar = __guard__(rootState.me.ozariaUserOptions != null ? rootState.me.ozariaUserOptions.avatar : undefined, x3 => x3.avatarCodeString) != null ? __guard__(rootState.me.ozariaUserOptions != null ? rootState.me.ozariaUserOptions.avatar : undefined, x3 => x3.avatarCodeString) : 'crown'
208 // eslint-disable-next-line camelcase
209 const context = _.merge({ external_ch1_avatar }, utils.i18n(plan, 'context'))
210 source = _.template(rawSource)(context)
211
212 if (!_.isEmpty(source)) {
213 return commit('setLevelSolution', {
214 autoFillCount: rootState.game.levelSolution.autoFillCount + 1,
215 source
216 })
217 } else {
218 noty({ text: 'No solution available.', timeout: 3000 })
219 return console.error(`Could not find solution for ${rootState.game.level.name}`)
220 }
221 } catch (e) {
222 const text = `Cannot auto fill solution: ${e.message}`
223 console.error(text)
224 return noty({ type: 'error', text })
225 }
226 }
227 }
228 },
229 getters: {
230 codeBankOpen (state) { return state.codeBankOpen },

Callers

nothing calls this directly

Calls 5

noSolutionFunction · 0.85
filterMethod · 0.80
templateMethod · 0.80
__guard__Function · 0.70
errorMethod · 0.45

Tested by

no test coverage detected