-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
278 lines (270 loc) · 12.4 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Vue.createApp({
mounted() {
document.querySelector("html").addEventListener("click", () => {this.$refs.userInput.focus()});
setTimeout(() => {
this.isLoading = false;
this.getRandomQuote();
pContentId.classList.add("show");
}, 1000);
this.highScoreList = JSON.parse(localStorage.getItem('localHighScoreList') || sessionStorage.getItem('sessionHighScoreList')) ?? {};
this.highScore = Number (localStorage.getItem('localOverallHSItem') || sessionStorage.getItem('sessionOverallHSItem'));
},
data() {
return {
quotes: [
{ id: 0, text: "There is nothing wrong with letting people who love you, help you.", byWho: "- Iroh", affinity: "fire"},
{ id: 1, text: "While it is always best to believe in oneself, a little help from others can be a great blessing.", byWho: "- Iroh", affinity: "fire"},
{ id: 2, text: "It is usually best to admit mistakes when they occur, and to seek to restore honor.", byWho: "- Iroh", affinity: "fire"},
{ id: 3, text: "It's time for you to look inward and begin asking yourself the big question: who are you and what do you want?", byWho: "- Iroh", affinity: "fire"},
{ id: 4, text: "You must never give in to despair. Allow yourself to slip down that road and you surrender to your lowest instincts.", byWho: "- Zuko", affinity: "fire"},
{ id: 5, text: "Life happens wherever you are, whether you make it or not.", byWho: "- Iroh", affinity: "fire"},
{ id: 6, text: "Sharing tea with a fascinating stranger is one of life's true delights.", byWho: "- Iroh", affinity: "fire"},
{ id: 7, text: "The greatest illusion of this world is the illusion of separation. Things you think are separate and different are actually one and the same.", byWho: "- Guru Pathik", affinity: "spirit"},
{ id: 8, text: "It is important to draw wisdom from many different places. If you take it from only one place, it becomes rigid and stale.", byWho: "- Iroh", affinity: "fire"},
{ id: 9, text: "Failure is only the opportunity to begin again. Only this time, more wisely.", byWho: "- Iroh", affinity: "fire"},
{ id: 10, text: "Sometimes the best way to solve your own problems is to help someone else.", byWho: "- Iroh", affinity: "fire"},
{ id: 11, text: "The Fire Nation and the Avatar are linked, but now the Avatar is the last hope for the world.", byWho: "- Roku", affinity: "fire"},
{ id: 12, text: "As the Avatar, you are not meant to hurt others, but to bring peace.", byWho: "- Roku", affinity: "fire"},
{ id: 13, text: "I know now that no one can give you your honor. It's something you earn for yourself by choosing to do what's right.", byWho: "- Zuko", affinity: "fire"},
{ id: 14, text: "In the end, we will remember not the words of our enemies, but the silence of our friends.", byWho: "- Roku", affinity: "fire"},
{ id: 15, text: "When we hit our lowest point, we are open to the greatest change.", byWho: "- Roku", affinity: "fire"},
{ id: 16, text: "Destiny is a funny thing. You never know how things are going to work out.", byWho: "- Aang", affinity: "air"},
{ id: 17, text: "Fire is the element of power. The people of the Fire Nation have desire and will, and the energy and drive to achieve what they want.", byWho: "- Iroh", affinity: "fire"},
{ id: 18, text: "The true mind can weather all the lies and illusions without being lost. The true heart can touch the poison of hatred without being harmed.", byWho: "- Lion Turtle", affinity: "spirit"},
{ id: 19, text: "You have light and peace inside of you. If you let it out, you can change the world around you.", byWho: "- Iroh", affinity: "fire"},
{ id: 20, text: "The greatest victories are those won without fighting.", byWho: "- Iroh", affinity: "fire"},
{ id: 21, text: "In the darkest times, hope is something you give yourself. That is the meaning of inner strength.", byWho: "- Iroh", affinity: "fire"},
{ id: 22, text: "Sometimes life is like this dark tunnel, you can't always see the light at the end of the tunnel, but if you just keep moving, you will come to a better place.", byWho: "- Iroh", affinity: "fire"},
{ id: 23, text: "Some friendships are so strong, they can even transcend lifetimes.", byWho: "- Katara", affinity: "water"},
{ id: 24, text: "Water is the element of change. The people of the Water Tribe are capable of adapting to many things.", byWho: "- Katara", affinity: "water"},
],
imageIndex: 0,
imageSources: [
"https://i1.sndcdn.com/artworks-000664630216-y0lu1g-t500x500.jpg",
"https://i1.sndcdn.com/avatars-9MToyZakOV3d5S5y-tW0azw-t500x500.jpg",
"https://i.pinimg.com/originals/72/ba/3e/72ba3ec4873f1b5ad6845108669afeba.png",
"https://i1.sndcdn.com/artworks-Mp1kZBy3udSm8MZM-sPWreQ-t500x500.jpg",
"https://i1.sndcdn.com/artworks-UZ5xlnRvFCzmDyGN-sk4UGQ-t500x500.jpg",
"https://i1.sndcdn.com/artworks-000623230642-uapld7-t500x500.jpg",
"https://i.pinimg.com/564x/7c/f8/7c/7cf87cff46ab2bfae880b0f16aac22de.jpg",
],
userInput: "",
userInputted: false,
randomQuote: null,
randomQuoteByWho: "",
randomQuoteAffinity: "",
correctlyTypedWords: [],
startTime: null,
endTime: null,
wordsTyped: 0,
charactersTyped: 0,
typingSpeed: 0,
highScore: 0,
highScoreMsg: "",
highScoreQuote: 0,
quoteHighScoreMsg: "",
ended: false,
currentQuoteId: null,
highScoreList: {},
isLoading: true,
isDefaultBody: true,
};
},
computed: {
quoteCharacters() {
return this.userInputted && this.randomQuote ?
this.randomQuote.map((char, index) => {
const correctlyTypedSoFar =
this.correctlyTypedWords.join("").length +
this.correctlyTypedWords.length;
const position = index - correctlyTypedSoFar
if (index < correctlyTypedSoFar) {
return { char: char.char, isCorrect: true };
}
else if (index < this.charactersTyped) {
const isCorrect = this.userInput[position] === char.char
return {
char: char.char,
isCorrect: isCorrect,
isIncorrect: !isCorrect,
isDefault: false,
};
}
else {
return char;
}
})
: this.randomQuote ? this.randomQuote : [];
},
quoteWords() {
return this.randomQuote ? this.randomQuote.map(char => char.char).join("").split(" ") : [];
},
imageSource() {
return this.imageSources[this.imageIndex];
},
highScoreListMapped () {
return Object.entries(this.highScoreList).map(([id, score]) => ({id, score, quote: this.quotes.find(quote => quote.id === parseInt(id)).text, byWho: this.quotes.find(quote => quote.id === parseInt(id)).byWho}));
},
},
methods: {
getRandomQuote() {
const randomIndex = Math.floor(Math.random() * this.quotes.length);
this.randomQuote = this.quotes[randomIndex].text.split("").map(char => ({char: char, isDefault: true}));
this.currentQuoteId = this.quotes[randomIndex].id;
this.randomQuoteByWho = this.quotes[randomIndex].byWho;
this.randomQuoteAffinity = this.quotes[randomIndex].affinity;
this.userInput = "";
this.correctlyTypedWords = [];
this.$nextTick(() => {
this.$refs.userInput.focus();
this.userInputted = false;
});
this.charactersTyped = 0;
this.ended = false;
this.closeModal();
},
startTyping() {
this.startTime = new Date();
},
endTyping() {
if (this.ended) return;
this.ended = true;
this.endTime = new Date();
const timeDiff = (this.endTime - this.startTime) / 1000; // Time difference in seconds
const minutes = timeDiff / 60; // Time difference in minutes
const correctWords = this.correctlyTypedWords.length;
this.typingSpeed = Math.round(correctWords / minutes);
if (!this.highScoreList[this.currentQuoteId] || (this.typingSpeed > this.highScoreList[this.currentQuoteId])) {
this.highScoreList[this.currentQuoteId] = this.typingSpeed;
this.quoteHighScoreMsg = "New Quote Highscore!";
}
else {
this.highScoreQuote = this.highScoreList[this.currentQuoteId];
this.quoteHighScoreMsg = "Quote Highscore: ";
}
if (this.typingSpeed > this.highScore) {
this.highScore = this.typingSpeed;
this.highScoreMsg = "New Overall Highscore!";
sessionStorage.setItem("sessionOverallHSItem", this.highScore);
}
else {
this.highScoreMsg = "Overall Highscore: ";
}
},
changeImg () {
const quoteBy = {
"- Aang": 0,
"- Iroh": 1,
"- Zuko": 2,
"- Guru Pathik": 3,
"- Lion Turtle": 4,
"- Roku": 5,
"- Katara": 6,
};
this.imageIndex = quoteBy[this.randomQuoteByWho];
byWhoModalId.classList.add("show");
},
closeModal() {
endModalId.classList.remove("show");
byWhoModalId.classList.remove("show");
highScoresModalId.classList.remove("show");
},
insideClick(event) {
event.stopPropagation();
},
copyText() {
const textArea = document.createElement("textarea");
textArea.value = '"' + this.quoteWords.join(" ") + '" ' + this.randomQuoteByWho;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy");
document.body.removeChild(textArea);
copyMsgId.classList.add("show");
setTimeout(() => {
copyMsgId.classList.remove("show");
}, 1000)
},
saveHighScore() {
localStorage.setItem("localHighScoreList", sessionStorage.getItem("sessionHighScoreList"));
localStorage.setItem("localOverallHSItem", this.highScore);
savedMsgId.classList.add("show");
setTimeout(() => {
savedMsgId.classList.remove("show");
}, 1000)
},
getHighScores() {
highScoresModalId.classList.add("show");
},
deleteHSQuote(index2) {
const hsListId = this.highScoreListMapped[index2]?.id;
const hsListScore = this.highScoreListMapped[index2].score;
if (hsListId) {
this.highScoreListMapped.splice(index2, 1);
delete this.highScoreList[hsListId];
}
if (hsListScore === this.highScore) {
this.highScoreListMapped.length ?
this.highScore = Math.max(...Object.values(this.highScoreList))
: this.highScore = 0;
sessionStorage.setItem("sessionOverallHSItem", this.highScore);
}
deletedMsgId.classList.add("show");
setTimeout(() => {
deletedMsgId.classList.remove("show");
}, 1000)
},
},
watch: {
userInput(newVal) {
this.userInputted = true;
let isLastWord = this.quoteWords.length - this.correctlyTypedWords.length === 1;
if (newVal[newVal.length - 1] === " " || isLastWord) {
if (
this.userInput.trim() ===
this.quoteWords[this.correctlyTypedWords.length]
) {
this.correctlyTypedWords.push(this.userInput.trim());
}
if (!isLastWord) this.userInput = "";
}
if (this.correctlyTypedWords.length === this.quoteWords.length) {
endModalId.classList.add("show");
this.userInput = "";
this.endTyping();
}
const correctlyTypedSoFar =
this.correctlyTypedWords.join("").length +
this.correctlyTypedWords.length;
this.charactersTyped = correctlyTypedSoFar + newVal.length;
},
userInputted(newVal) {
if (newVal) {
this.startTyping();
}
},
highScoreList: {
handler(newVal) {
sessionStorage.setItem("sessionHighScoreList", JSON.stringify(newVal));
},
deep: true,
},
randomQuoteAffinity(newVal) {
document.body.classList.remove("fireBody", "waterBody", "airBody", "earthBody", "spiritBody");
if (newVal === "fire") {
document.body.classList.add("fireBody");
}
if (newVal === "water") {
document.body.classList.add("waterBody");
}
if (newVal === "air") {
document.body.classList.add("airBody");
}
if (newVal === "earth") {
document.body.classList.add("earthBody");
}
if (newVal === "spirit") {
document.body.classList.add("spiritBody");
}
},
},
}).mount("#typingGame");