summaryrefslogtreecommitdiff
path: root/js/codeq/problem.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/codeq/problem.js')
-rw-r--r--js/codeq/problem.js70
1 files changed, 47 insertions, 23 deletions
diff --git a/js/codeq/problem.js b/js/codeq/problem.js
index 7ae54d8..d07e5fa 100644
--- a/js/codeq/problem.js
+++ b/js/codeq/problem.js
@@ -10,13 +10,23 @@
// processed form, hint key -> translation language -> value
// ================================================================================
+ defaultHintCondition = function (translation) {
+ // must contain at least one translation
+ var key;
+ if (!translation || typeof translation !== 'object') return false;
+ for (key in translation) {
+ if (!translation.hasOwnProperty(key)) continue;
+ return true;
+ }
+ return false;
+ },
processHints = function (rawTranslations) {
var defaultHint = {}, // here we put all the hints with their default translations
allHints = {}, // the result
allHintKeys = [],
tr, key, i, lang, hint, h, j;
// find the default hint translations, they will form the basis of default hints
- tr = chooseDefaultTranslation(rawTranslations, 'hint') || {};
+ tr = chooseDefaultTranslation(rawTranslations, 'hint', defaultHintCondition) || {};
for (key in tr) { // copy the hints
if (!tr.hasOwnProperty(key)) continue;
defaultHint[key] = tr[key];
@@ -26,7 +36,7 @@
for (i = langs.length - 1; i >= 0; i--) {
lang = langs[i];
tr = rawTranslations[lang];
- if (!tr || !tr.hint) continue; // skip unavailable translations or translations with no hints
+ if (!tr || !defaultHintCondition(tr.hint)) continue; // skip unavailable translations or translations with no hints
hint = tr.hint;
for (key in hint) {
if (!hint.hasOwnProperty(key) || !hint[key]) continue;
@@ -37,22 +47,26 @@
}
}
// create all translations for hints
+ for (i = allHintKeys.length - 1; i >= 0; i--) {
+ allHints[allHintKeys[i]] = {}; // create keys with no translations, we'll fill them in the next loop
+ }
for (i = langs.length - 1; i >= 0; i--) {
lang = langs[i];
tr = rawTranslations[lang];
// set up hints
- if (!tr || !tr.hint) {
+ if (!tr || !defaultHintCondition(tr.hint)) {
// there's no hint in the current language, copy the default in its entirety
- allHints[lang] = defaultHint;
+ for (j = allHintKeys.length - 1; j >= 0; j--) {
+ key = allHintKeys[j];
+ allHints[key][lang] = defaultHint[key];
+ }
}
else {
// make a copy of all hints, using the default hint value where a hint value is missing
- hint = {};
- allHints[lang] = hint;
h = tr.hint;
- for (j = allHintKeys.length; j >= 0; j--) {
+ for (j = allHintKeys.length - 1; j >= 0; j--) {
key = allHintKeys[j];
- hint[key] = h[key] || defaultHint[key];
+ allHints[key][lang] = h[key] || defaultHint[key];
}
}
}
@@ -64,9 +78,14 @@
// processed form, hint key -> translation language -> value
// ================================================================================
+ defaultPlanCondition = function (translation) {
+ // default plan must be non-empty
+ if (!translation || !(translation instanceof Array)) return false;
+ return translation.length > 0;
+ },
processPlans = function (rawTranslations) {
// find the default plan translation
- var defaultPlan = chooseDefaultTranslation(rawTranslations, 'plan') || [],
+ var defaultPlan = chooseDefaultTranslation(rawTranslations, 'plan', defaultPlanCondition) || [],
allPlans = {}, // the result
i, lang, tr;
// create all translations for plan
@@ -74,12 +93,12 @@
lang = langs[i];
tr = rawTranslations[lang];
// set up plan
- if (!tr || !tr.plan) {
- // there's no plan in the current language, copy the default plan
- allPlans[lang] = defaultPlan;
+ if (tr && defaultPlanCondition(tr.plan)) {
+ allPlans[lang] = tr.plan;
}
else {
- allPlans[lang] = tr.plan;
+ // there's no plan in the current language, copy the default plan
+ allPlans[lang] = defaultPlan;
}
}
return allPlans;
@@ -111,7 +130,7 @@
/**
* convert the input translations (arg0) for given keys (arg1..argN)
- * so each keys holds all its translations for ever language
+ * so each keys holds all its translations for every language
*/
convertTranslations = function () {
var translations = arguments[0] || {},
@@ -187,7 +206,10 @@
'language': languageIdentifier, // 'prolog', 'python', ...
'html': html.join(''), // the DOM structure (without textual content), as HTML text
'refs': problemReferences, // array of problem info {g: group, p: problem, id: problem_id}, referenced from DOM <a> elements
- 'hints': processHints(rawTranslations) // hint translations: keyword -> lang -> value
+ 'commonDef': {
+ 'hint': processHints(rawTranslations), // hint translations: keyword -> lang -> value
+ 'hint_type': data.hint_type || {}
+ }
};
},
@@ -218,7 +240,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.hints, userProblemData.solution);
+ codeq.globalStateMachine.transition(language, generalProblemData, data.commonDef, userProblemData.solution);
})
)
.fail(function (reason) {
@@ -233,14 +255,15 @@
// Problem definition processing
// ================================================================================
- chooseDefaultTranslation = function (rawTranslations, translationKey) {
+ chooseDefaultTranslation = function (rawTranslations, translationKey, condition) {
var tr = rawTranslations.en, // try English as the default
- lang;
- if (tr && tr[translationKey]) return tr[translationKey];
- for (lang in rawTranslations) { // find a translation with hints
- if (!rawTranslations.hasOwnProperty(lang) || rawTranslations[lang]) continue;
+ lang, value;
+ if (typeof condition !== 'function') condition = function (x) { return !!x; };
+ if (tr && condition(tr[translationKey])) return tr[translationKey];
+ for (lang in rawTranslations) { // find a translation with content
+ if (!rawTranslations.hasOwnProperty(lang)) continue;
tr = rawTranslations[lang];
- if (tr[translationKey]) return tr[translationKey];
+ if (tr && condition(tr[translationKey])) return tr[translationKey];
}
return null; // default must be chosen by the caller
},
@@ -254,7 +277,8 @@
'id': rawData.id,
'translations': convertTranslations(rawTranslations, 'title', 'name', 'slug', 'description'), // GUI translations: keyword -> lang -> value
'hint': processHints(rawTranslations), // hint translations: keyword -> lang -> value
- 'plan': processPlans(rawTranslations) // plan translations: keyword -> lang -> value
+ 'plan': processPlans(rawTranslations), // plan translations: keyword -> lang -> value
+ 'hint_type': rawData.hint_type || {}
};
},