Using a driver scaffold

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1511 Views - Last Post: 24 April 2018 - 04:24 PM Rate Topic: -----

#1 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Using a driver scaffold

Posted 10 April 2018 - 09:45 PM

This may seem like a dumb question but I cannot seem to find an answer anywhere. I ran into this Gem and it is asking to implement a driver. How would one go abouts doing that? Here is the git link

https://github.com/o...urnament-system

Again sorry for the dumb question.
Is This A Good Question/Topic? 0
  • +

Replies To: Using a driver scaffold

#2 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4425
  • View blog
  • Posts: 18,923
  • Joined: 24-September 08

Re: Using a driver scaffold

Posted 11 April 2018 - 11:08 AM

Well it depends on what kind of tournament you're running. The ReadMe was pretty clear tbh. There are a few pre-generated ones as well as formatting for a custom tourney type.
Was This Post Helpful? 0
  • +
  • -

#3 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Using a driver scaffold

Posted 11 April 2018 - 12:41 PM

View PostNeoTifa, on 11 April 2018 - 12:08 PM, said:

Well it depends on what kind of tournament you're running. The ReadMe was pretty clear tbh. There are a few pre-generated ones as well as formatting for a custom tourney type.


Sorry I should have been more clear. I am confused as to how to use a driver scaffold in altogether. I don't know where to put the driver and how to call it from the controllers and views. If that is how it should go.
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4425
  • View blog
  • Posts: 18,923
  • Joined: 24-September 08

Re: Using a driver scaffold

Posted 11 April 2018 - 01:04 PM

I didn't know this was for Rails. I don't know how you're starting a tourney, so I'd imagine an admin would start a new tourney, and that would create a new driver. For altering the values for that would be in the controllers, and just calling them to display or form posting would be in views.
Was This Post Helpful? 0
  • +
  • -

#5 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Using a driver scaffold

Posted 11 April 2018 - 01:24 PM

Ok thank you for that. But it still isnt answering my question. I am wondering how do I create this driver. And how where would I alter the values. I guess what I am asking for is where do I begin in making this gem work on my project.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14856
  • View blog
  • Posts: 59,273
  • Joined: 12-June 08

Re: Using a driver scaffold

Posted 11 April 2018 - 01:28 PM

The github link provides, more or less, an explanation, and description of how to call it.. as well as the code layout.
https://github.com/o...nt-system#usage

Then it also links this information:

Quote

, simply subclass this class and implement the interface functions.

http://www.rubydoc.i...urnament/Driver
Was This Post Helpful? 1
  • +
  • -

#7 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4425
  • View blog
  • Posts: 18,923
  • Joined: 24-September 08

Re: Using a driver scaffold

Posted 11 April 2018 - 01:57 PM

Like I said, readme was pretty straightforward.

driver = Driver.new

Was This Post Helpful? 0
  • +
  • -

#8 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Using a driver scaffold

Posted 11 April 2018 - 02:43 PM

Ok I am sorry but I am still very confused.

Step 1: Add Gem to GemFile
Step 2: Copy the driver to the controller folder

class Driver < TournamentSystem::Driver
  def matches
    ...
  end

  def seeded_teams
    ...
  end

  def ranked_teams
    ...
  end

  def get_match_winner(match)
    ...
  end

  def get_match_teams(match)
    ...
  end

  def get_team_score(team)
    ...
  end

  def get_team_matches(team)
    ...
  end

  def build_match(home_team, away_team)
    ...
  end
end



step 3:

Use driver = Driver.new in the view section in order to call one of the functions? Would this be correct in saying?
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4425
  • View blog
  • Posts: 18,923
  • Joined: 24-September 08

Re: Using a driver scaffold

Posted 12 April 2018 - 06:04 AM

I would probably say step 3 would be in a controller as well. Is this your first Rails/web app? Are you familiar with the MVC pattern?

This post has been edited by NeoTifa: 12 April 2018 - 06:05 AM

Was This Post Helpful? 0
  • +
  • -

#10 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Using a driver scaffold

