-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
138 lines (131 loc) · 5.19 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
let missionInfoJson;
async function loadMissionInfo() {
try {
const response = await fetch('MissionInfo.json');
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
missionInfoJson = await response.json();
} catch (error) {
console.error('There was a problem with the fetch operation:', error);
}
}
(async function () {
await loadMissionInfo();
let languages = Object.keys(missionInfoJson)
let chosenLanguge = getLanguageFromURL(languages)
createLangugeSelect(missionInfoJson,chosenLanguge)
translate(chosenLanguge)
reset()
})();
function getLanguageFromURL(allowedLanguges) {
const urlParams = new URLSearchParams(window.location.search);
let lang = urlParams.get('lang')
console.log(lang)
if (!allowedLanguges.includes(lang)) { lang = undefined }
return lang || 'english';
}
function translate(languge) {
document.getElementsByTagName('body')[0].style.direction = missionInfoJson[languge][0].textDirection
const article = document.getElementById('article')
missionInfoJson[languge].slice(1).forEach(mission => {
//create container
let container = document.createElement("div")
container.className = "misson"
article.appendChild(container)
//create name
container.appendChild(createText('h1', mission.name))
//create description
container.appendChild(createText('h4', mission.description))
//create img
let img = document.createElement('img')
img.src = mission.image
img.className = "misson-image"
container.appendChild(img)
//create missons container
let pointGiversContainer = document.createElement("div")
pointGiversContainer.className = "misson-buttons"
container.appendChild(pointGiversContainer)
//create extra info
if (mission.extra_info) {
pointGiversContainer.appendChild(createText('label', mission.extra_info + "\n"))
}
//create missons
mission.pointGivers.forEach(pointGiver => {
pointGiversContainer.appendChild(createText('label', pointGiver.name + " "))
let select = document.createElement('select')
select.onchange = calculate
select.className = "mission-select"
pointGiversContainer.appendChild(select)
if (pointGiver.type == "YESNO") {
select.appendChild(createOption(missionInfoJson[languge][0].no, 0))
select.appendChild(createOption(missionInfoJson[languge][0].yes, pointGiver.points))
}
else if (pointGiver.type == "MANY") {
for (let i = 0; i <= pointGiver.numberOfObjects; i++) {
if (i == 1) {
select.appendChild(createOption(i + " " + pointGiver.singleObjectName, pointGiver.points * i))
}
else { select.appendChild(createOption(i + " " + pointGiver.multibleObjectNames, pointGiver.points * i)) }
}
}
else if (pointGiver.type == "FORCED") {
Object.keys(pointGiver.points).forEach(pointName => {
let points = pointGiver.points[pointName]
select.appendChild(createOption(pointName, points))
});
}
pointGiversContainer.appendChild(createText('h1', ''))
})
});
}
function createText(type, text) {
let element = document.createElement(type)
let elementText = document.createTextNode(text)
element.appendChild(elementText)
return element
}
function createOption(text, value) {
let element = document.createElement('option')
let elementText = document.createTextNode(text)
element.value = value
element.appendChild(elementText)
return element
}
function calculate() {
const collection = document.getElementsByClassName("mission-select");
let score = 0
Array.from(collection).forEach(select => {
score = score + parseInt(select.value)
});
document.getElementById("score-display").innerText = "SCORE: " + score.toString()
score = 0
}
function reset() {
const collection = document.getElementsByClassName("misson-buttons");
var score = 0
Array.from(collection).forEach(div => {
const selectElements = div.querySelectorAll('select');
selectElements.forEach(selection => {
selection.selectedIndex = 0
})
})
calculate()
}
function createLangugeSelect(missionInfoJson,chosenLanguge){
console.log(chosenLanguge)
let select = document.getElementById('language-select')
let label = document.getElementById('language-text')
let languages = Object.keys(missionInfoJson)
languages.forEach(languge => {
select.appendChild(createOption(missionInfoJson[languge][0].name,languge))
})
console.log(missionInfoJson)
select.value = chosenLanguge
label.textContent = missionInfoJson[chosenLanguge][0].languge + ": "
select.style.left = '80px'
}
function changeLanguge(){
let select = document.getElementById('language-select')
window.location.href = "index.html?lang="+select.value;
}