Created
September 27, 2011 02:12
-
-
Save aboyle/1244137 to your computer and use it in GitHub Desktop.
Script to retrieve data from CivStats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use Irssi; | |
sub getSite { | |
my @file = (); | |
open(INF, "wget -qO- http://www.civstats.com/viewgame.php?gameid=2153 |"); | |
while(my $line = <INF>) { | |
chomp($line); | |
push(@file, $line); | |
} | |
close(INF); | |
return \@file; | |
} | |
sub parseFile { | |
my($file_ptr) = shift; | |
my @file = @{$file_ptr}; | |
my $player_info = 0; | |
my @player_list = (); | |
my @score_list = (); | |
my @active_list = (); | |
my $players = 0; | |
my $time_remaining; | |
my $year = ""; | |
for(my $cursor = 0; $cursor <= $#file; $cursor++) { | |
if($file[$cursor] =~ /turntimer/) { | |
$time_remaining = $file[$cursor]; | |
$time_remaining =~ s/.*turntimer\">//; | |
$time_remaining =~ s/<.*//; | |
} | |
if($file[$cursor] =~ /viewyear/) { | |
$year = $file[$cursor]; | |
$year =~ s/.*\">//; | |
$year =~ s/<.*//; | |
} | |
if($player_info == 1) { | |
if($players == 0) { #this is the info data | |
$cursor+=8; | |
$players++; | |
} elsif($file[$cursor] =~ /br/) { | |
$player_info = 0; | |
$cursor = $#file + 1; | |
} else { | |
my $star = $file[$cursor+1]; | |
my $player = $file[$cursor+2]; | |
my $leader = $file[$cursor+3]; | |
my $nation = $file[$cursor+4]; | |
my $score = $file[$cursor+6]; | |
my $status = $file[$cursor+7]; | |
$cursor +=8; | |
$players++; | |
$player =~ s/.*">//; | |
$player =~ s/<.*//; | |
$score =~ s/.*">//; | |
$score =~ s/<.*//; | |
push(@player_list, $player); | |
if($star !~ /\*/) { | |
push(@active_list, ""); | |
} else { | |
push(@active_list, "*"); | |
} | |
push(@score_list, $score); | |
} | |
} | |
if($file[$cursor] =~ /Player Summary/) { | |
$player_info = 1; | |
} | |
} | |
return $year, $time_remaining, \@active_list, \@player_list, \@score_list; | |
} | |
sub fix_time { | |
my($old_time, $active_ptr) = @_; | |
my $inactive_count = 0; | |
for(my $i = 0; $i <= $#{$active_ptr}; $i++) { | |
if(${$active_ptr}[$i] ne "*") { | |
$inactive_count++; | |
} | |
} | |
#Get a better time estimate | |
my($hour,$min,$sec) = $old_time =~ /(.*):(.*):(.*)/; | |
my $insec = $hour * 3600 + $min * 60 + $sec; | |
$insec = $insec * 1.161 + 33.348; | |
$insec += $inactive_count * 180; #3 mins per login | |
my $new_hour = int($insec/3600); | |
my $new_min = $insec % 3600; | |
my $new_sec = $new_min % 60; | |
$new_min = int($new_min / 60); | |
$new_sec = int($new_sec); | |
$new_hour = sprintf('%02d',$new_hour); | |
$new_min = sprintf('%02d',$new_min); | |
$new_sec = sprintf('%02d',$new_sec); | |
my $new_time = $new_hour . ":" . $new_min .":" . $new_sec; | |
return $new_time; | |
} | |
sub public_status { | |
my ($server, $data, $nick, $mask, $target) =@_; | |
# Debug | |
#Irssi::active_win()->print("Server: $server"); | |
#Irssi::active_win()->print("Msg : $data"); | |
#Irssi::active_win()->print("Nick : $nick"); | |
#Irssi::active_win()->print("Addr : $mask"); | |
#Irssi::active_win()->print("Target: $target"); | |
# /Debug | |
#This allows for me to also submit requests | |
if($target eq "") { | |
$target = $nick; | |
} | |
if($data=~ /^!status$/i) { #STATUS | |
my $file = &getSite(); | |
my($year, $time, $active_ptr, $player_ptr, $score_ptr) = &parseFile($file); | |
my $message = "Year: $year | Time Remaining: $time"; | |
$message .= " | \002"; | |
for(my $i = 0; $i <= $#{$player_ptr}; $i++) { | |
if(${$score_ptr}[$i] == 0) { | |
$message .= "\00301"; | |
} elsif(${$active_ptr}[$i] eq "*") { | |
$message .= "\00303"; | |
} else { | |
# $message .= "\00304,01"; | |
$message .= "\00304"; | |
} | |
$message .= ${$active_ptr}[$i] . ${$player_ptr}[$i] . " " . ${$score_ptr}[$i] . " "; | |
} | |
$server->command( "msg $target $message" ); | |
} | |
if($data=~ /^!time$/i) { #TIME | |
my $file = &getSite(); | |
my($year, $time, $active_ptr, $player_ptr, $score_ptr) = &parseFile($file); | |
my $new_time = fix_time($time, $active_ptr); | |
$server->command( "msg $target Time remaining this turn: $time "); | |
$server->command( "msg $target More realistic time estimate: $new_time" ); | |
} | |
if($data=~ /^!list$/i) { #LIST | |
my $file = &getSite(); | |
my($year, $time, $active_ptr, $player_ptr, $score_ptr) = &parseFile($file); | |
my @inactive_list = (); | |
for(my $i = 0; $i <= $#{$player_ptr}; $i++) { | |
if(${$active_ptr}[$i] ne "*") { | |
push(@inactive_list, ${$player_ptr}[$i]); | |
} | |
} | |
my $message = join(", ", @inactive_list); | |
$server->command( "msg $target Players who have not finished this turn: $message" ); | |
} | |
# else {} #nothing | |
} | |
Irssi::signal_add_last('message public', 'public_status'); | |
Irssi::signal_add_last('message own_public', 'public_status'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment