为用户创建添加徽章的条件

时间:2015-12-08 05:09:33

标签: ruby-on-rails ruby

我正在使用merit gem,这是根据winrate为我的用户添加徽章的条件

module Badges
  extend ActiveSupport::Concern

  included do
    after_update :badges_win_rate, if: :wr_60
  end

  def badges_win_rate
    wr = player.win_rate
    case
    when wr.between?(0.6, 0.7)
      player.add_badge(11) unless player.badges.any? {|b| b.id == 11}
    when wr.between?(0.7, 0.75)
      player.add_badge(12) unless player.badges.any? {|b| b.id == 12}
    when wr.between?(0.75, 0.8)
      player.add_badge(13) unless player.badges.any? {|b| b.id == 13}
    when wr.between?(0.8, 0.85)
      player.add_badge(14) unless player.badges.any? {|b| b.id == 14}
    when wr.between?(0.85, 0.9)
      player.add_badge(15) unless player.badges.any? {|b| b.id == 15}
    when wr > 0.9
      player.add_badge(16) unless player.badges.any? {|b| b.id == 16}
    end
  end

  private

  def wr_60
    player.win_rate > 0.59 && player.games.count > 20
  end
end

我有另一个方法调用rm_badge,它会根据徽章ID删除玩家的徽章。例如,当玩家的赢率从>下降时,如何使用此方法移除玩家的徽章? 0.9至> 0.85?感谢

1 个答案:

答案 0 :(得分:0)

根据您的评论,一种方法是使用方法rm_badge,如下所示:

module Badges
  extend ActiveSupport::Concern

  included do
    after_update :badges_win_rate, if: :wr_60
  end

  def badges_win_rate
    wr = player.win_rate

    remove_all_badges

    case
    when wr.between?(0.6, 0.7)
      player.add_badge(11) unless player.badges.any? {|b| b.id == 11}
    when wr.between?(0.7, 0.75)
      player.add_badge(12) unless player.badges.any? {|b| b.id == 12}
    when wr.between?(0.75, 0.8)
      player.add_badge(13) unless player.badges.any? {|b| b.id == 13}
    when wr.between?(0.8, 0.85)
      player.add_badge(14) unless player.badges.any? {|b| b.id == 14}
    when wr.between?(0.85, 0.9)
      player.add_badge(15) unless player.badges.any? {|b| b.id == 15}
    when wr > 0.9
      player.add_badge(16) unless player.badges.any? {|b| b.id == 16}
    end
  end

  private

  def wr_60
    player.win_rate > 0.59 && player.games.count > 20
  end

  def remove_all_badges
    if player.badges
      player.badges.each do |badge|
        player.rm_badge badge.id
      end
    end
  end
end