Thread Galaxywars-Script: Galaxywars ist ein browsergame (9 answers)
Opened by frozen at 2006-09-24 14:00

renee
 2006-09-24 14:29
#8724 #8724
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Du solltest auch bei auftretenden Fehlern die Fehlermeldung ausgeben. Wenn Du also eine Datei öffnest, dann solltest Du im else-Zweig ein die $! machen. Dann bemerkst Du gleich, wenn da etwas schief gelaufen ist.

Wo kommt in plan_jobs das $rohstoffe her? (Du solltest das Array am Anfang des Skripts deklarieren).

Das hier kompiliert ohne Fehlermeldungen: Für mehr Hilfe musst Du mehr Informationen bieten und mal selbst ein wenig ausprobieren!
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/perl
# vim: ts=4 sw=4

use strict;
use Time::Local;
use LWP::UserAgent;

my $base = "http://80.237.205.38/gw03/";
my $login_page = "login.php";
my $uebersicht_page = "uebersicht.php";
my $navbar_page = "n/navbar.php";
my $produktion_page = "produktion.php";

my $gap_time = 30;
my $normal_wait_time = 60*60*2;
my $job_file = "./jobs";
my $log_file = "./log";


# Create a user agent object
my $ua = LWP::UserAgent->new;
$ua->agent("galaxywars/0.1 ");

my $id = $ARGV[0];

my @attacks = ();
my @rohstoffe = (0, 0, 0, 0);

my %ships = (
'p1' => { 'name' => 'schakal', 'kosten' => [250, 0, 0, 75]},
'p2' => { 'name' => 'recycler', 'kosten' => [850, 850, 100, 0]},
'p3' => { 'name' => 'spionagesonde', 'kosten' => [0, 150, 0, 0]},
'p4' => { 'name' => 'renegade', 'kosten' => [1000, 0, 0, 0]},
'p12' => { 'name' => 'kleines handelsschiff', 'kosten' => [2000, 2000, 0, 0] }
);

my @production = (258, 165, 10, 14);

