package StampedeVotes;

use strict;
use warnings;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %config %blacklist %option %vote %vote_to_key %key_to_vote);

require Exporter;

@ISA       = qw(Exporter AutoLoader);
@EXPORT    = qw();
@EXPORT_OK = qw(&gotdate2time &time2date &date2time %config %blacklist %option %vote %vote_to_key %key_to_vote);

$VERSION   = 0.01;

use Date::Calc;


################################################################################
# Variables

%config = (
            # Vote period (as used by the RRDs and automatic poll closing
            period_start => '2024-02-25',
            period_end   => '2024-03-03',

            # Poll title (as used by the web page, graphs, etc)
            title        => 'Stampede #23',

            # GoT topic URL where the votes are placed
            topic_url    => 'https://gathering.tweakers.net/forum/list_messages/2235852',
          );


%blacklist = ( 
                #'33551886' => 'quote vote',
                #'33515826' => 'changed vote in <a href="http://gathering.tweakers.net/forum/list_message/33588871#33588871">33588871</a>',
                #'58374022' => 'no vote, breaks rrdupdate',
                '78254920' => 'start post',
             );

# General Options:
#
#  ABC@home
#  Acoustics@home
#  Amicable Numbers
#  Asteroids@Home
#  BOINC Seti@Home
#  BOINC µFluids@Home
#  Citizen Science Grid
#  Collatz Conjecture
#  Constellation
#  Cosmology@home
#  DENIS@Home
#  Docking
#  DPAD
#  D2OL
#  Einstein@Home
#  Enigma@Home
#  eOn 2
#  Einstein@home 
#  Folding@Home
#  GIMPS
#  GPUGRID
#  Leiden Classical
#  Malariacontrol.net
#  MilkyWay@Home
#  NFS@home
#  NumberFields@home
#  OGR-25
#  OGR-28
#  POEM@Home
#  Primaboinca
#  PrimeGrid
#  PSP-PRP
#  RakeSearch
#  RC5-72
#  Rosetta@Home
#  SAT@home
#  SiDock@home
#  SoB
#  SZTAKI
#  theSkyNet
#  TN-Grid
#  UD-Grid
#  Universe@home
#  VGTU@Home
#  World Community Grid 
#  YAFU
#  Yoyo@home

# Options for Stampede 23:
#  Amicable Numbers
#  Collatz Conjecture
#  DENIS@Home
#  Folding@Home
#  GIMPS
#  MilkyWay@Home
#  NumberFields@home
#  PrimeGrid
#  RakeSearch
#  RC5-72
#  Rosetta@Home
#  SiDock@home
#  TN-Grid
#  Yoyo@home
#  World Community Grid 

# Available options in the current vote
%vote = (
             'Amicable Numbers'     => 0,
             'Collatz Conjecture'   => 0,
             'DENIS@Home'           => 0,
             'Folding@Home'         => 0,
             'GIMPS'                => 0,
             'MilkyWay@Home'        => 0,
             'NumberFields@home'    => 0,
             'PrimeGrid'            => 0,
             'RakeSearch'           => 0,
             'RC5-72'               => 0,
             'Rosetta@Home'         => 0,
             'SiDock@home'          => 0,
             'TN-Grid'              => 0,
             'Yoyo@home'            => 0,
             'World Community Grid' => 0,
        );

