license changed GPL2 -> AGPL3
[aymargeddon/current.git] / src / FROGS / Util.pm
1 ##########################################################################
2 #
3 #   Copyright (c) 2003-2012 Aymargeddon Development Team
4 #
5 #   This file is part of "Last days of Aymargeddon" - a massive multi player
6 #   onine game of strategy      
7 #   
8 #        This program is free software: you can redistribute it and/or modify
9 #        it under the terms of the GNU Affero General Public License as
10 #        published by the Free Software Foundation, either version 3 of the
11 #        License, or (at your option) any later version.
12 #    
13 #        This program is distributed in the hope that it will be useful,
14 #        but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 #
17 #    See the GNU Affero General Public License for more details.
18 #    
19 #    You should have received a copy of the GNU Affero General Public License
20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #    
22 ###########################################################################
23 #
24
25 #
26 #  Here we gather some utility-functions
27 #
28
29 use strict;
30 use FROGS::DataBase;
31 use FROGS::Config qw ($conf);
32
33 package Util;
34 use Data::Dumper;
35
36 # parse the command-line (or any other string-array) 
37 # and overwrites $::conf with the new values
38 # example: ./scheduler.pl -DURATION-PRAY=1000 -MANA-BLESS_PRIEST=10
39 sub overwrite_config{
40   my @arr = @_;
41   for my $arg (@arr){
42     my ($left,$right) = split /=/,$arg;
43     my @parts = split /-/, $left;
44
45     # TODO: generalization
46     if($parts[2]){
47       $::conf->{"-$parts[1]"}->{"-$parts[2]"} = $right;
48     }else{
49       $::conf->{"-$parts[1]"} = $right;
50     }
51   }
52 }
53
54 # returns 1 if $scalar is in @list
55 sub is_in{
56   my ($scalar, @list) = @_;
57
58   for my $le (@list) {
59     return 1 if $le eq $scalar;
60   }
61   return 0;
62 }
63
64 # returns all elements of @$A which are _not_ in @$B
65 # in an array reference
66 sub without{
67   my ($A, $B) = @_;
68
69   my %h = ();
70   for my $b (@$B){
71     $h{$b} = 1;
72   }
73
74   my @A_without_B = ();
75   for my $a (@$A){
76     push @A_without_B, $a unless exists $h{$a};
77   }
78   return \@A_without_B;
79 }
80
81 sub log{
82   my ($string,$level) = @_;
83
84   my $abslevel = $level;
85   my $do = 0;
86
87   # TODO: dirty hack, use caller() instead
88   if($0 eq '-e'){
89     $string .= "<p>";
90     $do = 1 if $::conf->{-EPL_DEBUG} >= $abslevel;
91   }else{
92     $do = 1 if $::conf->{-DEBUG} >= $abslevel;
93
94     if($::conf->{-FULL_DEBUG_FILE}){
95       # FULL_LOG in file
96       if($level >= 0){
97         print FULL_LOG "$string\n";
98       }else{
99         print FULL_LOG "$string";
100       }
101     }
102   }
103   # negativ values for level prints no newline
104   if($do){
105     if($level >= 0){
106       print "$string\n";
107     }else{
108       print "$string";
109     }
110   }
111 }
112
113 sub open_log{
114   my $file = $::conf->{-FULL_DEBUG_FILE};
115   open(FULL_LOG,">$file") or die "can't open $file: $!";
116 }
117
118 sub close_log{
119   close FULL_LOG;
120 }
121
122 sub min{
123   my @list = @_;
124
125   my $min = 99999999;
126   for my $elem (@list){
127     $min = $elem if $elem < $min;
128   }
129   return $min;
130 }
131
132 sub max{
133   my @list = @_;
134
135   my $max = -99999999;
136   for my $elem (@list){
137     $max = $elem if $elem > $max;
138   }
139   return $max;
140 }
141
142 # returns a shuffled list
143 sub shuffle {
144   my $array_ref = shift;
145
146   my @shuffled = sort { int(rand(3)) - 1 } @$array_ref;
147
148   return \@shuffled;
149 }
150
151 1;
152