basketball 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5f2ba6492b8cb72e11f8a9ea3da6a5be5aa99964e706ab789798906d9ea1a98a
4
+ data.tar.gz: 6fc3fc8ba538c95bb36fdbfc0f764b6bcfeda45436964718fae20043fa7468ca
5
+ SHA512:
6
+ metadata.gz: 9182a915019659912848e06b9201b27b984bd1a13693fe5aed59c936a0e65b2a316de8cdeb4e7c2bb2ffbaea12fc5127109733dd09079165c0c33c73ad442095
7
+ data.tar.gz: be794d98881e00893212660a94312ee79748d3bd6016fa758b223bc08a4a287389eefc92d7cdac10b7484b126d92a352e7cc42d0fd2fa28d1f3ab6e8559551b1
data/.editorconfig ADDED
@@ -0,0 +1,8 @@
1
+ # See http://editorconfig.org/
2
+
3
+ [*]
4
+ trim_trailing_whitespace = true
5
+ indent_style = space
6
+ indent_size = 2
7
+ insert_final_newline = true
8
+ end_of_line = lf
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ *.gem
3
+ /tmp
4
+ /coverage
5
+ Gemfile.lock
6
+ /pkg
7
+ /data
8
+ /vendor
data/.rubocop.yml ADDED
@@ -0,0 +1,41 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ Exclude:
8
+ - bin/*
9
+ - lib/basketball/draft/cli.rb
10
+ - tmp/**/*
11
+ - vendor/**/*
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Metrics/MethodLength:
17
+ Max: 35
18
+
19
+ RSpec/ExampleLength:
20
+ Max: 20
21
+
22
+ RSpec/MultipleMemoizedHelpers:
23
+ Max: 10
24
+
25
+ Metrics/ParameterLists:
26
+ Max: 6
27
+
28
+ Metrics/AbcSize:
29
+ Max: 25
30
+
31
+ Metrics/ClassLength:
32
+ Max: 200
33
+
34
+ Metrics/BlockLength:
35
+ Max: 30
36
+
37
+ Metrics/CyclomaticComplexity:
38
+ Max: 9
39
+
40
+ Metrics/PerceivedComplexity:
41
+ Max: 9
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ 3.2.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ #### 0.0.1 - May 4th, 2023
2
+
3
+ * Initial release with Drafting module only
@@ -0,0 +1,73 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported. All complaints will be reviewed and investigated and will result in a response that
59
+ is deemed necessary and appropriate to the circumstances. The project team is
60
+ obligated to maintain confidentiality with regard to the reporter of an incident.
61
+ Further details of specific enforcement policies may be posted separately.
62
+
63
+ Project maintainers who do not follow or enforce the Code of Conduct in good
64
+ faith may face temporary or permanent repercussions as determined by other
65
+ members of the project's leadership.
66
+
67
+ ## Attribution
68
+
69
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
70
+ available at [http://contributor-covenant.org/version/1/4][version]
71
+
72
+ [homepage]: http://contributor-covenant.org
73
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'DISABLE_SIMPLECOV=true bin/rspec --format=documentation' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # RSpec files
8
+ rspec = dsl.rspec
9
+ watch(rspec.spec_helper) { rspec.spec_dir }
10
+ watch(rspec.spec_support) { rspec.spec_dir }
11
+ watch(rspec.spec_files)
12
+
13
+ # Ruby files
14
+ ruby = dsl.ruby
15
+ dsl.watch_spec_files_for(ruby.lib_files)
16
+ end
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2
+
3
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4
+
5
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # :basketball: Basketball
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/basketball.svg)](https://badge.fury.io/rb/basketball) [![CI](https://github.com/mattruggio/basketball/actions/workflows/ci.yaml/badge.svg)](https://github.com/mattruggio/basketball/actions/workflows/ci.yaml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
+
5
+ #### Basketball League Game Engine
6
+
7
+ This library is meant to serve as the domain for a basketball league/season simulator/turn-based game. It models core ideas such as: players, general managers, draft strategy, drafting, season generation, season simultation, playoff generation, playoff simulation, and more.
8
+
9
+ ## Installation
10
+
11
+ To install through Rubygems:
12
+
13
+ ````
14
+ gem install basketball
15
+ ````
16
+
17
+ You can also add this to your Gemfile using:
18
+
19
+ ````
20
+ bundle add basketball
21
+ ````
22
+
23
+ Install executable scripts:
24
+
25
+ ````
26
+ bundle binstubs basketball
27
+ ````
28
+
29
+ ## Main Modules
30
+
31
+ This library is broken down into several bounded contexts that can be consumed either via its Ruby API's or CLI through provided executable scripts:
32
+
33
+ ![Basketball Architecture - Overview.png](/docs/architecture/overview.png)
34
+
35
+ ## Drafting Module
36
+
37
+ The drafting module is responsible for providing a turn-based iterator allowing the consumer to either manually pick or simulate picks. Here is a cartoon showing the major components:
38
+
39
+ ![Basketball Architecture - Drafting.png](/docs/architecture/drafting.png)
40
+
41
+ ### The Drafting CLI
42
+
43
+ The drafting module is meant to be interfaces using its Ruby API by consuming applications. It also ships with a CLI which a user can interact with to emulate "the draft process". Technically speaking, the CLI provides an example application built on top of the Drafting module. Each time a CLI command is executed, its results will be resaved, so the output file can then be used as the next command's input file to string together commands. The following sections are example CLI interactions:
44
+
45
+ #### Generate a Fresh Draft
46
+
47
+ ```zsh
48
+ exe/basketball-draft -o tmp/draft.json
49
+ ```
50
+
51
+ #### N Top Available Players
52
+
53
+ ```zsh
54
+ exe/basketball-draft -i tmp/draft.json -t 10
55
+ ```
56
+
57
+ #### N Top Available Players for a Position
58
+
59
+ ```zsh
60
+ exe/basketball-draft -i tmp/draft.json -t 10 -q PG
61
+ ```
62
+
63
+ #### Output Current Rosters
64
+
65
+ ```zsh
66
+ exe/basketball-draft -i tmp/draft.json -r
67
+ ```
68
+
69
+ #### Simulate N picks
70
+
71
+ ```zsh
72
+ exe/basketball-draft -i tmp/draft.json -s 10
73
+ ```
74
+
75
+ #### Pick Players
76
+
77
+ ```zsh
78
+ exe/basketball-draft -i tmp/draft.json -p P-100,P-200,P-300
79
+ ```
80
+
81
+ #### Simulate the Rest of the Draft
82
+
83
+ ```zsh
84
+ exe/basketball-draft -i tmp/draft.json -a
85
+ ```
86
+
87
+ ## Contributing
88
+
89
+ ### Development Environment Configuration
90
+
91
+ Basic steps to take to get this repository compiling:
92
+
93
+ 1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (check basketball.gemspec for versions supported)
94
+ 2. Install bundler (gem install bundler)
95
+ 3. Clone the repository (git clone git@github.com:mattruggio/basketball.git)
96
+ 4. Navigate to the root folder (cd basketball)
97
+ 5. Install dependencies (bundle)
98
+
99
+ ### Running Tests
100
+
101
+ To execute the test suite run:
102
+
103
+ ````zsh
104
+ bin/rspec spec --format documentation
105
+ ````
106
+
107
+ Alternatively, you can have Guard watch for changes:
108
+
109
+ ````zsh
110
+ bin/guard
111
+ ````
112
+
113
+ Also, do not forget to run Rubocop:
114
+
115
+ ````zsh
116
+ bin/rubocop
117
+ ````
118
+
119
+ And auditing the dependencies:
120
+
121
+ ````zsh
122
+ bin/bundler-audit check --update
123
+ ````
124
+
125
+ ### Publishing
126
+
127
+ Note: ensure you have proper authorization before trying to publish new versions.
128
+
129
+ After code changes have successfully gone through the Pull Request review process then the following steps should be followed for publishing new versions:
130
+
131
+ 1. Merge Pull Request into main
132
+ 2. Update `version.rb` using [semantic versioning](https://semver.org/)
133
+ 3. Install dependencies: `bundle`
134
+ 4. Update `CHANGELOG.md` with release notes
135
+ 5. Commit & push main to remote and ensure CI builds main successfully
136
+ 6. Run `bin/rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
137
+
138
+ ## Code of Conduct
139
+
140
+ Everyone interacting in this codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mattruggio/basketball/blob/main/CODE_OF_CONDUCT.md).
141
+
142
+ ## License
143
+
144
+ This project is MIT Licensed.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[rubocop spec]
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './lib/basketball/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'basketball'
7
+ s.version = Basketball::VERSION
8
+ s.summary = 'Basketball League Game Engine'
9
+
10
+ s.description = <<-DESC
11
+ This library is meant to serve as the domain for a basketball league/season simulator/turn-based game.
12
+ It models core ideas such as: players, general managers, draft strategy, drafting, season generation, season simultation,
13
+ playoff generation, playoff simulation, and more.
14
+ DESC
15
+
16
+ s.authors = ['Matthew Ruggio']
17
+ s.email = ['mattruggio@icloud.com']
18
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(.github|bin|docs|spec)/}) }
19
+ s.bindir = 'exe'
20
+ s.executables = %w[basketball-draft]
21
+ s.homepage = 'https://github.com/mattruggio/basketball'
22
+ s.license = 'MIT'
23
+ s.metadata = {
24
+ 'bug_tracker_uri' => 'https://github.com/mattruggio/basketball/issues',
25
+ 'changelog_uri' => 'https://github.com/mattruggio/basketball/blob/main/CHANGELOG.md',
26
+ 'documentation_uri' => 'https://www.rubydoc.info/gems/basketball',
27
+ 'homepage_uri' => s.homepage,
28
+ 'source_code_uri' => s.homepage,
29
+ 'rubygems_mfa_required' => 'true'
30
+ }
31
+
32
+ s.required_ruby_version = '>= 3.2.1'
33
+
34
+ s.add_dependency('faker', '~>3.2')
35
+ s.add_dependency('slop', '~>4.10')
36
+
37
+ s.add_development_dependency('bundler-audit', '~>0.9')
38
+ s.add_development_dependency('guard-rspec', '~>4.7')
39
+ s.add_development_dependency('pry', '~>0.14')
40
+ s.add_development_dependency('rake', '~>13.0')
41
+ s.add_development_dependency('rspec', '~>3.12')
42
+ s.add_development_dependency('rubocop', '~>1.49')
43
+ s.add_development_dependency('rubocop-rake', '~>0.6')
44
+ s.add_development_dependency('rubocop-rspec', '~>2.20')
45
+ s.add_development_dependency('simplecov', '~>0.22')
46
+ s.add_development_dependency('simplecov-console', '~>0.9')
47
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'basketball'
6
+
7
+ Basketball::Drafting::CLI.new(args: ARGV).invoke!
@@ -0,0 +1,203 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Basketball
4
+ module Drafting
5
+ # Example:
6
+ # exe/basketball-draft -o tmp/draft.json
7
+ # exe/basketball-draft -i tmp/draft.json -o tmp/draft-wip.json -s 28 -p ONEALSH01,ONEALJE01 -t 10 -q PG
8
+ # exe/basketball-draft -i tmp/draft-wip.json -r -t 10
9
+ # exe/basketball-draft -i tmp/draft-wip.json -t 10 -q SG
10
+ # exe/basketball-draft -i tmp/draft-wip.json -s 30 -t 10
11
+ # exe/basketball-draft -i tmp/draft-wip.json -a -r
12
+ class CLI
13
+ attr_reader :opts, :serializer, :io
14
+
15
+ def initialize(args:, io: $stdout)
16
+ @io = io
17
+ @serializer = EngineSerializer.new
18
+ @opts = slop_parse(args)
19
+
20
+ if opts[:input].to_s.empty? && opts[:output].to_s.empty?
21
+ io.puts('Input and/or output paths are required.')
22
+
23
+ exit
24
+ end
25
+
26
+ freeze
27
+ end
28
+
29
+ def invoke!
30
+ engine = load_engine
31
+
32
+ execute(engine)
33
+
34
+ io.puts
35
+ io.puts('Status')
36
+
37
+ if engine.done?
38
+ io.puts('Draft is complete!')
39
+ else
40
+ io.puts("#{engine.remaining_picks} Remaining pick(s)")
41
+ io.puts("Round #{engine.current_round} pick #{engine.current_round_pick} for #{engine.current_team}")
42
+ end
43
+
44
+ write(engine)
45
+
46
+ rosters(engine)
47
+
48
+ query(engine)
49
+
50
+ self
51
+ end
52
+
53
+ private
54
+
55
+ def slop_parse(args)
56
+ Slop.parse(args) do |o|
57
+ o.banner = 'Usage: draft [options] ...'
58
+
59
+ o.string '-i', '--input',
60
+ 'Path to load the engine from. If omitted then a new draft will be generated.'
61
+ o.string '-o', '--output', 'Path to write the engine to (if omitted then input path will be used)'
62
+ o.integer '-s', '--simulate', 'Number of picks to simulate (default is 0).', default: 0
63
+ o.bool '-a', '--simulate-all', 'Simulate the rest of the draft', default: false
64
+ o.array '-p', '--picks', 'Comma-separated list of ordered player IDs to pick.', delimiter: ','
65
+ o.integer '-t', '--top', 'Output the top rated available players (default is 0).', default: 0
66
+ o.string '-q', '--query', "Filter TOP by position: #{Position::ALL_VALUES.join(', ')}."
67
+ o.bool '-r', '--rosters', 'Output all team rosters.', default: false
68
+
69
+ o.on '-h', '--help', 'Print out help, like this is doing right now.' do
70
+ io.puts(o)
71
+ exit
72
+ end
73
+ end.to_h
74
+ end
75
+
76
+ def load_engine
77
+ if opts[:input].to_s.empty?
78
+ io.puts('Input path was not provided, generating fresh teams and players')
79
+
80
+ generate_draft
81
+ else
82
+ io.puts("Draft loaded from: #{opts[:input]}")
83
+
84
+ read
85
+ end
86
+ end
87
+
88
+ def generate_draft
89
+ teams = 30.times.map do |i|
90
+ Team.new(
91
+ id: "T-#{i + 1}", name: Faker::Team.name
92
+ )
93
+ end
94
+
95
+ players = 450.times.map do |i|
96
+ Player.new(
97
+ id: "P-#{i + 1}",
98
+ first_name: Faker::Name.first_name,
99
+ last_name: Faker::Name.last_name,
100
+ position: Position.random,
101
+ overall: (0..100).to_a.sample
102
+ )
103
+ end
104
+
105
+ Engine.new(players:, teams:)
106
+ end
107
+
108
+ def rosters(engine)
109
+ return unless opts[:rosters]
110
+
111
+ io.puts
112
+ io.puts('Rosters')
113
+
114
+ engine.rosters.each do |roster|
115
+ io.puts(roster)
116
+ end
117
+ end
118
+
119
+ # rubocop:disable Metrics/AbcSize
120
+ def query(engine)
121
+ top = opts[:top]
122
+
123
+ return if top <= 0
124
+
125
+ search = PlayerSearch.new(engine.undrafted_players)
126
+ position = opts[:query].to_s.empty? ? nil : Position.new(opts[:query])
127
+ players = search.query(position:).take(opts[:top])
128
+
129
+ io.puts
130
+ io.print("Top #{top} available players")
131
+
132
+ if position
133
+ io.puts(" for #{position} position:")
134
+ else
135
+ io.puts(' for all positions:')
136
+ end
137
+
138
+ io.puts(players)
139
+ end
140
+ # rubocop:enable Metrics/AbcSize
141
+
142
+ def read
143
+ contents = File.read(opts[:input])
144
+ serializer.deserialize(contents)
145
+ end
146
+
147
+ # rubocop:disable Metrics/AbcSize
148
+ def execute(engine)
149
+ event_count = 0
150
+
151
+ io.puts
152
+ io.puts('New Events')
153
+
154
+ (opts[:picks] || []).each do |id|
155
+ break if engine.done?
156
+
157
+ player = engine.players.find { |p| p.id == id.to_s.upcase }
158
+
159
+ event = engine.pick!(player)
160
+
161
+ io.puts(event)
162
+
163
+ event_count += 1
164
+ end
165
+
166
+ engine.sim!(opts[:simulate]) do |event|
167
+ io.puts(event)
168
+
169
+ event_count += 1
170
+ end
171
+
172
+ if opts[:simulate_all]
173
+ engine.sim! do |event|
174
+ io.puts(event)
175
+
176
+ event_count += 1
177
+ end
178
+ end
179
+
180
+ io.puts("Generated #{event_count} new event(s)")
181
+
182
+ nil
183
+ end
184
+ # rubocop:enable Metrics/AbcSize
185
+
186
+ def write(engine)
187
+ output = opts[:output].to_s.empty? ? opts[:input] : opts[:output]
188
+
189
+ contents = serializer.serialize(engine)
190
+ out_dir = File.dirname(output)
191
+
192
+ FileUtils.mkdir_p(out_dir)
193
+
194
+ File.write(output, contents)
195
+
196
+ io.puts
197
+ io.puts("Draft written to: #{output}")
198
+
199
+ nil
200
+ end
201
+ end
202
+ end
203
+ end