2017-04-22 18:13:01 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-08-05 13:06:18 -04:00
|
|
|
# Note: SVG templates are heavily based on these:
|
|
|
|
# https://boardgamegeek.com/thread/813176/card-templates/page/1
|
|
|
|
|
2017-04-22 18:13:01 -04:00
|
|
|
import json
|
|
|
|
import math
|
|
|
|
import os
|
2017-07-13 18:50:32 -04:00
|
|
|
import subprocess
|
2017-04-27 19:53:23 -04:00
|
|
|
import sys
|
2017-04-22 18:13:01 -04:00
|
|
|
|
2017-07-13 18:50:32 -04:00
|
|
|
from lxml import etree
|
|
|
|
|
2017-04-27 19:53:23 -04:00
|
|
|
if getattr(sys, 'frozen', False): # we are running in a bundle
|
|
|
|
bundle_dir = sys._MEIPASS
|
|
|
|
else: # we are running in a normal Python environment
|
|
|
|
bundle_dir = os.path.dirname(os.path.abspath(__file__))
|
2017-04-22 18:13:01 -04:00
|
|
|
|
2017-08-03 19:03:17 -04:00
|
|
|
CARD_WIDTH = 181
|
|
|
|
CARD_HEIGHT = 253
|
2017-07-13 18:50:32 -04:00
|
|
|
|
2017-08-05 13:43:06 -04:00
|
|
|
with open(bundle_dir + "/templates/card.json") as f:
|
|
|
|
CARD_JSON_TEMPLATE = json.load(f)
|
|
|
|
|
2017-07-13 18:50:32 -04:00
|
|
|
def setText(tree, id, text):
|
|
|
|
element = tree.find('.//*[@id="' + id + '"]')
|
|
|
|
if element is None:
|
|
|
|
print("id", id, "not found")
|
|
|
|
return
|
|
|
|
elif element.tag == "{http://www.w3.org/2000/svg}flowRoot":
|
2017-07-13 23:22:19 -04:00
|
|
|
for e in element.findall("{http://www.w3.org/2000/svg}flowPara"):
|
|
|
|
element.remove(e) # clear child paragraphs
|
|
|
|
lines = str(text).splitlines()
|
|
|
|
for line in lines:
|
2017-08-05 13:06:18 -04:00
|
|
|
etree.SubElement(element,
|
|
|
|
"{http://www.w3.org/2000/svg}flowPara").text=line
|
2017-07-13 18:50:32 -04:00
|
|
|
else:
|
2017-08-03 18:14:48 -04:00
|
|
|
element.text = str(text)
|
2017-07-13 18:50:32 -04:00
|
|
|
|
|
|
|
def removeElement(tree, id):
|
|
|
|
mark = tree.find('.//*[@id="' + id + '"]')
|
|
|
|
if mark is not None:
|
|
|
|
mark.getparent().remove(mark)
|
|
|
|
|
|
|
|
def makeSVG(base, properties):
|
|
|
|
tree = etree.parse(base)
|
|
|
|
for id, text in properties.items():
|
|
|
|
if id not in ["count", "art"]:
|
|
|
|
setText(tree, id, text)
|
|
|
|
|
|
|
|
# remove HP mark if there is no hp
|
|
|
|
if "hp" not in properties:
|
|
|
|
removeElement(tree, "hpMark")
|
|
|
|
|
2017-07-13 23:21:58 -04:00
|
|
|
# remove setup box if there is no setup
|
|
|
|
if "setup" not in properties:
|
|
|
|
removeElement(tree, "setupBox")
|
|
|
|
|
2017-07-13 18:50:32 -04:00
|
|
|
if "art" in properties:
|
|
|
|
art = tree.find('.//*[@id="art"]')
|
|
|
|
if art is not None:
|
|
|
|
art.set("{http://www.w3.org/1999/xlink}href",
|
|
|
|
"file://" + bundle_dir + "/" + properties["art"])
|
|
|
|
return tree
|
|
|
|
|
|
|
|
def addCardToBase(svg, baseImg, baseX, cardNum):
|
2017-08-03 19:03:17 -04:00
|
|
|
for e in svg.findall('{http://www.w3.org/2000/svg}g'):
|
2017-08-05 13:06:18 -04:00
|
|
|
e.set("transform",
|
|
|
|
"{} translate({} {})".format(
|
|
|
|
e.get("transform", ""),
|
|
|
|
str((cardNum % baseX) * CARD_WIDTH),
|
|
|
|
str(int(cardNum / baseX) * CARD_HEIGHT)))
|
2017-08-03 19:03:17 -04:00
|
|
|
baseImg.getroot().append(svg.getroot())
|
2017-04-22 18:13:01 -04:00
|
|
|
|
2017-08-05 13:52:44 -04:00
|
|
|
def makeFace(baseImage, baseX, cardNum, deckType, cardFile, card):
|
|
|
|
path = os.path.join(bundle_dir, "images", deckType, cardFile)
|
|
|
|
fig = makeSVG(path, card)
|
2017-07-13 18:50:32 -04:00
|
|
|
addCardToBase(fig, baseImage, baseX, cardNum)
|
|
|
|
|
2017-04-22 18:13:01 -04:00
|
|
|
def makeFaces(deckJson, outfile):
|
2017-07-13 18:50:32 -04:00
|
|
|
baseX = math.ceil(math.sqrt(len(deckJson['deck']) + len(deckJson['character']) * 2))
|
2017-08-03 19:03:17 -04:00
|
|
|
baseImage = etree.ElementTree(
|
2017-08-05 13:06:18 -04:00
|
|
|
etree.Element('svg', attrib={'width': str(baseX * CARD_WIDTH) + "pt",
|
|
|
|
'height': str(baseX * CARD_HEIGHT) + "pt",
|
|
|
|
'version': "1.2",
|
|
|
|
'xmlns': "http://www.w3.org/2000/svg"}))
|
2017-07-13 18:50:32 -04:00
|
|
|
|
2017-08-05 13:52:44 -04:00
|
|
|
deckType = deckJson["type"]
|
2017-04-22 18:13:01 -04:00
|
|
|
|
|
|
|
cardNum = 0
|
2017-07-13 23:21:58 -04:00
|
|
|
# Make a card for each hero character card
|
2017-08-05 13:52:44 -04:00
|
|
|
if deckType == "hero":
|
2017-07-13 18:50:32 -04:00
|
|
|
for card in deckJson['character']:
|
2017-08-05 13:52:44 -04:00
|
|
|
makeFace(baseImage, baseX, cardNum, deckType, "charFront.svg", card)
|
2017-07-13 18:50:32 -04:00
|
|
|
cardNum += 1
|
2017-08-05 13:52:44 -04:00
|
|
|
makeFace(baseImage, baseX, cardNum, deckType, "charBack.svg", card)
|
2017-07-13 18:50:32 -04:00
|
|
|
cardNum += 1
|
2017-04-22 18:13:01 -04:00
|
|
|
|
2017-07-13 23:21:58 -04:00
|
|
|
# Make a character and instructions card for each villain card
|
2017-08-05 13:52:44 -04:00
|
|
|
elif deckType == "villain":
|
2017-07-13 23:21:58 -04:00
|
|
|
for card in deckJson['character']:
|
|
|
|
front = card["front"]
|
|
|
|
front["name"] = card["name"]
|
|
|
|
back = card["back"]
|
|
|
|
back["name"] = card["name"]
|
2017-08-05 13:52:44 -04:00
|
|
|
makeFace(baseImage, baseX, cardNum, deckType, "character.svg", front)
|
2017-07-13 23:21:58 -04:00
|
|
|
cardNum += 1
|
2017-08-05 13:52:44 -04:00
|
|
|
makeFace(baseImage, baseX, cardNum, deckType, "character.svg", back)
|
2017-07-13 23:21:58 -04:00
|
|
|
cardNum += 1
|
|
|
|
|
2017-08-05 13:52:44 -04:00
|
|
|
makeFace(baseImage, baseX, cardNum, deckType, "instructions.svg", front)
|
2017-07-13 23:21:58 -04:00
|
|
|
cardNum += 1
|
2017-08-05 13:52:44 -04:00
|
|
|
makeFace(baseImage, baseX, cardNum, deckType, "instructions.svg", back)
|
2017-07-13 23:21:58 -04:00
|
|
|
cardNum += 1
|
|
|
|
|
2017-07-13 18:50:32 -04:00
|
|
|
|
|
|
|
# Make a card for each card
|
2017-04-22 18:13:01 -04:00
|
|
|
for card in deckJson['deck']:
|
2017-08-05 13:52:44 -04:00
|
|
|
makeFace(baseImage, baseX, cardNum, deckType, "card.svg", card)
|
2017-04-22 18:13:01 -04:00
|
|
|
cardNum += 1
|
|
|
|
|
2017-08-03 19:03:17 -04:00
|
|
|
baseImage.write(outfile + ".svg")
|
2017-08-05 12:10:59 -04:00
|
|
|
command = ["inkscape", "-z",
|
|
|
|
"-f", outfile + ".svg",
|
|
|
|
"-w", str(baseX * CARD_WIDTH * 5),
|
|
|
|
"-e", outfile + ".png"]
|
|
|
|
print("To regenerate PNG after editing SVG, run:\n " + " ".join(command))
|
|
|
|
subprocess.run(command)
|
2017-07-13 18:50:32 -04:00
|
|
|
return baseX
|
2017-04-22 18:13:01 -04:00
|
|
|
|
2017-08-05 13:43:06 -04:00
|
|
|
def makeCardJson(nickname, description, cardID, outJson=None):
|
|
|
|
card = CARD_JSON_TEMPLATE.copy()
|
2017-07-13 23:21:58 -04:00
|
|
|
card.update({"Nickname": nickname,
|
2017-08-03 18:15:09 -04:00
|
|
|
"Description": description,
|
|
|
|
"CardID": cardID})
|
2017-08-05 13:38:26 -04:00
|
|
|
if outJson is not None:
|
|
|
|
outJson['ObjectStates'][0]['DeckIDs'].append(cardID)
|
|
|
|
outJson['ObjectStates'][0]['ContainedObjects'].append(card)
|
2017-07-13 23:21:58 -04:00
|
|
|
return card
|
|
|
|
|
2017-08-05 13:43:06 -04:00
|
|
|
def makeDoubleSidedCardJson(nickname, descriptionFront,
|
2017-08-05 13:38:26 -04:00
|
|
|
descriptionBack, cardID, outJson):
|
2017-08-05 13:43:06 -04:00
|
|
|
cardBack = makeCardJson(nickname, descriptionBack, cardID + 1)
|
2017-07-13 23:21:58 -04:00
|
|
|
|
2017-08-05 13:43:06 -04:00
|
|
|
card = makeCardJson(nickname, descriptionFront, cardID, outJson)
|
2017-08-05 13:38:26 -04:00
|
|
|
card.update({ "States": {"2": cardBack}})
|
2017-07-13 23:21:58 -04:00
|
|
|
return card
|
|
|
|
|
2017-07-13 18:50:32 -04:00
|
|
|
def makeJson(deckJson, imgWidth, outfile):
|
2017-04-27 19:53:23 -04:00
|
|
|
with open(bundle_dir + "/templates/deck.json") as f:
|
2017-04-22 18:13:01 -04:00
|
|
|
outJson = json.load(f)
|
|
|
|
|
|
|
|
# number of cards in x and y direction
|
|
|
|
outJson['ObjectStates'][0]['CustomDeck']['1'].update(
|
|
|
|
{"NumWidth": imgWidth,
|
2017-07-13 18:50:32 -04:00
|
|
|
"NumHeight": imgWidth,
|
2017-04-22 18:13:01 -04:00
|
|
|
#"FaceURL": "http://adam-desktop.dyn.wpi.edu:8000/" + outfile + ".png",
|
|
|
|
"FaceURL": "file://" + os.getcwd() + "/" + outfile + ".png",
|
|
|
|
"BackURL": "http://cloud-3.steamusercontent.com/ugc/156906385556221451/CE2C3AFE1759790CB0B532FFD636D05A99EC91F4/"})
|
|
|
|
|
|
|
|
# decks start at (10 * deck id)
|
|
|
|
cardNum = 100
|
2017-07-13 23:21:58 -04:00
|
|
|
|
|
|
|
if deckJson["type"] == "hero":
|
|
|
|
for card in deckJson['character']:
|
2017-08-05 13:38:26 -04:00
|
|
|
# character card
|
2017-08-05 13:43:06 -04:00
|
|
|
makeDoubleSidedCardJson(card['name'], "Active",
|
|
|
|
"Incapacitated", cardNum, outJson)
|
2017-07-13 23:21:58 -04:00
|
|
|
cardNum += 2
|
|
|
|
|
|
|
|
elif deckJson["type"] == "villain":
|
|
|
|
for card in deckJson['character']:
|
2017-08-05 13:38:26 -04:00
|
|
|
# character card
|
2017-08-05 13:43:06 -04:00
|
|
|
makeDoubleSidedCardJson(card['name'], "Front", "Back",
|
|
|
|
cardNum, outJson)
|
2017-07-13 23:21:58 -04:00
|
|
|
cardNum += 2
|
|
|
|
|
2017-08-05 13:38:26 -04:00
|
|
|
# instructions card
|
2017-08-05 13:43:06 -04:00
|
|
|
makeDoubleSidedCardJson(card['name'] + " instructions",
|
|
|
|
"Front", "Back", cardNum, outJson)
|
2017-07-13 23:21:58 -04:00
|
|
|
cardNum += 2
|
2017-04-22 18:13:01 -04:00
|
|
|
|
|
|
|
for card in deckJson['deck']:
|
2017-07-18 18:27:08 -04:00
|
|
|
for i in range(0, card.get('count', 1)):
|
2017-08-05 13:38:26 -04:00
|
|
|
# normal cards
|
2017-08-05 13:43:06 -04:00
|
|
|
makeCardJson(card['name'], card.get('keywords', ""),
|
|
|
|
cardNum, outJson)
|
2017-04-22 18:13:01 -04:00
|
|
|
cardNum += 1
|
|
|
|
|
|
|
|
with open(outfile + ".json", "w") as f:
|
|
|
|
json.dump(outJson, f)
|
|
|
|
|
2017-04-27 19:53:23 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) < 3:
|
|
|
|
print("not enough arguments!")
|
|
|
|
inputJson = input("Input file: ")
|
|
|
|
outfile = input("Output file (no suffix): ")
|
|
|
|
else:
|
|
|
|
inputJson = sys.argv[1]
|
|
|
|
outfile = sys.argv[2]
|
|
|
|
|
|
|
|
with open(inputJson) as f:
|
|
|
|
deckJson = json.load(f)
|
|
|
|
|
2017-07-13 18:50:32 -04:00
|
|
|
imgWidth = makeFaces(deckJson, outfile)
|
|
|
|
makeJson(deckJson, imgWidth, outfile)
|