-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathend.js
More file actions
151 lines (115 loc) · 4.63 KB
/
Copy pathend.js
File metadata and controls
151 lines (115 loc) · 4.63 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
const username = document.getElementById('username');
const saveScoreBtn = document.getElementById('saveScoreBtn');
const alignment = document.getElementById('alignment');
const finalmoralScore = document.getElementById('finalmoralScore');
const finalethicScore = document.getElementById('finalethicScore');
const mostRecentMoralScore = localStorage.getItem('mostRecentMoralScore');
const mostRecentEthicScore = localStorage.getItem('mostRecentEthicScore');
const questions = localStorage.getItem('questions');
// JSON to CSV Converter
function ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
var answers = ConvertToCSV(questions);
const ethicprogressBarfull = document.getElementById("finalethicScoreBarfull");
const moralprogressBarfull = document.getElementById("finalmoralScoreBarfull");
const highScores = JSON.parse(localStorage.getItem("highScores")) || [];
const MAX_HIGH_SCORES = 5;
const MAX_MORAL_SCORE = 25;
const MIN_MORAL_SCORE = 5;
const MAX_ETHIC_SCORE = 25;
const MIN_ETHIC_SCORE = 5;
function normalize(val, max, min) {
return (val - min) / (max - min);
}
const moralScoreNorm = normalize(parseFloat(mostRecentMoralScore), MAX_MORAL_SCORE, MIN_MORAL_SCORE) * 100;
const ethicScoreNorm = normalize(parseFloat(mostRecentEthicScore), MAX_ETHIC_SCORE, MIN_ETHIC_SCORE) * 100;
ethicprogressBarfull.style.width = `${ethicScoreNorm}%`;
moralprogressBarfull.style.width = `${moralScoreNorm}%`;
function makeArr(startValue, stopValue, cardinality) {
var arr = [];
var step = (stopValue - startValue) / (cardinality - 1);
for (var i = 0; i < cardinality; i++) {
arr.push(startValue + (step * i));
}
return arr;
}
const q = makeArr(0, 100, 4);
if (ethicScoreNorm >= q[0] && ethicScoreNorm < q[1]) {
//chaotic
if (moralScoreNorm >= q[0] && moralScoreNorm < q[1]) {
//chaotic evil
alignment.innerText = "Chaotic Evil";
} else if (moralScoreNorm >= q[1] && moralScoreNorm < q[2]) {
//chaotic neutral
alignment.innerText = "Chaotic Neutral";
} else if (moralScoreNorm >= q[2] && moralScoreNorm <= q[3]) {
//chaotic good
alignment.innerText = "Chaotic Good";
}
} else if (ethicScoreNorm >= q[1] && ethicScoreNorm < q[2]) {
//neutral
if (moralScoreNorm >= q[0] && moralScoreNorm < q[1]) {
//neutral evil
alignment.innerText = "Neutral Evil";
} else if (moralScoreNorm >= q[1] && moralScoreNorm < q[2]) {
//True neutral
alignment.innerText = "True Neutral";
} else if (moralScoreNorm >= q[2] && moralScoreNorm <= q[3]) {
//neutral good
alignment.innerText = "Neutral Good";
}
} else if (ethicScoreNorm >= q[2] && ethicScoreNorm <= q[3]) {
//lawful
if (moralScoreNorm >= q[0] && moralScoreNorm < q[1]) {
//lawful evil
alignment.innerText = "Lawful Evil";
} else if (moralScoreNorm >= q[1] && moralScoreNorm < q[2]) {
//lawful neutral
alignment.innerText = "Lawful Neutral";
} else if (moralScoreNorm >= q[2] && moralScoreNorm <= q[3]) {
//lawful good
alignment.innerText = "Lawful Good";
}
}
finalmoralScore.innerText = mostRecentMoralScore;
finalethicScore.innerText = mostRecentEthicScore;
username.addEventListener('keyup', () => {
saveScoreBtn.disabled = !username.value;
});
saveHighScore = (e) => {
e.preventDefault();
const score = {
moralscore: mostRecentMoralScore,
ethicscore: mostRecentEthicScore,
alignment: alignment.innerText,
score: parseInt(mostRecentMoralScore) + parseInt(mostRecentEthicScore),
name: username.value
};
highScores.push(score);
highScores.sort((a, b) => {
return b.score - a.score;
})
highScores.splice(MAX_HIGH_SCORES);
localStorage.setItem('highScores', JSON.stringify(highScores));
window.location.assign("./");
//dowload data
let csvContent = "data:text/csv;charset=utf-8,"
+ answers;
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_alignment_data.csv");
document.body.appendChild(link); // Required for FF
link.click();
};