Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Bot::BasicBot::Pluggable::Module::FloorTracker;
- {
- $Bot::BasicBot::Pluggable::Module::FloorTracker::VERSION = '0.01';
- }
- use base qw(Bot::BasicBot::Pluggable::Module);
- use warnings;
- use strict;
- use POSIX;
- use DBI;
- #
- # Connect to the MySQL Database
- #
- my $dbh = DBI->connect ( "DBI:SQLite:floorbot.db",
- "", "",
- { RaiseError => 1, AutoCommit => 1} );
- #
- # The main sub-routine which listens to all
- # channel traffic and interprets commands.
- #
- sub told {
- my ( $self, $mess ) = @_;
- my $body = $mess->{body};
- #
- # Makes sure we only care about messages
- # that start with !
- #
- return 0 unless defined $body;
- return 0 unless $body =~ /^!/;
- my ( $command, $param ) = split ( /\s+/, $body, 2 );
- $command = lc ( $command );
- #
- # A Test command, mainly to see if the module
- # is currently working of if I've screwed something
- # up. Will return if the user is authorized or not.
- #
- if ( $command eq "!isauth" ) {
- if ( !$self->authed ( $mess->{who} ) ) {
- return "Not Authorized.";
- }
- else {
- return "Authed ".$mess->{who};
- }
- }
- #
- # This will turn into the command which retrieves
- # info about a particular keyer, including average
- # time, floors done, etc.
- #
- if ( $command eq "!keyerinfo" ) {
- my ( $getuser, $fetchInfo ) = 0;
- if ( defined ( $param ) ) {
- $getuser = $param;
- }
- else
- {
- $getuser = $mess->{who};
- }
- $fetchInfo = $dbh->prepare("SELECT * FROM keyers WHERE irc_name = ?");
- $fetchInfo->execute( $getuser );
- while ( my $ref = $fetchInfo->fetchrow_hashref() ) {
- if ( $ref->{'floor_count'} > '0' && $ref->{'floor_count'} != '1' ) {
- return "$ref->{'irc_name'} has done $ref->{'floor_count'} floors.";
- }
- elsif ( $ref->{'floor_count'} == '1' )
- {
- return "$ref->{'irc_name'} has done one floor so far.";
- }
- else
- {
- return "$ref->{'irc_name'} has done no floors so far.";
- }
- }
- }
- #
- # The command that is used to start
- # a floor with the leeches as param-
- # eters.
- #
- if ( $command eq "!start" ) {
- return 0 if $param eq "";
- my ($ref, $fetchInfo, $sql ) = "";
- $sql = "SELECT count(*) FROM floors WHERE keyer = ? AND in_progress = '1'";
- $fetchInfo = $dbh->prepare ( $sql );
- $fetchInfo->execute ( $mess->{'who'} );
- $ref = $fetchInfo->fetchrow_array();
- if ( $ref != '0' ) {
- return "Please end the floor you are currently hosting.";
- }
- my $keyer = $mess->{who};
- my $start_time = time();
- $sql = "INSERT INTO floors (id, keyer, leeches, start_time, end_time, floor_time, in_progress) VALUES (NULL, ?, ?, ?, '0', '0', '1')";
- $dbh->do( $sql, undef, $keyer, $param, $start_time );
- return "Floor started. Keyer: $keyer Leeches: $param";
- }
- #
- # The command used to check the
- # status of a currently running floor,
- # or return all currently running
- # floors if no keyer specified.
- #
- if ( $command eq "!fl" ) {
- my ( $ref, $fetchInfo, $sql, $return_string, $floor_time, $floor_time_final, $hours, $minutes, $seconds) = "";
- my $current_time = time();
- if ( $param ne "" ) {
- $sql = "SELECT count(*) FROM floors WHERE keyer = ? AND in_progress = '1'";
- $fetchInfo = $dbh->prepare($sql);
- $fetchInfo->execute ( $param );
- $ref = $fetchInfo->fetchrow_array();
- if ( $ref == '0' ) {
- return "$param is not currently in a floor.";
- }
- $sql = "SELECT * FROM floors WHERE keyer = ? AND in_progress = '1'";
- $fetchInfo = $dbh->prepare($sql);
- $fetchInfo->execute ( $param );
- $ref = $fetchInfo->fetchrow_hashref();
- $floor_time = $current_time - $ref->{'start_time'};
- $hours = floor ( $floor_time / 3600 );
- $minutes = floor ( ( $floor_time - $hours * 3600 ) / 60 );
- $seconds = $floor_time % 60;
- $floor_time_final = join ( ':', $hours, $minutes, $seconds );
- $return_string = "$ref->{'keyer'} is keying for leeches: $ref->{'leeches'}. Floor time: $floor_time_final";
- }
- else
- {
- $sql = "SELECT count(*) FROM floors WHERE in_progress = '1'";
- $fetchInfo = $dbh->prepare($sql);
- $fetchInfo->execute ();
- $ref = $fetchInfo->fetchrow_array();
- if ( $ref == '0' ) {
- return "No active floors.";
- }
- else
- {
- $sql = "SELECT * FROM floors WHERE in_progress = '1'";
- $fetchInfo = $dbh->prepare($sql);
- $fetchInfo->execute ();
- $ref = $fetchInfo->fetchrow_hashref();
- while ( $ref = $fetchInfo->fetchrow_hashref() ) {
- $floor_time = $current_time - $ref->{'start_time'};
- $hours = floor ( $floor_time / 3600 );
- $minutes = floor ( ( $floor_time - $hours * 3600 ) / 60 );
- $seconds = $floor_time % 60;
- $floor_time_final = join ( ':', $hours, $minutes, $seconds );
- $return_string = join ( "\n", "$ref->{'keyer'} is keying for leeches: $ref->{'leeches'}. Floor time: $floor_time_final", $return_string);
- }
- }
- }
- return $return_string;
- }
- if ( $command eq "!stop" ) {
- my ( $fetchInfo, $sql, $ref ) = "";
- $sql = "SELECT * FROM floors WHERE keyer = ? AND in_progress = '1'";
- $fetchInfo = $dbh->prepare ( $sql );
- $fetchInfo->execute ( $mess->{who} );
- if ( !$fetchInfo ) {
- return "You are not currently hosting a floor.";
- }
- else
- {
- $sql = "UPDATE floors SET in_progress = '0' WHERE keyer = ?";
- $dbh->do ( $sql, undef, $mess->{who} );
- return "Floor ended.";
- }
- }
- #
- # This command will be used to add
- # users to the database. Must be
- # authorized.
- #
- if ( $command eq "!addkeyer" ) {
- addKeyer ( $self, $mess, $param );
- }
- }
- sub addKeyer {
- my ( $self, $mess, $param ) = @_;
- my ($sql, $sth ) = 0;
- return 0 if !$self->authed ( $mess->{who} );
- return 0 if $param eq "";
- $sql = "INSERT INTO keyers (id, irc_name, floor_count, avg_time, in_floor, available) VALUES (NULL, ?, '0', '0', '0', '0')";
- $dbh->do( $sql, undef, $param );
- return "Added $param to the keyer database.";
- }
Add Comment
Please, Sign In to add comment