2014-09-03 22:37:26 -07:00
package magic.data ;
import java.util.ArrayList ;
2017-06-19 04:59:21 -07:00
import java.util.Arrays ;
import java.util.Collections ;
2014-09-03 22:37:26 -07:00
import java.util.List ;
import java.util.Scanner ;
2020-01-15 12:02:42 -08:00
2014-09-03 22:37:26 -07:00
import magic.model.MagicCardDefinition ;
2015-06-01 06:17:41 -07:00
import magic.model.MagicDeck ;
import magic.utility.DeckUtils ;
2015-01-08 06:51:49 -08:00
import magic.utility.MagicResources ;
2014-09-03 22:37:26 -07:00
2015-06-19 18:56:40 -07:00
public class MagicPredefinedFormat extends MagicFormat {
2014-09-03 22:37:26 -07:00
// add new formats here...
// @name: display name in UI.
// @filename: case-sensitive name of file (without extension) in magic/data/formats.
2015-06-22 07:27:33 -07:00
public static final MagicFormat STANDARD = new MagicPredefinedFormat ( "Standard" , "standard" ) ;
public static final MagicFormat MODERN = new MagicPredefinedFormat ( "Modern" , "modern" ) ;
public static final MagicFormat LEGACY = new MagicPredefinedFormat ( "Legacy" , "legacy" ) ;
public static final MagicFormat VINTAGE = new MagicPredefinedFormat ( "Vintage" , "vintage" ) ;
public static final MagicFormat ICE_AGE_BLOCK = new MagicPredefinedFormat ( "Ice Age Block" , "ice_age_block" ) ;
public static final MagicFormat MIRAGE_BLOCK = new MagicPredefinedFormat ( "Mirage block" , "mirage_block" ) ;
public static final MagicFormat TEMPEST_BLOCK = new MagicPredefinedFormat ( "Tempest block" , "tempest_block" ) ;
public static final MagicFormat URZA_BLOCK = new MagicPredefinedFormat ( "Urza block" , "urza_block" ) ;
public static final MagicFormat MASQUES_BLOCK = new MagicPredefinedFormat ( "Masques block" , "masques_block" ) ;
public static final MagicFormat INVASION_BLOCK = new MagicPredefinedFormat ( "Invasion block" , "invasion_block" ) ;
public static final MagicFormat ODYSSEY_BLOCK = new MagicPredefinedFormat ( "Odyssey block" , "odyssey_block" ) ;
public static final MagicFormat ONSLAUGHT_BLOCK = new MagicPredefinedFormat ( "Onslaught block" , "onslaught_block" ) ;
public static final MagicFormat MIRRODIN_BLOCK = new MagicPredefinedFormat ( "Mirrodin block" , "mirrodin_block" ) ;
public static final MagicFormat KAMIGAWA_BLOCK = new MagicPredefinedFormat ( "Kamigawa block" , "kamigawa_block" ) ;
public static final MagicFormat RAVNICA_BLOCK = new MagicPredefinedFormat ( "Ravnica block" , "ravnica_block" ) ;
public static final MagicFormat TIME_SPIRAL_BLOCK = new MagicPredefinedFormat ( "Time Spiral block" , "time_spiral_block" ) ;
public static final MagicFormat LORWYN_SHADOWMOOR_BLOCK = new MagicPredefinedFormat ( "Lorwyn-Shadowmoor block" , "lorwyn_shadowmoor_block" ) ;
public static final MagicFormat SHARDS_OF_ALARA_BLOCK = new MagicPredefinedFormat ( "Shards of Alara block" , "shards_of_alara_block" ) ;
public static final MagicFormat ZENDIKAR_RISE_OF_THE_ELDRAZI_BLOCK = new MagicPredefinedFormat ( "Zendikar-Rise of the Eldrazi block" , "zendikar_rise_of_the_eldrazi_block" ) ;
public static final MagicFormat SCARS_OF_MIRRODIN_BLOCK = new MagicPredefinedFormat ( "Scars of Mirrodin block" , "scars_of_mirrodin_block" ) ;
public static final MagicFormat INNISTRAD_AVACYN_RESTORED_BLOCK = new MagicPredefinedFormat ( "Innistrad-Avacyn Restored block" , "innistrad_avacyn_restored_block" ) ;
public static final MagicFormat RETURN_TO_RAVNICA_BLOCK = new MagicPredefinedFormat ( "Return to Ravnica block" , "return_to_ravnica_block" ) ;
public static final MagicFormat THEROS_BLOCK = new MagicPredefinedFormat ( "Theros block" , "theros_block" ) ;
public static final MagicFormat KHANS_OF_TARKIR_BLOCK = new MagicPredefinedFormat ( "Khans of Tarkir block" , "khans_of_tarkir_block" ) ;
2016-03-25 15:34:29 -07:00
public static final MagicFormat BATTLE_FOR_ZENDIKAR_BLOCK = new MagicPredefinedFormat ( "Battle for Zendikar block" , "battle_for_zendikar_block" ) ;
2016-07-17 10:44:52 -07:00
public static final MagicFormat SHADOWS_OVER_INNISTRAD_BLOCK = new MagicPredefinedFormat ( "Shadows over Innistrad block" , "shadows_over_innistrad_block" ) ;
2016-09-21 11:43:06 -07:00
public static final MagicFormat KALADESH_BLOCK = new MagicPredefinedFormat ( "Kaladesh block" , "kaladesh_block" ) ;
2017-04-24 09:02:52 -07:00
public static final MagicFormat AMONKHET_BLOCK = new MagicPredefinedFormat ( "Amonkhet block" , "amonkhet_block" ) ;
2018-01-15 22:05:53 -08:00
public static final MagicFormat IXALAN_BLOCK = new MagicPredefinedFormat ( "Ixalan block" , "ixalan_block" ) ;
2015-06-19 18:56:40 -07:00
2015-06-22 07:27:33 -07:00
private static final List < MagicFormat > values = Collections . unmodifiableList ( Arrays . asList (
2015-06-19 18:56:40 -07:00
STANDARD ,
MODERN ,
LEGACY ,
VINTAGE ,
2018-01-15 22:05:53 -08:00
IXALAN_BLOCK ,
2017-04-24 09:02:52 -07:00
AMONKHET_BLOCK ,
2016-09-21 11:43:06 -07:00
KALADESH_BLOCK ,
2016-07-17 10:44:52 -07:00
SHADOWS_OVER_INNISTRAD_BLOCK ,
2016-03-25 15:34:29 -07:00
BATTLE_FOR_ZENDIKAR_BLOCK ,
2015-11-27 22:53:59 -08:00
KHANS_OF_TARKIR_BLOCK ,
2015-06-19 18:56:40 -07:00
THEROS_BLOCK ,
2015-11-27 22:53:59 -08:00
RETURN_TO_RAVNICA_BLOCK ,
INNISTRAD_AVACYN_RESTORED_BLOCK ,
SCARS_OF_MIRRODIN_BLOCK ,
ZENDIKAR_RISE_OF_THE_ELDRAZI_BLOCK ,
SHARDS_OF_ALARA_BLOCK ,
LORWYN_SHADOWMOOR_BLOCK ,
TIME_SPIRAL_BLOCK ,
RAVNICA_BLOCK ,
KAMIGAWA_BLOCK ,
MIRRODIN_BLOCK ,
ONSLAUGHT_BLOCK ,
ODYSSEY_BLOCK ,
INVASION_BLOCK ,
MASQUES_BLOCK ,
URZA_BLOCK ,
TEMPEST_BLOCK ,
MIRAGE_BLOCK ,
ICE_AGE_BLOCK
2015-06-19 19:17:28 -07:00
) ) ;
2015-06-19 18:56:40 -07:00
2015-06-22 07:27:33 -07:00
public static List < MagicFormat > values ( ) {
2015-06-19 18:56:40 -07:00
return values ;
}
2014-09-03 22:37:26 -07:00
private final String name ;
private final String filename ;
2015-06-01 06:17:41 -07:00
private final int minimumDeckSize ;
private final int maximumCardCopies ;
2015-11-27 22:53:59 -08:00
2015-06-01 06:17:41 -07:00
private final List < String > bannedCardNames = new ArrayList < > ( ) ;
private final List < String > restrictedCardNames = new ArrayList < > ( ) ;
private final List < MagicSets > magicSets = new ArrayList < > ( ) ;
2015-06-20 02:33:31 -07:00
private MagicPredefinedFormat ( final String name , final String filename , int minDeckSize , int maxCardCopies ) {
2014-09-03 22:37:26 -07:00
this . name = name ;
this . filename = filename ;
2015-06-01 06:17:41 -07:00
this . minimumDeckSize = minDeckSize ;
this . maximumCardCopies = maxCardCopies ;
}
2015-11-27 22:53:59 -08:00
2015-06-20 02:33:31 -07:00
private MagicPredefinedFormat ( final String name , final String filename ) {
2015-06-01 06:17:41 -07:00
this ( name , filename , 60 , 4 ) ;
2014-09-03 22:37:26 -07:00
}
2015-06-19 18:56:40 -07:00
@Override
2014-09-03 22:37:26 -07:00
public String getName ( ) {
return name ;
}
public String getFilename ( ) {
return filename ;
}
2015-06-01 06:17:41 -07:00
private void loadMagicFormatFile ( ) {
try ( final Scanner sc = new Scanner ( MagicResources . getFileContent ( this ) ) ) {
2014-09-03 22:37:26 -07:00
while ( sc . hasNextLine ( ) ) {
final String line = sc . nextLine ( ) . trim ( ) ;
2016-03-25 16:45:44 -07:00
if ( ! line . startsWith ( "#" ) & & ! line . isEmpty ( ) ) {
2014-09-03 22:37:26 -07:00
switch ( line . substring ( 0 , 1 ) ) {
2015-06-01 06:17:41 -07:00
case "!" :
bannedCardNames . add ( line . substring ( 1 ) ) ;
break ;
case "*" :
restrictedCardNames . add ( line . substring ( 1 ) ) ;
break ;
default :
magicSets . add ( MagicSets . valueOf ( line ) ) ;
2014-09-03 22:37:26 -07:00
}
}
}
}
2015-06-01 06:17:41 -07:00
}
2015-06-19 18:56:40 -07:00
@Override
2015-06-01 06:17:41 -07:00
public CardLegality getCardLegality ( MagicCardDefinition card , int cardCount ) {
2016-03-26 04:10:02 -07:00
if ( cardCount > maximumCardCopies & & ! card . canHaveAnyNumberInDeck ( ) ) {
2015-06-01 06:17:41 -07:00
return CardLegality . TooManyCopies ;
}
if ( magicSets . isEmpty ( ) ) {
loadMagicFormatFile ( ) ;
}
if ( isCardBanned ( card ) ) {
return CardLegality . Banned ;
} else if ( cardCount > 1 & & isCardRestricted ( card ) ) {
return CardLegality . Restricted ;
} else if ( isCardInFormat ( card ) ) { // this takes the longest so is done last.
return CardLegality . Legal ;
} else {
return CardLegality . Illegal ;
}
}
2015-06-19 18:56:40 -07:00
@Override
2015-06-01 06:17:41 -07:00
public int getMinimumDeckSize ( ) {
return minimumDeckSize ;
}
2017-06-16 20:19:03 -07:00
@Override
2015-06-01 06:17:41 -07:00
public boolean isDeckLegal ( final MagicDeck aDeck ) {
2016-03-26 03:49:46 -07:00
if ( aDeck . size ( ) < minimumDeckSize ) {
2015-06-01 06:17:41 -07:00
return false ;
}
for ( final MagicCardDefinition card : DeckUtils . getDistinctCards ( aDeck ) ) {
2016-03-25 16:39:44 -07:00
final int cardCountCheck = aDeck . getCardCount ( card ) ;
if ( ! isCardLegal ( card , cardCountCheck ) ) {
2015-06-01 06:17:41 -07:00
return false ;
}
}
return true ;
}
private boolean isCardBanned ( MagicCardDefinition aCard ) {
2016-03-25 16:45:44 -07:00
return ! bannedCardNames . isEmpty ( ) & & bannedCardNames . contains ( aCard . getName ( ) ) ;
2015-06-01 06:17:41 -07:00
}
2014-09-03 22:37:26 -07:00
2015-06-01 06:17:41 -07:00
private boolean isCardRestricted ( MagicCardDefinition card ) {
2016-03-25 16:45:44 -07:00
return ! restrictedCardNames . isEmpty ( ) & & restrictedCardNames . contains ( card . getName ( ) ) ;
2015-06-01 06:17:41 -07:00
}
private boolean isCardInFormat ( MagicCardDefinition card ) {
for ( MagicSets magicSet : magicSets ) {
if ( MagicSetDefinitions . isCardInSet ( card , magicSet ) ) {
return true ;
}
}
return false ;
2014-09-03 22:37:26 -07:00
}
}