lundi 21 septembre 2015

Grouping array by sequencial date

This is more of a brains storm question I would like to run something like

Student Model

id
name
age
so on...

GameStatus Model

id
game_id
student_id
status
created_at

Student has many game_statuses. so every time a student opens a game it is added to the game_status model

I want to send little messages like... Congratulation! You have completed 5 challenges in a row.

So the marks are 5, 10, 15, 20 and 30.

It's pretty simple till here. I just loop through students and then loop through their game_statuses and if the count comes up to one of those marks I'll send a message.

But I want to make sure they hit those marks in a row.

As in 5 in the last five days or 10 in the last 10 days not broken. So 3 times the week after last 1 time last week and 1 time this week will return 5 but it should not trigger the reward.

I was thinking of looking into the created_at date and check if they are in a array of dates such as [today, today -1, today -2, today -3, today -4]

@student = Student.find(param[:id])
@games = @student.game_statuses
total_games = []
for game in @games do
  if game.status = 'completed'
    total_games += game.id
  end
end
return total_games.count

Aucun commentaire:

Enregistrer un commentaire