diff options
author | Marko Pušnik <marko.pusnik@guru.si> | 2015-09-30 19:07:37 +0200 |
---|---|---|
committer | Marko Pušnik <marko.pusnik@guru.si> | 2015-09-30 19:07:37 +0200 |
commit | 8bb68a1e979022ead00535c25403b341f2cb24bb (patch) | |
tree | 0ad16cf9acb81cbb6f8484f32fed657e8c66dabb /js/codeq/problem.js | |
parent | ba6135a83273c625097fe7cdb59319a51acca31e (diff) | |
parent | 0da1117cfc28688633be7b8382aa60435bf740eb (diff) |
Merge branch 'master' of odie.guru.si:codeq-web
Conflicts:
index.html
Diffstat (limited to 'js/codeq/problem.js')
-rw-r--r-- | js/codeq/problem.js | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/js/codeq/problem.js b/js/codeq/problem.js index d07e5fa..e3de85a 100644 --- a/js/codeq/problem.js +++ b/js/codeq/problem.js @@ -83,25 +83,51 @@ if (!translation || !(translation instanceof Array)) return false; return translation.length > 0; }, + /** + * Structurally converts the plan definition into something that the + * hint module can work with. + * The input is the translations object from problem.json in the form + * {'sl': { ..., 'plan': [hint1_sl, hint2_sl, ...]}, 'en': { ..., 'plan': [hint1_en, hint2_en, ...]}, ...} + * The output is a list of hints in the plan, translations are in each hint: + * [{'sl': hint1_sl, 'en': hint1_en, ...}, {'sl': hint2_sl, 'en': hint2_en}, ...] + */ processPlans = function (rawTranslations) { // find the default plan translation var defaultPlan = chooseDefaultTranslation(rawTranslations, 'plan', defaultPlanCondition) || [], - allPlans = {}, // the result - i, lang, tr; - // create all translations for plan + result = [], + i, lang, tr, plan, j, fragment; + if (defaultPlan.length == 0) return result; // no plan + // copy all translations from plan for (i = langs.length - 1; i >= 0; i--) { lang = langs[i]; tr = rawTranslations[lang]; // set up plan if (tr && defaultPlanCondition(tr.plan)) { - allPlans[lang] = tr.plan; + plan = tr.plan; } else { // there's no plan in the current language, copy the default plan - allPlans[lang] = defaultPlan; + plan = defaultPlan; + } + if (!(plan instanceof Array)) plan = [plan]; + for (j = 0; j < plan.length; j++) { + if (j < result.length) fragment = result[j]; + else { + fragment = {}; + result.push(fragment); + } + fragment[lang] = plan[j]; } } - return allPlans; + // ensure each plan element has all translations + for (j = result.length - 1; j >= 0; j--) { + fragment = result[j]; + for (i = langs.length - 1; i >= 0; i--) { + lang = langs[i]; + if (!fragment[lang]) fragment[lang] = 'Missing plan for language ' + lang + ' at index ' + j; + } + } + return result; }, // ================================================================================ @@ -240,7 +266,7 @@ .spread(function (userProblemData, generalProblemData) { if (userProblemData.code !== 0) throw new Error('Failed to obtain user problem data, code: ' + userProblemData.code + ', message: ' + userProblemData.message); if (!generalProblemData) throw new Error('General problem data is not defined'); - codeq.globalStateMachine.transition(language, generalProblemData, data.commonDef, userProblemData.solution); + codeq.globalStateMachine.transition(language, generalProblemData, data.commonDef, userProblemData.data.solution); }) ) .fail(function (reason) { @@ -356,4 +382,4 @@ $('#navigation-problem').css('display', 'none').removeClass("active"); } }); -})();
\ No newline at end of file +})(); |