package DADA::App::BounceScoreKeeper; 
use strict; 


use lib qw(../ ../DADA ../perllib); 

use AnyDBM_File; 
use Fcntl qw(
O_WRONLY 
O_TRUNC 
O_CREAT 
O_CREAT 
O_RDWR
O_RDONLY
LOCK_EX
LOCK_SH 
LOCK_NB); 


use base qw(DADA::App::GenericDBFile);

sub new {
	my $class = shift;
	
	my %args = (-List => undef,
				-new_list => 0,  
					@_); 
					     
    my $self = SUPER::new $class (
    							  function => 'bounces',
    							 );  
	   
	   return $self;
}



sub tally_up_scores { 

    my $self   = shift; 
    my $scores = shift; 
    my $give_back_scores = {}; 
    
    $self->_open_db; 
		    
    foreach(keys %$scores){ 
       $self->{DB_HASH}->{$_} += $scores->{$_};
       #warn "$_ has a score of " . $self->{DB_HASH}->{$_}; 
       $give_back_scores->{$_} = $self->{DB_HASH}->{$_};
    }
    $self->_close_db; 
    
    return $give_back_scores; 
}




sub removal_list { 

    my $self         = shift; 
    my $threshold    = shift || 0; 
    my $removal_list = [];

    $self->_open_db; 
    
    foreach(keys %{$self->{DB_HASH}}){
    
          if($self->{DB_HASH}->{$_} >= $threshold){
            #warn "Adding $_ to removal list.";
            push(@$removal_list, $_);
        }

    }
    
    $self->_close_db; 
    
    return $removal_list

}




sub flush_old_scores { 

    my $self = shift; 
    my $threshold = shift || 0;

    $self->_open_db; 
    
    foreach(keys %{$self->{DB_HASH}}){
    
        if($self->{DB_HASH}->{$_} >= $threshold){
            #warn "Removing $_ from score card.";
            $self->{DB_HASH}->{$_} = 0; # for whatever reason that it doesn't get removed...
            delete($self->{DB_HASH}->{$_});
        }

    }
    
    $self->_close_db; 

    # Flushing - shouldn't be needed? 
	$self->_open_db; 
	$self->_close_db;

}




sub erase { 

    my $self = shift; 
    
    $self->_open_db; 
    $self->{DB_HASH} = {}; 
    $self->_close_db; 

    # Flushing - shouldn't be needed? 
	$self->_open_db; 
	$self->_close_db;
	
	return 1; 

}

=pod

=head1 COPYRIGHT 

Copyright (c) 1999-2007 Justin Simoni All rights reserved. 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, 
Boston, MA  02111-1307, USA.

=cut 



1;
