#!/usr/bin/perl -w # AUTHORS # Sean Forman # Hans Van Slooten # $Date: 2017-03-06 12:12:11 -0500 (Mon, 06 Mar 2017) $ # $Author: hvs $ # $Rev: 49675 $ # $HeadURL: http://svn.sports-reference.com/svn/br_repos/br/trunk/play-index/streak_finder.cgi $ # Copyright 2000-2017, SPORTS REFERENCE, LLC All rights reserved. # Get the site lib directory. use Carp; use CGI::Fast ':standard'; use FindBin qw($Bin); use JSON; use Modern::Perl '2010'; use lib "$Bin/../lib"; use SR::Auth::Users; use SR::Defaults; use SR::Statline; use SRlocal::Constants; use SRlocal::DB; use SRlocal::Defaults; use SRlocal::Stats; use SRlocal::Formatting; use SRlocal::PlayIndex; use SRlocal::PlayIndex::Constants; use SRlocal::PlayIndex::Formatting; use SRlocal::PlayIndex::Subscribe; use SRlocal::PlayIndex::Statdefine; use SRlocal::PlayIndex::StreakFinder; use SRlocal::Retrosheet::Plays; use SRlocal::Templates; chomp($0); MAIN: { my $dbh = get_site_db_connection( $Bin, $SRlocal::PlayIndex::Constants::PI_DSN ); my $dbhSR = get_site_db_connection( $Bin, 'SR' ); my $site_params_ref = get_site_params($Bin); my $srtemplates = new SRlocal::Templates(); while ( my $q = new CGI::Fast ) { ## Check the user's PI subscription status my %user = (); if ( $ENV{'HTTP_COOKIE'} ) { %user = %{ SR::Auth::Users::get_user($q) }; } my $sub_level = SRlocal::PlayIndex::Subscribe::subscriber_level( $dbhSR, $user{user_ID}, 'pi_subscribe' ); ## Query Parameters my $player_id = url_player_id( $q, 'id', '' ); my $year = url_alphanum( $q, 'year', 0 ); my $type = url_regex( $q, 'type', qr/^(b|p)$/, 'b' ); my $class = url_regex( $q, 'class', qr/^(team|player)$/, 'player' ); my $is_ajax = url_regex( $q, 'ajax', qr/^1$/, $FALSE ); my $is_submitted = url_regex( $q, 'request', qr/^1$/, $FALSE ); my $describe_only = url_regex( $q, 'describe_only', qr/^1$/, $FALSE ); my $suffix = url_regex( $q, 'suffix', qr/^_[a-z]+$/, '' ); my $title = ( $class eq 'team' ? 'Team ' : $EMPTY_STR ) . ( $type eq 'b' ? 'Batting' : $type eq 'p' ? 'Pitching' : 'Other' ) . ' Streak Finder'; our $page_url = $SRlocal::Constants::SITE_URL . '/play-index/steak_finder.cgi?'; if ($player_id) { my $player_name = gtrc( $dbh, 'bio', { player_ID => $player_id }, 'name_common' ); $title .= $SPACE . '—' . $SPACE . $player_name; } my @yah = ( 'Play Index', qq{$title} ); my $model = { header => { page_title => $title, page_url => $page_url, you_are_here => generate_you_are_here( \@yah ), qi_section => 'play_index', page_description => 'Find single-season or multi-year totals to create batting leaderboards by team, position, age and more', }, form_id => 'streak_finder', description => ' ', }; if ( !$describe_only ) { ## Get the model for generating the PI form my $form_settings = _forms( $dbh, $q, $srtemplates, $model, $class, $type, $player_id ); ## Generate the PI form $model->{form_output} = $srtemplates->process( 'Partials/Forms/Form_PI.tt2', $form_settings ); ## Add a note about data availablity $model->{note} = $SRlocal::PlayIndex::Constants::DATA_SEASONS; } # if this is a form submission, ajax or not, get the results. if ($is_submitted) { $model = SRlocal::PlayIndex::StreakFinder::run_report( $dbh, $q, $srtemplates, $model, { sub_level => $sub_level, user => $user{user_ID}, id => $player_id, year => $year, type => $type, describe_only => $describe_only, suffix_type => $suffix, } ); } ## Generate the page my $page_data; if ( !$describe_only ) { $page_data = $srtemplates->process( 'Pages/PlayIndex.tt2', $model ); } else { $page_data = $model->{description_array} ? encode_json( $model->{description_array} ) : "['$model->{description}']"; } ## Output the file to the user print $q->header( -type => ( !$describe_only ? 'text/html' : 'text/json' ), -charset => 'utf-8', -SR_SRC => 'build' ); print $page_data; } # cleanup and exit. $dbhSR->disconnect; $dbh->disconnect; } ################################################################### # Usage : zzzz # Purpose : zzzz # Returns : zzzz # Parameters : zzzz # Throws : zzzz # See Also : zzzz # Comments : zzzz ################################################################### sub _forms { my ( $dbh, $cgi, $srtemplates, $model, $class, $type, $player_id ) = @_; my $report = $class eq 'team' ? 't' . $type : $type; my $type_name = $type eq 'b' ? 'batting' : 'pitching'; my $type_name_2 = $type eq 'b' ? 'batter' : 'pitcher'; ## Build the main part of the PI form my $form_main = { fields => [ { type => 'hidden', name => 'as', val => ( $class eq 'team' ? 'team_' . $type_name : 'result_' . $type_name_2 ) }, { type => 'hidden', name => 'class', val => $class }, { type => 'hidden', name => 'offset', val => 0 }, { type => 'hidden', name => 'type', val => $type }, @{ $PI_STREAK_FINDER_DETAILS{$report}{additional_fields} }, ], }; push @{ $form_main->{fields} }, { type => 'hidden', name => 'id', val => $player_id } if $player_id; ## Any dynamic widgets need to be executed. my $i = 0; for my $field ( @{ $form_main->{fields} } ) { if ( ref $field eq 'CODE' ) { $form_main->{fields}[$i] = &$field($cgi); } $i++; } ## Build the filter criteria control my $form_criteria = SRlocal::PlayIndex::Constants::get_form_additional_criteria( 'streak_finder', $report, $class, $cgi ); ## Build the final form details my $form_settings = { method => 'get', action => '/play-index/streak_finder.cgi', name => 'streak_finder', script => $SRlocal::PlayIndex::Constants::GAME_YEAR_SELECT_JS . $SRlocal::PlayIndex::Constants::CHOOSE_TEAM_BY_NICKNAME_SELECT_JS, formdata => scalar $cgi->Vars, form_main => $form_main, form_criteria => $form_criteria, }; return $form_settings; } ################################################################### # Usage : zzzz # Purpose : zzzz # Returns : zzzz # Parameters : zzzz # Throws : zzzz # See Also : zzzz # Comments : zzzz ################################################################### sub _get_team_pitch_streak_form { my ($ref) = @_; my $dbh = $ref->{dbh}; my $year = is_empty_zero( $ref->{year} ); my $q_param = $ref->{q_param}; my $year_min = $FIRST_YEAR_RETRO; my $year_max = $CURRENT_YEAR_RETRO; my $years_select_min = SR::Forms::get_form_select( { class => $EMPTY_STR, id => 'min_year_game', name => 'min_year_game', size => 1, options => [ $year_min .. $year_max ], options_selected => [ $year || $year_min ], options_disabled => [ $year_min .. ( $FIRST_YEAR_RETRO - 1 ) ], } ); my $years_select_max = SR::Forms::get_form_select( { class => $EMPTY_STR, id => 'max_year_game', name => 'max_year_game', size => 1, options => [ $year_min .. $year_max ], options_selected => [ $year || $year_max ], options_disabled => [ $year_min .. ( $FIRST_YEAR_RETRO - 1 ) ], } ); my $hidden = $EMPTY_STR; my $years_out = SRlocal::PlayIndex::Formatting::get_gamelogs_years_select( $years_select_min, $years_select_max ); my $matching_criteria = SRlocal::PlayIndex::Constants::get_form_additional_criteria( 'streak_finder_1', 'tp', 'player', $q_param ); my $presets = <<"END_HTML";
Consecutive Ws / Losses / Shutouts
END_HTML my $matching_criteria_box = SRlocal::PlayIndex::Constants::get_streaks_matching_box( '
' . $matching_criteria . '
', $presets ); my $additional_criteria = SRlocal::PlayIndex::Constants::get_form_additional_criteria( 'streak_finder_2', 'tp', 'team', $q_param ); return <<"END_HTML"; $hidden
Find streaks when searching through all of the games matching these criteria
A pool of games matching these criteria will be created, and then streaks will be located. Games not matching these criteria will not break a streak.
$SRlocal::PlayIndex::Constants::GAME_TYPE_SELECT $years_out $SRlocal::PlayIndex::Constants::POST_SERIES_STREAK_SELECT_BOX
$SRlocal::PlayIndex::Constants::RESET_BUTTON
$SRlocal::PlayIndex::Constants::TEAM_AND_OPPONENT_GAME_BOX $SRlocal::PlayIndex::Constants::NOSHADE_DIVIDE $SRlocal::PlayIndex::Constants::HOME_AWAY_BOX $SRlocal::PlayIndex::Constants::GAME_SITE_BOX $SRlocal::PlayIndex::Constants::STREAK_AT_START_BOX_TEAMS $matching_criteria_box
Display additional, advanced criteria
Hide advanced criteria
$additional_criteria
END_HTML }