typeking 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +58 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/typeking.rb +11 -0
- data/lib/typeking/game_controller.rb +242 -0
- data/lib/typeking/user.rb +18 -0
- data/lib/typeking/version.rb +5 -0
- data/typeking.gemspec +33 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7d70c8a418e4287f6080862bfa045ddc7d29f9c382972b63a6caa932d148da1d
|
4
|
+
data.tar.gz: 84820a050ee309c49b453635ab39ebbd6ad0e778c8f3b2b28442c8f07d7b6593
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e992ae851ff5c997eb5128d9eb39a3d7f6df9e9c29128fdf7146bb696b3d98173740df34bda32e0ddf181a1ad610f1465d548ade8f1e8f34942c3defa8039925
|
7
|
+
data.tar.gz: 251f47eb13b01665f1ce81980c43cea454239f236dee44b751b5c0f7e00e6d9e86aee92ae9d41674a086f19692db7665dbbd79ec909d933e84a2b965172fd6f8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at anthony.lu1115@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in typeking.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "colorize", "~> 0.8.1"
|
13
|
+
|
14
|
+
gem "artii", "~> 2.1.2"
|
15
|
+
|
16
|
+
gem "tty-prompt", "~> 0.23.1"
|
17
|
+
|
18
|
+
gem "random-word", "~> 2.1.1"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
typeking (0.1.0)
|
5
|
+
artii (~> 2.1.2)
|
6
|
+
colorize (~> 0.8.1)
|
7
|
+
random-word (~> 2.1.1)
|
8
|
+
tty-prompt (~> 0.23.1)
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
artii (2.1.2)
|
14
|
+
colorize (0.8.1)
|
15
|
+
diff-lcs (1.4.4)
|
16
|
+
pastel (0.8.0)
|
17
|
+
tty-color (~> 0.5)
|
18
|
+
rake (13.0.3)
|
19
|
+
random-word (2.1.1)
|
20
|
+
rspec (3.10.0)
|
21
|
+
rspec-core (~> 3.10.0)
|
22
|
+
rspec-expectations (~> 3.10.0)
|
23
|
+
rspec-mocks (~> 3.10.0)
|
24
|
+
rspec-core (3.10.1)
|
25
|
+
rspec-support (~> 3.10.0)
|
26
|
+
rspec-expectations (3.10.1)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.10.0)
|
29
|
+
rspec-mocks (3.10.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.10.0)
|
32
|
+
rspec-support (3.10.2)
|
33
|
+
tty-color (0.6.0)
|
34
|
+
tty-cursor (0.7.1)
|
35
|
+
tty-prompt (0.23.1)
|
36
|
+
pastel (~> 0.8)
|
37
|
+
tty-reader (~> 0.8)
|
38
|
+
tty-reader (0.9.0)
|
39
|
+
tty-cursor (~> 0.7)
|
40
|
+
tty-screen (~> 0.8)
|
41
|
+
wisper (~> 2.0)
|
42
|
+
tty-screen (0.8.1)
|
43
|
+
wisper (2.0.1)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
x86_64-darwin-19
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
artii (~> 2.1.2)
|
50
|
+
colorize (~> 0.8.1)
|
51
|
+
rake (~> 13.0)
|
52
|
+
random-word (~> 2.1.1)
|
53
|
+
rspec (~> 3.0)
|
54
|
+
tty-prompt (~> 0.23.1)
|
55
|
+
typeking!
|
56
|
+
|
57
|
+
BUNDLED WITH
|
58
|
+
2.2.18
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 AnthonyALu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Typeking
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'typeking'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install typeking
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
Typeking::GameController.new()
|
23
|
+
```
|
24
|
+
|
25
|
+
|
26
|
+
## Development
|
27
|
+
|
28
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
29
|
+
|
30
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/typeking. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/typeking/blob/master/CODE_OF_CONDUCT.md).
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
39
|
+
|
40
|
+
## Code of Conduct
|
41
|
+
|
42
|
+
Everyone interacting in the Typeking project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/typeking/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "typeking"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/typeking.rb
ADDED
@@ -0,0 +1,242 @@
|
|
1
|
+
module Typeking
|
2
|
+
require "random-word"
|
3
|
+
require "colorize"
|
4
|
+
require "tty-prompt"
|
5
|
+
require "artii"
|
6
|
+
|
7
|
+
class GameController
|
8
|
+
|
9
|
+
#attr_accessor used for testing purposes. App will still run if all variables changed to attr_reader
|
10
|
+
attr_accessor :userHashes, :currentUid, :currentUserData, :userData
|
11
|
+
|
12
|
+
def initialize()
|
13
|
+
@userData = [] #database of user hashes
|
14
|
+
@userHashes = {} #stores indexes for all users
|
15
|
+
@currentUserData = {} #stores current stats
|
16
|
+
@currentUid = 0 #current user id
|
17
|
+
@leaderboardArr = []
|
18
|
+
setup_application
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup_application
|
22
|
+
system("clear") #clears terminal
|
23
|
+
artOpts = Artii::Base.new :font => 'slant' #create artii options using Artii Ruby gem
|
24
|
+
puts artOpts.asciify('Type King').colorize(:blue) #apply artii style to string to output to the application
|
25
|
+
if ARGV[0] == "-h" #command line argument that calls help function
|
26
|
+
gameController.help
|
27
|
+
else
|
28
|
+
ARGV.each do |i|
|
29
|
+
attempt_registration(i.chomp) #command line argument that registers each user
|
30
|
+
end
|
31
|
+
end
|
32
|
+
while ARGV.size > 0 #loop while there are elements in ARGV
|
33
|
+
ARGV.shift #delete ARGV so that application can get input later using 'gets'
|
34
|
+
end
|
35
|
+
start_screen
|
36
|
+
end
|
37
|
+
|
38
|
+
def start_screen
|
39
|
+
startChoices = {Login: 1, "New User": 2, Leaderboards: 3, Help: 4, Exit: 5} #Options for starting screen
|
40
|
+
startPrompt = TTY::Prompt.new #creates new prompt
|
41
|
+
choice = startPrompt.select("Welcome to Type King", startChoices) #receive input for prompt
|
42
|
+
if choice == 1 #user picked Login
|
43
|
+
user_login
|
44
|
+
elsif choice == 2 #user picked New User
|
45
|
+
user_register
|
46
|
+
elsif choice == 3 #user picked Leaderboards
|
47
|
+
show_leaderboards
|
48
|
+
elsif choice == 4
|
49
|
+
help
|
50
|
+
start_screen
|
51
|
+
else #user picked exit
|
52
|
+
puts "Thank you for playing!"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def help
|
57
|
+
puts "1. Register"
|
58
|
+
puts "2. Login"
|
59
|
+
puts "3. Play"
|
60
|
+
puts "4. Check your stats"
|
61
|
+
end
|
62
|
+
|
63
|
+
def user_register
|
64
|
+
registerPrompt = TTY::Prompt.new #create new prompt
|
65
|
+
username = registerPrompt.ask("What is your name?", default: "Anonymous") #ask for user name, default is 'anonymous'
|
66
|
+
puts attempt_registration(username) #calls attempt_registration method with username to check if user exists
|
67
|
+
start_screen #return to start screen so user can login
|
68
|
+
end
|
69
|
+
|
70
|
+
def attempt_registration(username)
|
71
|
+
if @userHashes[username] #check if user exists in @userHashes
|
72
|
+
return "You already have an account, please login" #user_register will puts this to the terminal
|
73
|
+
else
|
74
|
+
user = User.new(username) #creates new user
|
75
|
+
newHash = user.user_details #creates a new hash of user values
|
76
|
+
@userhashes = @userHashes.merge!(newHash) #adds user to hash
|
77
|
+
@userData << {:name => username, :high_score => 0, :accuracy => 0, :worst_character => "None!"}
|
78
|
+
return "Thank you for registering, please login and have fun!" #user_register will puts this to the terminal
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def user_login
|
83
|
+
loginPrompt = TTY::Prompt.new #creates new prompt
|
84
|
+
username = loginPrompt.ask("What is your name?", default: "Anonymous") #receive prompt input
|
85
|
+
puts(attempt_login(username)) #attempt to login
|
86
|
+
if @userHashes[username]
|
87
|
+
home_screen #if user exists go to home screen
|
88
|
+
else
|
89
|
+
start_screen #otherwise go back to start screen
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def attempt_login(username)
|
94
|
+
if @userHashes[username] #check if user exists
|
95
|
+
@currentUid = @userHashes[username] #update current user id to the username from the userhashes directory
|
96
|
+
@currentUserData = @userData[currentUid] #user data hash becomes hash from database
|
97
|
+
return "Hello #{@currentUserData[:name]}!" #user_login will puts this to terminal
|
98
|
+
else
|
99
|
+
return "You have not registered yet, please register first." #user_login will puts this to terminal
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
def display_leaders(leaderCount)
|
105
|
+
leaders = 0 #current number of users displayed on leaderboard
|
106
|
+
while leaders < leaderCount #loop while displayed leaders is less than actual users
|
107
|
+
leaderHash = @leaderboardArr[leaders] #creates new hash using leaders as the index as the highest score would start from index 0
|
108
|
+
puts "#{leaders+1}. #{leaderHash[:name]} - WPM: #{leaderHash[:high_score]}, Accuracy: #{leaderHash[:accuracy]}%, Worst Character: #{leaderHash[:worst_character]}" #uses hash to output leaderboards
|
109
|
+
leaders += 1 #increment leaders
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def show_leaderboards
|
114
|
+
@leaderboardArr = @userData.dup #creates a new duplicate array of all user data
|
115
|
+
puts "No entries yet!" unless @leaderboardArr.count > 0 #output when no users in data
|
116
|
+
@leaderboardArr.sort_by!{|w| w[:high_score]} #sort by ascending order
|
117
|
+
@leaderboardArr = @leaderboardArr.reverse #reverse order to descending
|
118
|
+
leaderCount = @leaderboardArr.count #checks how many users are in the array
|
119
|
+
display_leaders(leaderCount) #calls method based on number of users
|
120
|
+
start_screen #return to starting screen
|
121
|
+
end
|
122
|
+
|
123
|
+
def show_stats
|
124
|
+
@currentUserData = @userData[@currentUid] #double check that the stats shown are the highest, this line is used for testing
|
125
|
+
return "Name: #{@currentUserData[:name]}, WPM: #{@currentUserData[:high_score]}, Accuracy: #{@currentUserData[:accuracy]}, Least accurate letter: #{@currentUserData[:worst_character]}" #home_screen will puts this to application
|
126
|
+
end
|
127
|
+
|
128
|
+
def home_screen
|
129
|
+
entryPrompt = TTY::Prompt.new #creates new prompt
|
130
|
+
entryChoices = {Play: 1, Stats: 2, "Log out": 3} #options to choose from
|
131
|
+
choice = entryPrompt.select("Hello #{@currentUserData[:name]}, what would you like to do?", entryChoices) #receives choice input
|
132
|
+
if choice == 1 #play game selected
|
133
|
+
start_game #starts game
|
134
|
+
elsif choice == 2 #stats selected
|
135
|
+
puts show_stats #show user stats
|
136
|
+
home_screen #restart method
|
137
|
+
else
|
138
|
+
start_screen #logs out
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def start_game
|
143
|
+
wordsPrompt = TTY::Prompt.new #creates new prompt
|
144
|
+
words = wordsPrompt.ask("How many words do you want to type? (enter a number between 5 and 500)", default: 60) #asks user for number of words they would like to type. If no input, defaults to 30
|
145
|
+
if words.to_i < 5 || words.to_i > 499 #checks if entry is in range
|
146
|
+
puts "Invalid entry, please enter a number larger than 5 and smaller than 500"
|
147
|
+
start_game #restarts method
|
148
|
+
else
|
149
|
+
countdown = 3 #starts countdown to game
|
150
|
+
while countdown > 0 #loops while countown is higher than 0
|
151
|
+
puts countdown
|
152
|
+
countdown -= 1
|
153
|
+
sleep 1 #waits 1 second
|
154
|
+
end
|
155
|
+
begin_typing(words) #starts games with chosen number of words
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def begin_typing(wordCount)
|
160
|
+
wordsCorrect = [] #creates array of words that the user types correctly
|
161
|
+
wordsIncorrect = [] #creates array of words that the user types incorrectly
|
162
|
+
startingTime = Process.clock_gettime(Process::CLOCK_MONOTONIC) #gets current time and saves it in variable
|
163
|
+
wordsLeft = wordCount.to_i #words left to type are the words passed in argument from previous def
|
164
|
+
while wordsLeft > 0 #loops while there are words to type
|
165
|
+
newWord = RandomWord.nouns.next #starts with a random noun
|
166
|
+
while newWord.size > 8 #makes sure no words are over 8 characters long, generates a new word if the new word is longer than 8 characters
|
167
|
+
random = rand(2) #generators a random number from 0-1
|
168
|
+
if random == 1
|
169
|
+
newWord = RandomWord.adjs.next #if random number is '1', next word is an adjective
|
170
|
+
else
|
171
|
+
newWord = RandomWord.nouns.next #if random number is not '1', next word is a noun
|
172
|
+
end
|
173
|
+
end
|
174
|
+
puts newWord #prints word for user to type
|
175
|
+
input = gets.chomp
|
176
|
+
if newWord == input #checks if user has typed the word correctly
|
177
|
+
wordsCorrect << newWord.colorize(:green) #changes colour of word to green and pushes it to correct words array
|
178
|
+
else
|
179
|
+
wordsIncorrect << newWord.colorize(:red) #changes colour of word to red and pushes it to incorrect words array
|
180
|
+
end
|
181
|
+
wordsLeft -= 1 #user has typed another word
|
182
|
+
end
|
183
|
+
finishTime = Process.clock_gettime(Process::CLOCK_MONOTONIC) #gets current time and stores it in variable
|
184
|
+
elapsedTime = (finishTime - startingTime) #gets time elapsed by subtracting starting time from the finished time
|
185
|
+
calculate_results(elapsedTime, wordCount, wordsCorrect, wordsIncorrect) #calculate results using elapsed time, how many words the user typed and words that the user got correct/incorrect
|
186
|
+
end
|
187
|
+
|
188
|
+
def calculate_results(elapsedTime, totalWordCount, wordsCorrect, wordsIncorrect)
|
189
|
+
puts "Your final time to type #{totalWordCount} words was #{elapsedTime.round} seconds" #tells the user how long it took them to type the designated words
|
190
|
+
puts "Words typed correctly: #{wordsCorrect.join(" ")}" #shows all correctly typed words seperated by a space
|
191
|
+
puts "Words typed incorrectly: #{wordsIncorrect.join(" ")}" #shows all incorrectly typed words seperated by a space
|
192
|
+
puts "Calculating..."
|
193
|
+
puts calculate_speed(elapsedTime, totalWordCount, wordsCorrect) #returns wpm and accuracy
|
194
|
+
puts count_worst_letters(wordsIncorrect) #returns array of top 3 incorrect letters typed
|
195
|
+
home_screen #return to home screen
|
196
|
+
end
|
197
|
+
|
198
|
+
def count_worst_letters(incorrectWords)
|
199
|
+
letter_count = {} #create new array of letters incorrectly typed
|
200
|
+
incorrectWords.each do |word| #loop through each word
|
201
|
+
word = word.red.uncolorize #uncolorize to remove colorize characters
|
202
|
+
letterArr = word.split("") #split word into array of letters
|
203
|
+
letterArr.each do |letter| #loop through each letter
|
204
|
+
if !letter_count[letter] #if letter value is nil, add it to hash
|
205
|
+
letter_count[letter] = 1
|
206
|
+
else
|
207
|
+
letter_count[letter] += 1 #if letter exists, increment
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
if letter_count.empty? #check if hash is empty, meaning that they got everything correct
|
212
|
+
return "Great job, you got all the words correct!" #calculate_results will puts this to application
|
213
|
+
else
|
214
|
+
final_letters = letter_count.sort_by {|char, c| c}.reverse #sort hash by descending order
|
215
|
+
@currentUserData[:worst_character] = letter_count.max_by{|k, v| v} #change worst character in database to be the key with the highest value
|
216
|
+
return "Here are some characters that you may want to practice: #{final_letters[0]}, #{final_letters[1]}, #{final_letters[2]}" #calculate_results will puts this to terminal
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def calculate_speed(elapsedTime, totalWordCount, wordsCorrect)
|
221
|
+
charactersTyped = 0 #checks how many characters were typed
|
222
|
+
wordsCorrect.each do |word|
|
223
|
+
word = word.green.uncolorize #uncolorizes words so that word size will revert to normal
|
224
|
+
charactersTyped += word.size #adds characters of words to character count
|
225
|
+
end
|
226
|
+
timeMultiplier = 60 / elapsedTime #creates a multiplier to set typing rate to words per minute
|
227
|
+
wordsNormalized = charactersTyped * timeMultiplier #converts characters typed to characters per minute
|
228
|
+
wpm = ((wordsNormalized / 3)).round #wpm = characters typed per minute divided by average characters in words. Average is lower to be more accurate because user has to use the enter button and is not typing sentences
|
229
|
+
accuracy = (wordsCorrect.size.to_f / totalWordCount.to_f) * 100 #accuracy = correct words / total words
|
230
|
+
save_data(wpm, accuracy.to_i) unless wpm < @currentUserData[:high_score]#saves data if user has reached high score
|
231
|
+
return "You type #{wpm.to_s.colorize(:green)} word(s) per minute with #{accuracy.to_i}% accuracy!" #returns wpm and accuracy, outputted by calculate_results
|
232
|
+
end
|
233
|
+
|
234
|
+
def save_data(wpm, accuracy)
|
235
|
+
@currentUserData[:high_score] = wpm #updates highest wpm
|
236
|
+
@currentUserData[:accuracy] = accuracy #updates accuracy
|
237
|
+
@userData[@currentUid] = @currentUserData #updates database with current user data
|
238
|
+
end
|
239
|
+
|
240
|
+
|
241
|
+
end
|
242
|
+
end
|
data/typeking.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/typeking/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "typeking"
|
7
|
+
spec.version = Typeking::VERSION
|
8
|
+
spec.authors = ["AnthonyALu"]
|
9
|
+
spec.email = ["anthony.lu1115@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A gem to test your typing skills."
|
12
|
+
spec.homepage = "https://github.com/AnthonyALu/typeking"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 2.4.0"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
# Uncomment to register a new dependency of your gem
|
26
|
+
spec.add_dependency "colorize", "~> 0.8.1"
|
27
|
+
spec.add_dependency "artii", "~> 2.1.2"
|
28
|
+
spec.add_dependency "tty-prompt", "~> 0.23.1"
|
29
|
+
spec.add_dependency "random-word", "~> 2.1.1"
|
30
|
+
|
31
|
+
# For more information and examples about making a new gem, checkout our
|
32
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: typeking
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- AnthonyALu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: artii
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.1.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.1.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: tty-prompt
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.23.1
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.23.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: random-word
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.1.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.1.1
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- anthony.lu1115@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- CHANGELOG.md
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/typeking.rb
|
88
|
+
- lib/typeking/game_controller.rb
|
89
|
+
- lib/typeking/user.rb
|
90
|
+
- lib/typeking/version.rb
|
91
|
+
- typeking.gemspec
|
92
|
+
homepage: https://github.com/AnthonyALu/typeking
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: 2.4.0
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubygems_version: 3.1.6
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A gem to test your typing skills.
|
115
|
+
test_files: []
|