Skip to content

Instantly share code, notes, and snippets.

@hidde-vb
Last active September 30, 2024 17:32
Show Gist options
  • Save hidde-vb/09db1f363ab4e04be3e1136d75621cbf to your computer and use it in GitHub Desktop.
Save hidde-vb/09db1f363ab4e04be3e1136d75621cbf to your computer and use it in GitHub Desktop.
Python | Random Cube generator
# Simple random cube building scripts
# You have to download the Oracle card from https://scryfall.com/docs/api/bulk-data
# The land / spell ratio can be tuned below
#
# Requirements
# python 3.x
# ijson
import ijson
import random
# CONSTANTS
NUMBER_OF_SPELLS = 320
NUMBER_OF_LANDS = 40
SOURCE_FILE = "data/oracle-cards-20240920.json"
TARGET_FILE = "data/cube.txt"
# Filters for which cards to exclude
def is_invalid(card):
if len(card['set']) == 4: # These include art cards, token, ...
return True
if card['name'].startswith("A-"): # No alchemy rebalancing
return True
if card['layout'] == 'scheme': # No schemes
return True
if card["type_line"].startswith('Plane '): # No planes
return True
if card["type_line"].startswith('Phenomenon'): # No phenomenons (?)
return True
return False
spells = set()
lands = set()
print("parsing...")
with open(SOURCE_FILE, "rb") as source_file:
for record in ijson.items(source_file, "item"):
name = record["name"].split(' // ')[0]
if is_invalid(record):
continue
if record["type_line"].startswith("Land"):
lands.update({name})
else:
spells.update({name})
print('building...')
with open(TARGET_FILE, "w") as cube_file:
lands_sample = random.sample([l for l in lands], NUMBER_OF_LANDS)
spells_sample = random.sample([s for s in spells], NUMBER_OF_SPELLS)
for land in lands_sample:
cube_file.write(land + '\n')
for spell in spells_sample:
cube_file.write(spell + '\n')
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment