Использование Tournament-Systems Gem для создания турнира

Итак, я нашел этот драгоценный камень:

https://github.com/ozfortress/tournament-system

Я понял, что мне нужно скопировать драйвер, который это:

require 'ostruct'

require 'tournament_system'

class TestDriver < TournamentSystem::Driver
  # rubocop:disable Metrics/CyclomaticComplexity
  def initialize(options = {})
    @teams = options[:teams] || []
    @ranked_teams = options[:ranked_teams] || @teams
    @matches = options[:matches] || []
    @winners = options[:winners] || {}
    @scores = options[:scores] || Hash.new(0)
    @team_matches = options[:team_matches] || build_team_matches_from_matches
    @created_matches = []
  end
  # rubocop:enable Metrics/CyclomaticComplexity

  attr_accessor :scores
  attr_accessor :teams
  attr_accessor :ranked_teams
  attr_accessor :matches
  attr_accessor :created_matches

  def seeded_teams
    @teams
  end

  def get_match_teams(match)
    match
  end

  def get_match_winner(match)
    @winners[match]
  end

  def get_team_score(team)
    @scores[team]
  end

  def get_team_matches(team)
    @team_matches[team]
  end

  def build_match(home_team, away_team)
    @created_matches << OpenStruct.new(home_team: home_team,
                                       away_team: away_team)
  end

  private

  def build_team_matches_from_matches
    result = {}
    @teams.each { |team| result[team] = [] }

    @matches.each do |match|
      match.reject(&:nil?).each do |team|
        result[team] << match if result.include?(team)
      end
    end

    result
  end
end

Я знаю, что это всего лишь тестовый драйвер, но я буду использовать его только для тестирования. Моя проблема в том, что я не знаю, что делать дальше. Я знаю, что мне нужно сделать экземпляр с помощью

driver = TestDriver.new

Я предполагаю, что это сделано в контроллере, как я сделал здесь:

class TourneysController < ApplicationController
  before_action :set_tourney, only: [:show, :edit, :update, :destroy]
  helper_method :tournament
  # GET /tourneys
  def index
    @tourneys = Tourney.all
  end

  # GET /tourneys/1
  def show
  end

  # GET /tourneys/new
  def new
    @tourney = Tourney.new
  end

  # GET /tourneys/1/edit
  def edit
  end

  # POST /tourneys
  def create
    @tourney = Tourney.new(tourney_params)
    @tourney.user_id = current_user.id if current_user

    if @tourney.save
      redirect_to @tourney, notice: 'Tourney was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /tourneys/1
  def update
    if @tourney.update(tourney_params)
      redirect_to @tourney, notice: 'Tourney was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /tourneys/1
  def destroy
    @tourney.destroy
    redirect_to tourneys_url, notice: 'Tourney was successfully destroyed.'
  end

  def tournament

    a = []
    for team in 0..Tourney.all.count do
    if((Tourney.where(id: [team], user_id: [current_user.id]).pluck(:user_id)).join("") != "") 
      require "round_robin_tournament"
      teams = RoundRobinTournament.schedule ((Tourney.where(id: [team], user_id: [current_user.id]).pluck(:teamName)).join("").split(" "))

      teams.each_with_index do |day, index|
        day_teams = day.map { |team| "(#{team.first}, #{team.last})" }.join(", ")
        #puts "Day #{index + 1}: #{day_teams}"
        a.push("Day #{index + 1}: #{day_teams} <br>" )
      #render html: '<div>"Day #{index + 1}: #{day_teams}"</div>'.html_safe
      end
      a.push("<br>Tournament Code: <strong>")
      a.push(Tourney.where(id: [team], user_id: [current_user.id]).pluck(:tournamentCode)).join("")
      a.push("</strong><br><br>")
    end
  end
    @teamArray = render html: "<div>#{a.join("")}</div>".html_safe
    #Tourny.pluck(:teamName)
  end

  def newtournament

    driver = TestDriver.new

    # Generate a round of a single elimination tournament
    TournamentSystem::SingleElimination.generate driver

  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_tourney
      @tourney = Tourney.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def tourney_params
      params.require(:tourney).permit(:noteams, :teamName, :tournamentCode, :user_id)
    end
end

Моя проблема в том, что мне делать дальше?

0 ответов

Другие вопросы по тегам