Aca les dejo un codigo que sirve para generar un fixture de futbol, de la manera mas eficiente que he visto hasta el momento.
Para genrar un fixture lo que debes hacer es DeportFixture.new(lista de cuadros).groups eso te debuelve el fixture.
class DeportFixture
def initialize(elements)
@elements = elements
@elements << nil if elements.length.odd?
@round_number = @elements.length - 1
@rounds = Array.new(@round_number, nil)
@last_element = @elements[-1]
end
def groups
fixture = Array.new(@round_number, nil)
@round_number.times do |time|
@rounds[time] ||= []
if time == 0
(@elements.length / 2).times do |element_id|
@rounds[time] << @elements[element_id]
@rounds[time] << @elements[-(element_id+1)]
end
else
previous_round = @rounds[time-1].clone
first_element, second_element = previous_round[-1], @last_element
first_element, second_element = @last_element, previous_round[-1] if time.odd?
previous_round.delete(first_element)
previous_round.delete(second_element)
previous_round.compact!
@rounds[time] << first_element
@rounds[time] << second_element
new_round_elements = []
while not previous_round.empty?
elements = previous_round.slice!(-2,2)
elements.each {|element| new_round_elements << element}
end
new_round_elements.each {|element| @rounds[time] << element}
end
end
@rounds.each_with_index do |round, index|
fixture[index] = []
(round.length / 2).times do |team|
item_1 = round[(team * 2)]
item_2 = round[(team * 2) + 1]
fixture[index] << [item_1, item_2] unless item_1.nil? or item_2.nil?
end
end
fixture
end
end
Suscribirse a:
Enviar comentarios (Atom)
2 comentarios:
puedes dar más detalle de como se puede utilizar? no se entiende bien tu post, no se si debo hacerlo en un arhivo php o que o cómo, es decir no tienes un paso a paso?
Esto es en un archivo de ruby, lo unico que debes hacer es copiar el codigo pegarlo en un archivo de ruby y ejecutar lo siguiente
fixture = DeportFixture.new(["a", "b", "c"])
# ["a", "b", "c"] es la lista de cuadros
y luego
fixture.groups
Esto te devuelve un arreglo con las fechas para los partidos
Publicar un comentario