# Filters added to this controller apply to all controllers in the application. # Likewise, all the methods added will be available for all controllers. $n = 16 # number of teams class ApplicationController < ActionController::Base before_filter :set_user helper :trade helper :holding helper :market # Pick a unique cookie name to distinguish our session data from others' session :session_key => '_bracketology_session_id' def trade_info # get the user's recent trades @trade_list = @user.trades.find(:all, :order => "created_on DESC", :limit => 15) render(:partial => "trade/info") end def holding_info # get the user's current holdings holding_list = @user.holdings.find(:all) render(:partial => "holding/info", :locals => { :holding_list => holding_list, :cash_out => params[:cash_out] }) end def market_info market_list = @game.markets.find(:all, :conditions => ["status > 0"]) # global and conditional/open markets render(:partial => "market/list", :locals => {:market_list => market_list}) end def set_prices num_iter = 1000 total = 0 markets = Market.find(:all) # reset the market prices markets.each { |i| i.price = 0 } for iter in 1..num_iter # generate random outcome outcome = rand_outcome() # compute game winners w = winners(outcome) # compute number of shares on each outcome num_shares = 0 for market in markets if w[market.game-1] == market.team.id num_shares += market.outstanding_shares end end # adjust market prices for market in markets if w[market.game-1] == market.team.id market.price += Math.exp(num_shares.to_f/1000) end end total += Math.exp(num_shares.to_f / 1000) end # save new prices for market in markets market.price /= total market.save end end def winners(outcome) # initialize the matchups m1 = [1,3,5,7,9,11,13,15] m2 = [2,4,6,8,10,12,14,16] # initialize the winners w = Array.new(15,0) # iterate through the rounds to compute which teams played in each game round_pos = [0, 8, 12, 14, 15] for round in 0..3 start = round_pos[round] finish = round_pos[round + 1] - 1 game_pos = finish + 1 for pos in start..finish if outcome[pos] == 0 w[pos] = m1[pos] else w[pos] = m2[pos] end if pos % 2 == 1 m1[game_pos] = w[pos - 1] m2[game_pos] = w[pos] game_pos += 1 end end end return w end def rand_outcome(num_games=15) o = Array.new(num_games,0) for i in 0..(num_games - 1) o[i] = rand(2) end return o end protected def set_user @user = User.find(session[:id]) if @user.nil? && session[:id] end def logged_in if @user return true else return false end end end