sub get_best_build {
my @res = @_;
my @build = ();

my $count = &can_build(\@res, $ships{'p1'}->{'kosten'}, 3);
if ($count) { $build[++$#build] = ['p1',$count]; $count = 0; }

$count = &can_build(\@res, $ships{'p2'}->{'kosten'}, 2);
if ($count) { $build[++$#build] = ['p2',$count]; $count = 0; }

$count = &can_build(\@res, $ships{'p12'}->{'kosten'}, 4);
if ($count) { $build[++$#build] = ['p12',$count]; $count = 0; }

$count = &can_build(\@res, $ships{'p4'}->{'kosten'}, 0);
if ($count) { $build[++$#build] = ['p4',$count]; $count = 0; }

$count = &can_build(\@res, $ships{'p3'}->{'kosten'}, 1);
if ($count) { $build[++$#build] = ['p3',$count]; $count = 0; }

return \@build;
}

sub can_build {
my ($res, $cost, $which) = @_;
$res->[4] = 3000;
my @others = (0 .. 3); delete $others[$which];
my $count = 0;

while ($res->[$which] > 2000 and not grep { $res->[$_] < $cost->[$_]; } @others) {
for ((0 .. 3)) { $res->[$_] -= $cost->[$_]; }
$count ++;
}
delete $res->[4];

return $count;
}

sub insert {
my ($arr, $i, $elem) = @_;

for ((@$arr .. $i+1)) {
$arr->[$_] = $arr->[$_-1];
}

$arr->[$i] = $elem;
};

sub read_jobs {
my @ret = ();
my $item;
if (open (IN, '<', $job_file)) {
while(<IN>) {
chomp;
if (m/\[(\d+)\]/) {
$item = ($ret[++$#ret] = {'time'=>$1, 'build'=>[]});
}
elsif (m/(.+?) ?= ?(\d+)/) {
$item->{'build'}->[@{$item->{'build'}}] = [$1,$2];
}
}

close (IN);
}
print "Read Jobs: ".scalar @ret."\n";

return \@ret;
}

sub write_jobs {
my $jobs = shift;

print "Jobs: ".scalar @$jobs."\n";

if (open (OUT, '>', $job_file)) {
foreach my $job (@$jobs) {
print OUT '['.$job->{'time'}.'] '.scalar localtime ($job->{'time'})."\n";
for (@{$job->{'build'}}) {
print OUT $_->[0].'='.$_->[1].' '.$ships{$_->[0]}->{'name'}."\n";
}

print OUT "\n";
}

close (OUT);
}
}

sub plan_jobs {
my ($jobs, $ts, $tstr) = @_;
my ($min_ts, $max_ts) = ($ts - 5, $ts + 5);
my ($item, @res);

if (@$jobs) {
for ((0 .. $#$jobs)) {
if ($jobs->[$_]->{'time'} > $max_ts) {
&insert ($jobs, $_, {'time'=>$ts,'build'=>[]});
$item = $jobs->[$_];
last;
}
if ($jobs->[$_]->{'time'} > $min_ts and $jobs->[$_]->{'time'} < $max_ts) {
$item = $jobs->[$_];
last;
}
}
}

unless ($item) {
$jobs->[@$jobs] = {'time'=>$ts, 'build'=>[]};
$item = $jobs->[$#$jobs];
}


for ((0 .. 3)) {
$res[$_] = $rohstoffe[$_]+($production[$_]*(($ts-time())/3600));
}

$item->{'build'} = &get_best_build (@res);
}

sub get_next_job {
my ($jobs) = @_;
return shift (@$jobs);
}

sub do_job {
my ($job) = @_;

print "Starting to build\n";
&get_http ("POST", $base.$produktion_page, "u=$id&a=y&".join('&', map { $_->[0].'='.$_->[1]; } @{$job->{'build'}}));
sleep 2*$gap_time;

print "Removing ships\n";
for ((1 .. @{$job->{'build'}})) {
&get_http ("GET", $base.$produktion_page, "u=$id&auftr=1&dm=delete");
}
}

sub get_http {
my ($method, $url, $content, $referer) = @_;

if ($method == "GET") {
$url .= '?'.$content;
$content = "";
#print $url."\n";
}

# Create a request
my $req = HTTP::Request->new($method => $url);
if ($referer) { $req->header("Referer: $referer"); }
$req->content_type('application/x-www-form-urlencoded');
$req->content($content);

# Pass request to the user agent and get a response back
my $res = $ua->request($req);

# Check the outcome of the response
if ($res->is_success) {
if ($res->content =~ m/<font color="#FF0000" size="\+1">Fehler<\/font>/i) {
print "Fetching new id\n";
$id = &get_id ();
$_[2] =~ s/((^|&)u=).*?(&|$)/$1$id$3/i;
print "Trying with ".join(':',@_)."\n";
return &get_http (@_);
}
return $res->content;
}
else {
print $res->status_line, "\n";
return "";
}
}

sub get_id {
my $html = &get_http("POST",$base.$login_page,'i=y&name=<Benutzername>&pw=<Passwort>');

unless ($html =~ m/uebersicht\.php\?U=(.*?)"/i) {
die "Konnte mich nicht anmelden";
}
return $1;
}


while (1) {

my $wait_time = $normal_wait_time;

my $jobs = &read_jobs();

#$html = &get_http("GET",$base.$navbar_page,"u=$id&p1=1x296x10&c=1x296x10");

my $html = &get_http("GET",$base.$uebersicht_page,"U=$id");
for ((0 .. 3)) {
$html =~ m/<td.*?class="g">([\d\.]*)<\/td>/ig;
$rohstoffe[$_] = $1;
$rohstoffe[$_] =~ s/\.//g;
}


my $attack;
while ($html =~ m/<a title="(.*?)">.*?<div.*?title="(.*?)">.*?<font.*?>(.*?)<\/font>/ig)
{
my ($timestr, $secs, $msg) = ($1, $2, $3);
$msg =~ s/<.*?>//g;
#$time =~ m/(\d{1,2})\.(\d{1,2})\.(\d{4}) - (\d{2}):(\d{2}):(\d{2})/;
#$timestamp = timelocal ($6, $5, $4, $1, $2, $3);

print $timestr.": ".$msg."\n";

if ($msg =~ m/greift.*an/) {

&plan_jobs($jobs, time()+$secs, $timestr);

if ($secs < $wait_time) {
if ($secs < $gap_time + 5) {
$attack = &get_next_job($jobs) unless ($attack);
} else {
$wait_time = $secs - $gap_time;
}
}

}
}

&write_jobs ($jobs);

if ($attack) {
&do_job ($attack);
$attack = '';
next;
}

#print &get_http ("GET", $base.$produktion_page, "u=$id&auftr=1&dm=delete");
#$html = &get_http("GET",$base.$produktion_page,"u=$id");

#print $html;
print "Going to wait for $wait_time seconds...\n";
sleep $wait_time;
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread Galaxywars-Script: Galaxywars ist ein browsergame