Project

easy_rr

0.0
No commit activity in last 3 years
No release in over 3 years
Simple gem that generates round robin matches based on a received array of participants and an amount of times they will face each other.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.10
~> 10.0
 Project Readme

Installation

Add this line to your application's Gemfile:

gem 'easy_rr'

And then execute:

$ bundle

Or install it yourself as:

$ gem install easy_rr

Usage

Using easy_rr is very easy, as its name suggests. You just need to pass it an array and an integer to specify the amount of times that each participant will face each other.

It receives two parameters: an array and an integer.

The array consists of the users, teams, or participants that will be matched. The integer is the amount of times each participant will face each other participant. This defaults to 1.

Here's a little example:

	#You can pass an array
	teams = [ "Colombia", "Peru", "Venezuela", "Brazil" ]

	#Then you can call EasyRr to generate the matches
	games = EasyRr.matches(teams)

	#=> [[["Venezuela", "Brazil"], ["Peru", "Colombia"]], [["Peru", "Brazil"], ["Colombia", "Venezuela"]], [["Colombia", "Brazil"], ["Venezuela", "Peru"]]] 

By passing a second parameter you can specify the amount of times each participant will face each other participant, as said before.

So, in another little example:

	#The array

	teams = [ "Real Madrid", "Bayern Munchen", "Juventus", "F.C. Barcelona" ]

	#Generate the matches, this time you pass 2 as the number of times they will face each other
	games = EasyRr.matches(teams,2)

	#=> [[["Bayern Munchen", "F.C. Barcelona"], ["Juventus", "Real Madrid"]], [["Juventus", "F.C. Barcelona"], ["Real Madrid", "Bayern Munchen"]], [["Real Madrid", "F.C. Barcelona"], ["Bayern Munchen", "Juventus"]], [["Bayern Munchen", "F.C. Barcelona"], ["Juventus", "Real Madrid"]], [["Juventus", "F.C. Barcelona"], ["Real Madrid", "Bayern Munchen"]], [["Real Madrid", "F.C. Barcelona"], ["Bayern Munchen", "Juventus"]]] 

If you want, you can also pass an ActiveRecord object, for instance:

	#This can be the collection of participants

	participants = Model.teams

	#As this can also be passed

	participants = User.all

Either way, an array of integers can also be passed if you don't have the names of each participant but the ids.

	#Array of ids
	teams = [ 1,2,3,4 ]

	games = EasyRr.matches(teams)
	=> [[[2, 4], [3, 1]], [[3, 4], [1, 2]], [[1, 4], [2, 3]]] 

If the array passed is odd, the generated array will have one match where one of the teams is nil.

	#Array of ids
	teams = [ 1,2,3,4,5 ]

	games = EasyRr.matches(teams)
	=> [[[2, nil], [3, 1], [4, 5]], [[3, nil], [4, 2], [5, 1]], [[4, nil], [5, 3], [1, 2]], [[5, nil], [1, 4], [2, 3]]] 

Finally, you can loop through the generated array and create each match in your database according to the model you have.

	teams = [ "Italy", "France", "Argentina", "Ghana" ]

	teams.each do |j|
		j.map { |x| Model.create("params_here") }
	end

Enjoy.

License

The gem is available as open source under the terms of the MIT License.