Class: SGK::Gacha::PitySystem

Inherits:
Object
  • Object
show all
Defined in:
lib/sgk/gacha/pity_system.rb

Overview

使用例:

engine = SGK::Gacha::Engine.new(GachaCard.all)
pity = SGK::Gacha::PitySystem.new(engine, guarantee_limit: 100, guaranteed_rarity: :ultra_rare)

result = pity.draw(current_pity_count: user_current_pity)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, guarantee_limit: 100, guaranteed_rarity: :ultra_rare) ⇒ PitySystem

初期化

Parameters:

  • engine (SGK::Gacha::Engine)

    抽選に使用するガチャエンジン

  • guarantee_limit (Integer) (defaults to: 100)

    保証レアが出るまでの抽選回数(デフォルト: 100)

  • guaranteed_rarity (Symbol, String) (defaults to: :ultra_rare)

    保証するレアリティ(デフォルト: :ultra_rare)

Raises:

  • (ArgumentError)

    guarantee_limitが正の値でない場合



21
22
23
24
25
26
27
28
# File 'lib/sgk/gacha/pity_system.rb', line 21

def initialize(engine, guarantee_limit: 100, guaranteed_rarity: :ultra_rare)
  raise ArgumentError, "Engine must be a SGK::Gacha::Engine instance" unless engine.is_a?(Engine)
  raise ArgumentError, "Guarantee limit must be positive" unless guarantee_limit.positive?

  @engine = engine
  @guarantee_limit = guarantee_limit
  @guaranteed_rarity = guaranteed_rarity.to_sym
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



13
14
15
# File 'lib/sgk/gacha/pity_system.rb', line 13

def engine
  @engine
end

#guarantee_limitObject (readonly)

Returns the value of attribute guarantee_limit.



13
14
15
# File 'lib/sgk/gacha/pity_system.rb', line 13

def guarantee_limit
  @guarantee_limit
end

#guaranteed_rarityObject (readonly)

Returns the value of attribute guaranteed_rarity.



13
14
15
# File 'lib/sgk/gacha/pity_system.rb', line 13

def guaranteed_rarity
  @guaranteed_rarity
end

Instance Method Details

#draw(current_pity_count: 0) ⇒ Object

カード抽選

Parameters:

  • current_pity_count (Integer) (defaults to: 0)

    最後の保証カードからの抽選回数

Returns:

  • (Object)

    抽選されたカード(pity_count >= guarantee_limitの場合は保証UR)

Raises:

  • (ArgumentError)

    current_pity_countが負の値の場合



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sgk/gacha/pity_system.rb', line 35

def draw(current_pity_count: 0)
  raise ArgumentError, "Pity count must be non-negative" unless current_pity_count >= 0

  # 保証カードを返す
  if current_pity_count >= @guarantee_limit
    return draw_guaranteed_card
  end

  # それ以外の場合は通常の抽選を実行
  @engine.draw
end

#draw_multiple(count, current_pity_count: 0) ⇒ Array<Object>

複数のカードを抽選する

Parameters:

  • count (Integer)

    抽選するカードの枚数

  • current_pity_count (Integer) (defaults to: 0)

    最後の保証カードからの抽選回数

Returns:

  • (Array<Object>)

    保証カードの抽選カードの配列

Raises:

  • (ArgumentError)

    countが正の値でない、またはpity_countが負の値の場合



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sgk/gacha/pity_system.rb', line 53

def draw_multiple(count, current_pity_count: 0)
  raise ArgumentError, "Draw count must be positive" unless count.positive?
  raise ArgumentError, "Pity count must be non-negative" unless current_pity_count >= 0

  results = []
  current_count = current_pity_count

  count.times do
    card = draw(current_pity_count: current_count)
    results << card

    # 保証カードを引いた場合は抽選回数をリセット、それ以外はカウントを増やしていく
    if is_guaranteed_card?(card)
      current_count = 0
    else
      current_count += 1
    end
  end

  results
end

#next_guarantee_info(current_pity_count: 0) ⇒ Hash

次の保証までの残り回数と保証回数を取得する

Examples:

pity.next_guarantee_info(current_pity_count: 75)
# => { pity_count: 75, remaining: 25, guaranteed_at: 100 }

Parameters:

  • current_pity_count (Integer) (defaults to: 0)

    最後の保証カードからの抽選回数

Returns:

  • (Hash)

    次の保証までの残り回数と保証回数

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
# File 'lib/sgk/gacha/pity_system.rb', line 82

def next_guarantee_info(current_pity_count: 0)
  raise ArgumentError, "Pity count must be non-negative" unless current_pity_count >= 0

  {
    current_pity_count: current_pity_count,
    remaining: [@guarantee_limit - current_pity_count, 0].max,
    guaranteed_at: @guarantee_limit
  }
end