2011-07-13 20:29:20 +00:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2011-07-17 08:01:04 +00:00
|
|
|
#author: North
|
|
|
|
|
2011-07-13 20:29:20 +00:00
|
|
|
use Text::Template;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
|
|
|
|
my $authorFile = 'author.txt';
|
|
|
|
my $dataFile = 'mtg-cards-data.txt';
|
|
|
|
my $setsFile = 'mtg-sets-data.txt';
|
|
|
|
my $knownSetsFile = 'known-sets.txt';
|
2011-07-16 20:39:57 +00:00
|
|
|
my $keywordsFile = 'keywords.txt';
|
2011-07-13 20:29:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
my %cards;
|
|
|
|
my %sets;
|
|
|
|
my %knownSets;
|
2011-07-16 20:39:57 +00:00
|
|
|
my %keywords;
|
2011-07-13 20:29:20 +00:00
|
|
|
|
2011-07-16 20:39:57 +00:00
|
|
|
sub toCamelCase {
|
2011-07-13 20:29:20 +00:00
|
|
|
my $string = $_[0];
|
|
|
|
$string =~ s/\b([\w']+)\b/ucfirst($1)/ge;
|
|
|
|
$string =~ s/[-,\s\']//g;
|
|
|
|
$string;
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:55:28 +00:00
|
|
|
sub fixCost {
|
|
|
|
my $string = $_[0];
|
2011-08-07 19:02:43 +00:00
|
|
|
$string =~ s/{([2BUGRW])([2BUGRW])}/{$1\/$2}/g;
|
2011-07-17 12:55:28 +00:00
|
|
|
$string;
|
|
|
|
}
|
|
|
|
|
2011-07-13 20:29:20 +00:00
|
|
|
my $author;
|
|
|
|
if (-e $authorFile) {
|
|
|
|
open (DATA, $authorFile);
|
|
|
|
$author = <DATA>;
|
|
|
|
close(DATA);
|
|
|
|
} else {
|
|
|
|
$author = 'anonymous';
|
|
|
|
}
|
|
|
|
|
|
|
|
open (DATA, $dataFile) || die "can't open $dataFile";
|
|
|
|
while(my $line = <DATA>) {
|
|
|
|
my @data = split('\\|', $line);
|
|
|
|
$cards{$data[0]}{$data[1]} = \@data;
|
|
|
|
}
|
|
|
|
close(DATA);
|
|
|
|
|
|
|
|
open (DATA, $setsFile) || die "can't open $setsFile";
|
|
|
|
while(my $line = <DATA>) {
|
|
|
|
my @data = split('\\|', $line);
|
|
|
|
$sets{$data[0]}= $data[1];
|
2014-09-01 17:25:32 +00:00
|
|
|
#print "$data[0]--$data[1]\n"
|
2011-07-13 20:29:20 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2011-07-16 20:39:57 +00:00
|
|
|
open (DATA, $keywordsFile) || die "can't open $keywordsFile";
|
|
|
|
while(my $line = <DATA>) {
|
|
|
|
my @data = split('\\|', $line);
|
|
|
|
$keywords{toCamelCase($data[0])}= $data[1];
|
|
|
|
}
|
|
|
|
close(DATA);
|
|
|
|
|
2011-07-13 20:29:20 +00:00
|
|
|
my %cardTypes;
|
|
|
|
$cardTypes{'Artifact'} = 'CardType.ARTIFACT';
|
2014-06-25 08:37:07 +00:00
|
|
|
$cardTypes{'Conspiracy'} = 'CardType.CONSPIRACY';
|
2011-07-13 20:29:20 +00:00
|
|
|
$cardTypes{'Creature'} = 'CardType.CREATURE';
|
|
|
|
$cardTypes{'Enchantment'} = 'CardType.ENCHANTMENT';
|
|
|
|
$cardTypes{'Instant'} = 'CardType.INSTANT';
|
|
|
|
$cardTypes{'Land'} = 'CardType.LAND';
|
|
|
|
$cardTypes{'Sorcery'} = 'CardType.SORCERY';
|
2011-08-09 16:28:38 +00:00
|
|
|
$cardTypes{'Planeswalker'} = 'CardType.PLANESWALKER';
|
2014-06-05 19:20:50 +00:00
|
|
|
$cardTypes{'Tribal'} = 'CardType.TRIBAL';
|
2011-07-13 20:29:20 +00:00
|
|
|
|
|
|
|
my %raritiesConversion;
|
|
|
|
$raritiesConversion{'C'} = 'COMMON';
|
|
|
|
$raritiesConversion{'U'} = 'UNCOMMON';
|
|
|
|
$raritiesConversion{'R'} = 'RARE';
|
|
|
|
$raritiesConversion{'M'} = 'MYTHIC';
|
2013-05-11 21:06:30 +00:00
|
|
|
$raritiesConversion{'Special'} = 'SPECIAL';
|
2014-06-25 08:37:07 +00:00
|
|
|
$raritiesConversion{'Bonus'} = 'BONUS';
|
2011-07-13 20:29:20 +00:00
|
|
|
|
|
|
|
my %manaToColor;
|
|
|
|
$manaToColor{'B'} = 'Black';
|
|
|
|
$manaToColor{'U'} = 'Blue';
|
|
|
|
$manaToColor{'G'} = 'Green';
|
|
|
|
$manaToColor{'R'} = 'Red';
|
|
|
|
$manaToColor{'W'} = 'White';
|
|
|
|
|
|
|
|
|
|
|
|
# Get card name
|
2011-07-17 08:01:04 +00:00
|
|
|
my $cardName = $ARGV[0];
|
|
|
|
if(!$cardName) {
|
|
|
|
print 'Enter a card name: ';
|
|
|
|
$cardName = <STDIN>;
|
|
|
|
chomp $cardName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists $cards{$cardName}) {
|
|
|
|
die "Card name doesn't exist: $cardName\n";
|
|
|
|
}
|
2011-07-13 20:29:20 +00:00
|
|
|
|
|
|
|
# Check if card is already implemented
|
|
|
|
foreach my $setName (keys %{$cards{$cardName}}) {
|
2012-06-19 20:50:20 +00:00
|
|
|
if (exists $knownSets{$setName}) {
|
2011-07-16 20:39:57 +00:00
|
|
|
my $fileName = "../Mage.Sets/src/mage/sets/" . $knownSets{$setName} . "/" . toCamelCase($cardName) . ".java";
|
2011-07-13 20:29:20 +00:00
|
|
|
if(-e $fileName) {
|
2011-07-17 08:01:04 +00:00
|
|
|
die "$cardName is already implemented (set found in: $setName).\n";
|
2011-07-13 20:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Generate the cards
|
2011-07-17 08:01:04 +00:00
|
|
|
my $simpleOnly = $ARGV[1] || 'false';
|
|
|
|
my $template = Text::Template->new(TYPE => 'FILE', SOURCE => 'cardClass.tmpl', DELIMITERS => [ '[=', '=]' ]);
|
|
|
|
my $templateExtended = Text::Template->new(TYPE => 'FILE', SOURCE => 'cardExtendedClass.tmpl', DELIMITERS => [ '[=', '=]' ]);
|
2011-07-13 20:29:20 +00:00
|
|
|
my %vars;
|
|
|
|
|
|
|
|
$vars{'author'} = $author;
|
|
|
|
$vars{'name'} = $cardName;
|
2011-07-16 20:39:57 +00:00
|
|
|
$vars{'className'} = toCamelCase($cardName);
|
2011-07-13 20:29:20 +00:00
|
|
|
|
2011-07-17 08:01:04 +00:00
|
|
|
if ($simpleOnly ne 'true') {
|
|
|
|
print "Files generated:\n";
|
|
|
|
}
|
2011-07-14 20:44:56 +00:00
|
|
|
my $baseRarity = '';
|
2011-07-13 20:29:20 +00:00
|
|
|
foreach my $setName (keys %{$cards{$cardName}}) {
|
2012-06-19 20:50:20 +00:00
|
|
|
if (exists $knownSets{$setName}) {
|
2011-07-16 20:39:57 +00:00
|
|
|
my $fileName = "../Mage.Sets/src/mage/sets/" . $knownSets{$setName} . "/" . toCamelCase($cardName) . ".java";
|
2011-07-13 20:29:20 +00:00
|
|
|
my $result;
|
2011-07-14 20:44:56 +00:00
|
|
|
|
2011-07-13 20:29:20 +00:00
|
|
|
$vars{'set'} = $knownSets{$setName};
|
|
|
|
$vars{'expansionSetCode'} = $sets{$setName};
|
|
|
|
$vars{'cardNumber'} = $cards{$cardName}{$setName}[2];
|
|
|
|
$vars{'rarity'} = $raritiesConversion{$cards{$cardName}{$setName}[3]};
|
2011-07-14 20:44:56 +00:00
|
|
|
|
|
|
|
if (!$baseRarity) {
|
|
|
|
$baseRarity = $cards{$cardName}{$setName}[3];
|
|
|
|
|
2011-07-17 12:55:28 +00:00
|
|
|
$vars{'manaCost'} = fixCost($cards{$cardName}{$setName}[4]);
|
2011-07-13 20:29:20 +00:00
|
|
|
$vars{'power'} = $cards{$cardName}{$setName}[6];
|
|
|
|
$vars{'toughness'} = $cards{$cardName}{$setName}[7];
|
|
|
|
|
2012-06-19 20:50:20 +00:00
|
|
|
my @types;
|
2011-07-13 20:29:20 +00:00
|
|
|
$vars{'subType'} = '';
|
2012-06-19 20:50:20 +00:00
|
|
|
my $type = $cards{$cardName}{$setName}[5];
|
|
|
|
while ($type =~ m/([a-zA-Z]+)( )*/g) {
|
2011-07-13 20:29:20 +00:00
|
|
|
if (exists($cardTypes{$1})) {
|
|
|
|
push(@types, $cardTypes{$1});
|
2012-06-19 20:50:20 +00:00
|
|
|
} else {
|
2011-07-13 20:29:20 +00:00
|
|
|
if (@types) {
|
2011-07-16 20:39:57 +00:00
|
|
|
$vars{'subType'} .= "\n this.subtype.add(\"$1\");";
|
2011-07-13 20:29:20 +00:00
|
|
|
} else {
|
2011-07-16 20:39:57 +00:00
|
|
|
$vars{'subType'} .= "\n this.supertype.add(\"$1\");";
|
2011-07-13 20:29:20 +00:00
|
|
|
}
|
2012-06-19 20:50:20 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 20:29:20 +00:00
|
|
|
$vars{'type'} = join(', ', @types);
|
|
|
|
|
|
|
|
my %colors;
|
|
|
|
while ($vars{'manaCost'} =~ m/([BUGRW])/g) {
|
|
|
|
$colors{$manaToColor{$1}} = 1;
|
2012-06-19 20:50:20 +00:00
|
|
|
}
|
2011-07-13 20:29:20 +00:00
|
|
|
|
|
|
|
$vars{'colors'} = '';
|
|
|
|
foreach my $color (keys %colors) {
|
|
|
|
$vars{'colors'} .= "\n this.color.set$color(true);";
|
|
|
|
}
|
2011-07-16 20:39:57 +00:00
|
|
|
if ($vars{'colors'} || $vars{'power'}) {
|
|
|
|
$vars{'colors'} = "\n" . $vars{'colors'};
|
|
|
|
}
|
|
|
|
|
|
|
|
$vars{'abilitiesImports'} = '';
|
|
|
|
$vars{'abilities'} = '';
|
|
|
|
|
|
|
|
my @abilities = split('\$', $cards{$cardName}{$setName}[8]);
|
|
|
|
foreach my $ability (@abilities) {
|
|
|
|
$ability =~ s/ <i>.+?<\/i>//g;
|
|
|
|
|
|
|
|
my $notKeyWord;
|
|
|
|
foreach my $keyword (keys %keywords) {
|
2011-07-17 09:06:30 +00:00
|
|
|
if (toCamelCase($ability) =~ m/^$keyword(?=[A-Z{\d]|$)/g) {
|
|
|
|
$notKeyWord = 'false';
|
2011-07-16 20:39:57 +00:00
|
|
|
my @ka = split(', ', $ability);
|
|
|
|
foreach my $kw (@ka) {
|
|
|
|
my $kwUnchanged = $kw;
|
2011-07-17 09:06:30 +00:00
|
|
|
foreach my $kk (keys %keywords) {
|
|
|
|
if (toCamelCase($kw) =~ m/^$kk(?=[A-Z{\d]|$)/g) {
|
|
|
|
$kw = $kk;
|
2011-07-17 08:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-16 20:39:57 +00:00
|
|
|
if ($keywords{$kw}) {
|
2012-05-31 22:06:05 +00:00
|
|
|
$vars{'abilities'} .= "\n // " . ucfirst($kwUnchanged);
|
2011-07-16 20:39:57 +00:00
|
|
|
if ($keywords{$kw} eq 'instance') {
|
|
|
|
$vars{'abilities'} .= "\n this.addAbility(" . $kw . "Ability.getInstance());";
|
|
|
|
} elsif ($keywords{$kw} eq 'new') {
|
|
|
|
$vars{'abilities'} .= "\n this.addAbility(new " . $kw . "Ability());";
|
|
|
|
} elsif ($keywords{$kw} eq 'number') {
|
|
|
|
$ability =~ m/(\b\d+?\b)/g;
|
|
|
|
$vars{'abilities'} .= "\n this.addAbility(new " . $kw . 'Ability(' . $1 . '));';
|
|
|
|
} elsif ($keywords{$kw} eq 'cost') {
|
|
|
|
$ability =~ m/({.*})/g;
|
2011-07-17 12:55:28 +00:00
|
|
|
$vars{'abilities'} .= "\n this.addAbility(new " . $kw . 'Ability(new ManaCostsImpl("' . fixCost($1) . '")));';
|
2011-07-16 20:39:57 +00:00
|
|
|
$vars{'abilitiesImports'} .= "\nimport mage.abilities.costs.mana.ManaCostsImpl;";
|
2013-05-25 13:43:41 +00:00
|
|
|
} elsif ($keywords{$kw} eq 'card, manaString') {
|
|
|
|
$ability =~ m/({.*})/g;
|
|
|
|
$vars{'abilities'} .= "\n this.addAbility(new " . $kw . 'Ability(this, "' . fixCost($1) . '"));';
|
2014-06-09 22:36:19 +00:00
|
|
|
} elsif ($keywords{$kw} eq 'card, cost') {
|
|
|
|
$ability =~ m/({.*})/g;
|
|
|
|
$vars{'abilities'} .= "\n this.addAbility(new " . $kw . 'Ability(this, new ManaCostsImpl("' . fixCost($1) . '")));';
|
|
|
|
$vars{'abilitiesImports'} .= "\nimport mage.abilities.costs.mana.ManaCostsImpl;";
|
2011-07-16 20:39:57 +00:00
|
|
|
}
|
2013-05-25 13:43:41 +00:00
|
|
|
|
2011-07-16 20:39:57 +00:00
|
|
|
|
|
|
|
$vars{'abilitiesImports'} .= "\nimport mage.abilities.keyword." . $kw . "Ability;";
|
|
|
|
} else {
|
|
|
|
$vars{'abilities'} .= "\n // $kwUnchanged";
|
2011-07-17 08:01:04 +00:00
|
|
|
if ($simpleOnly eq 'true') {
|
|
|
|
exit 0;
|
|
|
|
}
|
2011-07-16 20:39:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$notKeyWord) {
|
|
|
|
$vars{'abilities'} .= "\n // $ability";
|
2011-07-17 08:01:04 +00:00
|
|
|
if ($simpleOnly eq 'true') {
|
|
|
|
exit 0;
|
|
|
|
}
|
2011-07-16 20:39:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($vars{'abilities'}) {
|
|
|
|
$vars{'abilities'} = "\n" . $vars{'abilities'};
|
|
|
|
}
|
2011-07-14 20:44:56 +00:00
|
|
|
|
2011-07-13 20:29:20 +00:00
|
|
|
$vars{'baseSet'} = $vars{'set'};
|
|
|
|
$vars{'baseClassName'} = $vars{'className'};
|
|
|
|
|
|
|
|
$result = $template->fill_in(HASH => \%vars);
|
|
|
|
} else {
|
2011-07-14 20:44:56 +00:00
|
|
|
$vars{'rarityExtended'} = '';
|
|
|
|
if ($baseRarity ne $cards{$cardName}{$setName}[3]) {
|
2011-07-16 20:39:57 +00:00
|
|
|
$vars{'rarityExtended'} = "\n this.rarity = Rarity.$raritiesConversion{$cards{$cardName}{$setName}[3]};";
|
2011-07-14 20:44:56 +00:00
|
|
|
}
|
2011-07-13 20:29:20 +00:00
|
|
|
$result = $templateExtended->fill_in(HASH => \%vars);
|
|
|
|
}
|
2011-07-14 20:44:56 +00:00
|
|
|
|
2011-07-13 20:29:20 +00:00
|
|
|
open CARD, "> $fileName";
|
2011-07-17 12:55:28 +00:00
|
|
|
print CARD $result;
|
2011-07-13 20:29:20 +00:00
|
|
|
close CARD;
|
2011-07-14 20:44:56 +00:00
|
|
|
|
2011-07-17 08:01:04 +00:00
|
|
|
print "$vars{'set'}.$vars{'className'}\n";
|
2014-09-01 17:25:32 +00:00
|
|
|
} else {
|
|
|
|
print "Set not found in known sets: $setName\n";
|
2012-06-19 20:50:20 +00:00
|
|
|
}
|
2011-07-13 20:29:20 +00:00
|
|
|
}
|