############################################# ### call this script like so: ### perl sortNSHL.pl [download directory] [season number] [team name] ### example: perl sortNSHL.pl c:\temp\NSHL\ 9 RIOT ### This script requires the LWP (lib-www perl) package. ### ### - Colin Reid, GM Regina Riot ### colreid@gpfn.sk.ca ############################################# ###### extract my team and put it in its own file sub ExtractMyTeam { my ($startteam, $myteam, $path, $sortfile) = @_; $myteamfile = $path . $myteam . ' Rosters.txt'; $myteamoldfile = $path . $myteam . ' Rosters OLD.txt'; if (-e $myteamfile) {`cp "$myteamfile" "$myteamoldfile"`}; open (TEAMS, $sortfile); open (MYTEAM, '>' . $myteamfile) or die 'Could not open team file to extract rosters.'; $start = 'F'; while (){ chomp; $tmp = $_; if ($tmp =~ /$startteam$myteam/){ $start = "T"; } if ($tmp =~ /===================/){ $start = "F"; } if ($start eq "T"){ print MYTEAM $tmp . "\n"; } } close (MYTEAM); close (TEAMS); print "Successfully extracted team to $myteamfile\n"; } #### sort players by total, and then by position #### sub teamsort { my $numplayers = $#team; my $outstr = ''; my $tempstr = ''; my $rating1 = ''; my $rating2 = ''; my $position1 = ''; my $position2 = ''; my %pos = ('C' => 1, 'LW' => 2, 'RW' => 3, 'D' => 4, 'G' => 5); for ($x = 0; $x < $numplayers; $x++){ for ($y = 0; $y < $numplayers; $y++){ if ($team[$y] =~ / (\d{2})/){ $rating1 = $1; } if ($team[$y + 1] =~ / (\d{2})/){ $rating2 = $1; } if ($rating1 < $rating2){ $tempstr = $team[$y]; $team[$y] = $team[$y + 1]; $team[$y + 1] = $tempstr; } } } for ($x = 0; $x < $numplayers; $x++){ for ($y = 0; $y < $numplayers; $y++){ if ($team[$y] =~ / (C|LW|RW|D|G) /){ $position1 = $1; } if ($team[$y + 1] =~ / (C|LW|RW|D|G) /){ $position2 = $1; } if ($pos{$position1} > $pos{$position2}){ $tempstr = $team[$y]; $team[$y] = $team[$y + 1]; $team[$y + 1] = $tempstr; } } } for ($y = 0; $y < $numplayers; $y++){ if ($team[$y] =~ / (C|LW|RW|D|G) /){ $position1 = $1; } if ($team[$y + 1] =~ / (C|LW|RW|D|G) /){ $position2 = $1; } if ($pos{$position2} > $pos{$position1}){ $team[$y] .= "\n"; } } foreach $player (@team){ print OUT1 $player . "\n"; #print $player . "\n"; } while ($#team >= 0){ pop @team, $outstr; #print OUT1 $outstr . "\n"; #print $outstr . "\n"; } } ######## get new rosters from NSHL.com sub getrosters{ my ($url, $fn) = @_; use HTTP::Request::Common; use LWP::UserAgent; my $ua1 = LWP::UserAgent->new; $ua1->agent("Mozilla/2.0 (compatible; MSIE 3.02; Update a; AK; Windows 95)"); $ua1->timeout(120); $meth = "GET"; my $req = new HTTP::Request $meth,$url; my $res = $ua1->request($req); if ($res->is_success){ print "Successfully downloaded rosters to $fn\n"; my $result = $res->content; open (outf1, ">$fn"); print outf1 $result; close (outf1); } else { my $msg = $res->as_string(); die "$msg\n"; } } ############################################# ############### MAIN PROGRAM ################ ############################################# ### call this script like so: ### perl sortNSHL.pl [download directory] [season number] [team name] ### example: perl sortNSHL.pl c:\temp\NSHL\ 9 RIOT ### This script requires the LWP (lib-www perl) package. ### ### - Colin Reid, GM Regina Riot ### colreid@gpfn.sk.ca ############################################# $path = $ARGV[0]; # $path = "c:\\temp\\NSHL\\"; $seasonnumber = $ARGV[1]; # $seasonnumber = '9'; $myteam = $ARGV[2]; # $myteam = 'RIOT'; ###### $url = 'http://www.nshl.com/roster' . $seasonnumber . '.txt'; $infile = $path . 'NSHL' . $seasonnumber . ' rosters.txt'; $sortfile = $path . 'NSHL' . $seasonnumber . ' rosters (sorted).txt'; getrosters($url, $infile); $begin = ' \# player\'s name Inj H Ps Ag Qk Pw Fa In Pn Sk Pa PC Ck Sh Ttl'; $end = '---------------------------'; $startteam = 'Team : '; $start = "F"; open (IN1, $infile) or die 'Could not open league rosters file for sorting'; open (OUT1, ">$sortfile") or die 'Could not write to "sorted" league rosters file'; while (){ chomp; $temp = $_; if ($temp =~ /$startteam/){ $temp = '============================================================================' . "\n\n" . $temp; $start = "F"; } if ($temp =~ /$end/){ teamsort; $start = "F"; } if ($start eq "F"){ print OUT1 "$temp\n"; #print "$temp\n"; } else { push @team, $temp; } if ($temp =~ /$begin/){ $start = "T"; #print OUT1 "$temp\n"; #print "$temp\n"; } } close (OUT1); close (IN1); print "Successfully sorted rosters to $sortfile\n"; ExtractMyTeam($startteam, $myteam, $path, $sortfile); print "done!\n";