## #!/usr/bin/perl

my %namepoints = ();
my $input = "";
my $tempname = "";
my $temppoints = "";
my %currentnames = ();
my %activepoints = ();
my %finalpoints = ();

open(NAMES, "/home/estiann/names.txt");

$input = <NAMES>; 
	 until($input eq "") {
		if($input =~ /(\w+) (\d+)./) {
			$tempname = $1; $temppoints = $2; }
		 $namepoints{$tempname} = $temppoints;
		 $input = <NAMES>; }
close(NAMES);

print("CURRENT POINTS:",%namepoints,"\n");
sub chatter {
my $ladeeda = "";
 if(/chats to everybody[:,] 'chatmordpoints (\w+)'/) {
	$ladeeda = ucfirst(lc($1));
   if (!(exists $namepoints{$ladeeda})) { mcl_chatall("$col_C\[mordpoints\] ${col_W}Error! $col_B$ladeeda${col_W} was not on the list"); }
   else { mcl_chatall("$col_C\[mordpoints\] ${col_B}$ladeeda ${col_W}has ${col_G}$namepoints{$ladeeda}${col_W} points!"); }
}
}

#callout_add(\&updatehash, 60);

sub cmd_mordon {
  my $tempname = ucfirst(lc($_));;
#  my $currentsize = @currentnames;
  my $temptime = "";
  if(!(exists $namepoints{$tempname})) { $namepoints{$tempname}="0"; }
  $currentnames{$tempname}=$namepoints{$tempname};
  mcl_chatall("$col_C\[mordpoints\] ${col_B}$tempname${col_W} has been ${col_G}ACTIVATED. $col_C\[$namepoints{$tempname}\]");
  $temptime = time(); print("$temptime\n");
  $activepoints{$tempname}=$temptime;
}

sub cmd_mordoff {
  my $tempname = ucfirst(lc($_));
  my $temppoints = "";
  my $temptime = "";
  $temptime = time();
if($_ eq "all") {
 foreach $temp (sort keys %currentnames) {
  $temppoints = int(($temptime - $activepoints{$temp})/60);
  $namepoints{$temp} = $namepoints{$temp}+$temppoints;
  mcl_chatall("$col_C\[mordpoints\] ${col_B}$temp${col_W} has been ${col_G}DEACTIVATED. ${col_C}\[$temppoints/$namepoints{$temp}\]");
  delete $currentnames{$temp};	 
} }
else {	if(!(exists $currentnames{$tempname})) { print("****** Warning, that name is not on the list! ******\n"); } else {
#  $temptime = time();
  $temppoints = int(($temptime - $activepoints{$tempname})/60); 
  $namepoints{$tempname} = $namepoints{$tempname}+$temppoints;
  mcl_chatall("$col_C\[mordpoints\] ${col_B}$tempname${col_W} has been ${col_G}DEACTIVATED. ${col_C}\[$temppoints/$namepoints{$tempname}\]");  
  delete $currentnames{$tempname};
} } }

sub cmd_mordwho {
my @temparray = ();
my $tempsplit = ();
my $temparraysize = 0;
#  my $currentsize = @currentnames;
#  my $i = 0;
# foreach $temp (sort keys %currentnames)  { 
#    $currentnames{$temp} = $namepoints{$temp}; }
# foreach $temp2 (sort { $a <=> $b } values %currentnames) {
#   mcl_chatall("$col_C\[mordpoints\] ${col_B}$temp2${col_W} - ${col_G}$currentnames{$temp2} points");
if ($_ eq "chat") {  foreach $temp (sort keys %currentnames) {
     mcl_chatall("$col_C\[mordpoints\] ${col_B}$temp${col_W} - ${col_G}$currentnames{$temp} points");
 }   }
elsif ($_ eq "say") {
 foreach $temp (sort keys %currentnames) {
# mcl_send("ft [mordpoints] $temp - $currentnames{$temp}"); }
 $temparraysize = @temparray;
 $temparray[$temparraysize] = $temp; } $tempsplit = join(" ",@temparray); mcl_send("say [mordpoints] The following people are currently activated:",$tempsplit,".");
# mcl_send("say To find out what your points are, whisper Mordegast mordpoints yourname!"); 
}
#print(%currentnames,"\n");
#print(values(%currentnames),"\n");
#print(keys(%currentnames),"\n");
}

sub cmd_mordsavelist {
open(TEMPNAMES, ">/home/estiann/tempnames.txt");
  foreach $temp (sort keys %namepoints) {
   if ($namepoints{$temp}=="0") { } else {
    print TEMPNAMES "$temp $namepoints{$temp}.\n"; } }
print TEMPNAMES "\n";
close(TEMPNAMES);
system("cp /home/estiann/tempnames.txt /home/estiann/names.txt");
}

#sub updatehash {
#  foreach $temp (sort keys %currentnames) {
#    $currentnames{$temp} = $namepoints{$temp}; }
#}


output_add(\&chatter);
1;

sub cmd_chatpoints {
 mcl_chatall(%namepoints); }

#foreach $test (sort { $a <=> $b } keys %namepoints) {
# print("$test has $namepoints{$test} points\n");
#}
