]> ruin.nu Git - NDIRC.git/blob - PA.pm
modules
[NDIRC.git] / PA.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19 package ND::IRC::PA;
20 use strict;
21 use warnings;
22 use ND::DB;
23 use ND::IRC::Access;
24 use ND::IRC::Misc;
25 use POSIX;
26 require Exporter;
27
28 our @ISA = qw/Exporter/;
29
30 our @EXPORT = qw/checkPlanet checkGal shipEff shipStop parseValue prettyValue/;
31
32 sub checkPlanet {
33         my ($x,$y,$z,$intel) = @_;
34         DB();
35         my $f = $ND::DBH->prepare("SELECT ruler,planet,race,score,size,value,scorerank,sizerank,valuerank, xp, xprank, alliance FROM current_planet_stats WHERE x = ? AND y = ? and z = ?");
36         $f->execute($x,$y,$z);
37         while (my @row = $f->fetchrow()){
38                 @row = map (valuecolor(1),@row);
39                 my $ally = "";
40                 $ally = " Alliance=$row[11]," if $intel;
41                 $ND::server->command("notice $ND::target $x:$y:$z $row[0] OF $row[1],$ally Race=$row[2], Score=$row[3] ($row[6]), Size=$row[4] ($row[7]), Value=$row[5] ($row[8]), XP=$row[9] ($row[10])");
42         }
43 }
44 sub checkGal {
45         my ($x,$y) = @_;
46         DB();
47         my $f = $ND::DBH->prepare("SELECT name,score,size,value FROM galaxies WHERE x = ? AND y = ? and tick = (SELECT max(tick) from galaxies)");
48         $f->execute($x,$y);
49         while (my @row = $f->fetchrow()){
50                 @row = map (valuecolor(1),@row);
51                 $ND::server->command("notice $ND::target $x:$y $row[0], Score=$row[1], Size=$row[2], Value=$row[3]");
52         }
53 }
54
55 sub shipEff {
56         my ($amount,$ship,$value) = @_;
57         $ship = "\%$ship\%";
58         $amount = parseValue($amount);
59         $value = parseValue($value);
60         $value *= -1.5 if defined $value and $value < 0;
61
62         my @ship = $ND::DBH->selectrow_array(q{
63 SELECT name,target,"type",damage,metal+crystal+eonium,init,"class",guns,race
64 FROM ship_stats WHERE name ILIKE ?
65                 }, undef, $ship);
66         if (@ship){
67                 my $type = "kill";
68                 $type = "stun" if $ship[2] eq 'Emp';
69                 $type = "steal" if ($ship[2] eq 'Steal') or ($ship[2] eq 'Pod');
70
71                 $amount = int(($value*100/$ship[4])) if $amount eq 'value';
72                 $value = prettyValue(($amount*$ship[4]/100));
73                 my $text = "$amount $ship[0] ($ship[5]:$value) will $type:";
74                 my $st = $ND::DBH->prepare(q{
75                         SELECT name,"class","type",armor,metal+crystal+eonium,init,target,eres,race
76                         FROM ship_stats WHERE "class" = ?
77                         });
78                 $st->execute($ship[1]);
79                 while (my @target = $st->fetchrow()){
80                         my $dead = $ship[2] eq 'Emp' ? int($amount*$ship[7]*(100-$target[7])/100) : int($amount*$ship[3]/$target[3]);
81                         $value = prettyValue($dead*$target[4]/100);
82                         if (($target[6] eq $ship[6]) and ($target[5] <= $ship[5])){
83                                 $target[5] = "${ND::C}04$target[5]$ND::C";
84                         }elsif(($target[6] eq $ship[6]) and ($target[5] > $ship[5])){
85                                 $target[5] = "${ND::C}12$target[5]$ND::C";
86                         }
87                         $target[0] = "${ND::C}04$target[0]$ND::C" if $target[2] eq 'Norm' || $target[2] eq 'Cloak';
88                         $target[0] = "${ND::C}12$target[0]$ND::C" if $target[2] eq 'Emp';
89                         $target[0] = "${ND::C}13$target[0]$ND::C" if $target[2] eq 'Steal';
90                         $text .= " $ND::B$dead$ND::B $target[0] ($target[5]:$value),";
91                 }
92                 chop $text;
93                 $ND::server->command("notice $ND::target $text");
94         }
95         #print $text;
96 }
97
98 sub shipStop {
99         my ($amount,$ship,$value) = @_;
100         $ship = "\%$ship\%";
101         $amount = parseValue($amount);
102         $value = parseValue($value);
103         $value *= -1.5 if defined $value and $value < 0;
104
105         my @ship = $ND::DBH->selectrow_array(q{
106 SELECT name,target,"type",armor,metal+crystal+eonium,init,"class",eres,race
107 FROM ship_stats WHERE name ILIKE ?
108                 }, undef, $ship);
109         if (@ship){
110                 $ship[0] = "${ND::C}04$ship[0]$ND::C" if $ship[2] eq 'Norm';
111                 $ship[0] = "${ND::C}12$ship[0]$ND::C" if $ship[2] eq 'Emp';
112                 $ship[0] = "${ND::C}13$ship[0]$ND::C" if $ship[2] eq 'Steal';
113
114                 $amount = int(($value*100/$ship[4])) if $amount eq 'value';
115                 $value = prettyValue(($amount*$ship[4]/100));
116                 my $text = "To stop $amount $ship[0] ($ship[5]:$value) you need:";
117                 my $st = $ND::DBH->prepare(q{
118                         SELECT name,"class","type",damage,metal+crystal+eonium,init,target,guns,race
119                         FROM ship_stats WHERE "target" = ?
120                         });
121                 $st->execute($ship[6]);
122                 while (my @stopper = $st->fetchrow()){
123                         my $needed = $stopper[2] eq 'Emp' ? ceil($amount*100/(100-$ship[7])/$stopper[7]) : ceil($amount*$ship[3]/$stopper[3]);
124                         $value = prettyValue($needed*$stopper[4]/100);
125                         if (($stopper[1] eq $ship[1]) and ($ship[5] <= $stopper[5])){
126                                 $stopper[5] = "${ND::C}04$stopper[5]$ND::C";
127                         }elsif(($stopper[1] eq $ship[1]) and ($ship[5] > $stopper[5])){
128                                 $stopper[5] = "${ND::C}12$stopper[5]$ND::C";
129                         }
130                         $stopper[0] = "${ND::C}04$stopper[0]$ND::C" if $stopper[2] eq 'Norm' || $stopper[2] eq 'Cloak';
131                         $stopper[0] = "${ND::C}12$stopper[0]$ND::C" if $stopper[2] eq 'Emp';
132                         $stopper[0] = "${ND::C}13$stopper[0]$ND::C" if $stopper[2] eq 'Steal';
133                         $text .= " $ND::B$needed$ND::B $stopper[0] ($stopper[5]:$value),";
134                 }
135                 chop $text;
136                 $ND::server->command("notice $ND::target $text");
137         }
138         #print $text;
139 }
140
141 sub parseValue {
142         if (defined $_[0] && $_[0] =~ /^(-?\d+(?:\.\d+)?)([khMG])?$/){
143                 return $1 unless defined $2;
144                 return $1*100 if $2 eq 'h';
145                 return $1*1000 if $2 eq 'k';
146                 return $1*1000000 if $2 eq 'M';
147                 return $1*1000000000 if $2 eq 'G';
148         }
149         return $_[0];
150 }
151
152 sub prettyValue {
153         my ($value) = @_;
154         my $unit = '';
155         my @units = ('k','M','G','T');
156         for (my $i = 0; $value >= 1000;$i++){
157                 $value /= 1000;
158                 $unit = $units[$i];
159         }
160         return sprintf('%.2f%s', $value,$unit);
161 }
162
163
164 sub min {
165         my ($x,$y) = @_;
166         return ($x > $y ? $y : $x);
167 }
168
169 sub max {
170         my ($x,$y) = @_;
171         return ($x < $y ? $y : $x);
172 }
173
174 sub calcXp {
175         my ($x,$y,$z,$roids) = @_;
176
177         my ($avalue,$ascore) = $ND::DBH->selectrow_array(q{
178                 SELECT value,score FROM current_planet_stats WHERE 
179                         id = (SELECT planet FROM users WHERE hostmask ILIKE ?);
180                 }, undef, $ND::address);
181         my ($tvalue,$tscore) = $ND::DBH->selectrow_array(q{
182                 SELECT value,score FROM current_planet_stats WHERE 
183                         x = ? AND y = ? and z = ?;
184                 }, undef, $x,$y,$z);
185         unless (defined $avalue && defined $ascore){
186                 $ND::server->command("notice $ND::target You don't have a planet specified");
187                 return;
188         }
189         unless (defined $tvalue && defined $tscore){
190                 $ND::server->command("notice $ND::target Doesn't seem to be a planet at $x:$y:$z");
191                 return;
192         }
193         my $xp = int(max($roids * 10 * (min(2,$tscore/$ascore) + min(2,$tvalue/$avalue) - 1),0));
194         my $score = 60 * $xp;
195         $ND::server->command("notice $ND::target You will gain $ND::B$xp$ND::B XP, $ND::B$score$ND::B score, if you steal $roids roids from $x:$y:$z");
196 }
197
198 1;