%option = (
               'ABC@home'             => { 
                                           key     => 'abc', 
                                           matches => [ 
                                                        'ABC@home',
                                                        'ABC-home',
                                                        'ABC',
                                                      ],
                                         },
               'Acoustics@home'       => { 
                                           key     => 'acoustics', 
                                           matches => [ 
                                                        'Acoustics@home',
                                                        'Acoustics-home',
                                                        'Acoustics',
                                                      ],
                                         },
               'Amicable Numbers'     => { 
                                           key     => 'amicable', 
                                           matches => [ 
                                                        'Amicable Numbers',
                                                        'AmicableNumbers',
                                                      ],
                                         },
               'Asteroids@Home'       => { 
                                           key     => 'asteroids', 
                                           matches => [ 
                                                        'Asteroids@Home',
                                                        'Asteroids-Home',
                                                        'Asteroids',
                                                      ],
                                         },
               'BOINC Seti@Home'      => { 
                                           key     => 'seti', 
                                           matches => [ 
                                                        'BOINC\s*Seti@Home',
                                                        'BOINC\s*Seti-Home',
                                                        'Seti@Home',
                                                        'Seti',
                                                        '\bS@H',
                                                      ],
                                         },
               'BOINC µFluids@Home'   => { 
                                           key     => 'ufluids',
                                           matches => [ 
                                                        '.Fluids@Home',
                                                      ],
                                         },
               'Citizen Science Grid' => { 
                                           key     => 'citizen', 
                                           matches => [ 
                                                        'Citizen\s*Science\s*Grid',
                                                        'Citizen',
                                                        'CSG',
                                                      ],
                                         },
               'Collatz Conjecture'   => { 
                                           key     => 'collatz', 
                                           matches => [ 
                                                        'Collatz\s*Conjecture',
                                                        'Collatz',
                                                      ],
                                         },
               'Constellation'        => { 
                                           key     => 'constellation', 
                                           matches => [ 
                                                        'Constellation',
                                                      ],
                                         },
               'Cosmology@home'       => { 
                                           key     => 'cosmology', 
                                           matches => [ 
                                                        'Cosmology@home',
                                                        'Cosmology-home',
                                                        'Cosmology',
                                                        '\bC@H',
                                                      ],
                                         },
               'DENIS@Home'           => { 
                                           key     => 'denis', 
                                           matches => [ 
                                                        'DENIS@Home',
                                                        'DENIS',
                                                        '\bD@H',
                                                      ],
                                         },
               'Docking'              => { 
                                           key     => 'docking', 
                                           matches => [ 
                                                        'Docking',
                                                        'Dock',
                                                      ],
                                         },
               'DPAD'                 => { 
                                           key     => 'dpad',
                                           matches => [ 
                                                        'DPAD',
                                                      ],
                                         },
               'D2OL'                 => { 
                                           key     => 'd2ol',
                                           matches => [ 
                                                        'D2OL',
                                                      ],
                                         },
               'Einstein@Home'        => { 
                                           key     => 'einstein',
                                           matches => [ 
                                                        'Einstein@Home',
                                                        'Einstein',
                                                        '\bE@H',
                                                      ],
                                         },
               'Enigma@home'          => { 
                                           key     => 'enigma',
                                           matches => [ 
                                                        'Enigma@home',
                                                        'Enigma',
                                                      ],
                                         },
               'eOn 2'                => { 
                                           key     => 'eOn2',
                                           matches => [ 
                                                        'eOn 2',
                                                        'eOn',
                                                      ],
                                         },
               'Einstein@home'        => { 
                                           key     => 'einstein',
                                           matches => [ 
                                                        'Einstein@home',
                                                        'Einstein',
                                                        '\bE@H',
                                                      ],
                                         },
               'Folding@Home'         => { 
                                           key     => 'folding',
                                           matches => [ 
                                                        'Folding@Home',
                                                        'Folding',
                                                        '\bF@H',
                                                        'FAH',
                                                      ],
                                         },
               'GIMPS'                => { 
                                           key     => 'gimps', 
                                           matches => [ 
                                                        'GIMPS',
                                                        'GIMP',
                                                      ],
                                         },
               'GPUGRID'              => { 
                                           key     => 'gpugrid', 
                                           matches => [ 
                                                        'GPUGRID',
                                                      ],
                                         },
               'Leiden Classical'     => { 
                                           key     => 'leiden',
                                           matches => [ 
                                                        'Leiden\s*Classical',
                                                        'Leiden',
                                                      ],
                                         },
               'Malariacontrol.net'   => { 
                                           key     => 'malaria',
                                           matches => [ 
                                                        'Malariacontrol\.net',
                                                        'Malariacontrol',
                                                      ],
                                         },
               'MilkyWay@Home'        => { 
                                           key     => 'milkyway',
                                           matches => [ 
                                                        'MilkyWay@Home',
                                                        'MilkyWay',
                                                        '\bM@H',
                                                      ],
                                         },
               'NFS@home'             => { 
                                           key     => 'nfs', 
                                           matches => [ 
                                                        'NFS@home',
                                                        'NFS-home',
                                                        'NFS',
                                                        '\bN@H',
                                                      ],
                                         },
               'NumberFields@home'    => { 
                                           key     => 'numberfields', 
                                           matches => [ 
                                                        'NumberFields@home',
                                                        'NumberFields',
                                                        '\bNF@H',
                                                      ],
                                         },
               'OGR-28'               => { 
                                           key     => 'ogr',
                                           matches => [ 
                                                        'OGR-28',
                                                        'OGR',
                                                      ],
                                         },
               'Primaboinca'          => { 
                                           key     => 'prima',
                                           matches => [ 
                                                        'Primaboinca',
                                                        'Prima',
                                                      ],
                                         },
               'PrimeGrid'            => { 
                                           key     => 'prime',
                                           matches => [ 
                                                        'Prime-Grid',
                                                        'PrimeGrid',
                                                      ],
                                         },
               'POEM@Home'            => { 
                                           key     => 'poem', 
                                           matches => [ 
                                                        'POEM@Home',
                                                        'POEM',
                                                        '\bP@H',
                                                      ],
                                         },
               'PSP-PRP'              => { 
                                           key     => 'pspprp', 
                                           matches => [ 
                                                        'PSP-PRP',
                                                        'PSP PRP',
                                                        'PSP',
                                                        'PRP',
                                                      ],
                                         },
               'RakeSearch'           => { 
                                           key     => 'rakesearch',
                                           matches => [ 
                                                        'RakeSearch',
                                                        'Rake Search',
                                                      ],
                                         },
               'RC5-72'               => { 
                                           key     => 'rc5',
                                           matches => [ 
                                                        'RC5-72',
                                                        'RC5',
                                                      ],
                                         },
               'Rosetta@Home'         => { 
                                           key     => 'rosetta',
                                           matches => [ 
                                                        'Rosetta@Home',
                                                        'Rosetta',
                                                        '\bR@H',
                                                      ],
                                         },
               'SAT@home'             => { 
                                           key     => 'sat',
                                           matches => [ 
                                                        'SAT@home',
                                                        'SAT',
                                                      ],
                                         },
               'SiDock@home'          => { 
                                           key     => 'sidock',
                                           matches => [ 
                                                        'SiDock@home',
                                                        'SiDock',
                                                      ],
                                         },
               'SoB'                  => { 
                                           key     => 'sob',
                                           matches => [ 
                                                        'Seventeen Or Bust',
                                                        'SoB',
                                                      ],
                                         },
               'SZTAKI'               => { 
                                           key     => 'sztaki',
                                           matches => [ 
                                                        'SZTAKI',
                                                      ],
                                         },
               'theSkyNet'            => { 
                                           key     => 'skynet',
                                           matches => [ 
                                                        'the\s*Sky\s*Net\s*POGS',
                                                        'the\s*Sky\s*Net',
                                                        'Sky\s*Net',
                                                      ],
                                         },
               'TN-Grid'              => { 
                                           key     => 'tngrid',
                                           matches => [ 
                                                        'TN\S*Grid',
                                                        '\bTN',
                                                      ],
                                         },
               'UD-Grid'              => { 
                                           key     => 'ud',
                                           matches => [ 
                                                        'UD-Grid',
                                                        'UD\s*Grid',
                                                        'UD',
                                                      ],
                                         },
               'Universe@home'        => { 
                                           key     => 'universe',
                                           matches => [ 
                                                        'Universe@home',
                                                        'Universe-home',
                                                        'Universe',
                                                        '\bU@H',
                                                      ],
                                         },
               'VGTU@Home'            => { 
                                           key     => 'vgtu', 
                                           matches => [ 
                                                        'VGTU project@home',
                                                        'VGTU@home',
                                                        'VGTU-home',
                                                        'VGTY',
                                                        '\bV@H',
                                                      ],
                                         },
               'World Community Grid' => { 
                                           key     => 'wcg',
                                           matches => [ 
                                                        'World\s*Community\s*Grid',
                                                        'WCG',
                                                      ],
                                         },
               'YAFU'                 => { 
                                           key     => 'yafu',
                                           matches => [ 
                                                        'YAFU',
                                                      ],
                                         },
               'Yoyo@home'            => { 
                                           key     => 'yoyo', 
                                           matches => [ 
                                                        'Yoyo@home',
                                                        'Yoyo-home',
                                                        'Yoyo',
                                                        '\bY@H',
                                                      ],
                                         },
        );
        
%vote_to_key = ();
%key_to_vote = ();

foreach my $vote (keys %vote) {
        next if(!$option{$vote} || !$option{$vote}->{key});

        my $key = $option{$vote}->{key};

        $vote_to_key{$vote} = $key;

        $key_to_vote{$key} = $vote;
}

################################################################################
# Subroutines

sub time2gotdate {
        my $time = shift;

        # Convert epoch to GoT date
        #
        # Epoch   : 1235384580
        # GoT Date: maandag 23 februari 2009 11:23
        #
        # Epoch date is in UTC
        # GoT   date is in CET
        
        my ($year,$month,$day, $hour,$min,$sec) = Date::Calc::Time_to_Date($time);

        my ($dd_year,$dd_month,$dd_day, $dd_hour,$dd_min,$dd_sec) = Date::Calc::Timezone();

        ($year,$month,$day, $hour,$min,$sec) = Date::Calc::Add_Delta_YMDHMS(
                                                                             $year,$month,$day, $hour,$min,$sec,
                                                                             $dd_year,$dd_month,$dd_day, $dd_hour,$dd_min,$dd_sec,
                                                                           );

        my $dow = Date::Calc::Day_of_Week_to_Text(
                                                   Date::Calc::Day_of_Week($year, $month, $day),
                                                   Date::Calc::Decode_Language("Nederlands")
                                                 );
        $dow = lc($dow);

        $month = Date::Calc::Month_to_Text($month, Date::Calc::Decode_Language("Nederlands"));

        return sprintf("%s %02d %s %d %02d:%02d", $dow, $day, $month, $year, $hour, $min);
}

sub gotdate2time {
        my $date = shift;

        # Convert old GoT date to epoch
        # 
        # GoT Date: maandag 23 februari 2009 11:23
        # Epoch   : 1235384580
        #
        # GoT   date is in CET
        # Local date is in CET
        
        if($date =~ /^(\w+)\s+(\d+)\s+(\w+)\s+(\d+)\s+(\d+):(\d+)$/) {
                my $dow    = $1;
                my $day    = $2;
                my $month  = $3;
                my $year   = $4;
                my $hour   = $5;
                my $minute = $6;
                my $second = 0;

                $month = Date::Calc::Decode_Month($month, Date::Calc::Decode_Language("Nederlands"));

                return Date::Calc::Date_to_Time($year, $month, $day, $hour, $minute, $second);
        }

        return $date;
}

sub time2date {
        my $time = shift;

        # Convert epoch to date
        #
        # Epoch: 1235384580
        # Date : 2009-02-23 11:23:00
        
        my ($year,$month,$day, $hour,$min,$sec) = Date::Calc::Time_to_Date($time);
        
        return sprintf("%d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $hour, $min, $sec);
}

sub date2time {
        my $date = shift;

        # Convert Date to epoch
        #
        # Date : 2009-02-23 11:23:00
        # Epoch: 1235384580
        #
        # Local date is in CET
        
        if($date =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/) {
                my $year   = $1;
                my $month  = $2;
                my $day    = $3;
                my $hour   = $4;
                my $minute = $5;
                my $second = $6;

                return Date::Calc::Date_to_Time($year, $month, $day, $hour, $minute, $second);
        }

        return $date;
}

1;
