Skip to content

Instantly share code, notes, and snippets.

@jmarmolejos
Created January 22, 2013 12:48
Show Gist options
  • Save jmarmolejos/4594415 to your computer and use it in GitHub Desktop.
Save jmarmolejos/4594415 to your computer and use it in GitHub Desktop.
def self.get_events_count token
notified_user = ApiKey.find_by_push_token(token).user
notifications_count = 0
# Getting events from active games
active_games = Match.find(:all, :conditions => [ "challenger_id = ? or opponent_id = ? and is_over = false or is_over is null", notified_user.id, notified_user.id ])
active_games.each do |game|
is_challenger = game.challenger_id == notified_user.id
if is_challenger
possible_round = game.rounds.where("challenger_score is null and number != 1").order("number ASC").first
if !possible_round.nil?
preceding_round = game.rounds.find_by_number(possible_round.number - 1)
notifications_count += 1 unless preceding_round.opponent_score.nil?
end
else
possible_round = game.rounds.where("opponent_score is null and challenger_score is not null").order("number ASC").first
notifications_count += 1 unless possible_round.nil?
end
end
# Getting events from finished games
finished_games = Match.find(:all, :conditions => [ "((challenger_id = ? and (challenger_saw_over_animation = false or challenger_saw_over_animation is null))
or (opponent_id = ? and (opponent_saw_over_animation = false or opponent_saw_over_animation is null)))
and is_over = true and expired=false", notified_user.id, notified_user.id ])
finished_games.each do |game|
notifications_count += 1
end
notifications_count
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment