Skip to content

Instantly share code, notes, and snippets.

@douglascamata
Created November 20, 2012 23:33
Show Gist options
  • Save douglascamata/4122010 to your computer and use it in GitHub Desktop.
Save douglascamata/4122010 to your computer and use it in GitHub Desktop.
Score!
def calculate
bet_scores_per_type = {
winner_fighter_id: { @result[:winner_fighter_id] => 10 },
fight_victory_type_id: { @result[:fight_victory_type_id] => 30},
round: { @result[:details][:round] => 50}
}
user_bets_per_type = {
winner_fighter_id: @betting[:bet_fighter_id],
fight_victory_type_id: @betting[:fight_victory_type_id]
round: @betting[:details][:round]
}
user_score = 0
user_bets_per_type.each do |type, bet|
user_score += bet_scores_per_type[type][bet] if bet_scores_per_type[type].has_key?(bet)
end
user_core
end
def calculate
score = 0
score += 20 if @result[:winner_fighter_id] == @betting[:bet_fighter_id]
score += 30 if @result[:fight_victory_type_id] == @betting[:fight_victory_type_id]
score += 50 if @result[:details][:round] == @betting[:details][:round]
score
end
@passalini
Copy link

class BettingScore
  attr_accessor :result_scores_per_type, :user_bets_per_type, :score

  def initialize(result, betting)
    @result_scores_per_type = {
      winner_fighter_id: { result[:winner_fighter_id] => add(20) },
      fight_victory_type_id: { result[:fight_victory_type_id] => add(30)},
      round: { result[:details][:round] => add(50)},
      minutes: { result[:details][:minutes] => mult(2)},
      seconds: { result[:details][:seconds] => mult(2)}
    }

    @user_bets_per_type = {
      winner_fighter_id: betting[:bet_fighter_id],
      fight_victory_type_id: betting[:fight_victory_type_id],
      round: betting[:details][:round],
      minutes: betting[:details][:minutes],
      seconds: betting[:details][:seconds]
    }
  end

  def score
   @score ||= calculate 
  end

  private
  def calculate
    score = 0

    @user_bets_per_type.each do |type, bet|
      score = @result_scores_per_type[type][bet].call(score) if @result_scores_per_type[type].has_key?(bet)
    end

    score
  end

  def add(prize)
    proc{|score| score += prize}
  end

  def mult(prize)
    proc{|score| score *= prize}
  end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment