Guest User

Untitled

a guest
Jul 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.89 KB | None | 0 0
  1. package Bot::BasicBot::Pluggable::Module::FloorTracker;
  2. {
  3.   $Bot::BasicBot::Pluggable::Module::FloorTracker::VERSION = '0.01';
  4. }
  5. use base qw(Bot::BasicBot::Pluggable::Module);
  6. use warnings;
  7. use strict;
  8.  
  9. use POSIX;
  10. use DBI;
  11.  
  12. #
  13. # Connect to the MySQL Database
  14. #
  15. my $dbh = DBI->connect ( "DBI:SQLite:floorbot.db",
  16.                             "", "",
  17.                             { RaiseError => 1, AutoCommit => 1} );
  18.  
  19.  
  20. #
  21. # The main sub-routine which listens to all
  22. # channel traffic and interprets commands.
  23. #
  24. sub told {
  25.     my ( $self, $mess ) = @_;
  26.     my $body = $mess->{body};
  27.    
  28.     #
  29.     # Makes sure we only care about messages
  30.     # that start with !
  31.     #
  32.     return 0 unless defined $body;
  33.     return 0 unless $body =~ /^!/;
  34.    
  35.     my ( $command, $param ) = split ( /\s+/, $body, 2 );
  36.     $command = lc ( $command );
  37.    
  38.     #
  39.     # A Test command, mainly to see if the module
  40.     # is currently working of if I've screwed something
  41.     # up. Will return if the user is authorized or not.
  42.     #
  43.     if ( $command eq "!isauth" ) {
  44.         if ( !$self->authed ( $mess->{who} ) ) {
  45.             return "Not Authorized.";
  46.         }
  47.         else {
  48.             return "Authed ".$mess->{who};
  49.         }
  50.     }
  51.    
  52.     #
  53.     # This will turn into the command which retrieves
  54.     # info about a particular keyer, including average
  55.     # time, floors done, etc.
  56.     #
  57.     if ( $command eq "!keyerinfo" ) {
  58.         my ( $getuser, $fetchInfo ) = 0;
  59.        
  60.         if ( defined ( $param ) ) {
  61.             $getuser = $param;
  62.         }
  63.         else
  64.         {
  65.             $getuser = $mess->{who};
  66.         }
  67.        
  68.         $fetchInfo = $dbh->prepare("SELECT * FROM keyers WHERE irc_name = ?");
  69.         $fetchInfo->execute( $getuser );
  70.        
  71.         while ( my $ref = $fetchInfo->fetchrow_hashref() ) {
  72.             if ( $ref->{'floor_count'} > '0' && $ref->{'floor_count'} != '1' ) {
  73.                 return "$ref->{'irc_name'} has done $ref->{'floor_count'} floors.";
  74.             }
  75.             elsif ( $ref->{'floor_count'} == '1' )
  76.             {
  77.                 return "$ref->{'irc_name'} has done one floor so far.";
  78.             }
  79.             else
  80.             {
  81.                 return "$ref->{'irc_name'} has done no floors so far.";
  82.             }
  83.         }
  84.     }
  85.    
  86.     #
  87.     # The command that is used to start
  88.     # a floor with the leeches as param-
  89.     # eters.
  90.     #
  91.     if ( $command eq "!start" ) {
  92.         return 0 if $param eq "";
  93.         my ($ref, $fetchInfo, $sql ) = "";
  94.        
  95.         $sql = "SELECT count(*) FROM floors WHERE keyer = ? AND in_progress = '1'";
  96.         $fetchInfo = $dbh->prepare ( $sql );
  97.         $fetchInfo->execute ( $mess->{'who'} );
  98.         $ref = $fetchInfo->fetchrow_array();
  99.         if ( $ref != '0' ) {
  100.             return "Please end the floor you are currently hosting.";
  101.         }
  102.        
  103.         my $keyer = $mess->{who};
  104.         my $start_time = time();
  105.        
  106.         $sql = "INSERT INTO floors (id, keyer, leeches, start_time, end_time, floor_time, in_progress) VALUES (NULL, ?, ?, ?, '0', '0', '1')";
  107.         $dbh->do( $sql, undef, $keyer, $param, $start_time );
  108.        
  109.         return "Floor started. Keyer: $keyer Leeches: $param";
  110.     }
  111.    
  112.     #
  113.     # The command used to check the
  114.     # status of a currently running floor,
  115.     # or return all currently running
  116.     # floors if no keyer specified.
  117.     #
  118.     if ( $command eq "!fl" ) {
  119.         my ( $ref, $fetchInfo, $sql, $return_string, $floor_time, $floor_time_final, $hours, $minutes, $seconds) = "";
  120.         my $current_time = time();
  121.        
  122.         if ( $param ne "" ) {
  123.             $sql = "SELECT count(*) FROM floors WHERE keyer = ? AND in_progress = '1'";
  124.             $fetchInfo = $dbh->prepare($sql);
  125.             $fetchInfo->execute ( $param );
  126.             $ref = $fetchInfo->fetchrow_array();
  127.            
  128.             if ( $ref == '0' ) {
  129.                 return "$param is not currently in a floor.";
  130.             }
  131.            
  132.             $sql = "SELECT * FROM floors WHERE keyer = ? AND in_progress = '1'";
  133.             $fetchInfo = $dbh->prepare($sql);
  134.             $fetchInfo->execute ( $param );
  135.             $ref = $fetchInfo->fetchrow_hashref();
  136.            
  137.             $floor_time = $current_time - $ref->{'start_time'};
  138.             $hours = floor ( $floor_time / 3600 );
  139.             $minutes = floor ( ( $floor_time - $hours * 3600 ) / 60 );
  140.             $seconds = $floor_time % 60;
  141.             $floor_time_final = join ( ':', $hours, $minutes, $seconds );
  142.            
  143.             $return_string = "$ref->{'keyer'} is keying for leeches: $ref->{'leeches'}. Floor time: $floor_time_final";
  144.         }
  145.         else
  146.         {
  147.             $sql = "SELECT count(*) FROM floors WHERE in_progress = '1'";
  148.             $fetchInfo = $dbh->prepare($sql);
  149.             $fetchInfo->execute ();
  150.             $ref = $fetchInfo->fetchrow_array();
  151.             if ( $ref == '0' ) {
  152.                 return "No active floors.";
  153.             }
  154.             else
  155.             {
  156.                 $sql = "SELECT * FROM floors WHERE in_progress = '1'";
  157.                 $fetchInfo = $dbh->prepare($sql);
  158.                 $fetchInfo->execute ();
  159.                 $ref = $fetchInfo->fetchrow_hashref();
  160.                
  161.                 while ( $ref = $fetchInfo->fetchrow_hashref() ) {
  162.            
  163.                     $floor_time = $current_time - $ref->{'start_time'};
  164.                     $hours = floor ( $floor_time / 3600 );
  165.                     $minutes = floor ( ( $floor_time - $hours * 3600 ) / 60 );
  166.                     $seconds = $floor_time % 60;
  167.                     $floor_time_final = join ( ':', $hours, $minutes, $seconds );
  168.                
  169.                     $return_string = join ( "\n", "$ref->{'keyer'} is keying for leeches: $ref->{'leeches'}. Floor time: $floor_time_final", $return_string);
  170.                 }
  171.             }
  172.         }
  173.        
  174.         return $return_string;
  175.     }
  176.        
  177.        
  178.     if ( $command eq "!stop" ) {
  179.         my ( $fetchInfo, $sql, $ref ) = "";
  180.        
  181.         $sql = "SELECT * FROM floors WHERE keyer = ? AND in_progress = '1'";
  182.         $fetchInfo = $dbh->prepare ( $sql );
  183.         $fetchInfo->execute ( $mess->{who} );
  184.         if ( !$fetchInfo ) {
  185.             return "You are not currently hosting a floor.";
  186.         }
  187.         else
  188.         {
  189.             $sql = "UPDATE floors SET in_progress = '0' WHERE keyer = ?";
  190.             $dbh->do ( $sql, undef, $mess->{who} );
  191.             return "Floor ended.";
  192.         }
  193.     }
  194.        
  195.     #
  196.     # This command will be used to add
  197.     # users to the database. Must be
  198.     # authorized.
  199.     #
  200.     if ( $command eq "!addkeyer" ) {
  201.         addKeyer ( $self, $mess, $param );     
  202.     }
  203.            
  204. }
  205.  
  206. sub addKeyer {
  207.     my ( $self, $mess, $param ) = @_;
  208.     my ($sql, $sth ) = 0;
  209.     return 0 if !$self->authed ( $mess->{who} );
  210.     return 0 if $param eq "";
  211.     $sql = "INSERT INTO keyers (id, irc_name, floor_count, avg_time, in_floor, available) VALUES (NULL, ?, '0', '0', '0', '0')";
  212.     $dbh->do( $sql, undef, $param );
  213.        
  214.     return "Added $param to the keyer database.";
  215. }
Add Comment
Please, Sign In to add comment