Skip to content

Instantly share code, notes, and snippets.

@Chandler
Last active August 29, 2015 13:57
Show Gist options
  • Save Chandler/9845333 to your computer and use it in GitHub Desktop.
Save Chandler/9845333 to your computer and use it in GitHub Desktop.
class Api::StatsController < Api::ApiController
def self.timeseries(game_id)
data = []
game = Game.find(game_id)
day = game.registration_start.to_date
# loop in 1 day increments from registration_start
# to game_end
while day < game.game_end.to_date
players = Player.where("game_id = ? AND created_at <= ?", game_id, day)
group = players.group_by { |player| player.true_status }
data << [
day.to_time.to_i,
(group[:human] || {}).length,
(group[:zombie] || {}).length,
(group[:starved] || {}).length
]
day += 1.day
end
data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment