social-game-kit 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 65571e588b48927227617f828d9f137a8266e8d84f2720ae4025efa5b20f97c1
4
+ data.tar.gz: 15473e8e6a643022c099bfc04cceb8f3c19486395ea6a3f31b98ae44d301571a
5
+ SHA512:
6
+ metadata.gz: b681aed93bc74186fb42e9a27a745ef720c21522aba4733c7852ff46dcc5649574fbf6ea555da85364beac0795ca372a5d37ac0383eaba6ab10ba6b13c59f3b0
7
+ data.tar.gz: 21e9ea9293a84a29eb1ad2dcc7a9757caf1b8d04181c38b3f8d1fe2b8d71e7829f6d9e92c55bc7b4b46b785f6cf013a08da75dc26e8df1957bfa404c0ad79bdf
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ EnforcedStyle: double_quotes
11
+
12
+ # テスト用の簡易モデルなのでドキュメント不要
13
+ Style/Documentation:
14
+ Exclude:
15
+ - "spec/**/*"
16
+
17
+ # ブロック長は指定しない
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - "spec/**/*"
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-11-02
4
+
5
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 NYUWAMOCHI
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 sugawara_nagisa
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,296 @@
1
+ # Social Game Kit
2
+
3
+ Ruby on Railsアプリケーション向けのソーシャルゲーム機能を提供するgemです。
4
+ 重み付き確率に基づくガチャ機能を実装しており、メモリ効率を考慮した設計になっています。
5
+
6
+ ## 機能
7
+
8
+ - 重み付きガチャ機能
9
+ - 確率計算機能
10
+ - 単発・複数回ガチャ対応(10連ガチャ等)
11
+
12
+ ## インストール
13
+
14
+ Gemfileに以下を追加:
15
+
16
+ ```ruby
17
+ gem 'social-game-kit'
18
+ ```
19
+
20
+ その後、以下を実行:
21
+
22
+ ```bash
23
+ bundle install
24
+ ```
25
+
26
+ ## 要件
27
+
28
+ - Ruby 3.1.0以上
29
+ - Rails 6.0以上
30
+ - ActiveRecord
31
+ - ActiveSupport
32
+
33
+ ## 使用方法
34
+
35
+ ### 基本的な使用方法
36
+
37
+ ```ruby
38
+ require 'sgk'
39
+
40
+ # カードマスタデータ(ActiveRecordモデル)から抽選
41
+ cards = GachaCard.all
42
+
43
+ # エンジンの初期化
44
+ engine = SGK::Gacha::Engine.new(cards)
45
+
46
+ # 単発抽選
47
+ card = engine.draw
48
+ puts card.name # => "コモン" など
49
+ puts card.rarity # => "common"
50
+
51
+ # 複数回抽選(10連ガチャ)
52
+ results = engine.draw_multiple(10)
53
+ puts results.size # => 10
54
+
55
+ # 確率の計算
56
+ probabilities = engine.probabilities
57
+ # => {1=>70.0, 2=>25.0, 3=>4.0, 4=>1.0}
58
+ ```
59
+
60
+ ### Railsアプリケーションでの例
61
+
62
+ ```ruby
63
+ # app/models/gacha_card.rb
64
+ class GachaCard < ApplicationRecord
65
+ validates :name, :rarity, :weight, presence: true
66
+ validates :weight, numericality: { greater_than: 0 }
67
+
68
+ enum rarity: {
69
+ common: 0,
70
+ rare: 1,
71
+ super_rare: 2,
72
+ ultra_rare: 3
73
+ }
74
+ end
75
+
76
+ # app/controllers/gacha_controller.rb
77
+ class GachaController < ApplicationController
78
+ def draw
79
+ engine = SGK::Gacha::Engine.new(GachaCard.all)
80
+ @result = engine.draw
81
+
82
+ # ユーザーにカードを付与する処理など
83
+ current_user.gacha_cards << @result
84
+
85
+ render json: @result.json_format
86
+ end
87
+
88
+ def draw_multiple
89
+ count = params[:count].to_i.clamp(1, 10) # 最大10連
90
+ engine = SGK::Gacha::Engine.new(GachaCard.all)
91
+ @results = engine.draw_multiple(count)
92
+
93
+ current_user.gacha_cards.push(*@results)
94
+
95
+ render json: {
96
+ cards: @results.map(&:json_format)
97
+ }
98
+ end
99
+
100
+ def probabilities
101
+ engine = SGK::Gacha::Engine.new(GachaCard.all)
102
+
103
+ render json: engine.probabilities
104
+ end
105
+ end
106
+ ```
107
+
108
+ ### ガチャカードモデルの例
109
+
110
+ ```ruby
111
+ # db/migrate/20240101000000_create_gacha_cards.rb
112
+ class CreateGachaCards < ActiveRecord::Migration[7.0]
113
+ def change
114
+ create_table :gacha_cards do |t|
115
+ t.string :name, null: false
116
+ t.string :rarity, null: false
117
+ t.integer :weight, null: false, default: 1
118
+ t.text :description
119
+ t.string :image_url
120
+
121
+ t.timestamps
122
+ end
123
+
124
+ add_index :gacha_cards, :rarity
125
+ add_index :gacha_cards, :weight
126
+ end
127
+ end
128
+
129
+ # db/seeds.rb
130
+ GachaCard.create!([
131
+ { name: "コモンカード", rarity: :common, weight: 70 },
132
+ { name: "レアカード", rarity: :rare, weight: 25 },
133
+ { name: "スーパーレアカード", rarity: :super_rare, weight: 4 },
134
+ { name: "ウルトラレアカード", rarity: :ultra_rare, weight: 1 }
135
+ ])
136
+ ```
137
+
138
+ ## 重み付き抽選アルゴリズム
139
+
140
+ このgemは累積重み方式を使用して、効率的にカードを抽選します。
141
+
142
+ ### アルゴリズムの説明
143
+
144
+ 1. 全カードの重みを合計(total_weight)
145
+ 2. 0〜total_weightの範囲でランダム値を生成
146
+ 3. 累積重みを計算しながら、ランダム値が該当する範囲のカードを選択
147
+
148
+ ### 例
149
+
150
+ ```
151
+ カードA: 重み70 (累積: 0-69)
152
+ カードB: 重み25 (累積: 70-94)
153
+ カードC: 重み4 (累積: 95-98)
154
+ カードD: 重み1 (累積: 99)
155
+ 合計: 100
156
+
157
+ ランダム値75 → カードBを選択
158
+ ```
159
+
160
+ ## APIリファレンス
161
+
162
+ ### SGK::Gacha::Engine
163
+
164
+ #### `initialize(card_relation)`
165
+
166
+ ActiveRecord::Relationを受け取ります。
167
+
168
+ ```ruby
169
+ engine = SGK::Gacha::Engine.new(GachaCard.all)
170
+ ```
171
+
172
+ #### `draw`
173
+
174
+ 単発ガチャを実行し、カードを返します。
175
+
176
+ ```ruby
177
+ card = engine.draw
178
+ ```
179
+
180
+ #### `draw_multiple(count)`
181
+
182
+ 複数回ガチャを実行します。countは正の整数である必要があります。
183
+
184
+ ```ruby
185
+ cards = engine.draw_multiple(10) # 10連ガチャ
186
+ ```
187
+
188
+ #### `probabilities`
189
+
190
+ 各カードの確率(パーセント)をハッシュで返します。
191
+
192
+ ```ruby
193
+ probs = engine.probabilities
194
+ # => {1=>70.0, 2=>25.0, 3=>4.0, 4=>1.0}
195
+ ```
196
+
197
+ ### SGK::Gacha::Result
198
+
199
+ ガチャ結果をラップするクラスです。
200
+
201
+ #### `card`
202
+
203
+ 抽選されたカードオブジェクトを返します(読み取り専用)。
204
+
205
+ #### `name`
206
+
207
+ カード名を返します。
208
+
209
+ #### `rarity`
210
+
211
+ カードのレア度を返します。
212
+
213
+ #### `json_format`
214
+
215
+ カード情報をハッシュで返します(JSON化用)。
216
+
217
+ ```ruby
218
+ result = engine.draw
219
+ result.json_format
220
+ # => { card_id: 1, name: "コモン", rarity: "common" }
221
+ ```
222
+
223
+ ## メモリ効率について
224
+
225
+ このgemの`probabilities`メソッドはハッシュを使用しており、実際に存在するカードのIDだけを保存します。
226
+ IDが飛び飛びの場合(例: 1-100と1000-10000)でも、間のIDのエントリは作成されません。
227
+
228
+ ⚠️ **配列を使った場合の問題**
229
+
230
+ ```ruby
231
+ # ❌ 配列を使った実装(メモリを大量に消費)
232
+ arr = []
233
+ arr[1000] = "value"
234
+ # インデックス0-999もメモリに確保される
235
+ ```
236
+
237
+ このgemはハッシュを使用することでこの問題を回避しています。
238
+
239
+ ## 開発
240
+
241
+ リポジトリをクローンした後:
242
+
243
+ ```bash
244
+ bundle install
245
+ ```
246
+
247
+ テストを実行:
248
+
249
+ ```bash
250
+ bundle exec rspec
251
+ ```
252
+
253
+ コード品質をチェック:
254
+
255
+ ```bash
256
+ bundle exec rubocop
257
+ ```
258
+
259
+ 対話的なプロンプトで実験:
260
+
261
+ ```bash
262
+ bin/console
263
+ ```
264
+
265
+ gemをローカルにインストール:
266
+
267
+ ```bash
268
+ bundle exec rake install
269
+ ```
270
+
271
+ ## テスト
272
+
273
+ このgemは包括的なテストスイートを含みます。
274
+
275
+ ```bash
276
+ # 全テスト実行
277
+ bundle exec rake
278
+
279
+ # RSpecのみ実行
280
+ bundle exec rspec
281
+
282
+ # RuboCopのみ実行
283
+ bundle exec rubocop
284
+ ```
285
+
286
+ ## 貢献
287
+
288
+ バグレポートやプルリクエストはGitHubで歓迎します。
289
+
290
+ https://github.com/NYUWAMOCHI/social-game-kit
291
+
292
+ ## ライセンス
293
+
294
+ このgemは[MIT License](https://opensource.org/licenses/MIT)の下でオープンソースとして利用可能です。
295
+
296
+ 詳細は[LICENSE.txt](LICENSE.txt)を参照してください。
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SGK
4
+ module Gacha
5
+ # Gacha engine that implements weighted random card selection.
6
+ #
7
+ # This engine takes an ActiveRecord relation of cards and provides methods
8
+ # to draw cards based on their weights, supporting both single draws and
9
+ # multiple draws in one operation.
10
+ class Engine
11
+ def initialize(card_relation)
12
+ @cards = card_relation.to_a
13
+ validate_cards!
14
+ @total_weight = @cards.sum(&:weight)
15
+ end
16
+
17
+ def draw
18
+ return nil if @cards.empty?
19
+
20
+ random_value = rand(0...@total_weight)
21
+ current_sum = 0
22
+
23
+ @cards.each do |card|
24
+ current_sum += card.weight
25
+ return card if random_value < current_sum
26
+ end
27
+
28
+ # NOTE: 浮動小数点誤差を考慮して最後のカードを返す実装だが、重み付き抽選の特性上起こる可能性はほとんどない。
29
+ # Returns the last card as a fallback for floating-point precision errors,
30
+ # though this scenario is highly unlikely given the nature of weighted random selection.
31
+ @cards.last
32
+ end
33
+
34
+ # Draw multiple cards in a single operation (e.g., 10-draw gacha).
35
+ #
36
+ # @param count [Integer] Number of cards to draw
37
+ # @return [Array<Object>] Array of drawn cards
38
+ # @raise [ArgumentError] If count is not positive
39
+ def draw_multiple(count)
40
+ raise ArgumentError, "Draw count must be positive" unless count.positive?
41
+
42
+ Array.new(count) { draw }
43
+ end
44
+
45
+ def probabilities
46
+ result = {}
47
+ @cards.each do |card|
48
+ result[card.id] = (card.weight.to_f / @total_weight * 100).round(2)
49
+ end
50
+ result
51
+ end
52
+
53
+ private
54
+
55
+ def validate_cards!
56
+ raise ArgumentError, "Cards cannot be empty" if @cards.empty?
57
+ raise ArgumentError, "All cards must respond to :weight" unless @cards.all? { |card| card.respond_to?(:weight) }
58
+ raise ArgumentError, "All weights must be positive" unless @cards.all? { |card| card.weight.positive? }
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SGK
4
+ module Gacha
5
+ # Represents the result of a gacha draw.
6
+ #
7
+ # This class wraps a card object and provides convenient accessors
8
+ # for common card properties. It can be serialized to a hash for
9
+ # logging or API responses.
10
+ class Result
11
+ attr_reader :card
12
+
13
+ def initialize(card)
14
+ @card = card
15
+ end
16
+
17
+ def name
18
+ @card.name
19
+ end
20
+
21
+ def rarity
22
+ @card.rarity
23
+ end
24
+
25
+ # Convert the result to a hash for JSON serialization.
26
+ #
27
+ # @return [Hash] A hash containing card_id, name, and rarity
28
+ def json_format
29
+ {
30
+ card_id: @card.id,
31
+ name: @card.name,
32
+ rarity: @card.rarity
33
+ }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SGK
4
+ VERSION = "0.1.0"
5
+ end
data/lib/sgk.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "sgk/version"
4
+ require_relative "sgk/gacha/engine"
5
+ require_relative "sgk/gacha/result"
6
+
7
+ module SGK
8
+ class Error < StandardError; end
9
+ end
data/sig/ssg.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ssg
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social-game-kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sugawara_nagisa
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-11-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '6.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '6.0'
40
+ description: Rails向けソーシャルゲーム機能kit。重み付きガチャエンジンなど
41
+ email:
42
+ - nyuwamochi@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".rspec"
48
+ - ".rubocop.yml"
49
+ - CHANGELOG.md
50
+ - LICENSE
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/sgk.rb
55
+ - lib/sgk/gacha/engine.rb
56
+ - lib/sgk/gacha/result.rb
57
+ - lib/sgk/version.rb
58
+ - sig/ssg.rbs
59
+ homepage: https://github.com/NYUWAMOCHI/social-game-kit
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/NYUWAMOCHI/social-game-kit
64
+ source_code_uri: https://github.com/NYUWAMOCHI/social-game-kit
65
+ changelog_uri: https://github.com/NYUWAMOCHI/social-game-kit/blob/main/CHANGELOG.md
66
+ rubygems_mfa_required: 'true'
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.1.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.6.2
82
+ specification_version: 4
83
+ summary: Social Game Kit for Ruby on Rails
84
+ test_files: []