streak
streak is a gem for calculating win/loss streaks. It uses Redis as its backend for collecting the data.
Installation
Add this line to your application's Gemfile:
gem 'streak'
And then execute:
$ bundle
Or install it yourself as:
$ gem install streak
Usage
streak is configurable with respect to its keys to allow for tracking other positive/negative things in a game like wins and losses, kills and deaths, etc.
# Configuration
Streak.configure do |configuration|
configuration.redis = Redis.new
configuration.namespace = 'streak'
configuration.positive_key = 'wins'
configuration.positive_total_key = 'wins_total'
configuration.positive_streak_key = 'wins_streak'
configuration.negative_key = 'losses'
configuration.negative_total_key = 'losses_total'
configuration.negative_streak_key = 'losses_streak'
configuration.total_key = 'total'
end
Streak.aggregate('david', 3) # 3 wins
Streak.aggregate('david', -2) # 2 losses
Streak.aggregate('david', 5) # 5 wins
Streak.aggregate('david', -1) # 1 loss
Streak.statistics('david')
=> {:wins=>0, :wins_total=>8, :wins_streak=>5, :losses=>1, :losses_total=>3, :losses_streak=>2, :total=>11}
Streak.statistics('david', [Streak.positive_streak_key, Streak.negative_streak_key])
=> {:wins_streak=>5, :losses_streak=>2}
Streak.reset_statistics('david')
Streak.statistics('david')
=> {:wins=>0, :wins_total=>0, :wins_streak=>0, :losses=>0, :losses_total=>0, :losses_streak=>0, :total=>0}
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Copyright
Copyright (c) 2012 David Czarnecki. See LICENSE for further details.