Added script to generate cards that can be completely generated.

Fixed issue in gen-card.pl
This commit is contained in:
North 2011-07-17 11:01:04 +03:00
parent c8885e3fcd
commit 4e4b71d2f1
3 changed files with 100 additions and 15 deletions

View file

@ -1,5 +1,7 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
#author: North
use Text::Template; use Text::Template;
use strict; use strict;
@ -83,30 +85,40 @@ $manaToColor{'W'} = 'White';
# Get card name # Get card name
print 'Enter a card name: '; my $cardName = $ARGV[0];
my $cardName = <STDIN>; if(!$cardName) {
chomp $cardName; print 'Enter a card name: ';
$cardName = <STDIN>;
chomp $cardName;
}
if (!exists $cards{$cardName}) {
die "Card name doesn't exist: $cardName\n";
}
# Check if card is already implemented # Check if card is already implemented
foreach my $setName (keys %{$cards{$cardName}}) { foreach my $setName (keys %{$cards{$cardName}}) {
if (exists $knownSets{$setName}) { if (exists $knownSets{$setName}) {
my $fileName = "../Mage.Sets/src/mage/sets/" . $knownSets{$setName} . "/" . toCamelCase($cardName) . ".java"; my $fileName = "../Mage.Sets/src/mage/sets/" . $knownSets{$setName} . "/" . toCamelCase($cardName) . ".java";
if(-e $fileName) { if(-e $fileName) {
die "$cardName is already implemented (set found: $setName).\n"; die "$cardName is already implemented (set found in: $setName).\n";
} }
} }
} }
# Generate the cards # Generate the cards
my $template = Text::Template->new(SOURCE => 'cardClass.tmpl', DELIMITERS => [ '[=', '=]' ]); my $simpleOnly = $ARGV[1] || 'false';
my $templateExtended = Text::Template->new(SOURCE => 'cardExtendedClass.tmpl', DELIMITERS => [ '[=', '=]' ]); my $template = Text::Template->new(TYPE => 'FILE', SOURCE => 'cardClass.tmpl', DELIMITERS => [ '[=', '=]' ]);
my $templateExtended = Text::Template->new(TYPE => 'FILE', SOURCE => 'cardExtendedClass.tmpl', DELIMITERS => [ '[=', '=]' ]);
my %vars; my %vars;
$vars{'author'} = $author; $vars{'author'} = $author;
$vars{'name'} = $cardName; $vars{'name'} = $cardName;
$vars{'className'} = toCamelCase($cardName); $vars{'className'} = toCamelCase($cardName);
print "Files generated:\n"; if ($simpleOnly ne 'true') {
print "Files generated:\n";
}
my $baseRarity = ''; my $baseRarity = '';
foreach my $setName (keys %{$cards{$cardName}}) { foreach my $setName (keys %{$cards{$cardName}}) {
if (exists $knownSets{$setName}) { if (exists $knownSets{$setName}) {
@ -168,8 +180,11 @@ foreach my $setName (keys %{$cards{$cardName}}) {
my @ka = split(', ', $ability); my @ka = split(', ', $ability);
foreach my $kw (@ka) { foreach my $kw (@ka) {
my $kwUnchanged = $kw; my $kwUnchanged = $kw;
$kw = toCamelCase($kw); foreach my $keyword (keys %keywords) {
if (index(toCamelCase($kw), $keyword) eq 0) {
$kw = $keyword;
}
}
if ($keywords{$kw}) { if ($keywords{$kw}) {
if ($keywords{$kw} eq 'instance') { if ($keywords{$kw} eq 'instance') {
$vars{'abilities'} .= "\n this.addAbility(" . $kw . "Ability.getInstance());"; $vars{'abilities'} .= "\n this.addAbility(" . $kw . "Ability.getInstance());";
@ -187,6 +202,9 @@ foreach my $setName (keys %{$cards{$cardName}}) {
$vars{'abilitiesImports'} .= "\nimport mage.abilities.keyword." . $kw . "Ability;"; $vars{'abilitiesImports'} .= "\nimport mage.abilities.keyword." . $kw . "Ability;";
} else { } else {
$vars{'abilities'} .= "\n // $kwUnchanged"; $vars{'abilities'} .= "\n // $kwUnchanged";
if ($simpleOnly eq 'true') {
exit 0;
}
} }
} }
} }
@ -194,6 +212,9 @@ foreach my $setName (keys %{$cards{$cardName}}) {
if (!$notKeyWord) { if (!$notKeyWord) {
$vars{'abilities'} .= "\n // $ability"; $vars{'abilities'} .= "\n // $ability";
if ($simpleOnly eq 'true') {
exit 0;
}
} }
} }
if ($vars{'abilities'}) { if ($vars{'abilities'}) {
@ -216,6 +237,6 @@ foreach my $setName (keys %{$cards{$cardName}}) {
print CARD $result; print CARD $result;
close CARD; close CARD;
print "$fileName\n"; print "$vars{'set'}.$vars{'className'}\n";
} }
} }

View file

@ -1,5 +1,7 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
#author: North
use Text::Template; use Text::Template;
use strict; use strict;
@ -16,11 +18,15 @@ my %knownSets;
my @setCards; my @setCards;
print "Enter a set name: "; # gets the set name
my $setName = <STDIN>; my $setName = $ARGV[0];
chomp $setName; if(!$setName) {
print 'Enter a set name: ';
$setName = <STDIN>;
chomp $setName;
}
my $template = Text::Template->new(SOURCE => 'cardExtendedClass.tmpl', DELIMITERS => [ '[=', '=]' ]); my $template = Text::Template->new(TYPE => 'FILE', SOURCE => 'cardExtendedClass.tmpl', DELIMITERS => [ '[=', '=]' ]);
sub toCamelCase { sub toCamelCase {
my $string = $_[0]; my $string = $_[0];
@ -76,7 +82,7 @@ $vars{'author'} = $author;
$vars{'set'} = $knownSets{$setName}; $vars{'set'} = $knownSets{$setName};
$vars{'expansionSetCode'} = $sets{$setName}; $vars{'expansionSetCode'} = $sets{$setName};
print "Files generated:\n"; print "Extended cards generated:\n";
foreach my $cardName (@setCards) { foreach my $cardName (@setCards) {
my $className = toCamelCase($cardName); my $className = toCamelCase($cardName);
my $currentFileName = "../Mage.Sets/src/mage/sets/" . $knownSets{$setName} . "/" . $className . ".java"; my $currentFileName = "../Mage.Sets/src/mage/sets/" . $knownSets{$setName} . "/" . $className . ".java";

View file

@ -0,0 +1,58 @@
#!/usr/bin/perl -w
#author: North
use strict;
my $dataFile = 'mtg-cards-data.txt';
my $knownSetsFile = "known-sets.txt";
my %knownSets;
my @setCards;
sub toCamelCase {
my $string = $_[0];
$string =~ s/\b([\w']+)\b/ucfirst($1)/ge;
$string =~ s/[-,\s\']//g;
$string;
}
print 'Enter a set name: ';
my $setName = <STDIN>;
chomp $setName;
open (DATA, $dataFile) || die "can't open $dataFile";
while(my $line = <DATA>) {
my @data = split('\\|', $line);
if ($data[1] eq $setName) {
push(@setCards, \@data);
}
}
close(DATA);
open (DATA, $knownSetsFile) || die "can't open $knownSetsFile";
while(my $line = <DATA>) {
my @data = split('\\|', $line);
$knownSets{$data[0]}= $data[1];
}
close(DATA);
if(!exists $knownSets{$setName}) {
die "You must add the set to known-sets.txt\n";
}
my $packageName = $knownSets{$setName};
$setName =~ s/"/\\"/g;
system("gen-existing-cards-by-set.pl \"$setName\"");
# Generate missing simple cards
print "Simple cards generated: \n";
foreach my $cardName (@setCards) {
my $fileName = "../Mage.Sets/src/mage/sets/" . $packageName . "/" . toCamelCase(${$cardName}[0]) . ".java";
if(!-e $fileName) {
system("gen-card.pl \"${$cardName}[0]\" true");
}
}