-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
148 lines (136 loc) · 5.11 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
139
140
141
142
143
144
145
146
147
148
// Typing Game Prototype
// Created by Catalina Escalona, Winter Semester 2023-24
// Course: "Web Coding: Intro to Web Development and Game Prototyping"
// University of Applied Arts Vienna
const quotes = [
"And I am bored to death with it. Bored to death with this place, bored to death with my life, bored to death with myself.",
"There were two classes of charitable people: one, the people who did a little and made a great deal of noise; the other, the people who did a great deal and made no noise at all.",
"Constancy in love is a good thing; but it means nothing, and is nothing, without constancy in every kind of effort.",
"A word in earnest is as good as a speech.",
"The universe makes rather an indifferent parent, I'm afraid.",
"I only ask to be free. The butterflies are free. Mankind will surely not deny to Harold Skimpole what it concedes to the butterflies.",
"When he has nothing else to do, he can always contemplate his own greatness. It is a considerable advantage to a man, to have so inexhaustible a subject.",
"But injustice breeds injustice; the fighting with shadows and being defeated by them necessitates the setting up of substances to combat.",
"I find the nights long, for I sleep but little, and think much.",
"Anything to vary this detestable monotony.",
"Chance people on the bridges peeping over the parapets into a nether sky of fog, with fog all round them, as if they were up in a balloon, and hanging in the misty clouds.",
"Mr. Snagsby, as a timid man, is accustomed to cough with a variety of expressions, and so to save words.",
"Trust in nothing but in Providence and your own efforts. Never separate the two, like the heathen waggoner.",
"He is an honorable, obstinate, truthful, high-spirited, intensely prejudiced, perfectly unreasonable man.",
"I had a confident expectation that things would come round and be all square.",
"Lady Dedlock is always the same exhausted deity, surrounded by worshippers, and terribly liable to be bored to death, even while presiding at her own shrine.",
]
const stats = {
words: 0,
characters: 0,
time: 0,
fastest: {
quote: '',
time: 0,
},
achievements: {
ftl: {
label: '',
explanation: '',
unlocked: false
},
dontpanic: {
label: '',
explanation: '',
unlocked: false
},
fourtytwo: {
label: '',
explanation: '',
unlocked: false
},
},
reset: function () {
this.words = 0
this.characters = 0
this.time = 0
this.fastest.quote = ''
for (const key in this.achievements) {
this.achievements[key].unlocked = false
}
}
}
let uncompleted = []
let timeStart
let typingErrors
// little helper function to get a random integer
function randInt(max) {
return Math.floor(Math.random() * max);
}
function resetQuotes () {
let quotePool = [...quotes]
// use the following for debugging, to get a quick test sentence first
//uncompleted.push('Test sentence.')
while (quotePool.length > 0) {
let i = randInt(quotePool.length)
let [quote] = quotePool.splice(i, 1)
uncompleted.push(quote)
}
}
function resetCompletedQuotes () {
$( '#completed ul' ).children().remove()
}
function resetGame () {
resetCompletedQuotes()
resetQuotes()
stats.reset()
$( '#input' )
.removeClass('correct')
.removeClass('incorrect')
.attr('disabled', 'true')
.val('')
$( '#current-quote' ).html('— Hit the start button to get the first quote —')
}
function nextQuote () {
$( '#success' ).text('')
$( '#current-quote' ).text(uncompleted[0])
$( '#input' )
.removeClass('correct')
.removeClass('incorrect')
.val('')
.removeAttr('disabled')
.focus()
timeStart = Date.now()
typingErrors = 0
}
function processInput () {
$input = $( '#input' )
quote = uncompleted[0]
value = $input.val()
if ( quote.startsWith( value ) ) {
$input.addClass('correct')
$input.removeClass('incorrect')
} else {
// only count a typing error if the sentence was not already incorrect
if (! $input.hasClass('incorrect')) {
typingErrors++
}
$input.addClass('incorrect')
$input.removeClass('correct')
}
if ( quote === value ) {
let timeEnd = Date.now()
seconds = ( timeEnd - timeStart ) / 1000
$input.attr('disabled', 'true')
$( '#success' ).text('Wonderful! You completed this quote in '+seconds+' seconds with '+typingErrors+' typing error(s).')
$( '#completed ul' ).append( $( '<li>'+quote+'</li>' ) )
uncompleted.splice(0, 1)
}
// now check if this was the last quote
if ( uncompleted.length === 0 ) {
$( '#start' ).remove()
$( '#reset' ).remove()
$( '#game-completed' ).show( 'slow' )
}
}
$( document ).ready(function () {
resetGame()
$( '#reset' ).on('click', resetGame)
$( '#start' ).on('click', nextQuote)
$( '#input' ).on('input', processInput).val('')
})