gacha 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +88 -0
- data/Rakefile +1 -0
- data/gacha.gemspec +24 -0
- data/lib/gacha.rb +5 -0
- data/lib/gacha/box.rb +79 -0
- data/lib/gacha/item.rb +14 -0
- data/lib/gacha/version.rb +3 -0
- data/spec/gacha_spec.rb +54 -0
- data/spec/spec_helper.rb +6 -0
- metadata +109 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Taketo Yoshida
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
Gacha
|
2
|
+
======================
|
3
|
+
|
4
|
+
Gacha is a library for Ruby to pick up the one with a specified probability from some items.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'gacha'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install gacha
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Here is a simple example to pick up the item from 3 items which are Ruby, Python and Perl. Probability of each item is below.
|
23
|
+
|
24
|
+
- Ruby : 55%
|
25
|
+
- Python : 20%
|
26
|
+
- Perl : 25%
|
27
|
+
|
28
|
+
### Simple Example
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'gacha'
|
32
|
+
|
33
|
+
box = Gacha::Box.new
|
34
|
+
items = [
|
35
|
+
Gacha::Item.new("Ruby", 55),
|
36
|
+
Gacha::Item.new("Python", 20),
|
37
|
+
Gacha::Item.new("Perl", 25),
|
38
|
+
]
|
39
|
+
items.each do |item|
|
40
|
+
box.add(item)
|
41
|
+
end
|
42
|
+
|
43
|
+
item = box.draw
|
44
|
+
p item.id # Ruby or Python or Perl
|
45
|
+
```
|
46
|
+
|
47
|
+
### Consumable Item
|
48
|
+
|
49
|
+
if you want to use consumable item, you should write below code.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
require 'gacha'
|
53
|
+
|
54
|
+
box = Gacha::Box.new
|
55
|
+
items = [
|
56
|
+
Gacha::Item.new("Ruby", 55),
|
57
|
+
Gacha::Item.new("Python", 20),
|
58
|
+
Gacha::Item.new("Perl", 25),
|
59
|
+
]
|
60
|
+
items.each do |item|
|
61
|
+
box.add(item)
|
62
|
+
end
|
63
|
+
box.set_consumable(true)
|
64
|
+
|
65
|
+
p box.draw # Ruby or Python or Perl
|
66
|
+
p box.draw # Ruby or Python or Perl
|
67
|
+
p box.draw # Ruby or Python or Perl
|
68
|
+
p box.draw # nil
|
69
|
+
```
|
70
|
+
|
71
|
+
### Remove Item from Box
|
72
|
+
```ruby
|
73
|
+
require 'gacha'
|
74
|
+
|
75
|
+
box = Gacha::Box.new
|
76
|
+
item = Gacha::Item.new("test1", 30)
|
77
|
+
box.remove(item)
|
78
|
+
p box.empty? # true
|
79
|
+
p box.count # 0
|
80
|
+
```
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
1. Fork it ( http://github.com/<my-github-username>/gacha/fork )
|
85
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
86
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
87
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
88
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/gacha.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gacha/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gacha"
|
8
|
+
spec.version = Gacha::VERSION
|
9
|
+
spec.authors = ["Taketo Yoshida"]
|
10
|
+
spec.email = ["tamanyan.sss@gmail.com"]
|
11
|
+
spec.summary = %q{Gacha library}
|
12
|
+
spec.description = %q{Gacha is a library for Ruby to pick up the one with a specified probability from some items.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/gacha.rb
ADDED
data/lib/gacha/box.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
module Gacha
|
2
|
+
class Box
|
3
|
+
def initialize()
|
4
|
+
self.clear
|
5
|
+
@consumable = false
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
"Gacha::Box #{@items.join(", ")}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def draw
|
13
|
+
position = self.random_position
|
14
|
+
return nil if position == nil
|
15
|
+
item = self.find(position)
|
16
|
+
if item != nil and @consumable
|
17
|
+
self.remove(item)
|
18
|
+
end
|
19
|
+
return item
|
20
|
+
end
|
21
|
+
|
22
|
+
def add(item)
|
23
|
+
@items.push(item)
|
24
|
+
@total_rate += item.rate
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove(item)
|
28
|
+
@items.delete_if do |x|
|
29
|
+
if x == item
|
30
|
+
@total_rate -= item.rate
|
31
|
+
true
|
32
|
+
else
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def clear
|
39
|
+
@items = []
|
40
|
+
@total_rate = 0
|
41
|
+
end
|
42
|
+
|
43
|
+
def empty?
|
44
|
+
@items.empty? and @total_rate == 0
|
45
|
+
end
|
46
|
+
|
47
|
+
def count
|
48
|
+
@items.count
|
49
|
+
end
|
50
|
+
|
51
|
+
def set_consumable(consumable)
|
52
|
+
@consumable = consumable
|
53
|
+
end
|
54
|
+
|
55
|
+
def consumable?
|
56
|
+
@consumable
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
def random_position
|
61
|
+
if @total_rate < 1
|
62
|
+
nil
|
63
|
+
else
|
64
|
+
rand(@total_rate)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def find(position)
|
69
|
+
current = 0
|
70
|
+
@items.each do |item|
|
71
|
+
current += item.rate
|
72
|
+
if position < current
|
73
|
+
return item
|
74
|
+
end
|
75
|
+
end
|
76
|
+
return nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/gacha/item.rb
ADDED
data/spec/gacha_spec.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'gacha/box'
|
3
|
+
require 'gacha/item'
|
4
|
+
|
5
|
+
describe Gacha do
|
6
|
+
let :box do
|
7
|
+
Gacha::Box.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should add item" do
|
11
|
+
box.add(Gacha::Item.new("test", 30))
|
12
|
+
expect(box.count).to eq(1)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should pick up item" do
|
16
|
+
item = Gacha::Item.new("test1", 30)
|
17
|
+
box.add(item)
|
18
|
+
expect(box.draw).to eq(item)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should add items" do
|
22
|
+
items = [
|
23
|
+
Gacha::Item.new("test1", 30),
|
24
|
+
Gacha::Item.new("test2", 30),
|
25
|
+
Gacha::Item.new("test3", 30),
|
26
|
+
Gacha::Item.new("test4", 30),
|
27
|
+
Gacha::Item.new("test5", 30),
|
28
|
+
]
|
29
|
+
items.each do |item|
|
30
|
+
box.add(item)
|
31
|
+
end
|
32
|
+
expect(box.count).to eq(5)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should remove item" do
|
36
|
+
item = Gacha::Item.new("test1", 30)
|
37
|
+
box.remove(item)
|
38
|
+
expect(box.count).to eq(0)
|
39
|
+
expect(box.empty?).to be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be consumable" do
|
43
|
+
box.set_consumable(true)
|
44
|
+
item = Gacha::Item.new("test1", 30)
|
45
|
+
box.add(item)
|
46
|
+
expect(box.draw).to eq(item)
|
47
|
+
expect(box.empty?).to be_true
|
48
|
+
|
49
|
+
item = Gacha::Item.new("test2", 30)
|
50
|
+
box.add(item)
|
51
|
+
expect(box.draw).to eq(item)
|
52
|
+
expect(box.draw).to be_nil
|
53
|
+
end
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gacha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Taketo Yoshida
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.5'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.5'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Gacha is a library for Ruby to pick up the one with a specified probability
|
63
|
+
from some items.
|
64
|
+
email:
|
65
|
+
- tamanyan.sss@gmail.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- gacha.gemspec
|
76
|
+
- lib/gacha.rb
|
77
|
+
- lib/gacha/box.rb
|
78
|
+
- lib/gacha/item.rb
|
79
|
+
- lib/gacha/version.rb
|
80
|
+
- spec/gacha_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
homepage: ''
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.23
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: Gacha library
|
107
|
+
test_files:
|
108
|
+
- spec/gacha_spec.rb
|
109
|
+
- spec/spec_helper.rb
|