views:

60

answers:

3

The rule at work here is that users can be awarded badges for a streak of 10, 20, and 30. But the user can't be awarded multiple badges for the same streak. I'm tracking consec wins in the user model.

For example, if the user hits a 10-streak, the user is awarded a 10-streak badge. If the user is on a 20-streak, he/she receives a 20-streak badge. If the user is on a 30-game win streak, the user is awarded a 30-streak badge. The user shouldn't be awarded three 10-streak badges -- only one 10-streak, one 20-streak, and one 30-streak.

Further, if the user reaches a 40-win streak, then the user should be awarded a 10-streak badge. If the user hits 50, then he/she should be awarded a 20-streak badge. If the user hits 60, the user should be award a 30-streak badge. If the user hits 70, the user should be awarded a 10-streak. I think you get the pattern here. A 30-streak trophy is the max a user can get. But the user can be on an infinite winning streak.

  def check_win_streak(streak)
    badge = 10
    while badge < BADGE::MAX_STREAK_BADGE_SIZE do # MAX_STREAK_BADGE_SIZE = 30

       if streak < badge then
         break
       end

       if (streak % badge == 0) then
         award_streak_badge(badge)
       end

       badge += 10
    end
  end
A: 

try MOD arithmatic.

mod the streak by 30 - and you will get results that repeat in each band of 30...

Randy
A: 

Modulus to get rid of the rests i.e. 62 % 30 = 2. Then there is division to get the number of 30 streaks.

Starting with 74
74 % 30 = 14
(74 - 14) / 30 = 2
14 - (14 % 10) = 10
Result: 2x30 streaks and 1x10 streak
Don
+1  A: 

Don and Randy gave the general idea. Here is the complete code:

def check_win_streak(streak)
  if streak % 10 == 0
    award_streak_badge(streak % badge::MAX_STREAK_BADGE_SIZE)
  end
end
Michael Ulm