]> gitweb.factorcode.org Git - factor.git/commitdiff
scryfall: add some words to filter by power/toughness
authorDoug Coleman <doug.coleman@gmail.com>
Wed, 3 Apr 2024 03:26:41 +0000 (22:26 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 3 Apr 2024 13:36:41 +0000 (08:36 -0500)
extra/scryfall/scryfall.factor

index f2c945be3a5315db5d85ba35ff5a5e1c5639b042..f8bdfb02efba252f9ffb0456ffea7028dc53567b 100644 (file)
@@ -1,10 +1,11 @@
 ! Copyright (C) 2024 Doug Coleman.
 ! See https://factorcode.org/license.txt for BSD license.
 USING: accessors arrays assocs assocs.extras calendar
-combinators grouping http.download images.loader images.viewer
-io io.directories json json.http kernel math math.parser
-math.statistics namespaces sequences sequences.extras sets
-sorting splitting ui.gadgets.panes unicode urls ;
+combinators combinators.short-circuit grouping http.download
+images.loader images.viewer io io.directories json json.http
+kernel math math.parser math.statistics namespaces sequences
+sequences.extras sets sorting splitting strings ui.gadgets.panes
+unicode urls ;
 IN: scryfall
 
 CONSTANT: scryfall-oracle-json-path "resource:scryfall-oracle-json"
@@ -51,6 +52,9 @@ MEMO: scryfall-rulings-json ( -- json )
 : map-card-faces ( assoc quot -- seq )
     [ "card_faces" of ] dip map ; inline
 
+: filter-card-faces ( assoc quot -- seq )
+    '[ [ "card_faces" of ] [ ] [ 1array ] ?if _ any? ] filter ; inline
+
 : card>image-uris ( assoc -- seq )
     [ "image_uris" of ]
     [ 1array ]
@@ -234,6 +238,9 @@ MEMO: all-cards-by-name ( -- assoc )
 : any-type? ( seq name -- ? ) [ type-line-of ] dip >lower '[ first [ >lower ] map _ member-of? ] any? ;
 : any-subtype? ( seq name -- ? ) [ type-line-of ] dip >lower '[ second [ >lower ] map _ member-of? ] any? ;
 
+: filter-type ( seq text -- seq' ) '[ _ any-type? ] filter ;
+: filter-subtype ( seq text -- seq' ) '[ _ any-subtype? ] filter ;
+
 : filter-basic ( seq -- seq' ) [ "Basic" any-type? ] filter ;
 : filter-basic-subtype ( seq text -- seq' ) [ filter-basic ] dip '[ _ any-subtype? ] filter ;
 : filter-land ( seq -- seq' ) [ "Land" any-type? ] filter ;
@@ -276,6 +283,30 @@ MEMO: all-cards-by-name ( -- assoc )
 
 : filter-flash ( seq -- seq' ) "Flash" filter-by-oracle-text ;
 
+: power>n ( string -- n/f )
+    [ "*" = ] [ drop -1 ] [ string>number ] ?if ;
+
+: mtg<  ( string/n/f n -- seq' ) [ power>n ] dip { [ and ] [ < ] } 2&& ;
+: mtg<= ( string/n/f n -- seq' ) [ power>n ] dip { [ and ] [ <= ] } 2&& ;
+: mtg>  ( string/n/f n -- seq' ) [ power>n ] dip { [ and ] [ > ] } 2&& ;
+: mtg>= ( string/n/f n -- seq' ) [ power>n ] dip { [ and ] [ >= ] } 2&& ;
+: mtg=  ( string/n/f n -- seq' ) [ power>n ] dip { [ and ] [ = ] } 2&& ;
+
+: filter-power=* ( seq -- seq' ) [ "power" of "*" = ] filter-card-faces ;
+: filter-toughness=* ( seq -- seq' ) [ "toughness" of "*" = ] filter-card-faces ;
+
+: filter-power= ( seq n -- seq' ) '[ "power" of _ mtg= ] filter-card-faces ;
+: filter-power< ( seq n -- seq' ) '[ "power" of _ mtg< ] filter-card-faces ;
+: filter-power> ( seq n -- seq' ) '[ "power" of _ mtg> ] filter-card-faces ;
+: filter-power<= ( seq n -- seq' ) '[ "power" of _ mtg<= ] filter-card-faces ;
+: filter-power>= ( seq n -- seq' ) '[ "power" of _ mtg>= ] filter-card-faces ;
+
+: filter-toughness= ( seq n -- seq' ) '[ "toughness" of _ mtg= ] filter-card-faces ;
+: filter-toughness< ( seq n -- seq' ) '[ "toughness" of _ mtg< ] filter-card-faces ;
+: filter-toughness> ( seq n -- seq' ) '[ "toughness" of _ mtg> ] filter-card-faces ;
+: filter-toughness<= ( seq n -- seq' ) '[ "toughness" of _ mtg<= ] filter-card-faces ;
+: filter-toughness>= ( seq n -- seq' ) '[ "toughness" of _ mtg>= ] filter-card-faces ;
+
 : map-props ( seq props -- seq' ) '[ _ intersect-keys ] map ;
 
 : gadgets. ( seq -- )