Posted 12 April 2018 - 09:32 AM

I am pretty new at rails/web app. I have just been following tutorials and I get the concept of controllers, views and the whole MVC patterns. I have just never used drivers yet since none of the tutorials have had them. So I wanted to dig deeper.

Your saying that in step 3 I should do:

  def newtournament
  
    driver = TestDriver.new

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



and than in the view do this

<%= newtournament %>

Was This Post Helpful? 0
  • +
  • -

#11 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4425
  • View blog
  • Posts: 18,923
  • Joined: 24-September 08

Re: Using a driver scaffold

Posted 13 April 2018 - 07:09 AM

No, you would say something like driver.getTeamScore in the views.
Was This Post Helpful? 0
  • +
  • -

#12 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Using a driver scaffold

Posted 13 April 2018 - 08:54 AM

So I did

<%= driver.get_team_score %>


But I got this error:

undefined local variable or method `driver' for #<#<Class:0x00007fbb060e2680>:0x00007fbb060e1398>
Was This Post Helpful? 0
  • +
  • -

#13 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4425
  • View blog
  • Posts: 18,923
  • Joined: 24-September 08

Re: Using a driver scaffold

Posted 13 April 2018 - 10:55 AM

Can you show all the code around it? Sorry, I'm only mildly familiar with Rails as I'm still learning. I'm more in the Cucumber realm, but I'll help where I can.

This post has been edited by NeoTifa: 13 April 2018 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

#14 Brandon222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 159
  • Joined: 16-November 08

Re: Using a driver scaffold

Posted 13 April 2018 - 02:24 PM

Thank you for helping. Here is my code:

Driver:

require 'ostruct'

require 'tournament_system'

class TestDriver < TournamentSystem::Driver
  helper_method :seeded_teams
  # 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



View

<p id="notice"><%= notice %></p>

<h1>Tourneys</h1>

<table>
  <thead>
    <tr>
      <th>Noteams </th>
      <th> Teamname </th>
      <th>Tournamentcode</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @tourneys.each do |tourney| %>
    <% if tourney.user_id == current_user.id %>
      <tr>
        <td><%= tourney.noteams %></td>
        <td><%= tourney.teamName %></td>
        <td><%= tourney.tournamentCode %></td>
        
        <td><%= link_to 'Show', tourney %></td>
        <td><%= link_to 'Edit', edit_tourney_path(tourney) %></td>
        <td><%= link_to 'Destroy', tourney, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
      <% end %>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Tourney', new_tourney_path %>

<%= tournament %>
<%= driver.get_team_score() %>



Controller

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



Route:

Rails.application.routes.draw do

  resources :tourneys
  resources :posts
  resources :comments, only: [:create, :destroy]
  devise_for :users
  resources :users do
    member do
      get :friends
      get :followers
      get :deactivate
      get :mentionable
    end
  end

  resources :events do
    collection do
      get :calendar
    end
  end

  authenticated :user do
    root to: 'home#index', as: 'home'
  end
  unauthenticated :user do
    root 'home#front'
  end

  resources :conversations do
    resources :messages
  end

  match :follow, to: 'follows#create', as: :follow, via: :post
  match :unfollow, to: 'follows#destroy', as: :unfollow, via: :post
  match :like, to: 'likes#create', as: :like, via: :post
  match :unlike, to: 'likes#destroy', as: :unlike, via: :post
  match :find_friends, to: 'home#find_friends', as: :find_friends, via: :get
  match :about, to: 'home#about', as: :about, via: :get

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end



The project is being built on top of another project so incase your wondering what the other parts in there are. Thanks again for the help
Was This Post Helpful? 0
  • +
  • -

#15 NeoTifa   User is online

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4425
  • View blog
  • Posts: 18,923
  • Joined: 24-September 08

Re: Using a driver scaffold

Posted 16 April 2018 - 11:38 AM

def get_team_score(team)

	    @scores[team]

	  end



This has a parameter and <%= driver.get_team_score() %> isn't passing one. Could that be it?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2