├── .gitignore
├── CategoryData.py
├── KeywordData.py
├── standard-format.json
├── gwent.py
├── README.md
├── CardData.py
├── LICENSE
└── GwentUtils.py
/.gitignore:
--------------------------------------------------------------------------------
1 | raw/*
2 |
3 | data_definitions.zip
4 | data_definitions/
5 | keywords*.json
6 | cards*.json
7 | categories*.json
8 |
9 | __pycache__/
10 | .DS_Store
11 |
--------------------------------------------------------------------------------
/CategoryData.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python3
2 | import GwentUtils
3 |
4 | def create_category_json(gwent_data_helper):
5 | categories = {}
6 | for locale in GwentUtils.LOCALES:
7 | categoriesByLocale = gwent_data_helper.categories[locale]
8 | for category_id in categoriesByLocale:
9 | text = categoriesByLocale[category_id]
10 | if categories.get(category_id) is None:
11 | categories[category_id] = {}
12 |
13 | categories[category_id][locale] = text
14 | return categories
15 |
--------------------------------------------------------------------------------
/KeywordData.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python3
2 | import GwentUtils
3 |
4 |
5 | def create_keyword_json(gwent_data_helper):
6 | keywords = {}
7 | for locale in GwentUtils.LOCALES:
8 | keywordsByLocale = gwent_data_helper.get_keyword_tooltips(locale)
9 | for keyword_id in keywordsByLocale:
10 | tooltip = keywordsByLocale[keyword_id]
11 | if keywords.get(keyword_id) is None:
12 | keywords[keyword_id] = {}
13 |
14 | keywords[keyword_id][locale] = {}
15 | keywords[keyword_id][locale]['raw'] = tooltip
16 | keywords[keyword_id][locale]['text'] = GwentUtils.clean_html(tooltip)
17 | return keywords
18 |
--------------------------------------------------------------------------------
/standard-format.json:
--------------------------------------------------------------------------------
1 | "122101": {
2 | "categories": [
3 | "Doomed",
4 | "Stubborn"
5 | ],
6 | "faction": "Northern Realms",
7 | "flavor": {
8 | "de-DE": "Ich war kein guter Vater, ich wei\u00df, aber ... vielleicht ist es noch nicht zu sp\u00e4t.",
9 | "en-US": "I've not been a good father, I know, but\u2026 perhaps it's not too late.",
10 | "etc.": "etc.",
11 | },
12 | "info": {
13 | "de-DE": "Generiere einen Fehlgeborenen oder einen T\u00f6lpelbold.",
14 | "en-US": "Spawn a Botchling or a Lubberkin.",
15 | "etc.": "etc.",
16 | },
17 | "infoRaw": {
18 | "de-DE": "Generiere einen Fehlgeborenen oder einen T\u00f6lpelbold.",
19 | "en-US": "Spawn a Botchling or a Lubberkin.",
20 | "etc.": "etc.",
21 | },
22 | "ingameId": "122101",
23 | "positions": [
24 | "Siege"
25 | ],
26 | "loyalties": [
27 | "Loyal"
28 | ],
29 | "name": {
30 | "de-DE": "Blutiger Baron",
31 | "en-US": "Bloody Baron",
32 | "etc.": "etc.",
33 | },
34 | "related": [
35 | "122401",
36 | "122402"
37 | ],
38 | "released": true,
39 | "strength": 6,
40 | "type": "Gold",
41 | "variations": {
42 | "12210100": {
43 | "art": {
44 | "artist": "Bart\u0142omiej Gawe\u0142",
45 | "high": "https://firebasestorage.googleapis.com/v0/b/gwent-9e62a.appspot.com/o/images%2F122101%2F12210100%2Fhigh.png?alt=media",
46 | "low": "https://firebasestorage.googleapis.com/v0/b/gwent-9e62a.appspot.com/o/images%2F122101%2F12210100%2Flow.png?alt=media",
47 | "medium": "https://firebasestorage.googleapis.com/v0/b/gwent-9e62a.appspot.com/o/images%2F122101%2F12210100%2Fmedium.png?alt=media",
48 | "original": "https://firebasestorage.googleapis.com/v0/b/gwent-9e62a.appspot.com/o/images%2F122101%2F12210100%2Foriginal.png?alt=media",
49 | "thumbnail": "https://firebasestorage.googleapis.com/v0/b/gwent-9e62a.appspot.com/o/images%2F122101%2F12210100%2Fthumbnail.png?alt=media"
50 | },
51 | "availability": "BaseSet",
52 | "collectible": true,
53 | "craft": {
54 | "premium": 1600,
55 | "standard": 800
56 | },
57 | "mill": {
58 | "premium": 800,
59 | "standard": 200
60 | },
61 | "rarity": "Legendary",
62 | "variationId": "12210100"
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/gwent.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python3
2 | import argparse
3 | import os
4 | import sys
5 | import GwentUtils
6 |
7 | from datetime import datetime
8 | import CardData
9 | import KeywordData
10 | import CategoryData
11 |
12 | parser = argparse.ArgumentParser(description="Transform the Gwent card data contained in xml files into a "
13 | "standardised JSON format. See README for more info.",
14 | epilog="Usage example:\n./master_xml.py ./pathToXML v0-9-10",
15 | formatter_class=argparse.RawTextHelpFormatter)
16 | parser.add_argument("inputFolder", help="unzipped data_definitions.zip. Folder containing the xml files.")
17 | parser.add_argument("-p", "--patch", help="Specifies the Gwent patch version. Used to create image urls.")
18 | parser.add_argument("-i", "--images", help="Base image url to use for card images. See README for more info.")
19 | parser.add_argument("-l", "--language", help="Includes just the translations for the selected language. Results in much smaller json files. Choose from: en-US, de-DE, es-ES, es-MX, fr-FR, it-IT, ja-JP, ko-KR, pl-PL, pt-BR, ru-RU, zh-CN, zh-TW")
20 | args = parser.parse_args()
21 | patch = args.patch
22 | rawFolder = args.inputFolder
23 | base_image_url = args.images
24 | locale = args.language
25 | if locale:
26 | GwentUtils.LOCALES = [locale]
27 | if not base_image_url:
28 | base_image_url = "https://firebasestorage.googleapis.com/v0/b/gwent-9e62a.appspot.com/o/images%2F{patch}%2F{cardId}%2F{variationId}%2F{size}.png?alt=media"
29 | if not patch:
30 | exit("Error: If you are not supplying an image url, you need to specify the patch name using --patch.\n"
31 | "This is because the default image url uses the patch name to generate the image url.\n"
32 | "See README for more info.")
33 | elif "{patch}" in base_image_url and not patch:
34 | exit("Your image url contains {patch} but you have not supplied a patch name using -p. See README for more info")
35 |
36 | # Add a backslash on the end if it doesn't exist.
37 | if rawFolder[-1] != "/":
38 | rawFolder = rawFolder + "/"
39 |
40 | if not os.path.isdir(rawFolder):
41 | print(rawFolder + " is not a valid directory")
42 | exit()
43 |
44 | gwentDataHelper = GwentUtils.GwentDataHelper(rawFolder)
45 |
46 | # Save under v0-9-10_2017-09-05.json if the script is ran on 5 September 2017 with patch v0-9-10.
47 | BASE_FILENAME = patch + "_" + datetime.utcnow().strftime("%Y-%m-%d") + ".json"
48 |
49 | print("Creating keyword JSON...")
50 | keywordsJson = KeywordData.create_keyword_json(gwentDataHelper)
51 | filename = "keywords_" + BASE_FILENAME
52 | filepath = os.path.join(rawFolder + "../" + filename)
53 | GwentUtils.save_json(filepath, keywordsJson)
54 |
55 | print("Creating categories JSON...")
56 | categoriesJson = CategoryData.create_category_json(gwentDataHelper)
57 | filename = "categories_" + BASE_FILENAME
58 | filepath = os.path.join(rawFolder + "../" + filename)
59 | GwentUtils.save_json(filepath, categoriesJson)
60 |
61 | print("Creating card data JSON...")
62 | cardsJson = CardData.create_card_json(gwentDataHelper, patch, base_image_url)
63 | filename = "cards_" + BASE_FILENAME
64 | filepath = os.path.join(rawFolder + "../" + filename)
65 | print("Found %s cards." % (len(cardsJson)))
66 | GwentUtils.save_json(filepath, cardsJson)
67 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Gwent Data
2 | This project contains scripts that transforms the Gwent card data contained in xml files into a nice json format that you can use in your Gwent projects.
3 |
4 | # DEPRECATED
5 | This project is no longer actively maintained. I haven't played Gwent in a long time and have finally lost interest in maintaining my own personal Gwent project that uses gwent-data. Thank you for your support since the closed beta.
6 |
7 | ## Usage
8 | 1. Find and unzip "Path\to\Gwent\GWENT_Data\StreamingAssets\data_definitions". It's a zip file, even if your OS doesn't recognise it as such.
9 | 2. Unzip data_definitions.zip e.g. `unzip data_definitions.zip -d data_definitions`
10 | 3. `gwent-data` uses the Gwent patch version name to generate urls for the card images. Therefore, if you are not supplying your own image url, you'll need to get the latest patch name. Open GOG to find the name of the latest Gwent version e.g. `v1.2.1`
11 | 4. Run gwent.py, passing in the data_definitions directory and the patch version name.
12 | e.g. `python3 gwent.py data_definitions/ -p v1.2.1`
13 | 5. Make sure your project conforms to the [Gwent Fan Content Guidelines](https://www.playgwent.com/en/fan-content).
14 |
15 | ### (Optional) Using your own card images
16 | When a Gwent update is released, CDPR sends me a zip file with the new card images. I then run a script to upload them to a Google Cloud Storage bucket so they can be used in `gwent-data`. It usually takes a couple of days after the update for CDPR to send me the images and for me to upload them. Sometimes I am away when an update is released and I am unable to upload card images for an extended period of time. Therefore you may want to host your own card images. CDPR will supply them to you if you message Burza with some details on your Gwent project.
17 |
18 | You can use the `-i` or `--images` option to specify your own image url. If you pass in a url with placeholder strings, `gwent-data` will replace them with the correct values.
19 |
20 | E.g.
21 |
22 | ```
23 | python3 gwent.py data_definitions/ -i www.example.com/{cardId}.png
24 | ```
25 |
26 | This will correctly replace `{cardId}` with the correct value for each card.
27 |
28 | Here are all the placeholders you can use:
29 |
30 | | Placeholder | Replaced By | Notes |
31 | |-----------------|-------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
32 | | `{patch}` | The Gwent patch version name | If you use this in your image url, you must also supply a patch name using the `-p` argument. E.g. `python3 gwent.py data_definitions/ -i www.example.com/{patch}/{cardId}.png -p v1.2.1` |
33 | | `{cardId}` | The card's ingame id | |
34 | | `{variationId}` | The id of the variation of the card | Variations are an artifact of the old way Gwent stored card data. Currently all cards have 1 variation. |
35 | | `{size}` | The size of the image | Possible values: `original`, `high`, `medium`, `low`, `thumbnail` |
36 | | `{artId}` | The id of this card image | Each card image has an art id that is different to the card id. In the future, cards may have more than 1 card art. |
37 |
--------------------------------------------------------------------------------
/CardData.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python3
2 | import re
3 | import GwentUtils
4 |
5 | IMAGE_SIZES = ['original', 'high', 'medium', 'low', 'thumbnail']
6 |
7 | """
8 | Constants from Gwent Client.
9 | """
10 | COMMON = 1
11 | RARE = 2
12 | EPIC = 4
13 | LEGENDARY = 8
14 |
15 | LEADER = 1
16 | BRONZE = 2
17 | SILVER = 4
18 | GOLD = 8
19 |
20 | NEUTRAL = 1
21 | MONSTER = 2
22 | NILFGAARD = 4
23 | NORTHERN_REALMS = 8
24 | SCOIATAEL = 16
25 | SKELLIGE = 32
26 | SYNDICATE = 64
27 |
28 | TYPE_LEADER = 1
29 | TYPE_SPELL = 2
30 | TYPE_UNIT = 4
31 | TYPE_ARTIFACT = 8
32 | TYPE_STRATEGEM = 16
33 |
34 | """
35 | Mill and Crafting values for each rarity.
36 | """
37 | CRAFT_VALUES = {}
38 | CRAFT_VALUES[COMMON] = {"standard": 30, "premium": 200, "upgrade": 100}
39 | CRAFT_VALUES[RARE] = {"standard": 80, "premium": 400, "upgrade": 200}
40 | CRAFT_VALUES[EPIC] = {"standard": 200, "premium": 800, "upgrade": 300}
41 | CRAFT_VALUES[LEGENDARY] = {"standard": 800, "premium": 1600, "upgrade": 400}
42 |
43 | MILL_VALUES = {}
44 | MILL_VALUES[COMMON] = {"standard": 10, "premium": 10, "upgrade": 20}
45 | MILL_VALUES[RARE] = {"standard": 20, "premium": 20, "upgrade": 50}
46 | MILL_VALUES[EPIC] = {"standard": 50, "premium": 50, "upgrade": 80}
47 | MILL_VALUES[LEGENDARY] = {"standard": 200, "premium": 200, "upgrade": 120}
48 |
49 | """
50 | Gwent Client ID -> Gwent Data ID mapping.
51 | """
52 | RARITIES = { COMMON: "Common", RARE: "Rare", EPIC: "Epic", LEGENDARY: "Legendary"}
53 | TIERS = { LEADER: "Leader", BRONZE: "Bronze", SILVER: "Silver", GOLD: "Gold"}
54 | FACTIONS = { NEUTRAL: "Neutral", MONSTER: "Monster", NILFGAARD: "Nilfgaard",
55 | NORTHERN_REALMS: "Northern Realms", SCOIATAEL: "Scoiatael", SKELLIGE: "Skellige", SYNDICATE: "Syndicate"}
56 | TYPES = { TYPE_LEADER: "Leader", TYPE_SPELL: "Spell", TYPE_UNIT: "Unit", TYPE_ARTIFACT: "Artifact", TYPE_STRATEGEM: "Strategem"}
57 |
58 | """
59 | Gwent Card Sets
60 | """
61 | TOKEN_SET = 0
62 | BASE_SET = 1
63 | TUTORIAL_SET = 2
64 | THRONEBREAKER_SET = 3
65 | UNMILLABLE_SET = 10
66 | CRIMSONCURSE_SET = 11
67 | NOVIGRAD_SET = 12
68 | IRON_JUDGEMENT_SET = 13
69 | MERCHANTS_OF_OFIR_SET = 14
70 |
71 | CARD_SETS = {
72 | TOKEN_SET: "NonOwnable",
73 | BASE_SET: "BaseSet",
74 | TUTORIAL_SET: "Tutorial",
75 | THRONEBREAKER_SET: "Thronebreaker",
76 | UNMILLABLE_SET: "Unmillable",
77 | CRIMSONCURSE_SET: "CrimsonCurse",
78 | NOVIGRAD_SET: "Novigrad",
79 | IRON_JUDGEMENT_SET: "IronJudgement",
80 | MERCHANTS_OF_OFIR_SET: "MerchantsOfOfir",
81 | }
82 |
83 | # Gaunter's 'Higher than 5' and 'Lower than 5' are not actually cards.
84 | INVALID_TOKENS = ['200175', '200176']
85 |
86 | def create_card_json(gwent_data_helper, patch, base_image_url):
87 | cards = {}
88 |
89 | card_templates = gwent_data_helper.card_templates
90 | for template_id in card_templates:
91 | template = card_templates[template_id]
92 | card = {}
93 | card_id = template.attrib['Id']
94 | card['ingameId'] = card_id
95 | card['strength'] = int(template.find('Power').text)
96 | tier = int(template.find('Tier').text)
97 | card['type'] = TIERS.get(tier)
98 | card_type = int(template.find('Type').text)
99 | card['cardType'] = TYPES.get(card_type)
100 | card['faction'] = FACTIONS.get(int(template.find('FactionId').text))
101 | secondaryFaction = template.find('SecondaryFactionId')
102 | if secondaryFaction != None and int(secondaryFaction.text) in FACTIONS:
103 | card['secondaryFaction'] = FACTIONS.get(int(secondaryFaction.text))
104 | card['provision'] = int(template.find('Provision').text)
105 | if (tier == LEADER):
106 | # Mulligan values are the same for every leader now.
107 | card['mulligans'] = 0
108 | card['provisionBoost'] = int(template.find('Provision').text)
109 |
110 | maxRange = int(template.find('MaxRange').text)
111 | if (maxRange > -1):
112 | card['reach'] = maxRange
113 |
114 | card['name'] = {}
115 | card['flavor'] = {}
116 | for region in GwentUtils.LOCALES:
117 | card['name'][region] = gwent_data_helper.card_names.get(region).get(card_id)
118 | card['flavor'][region] = gwent_data_helper.flavor_strings.get(region).get(card_id)
119 |
120 | # False by default, will be set to true if collectible or is a token of a released card.
121 | card['released'] = False
122 |
123 | # Tooltips
124 | card['info'] = {}
125 | card['infoRaw'] = {}
126 | for locale in GwentUtils.LOCALES:
127 | tooltip = gwent_data_helper.tooltips[locale].get(card_id)
128 | if tooltip is not None:
129 | card['infoRaw'][locale] = tooltip
130 | card['info'][locale] = GwentUtils.clean_html(tooltip)
131 |
132 | # Keywords.
133 | card['keywords'] = gwent_data_helper.keywords.get(card_id)
134 |
135 | # Units no longer have a row restriction.
136 | card['positions'] = ["Melee", "Ranged", "Siege"]
137 |
138 | # Loyalty
139 | card['loyalties'] = []
140 | placement = template.find('Placement')
141 | if placement.attrib['PlayerSide'] != "0":
142 | card['loyalties'].append("Loyal")
143 | if placement.attrib['OpponentSide'] != "0":
144 | card['loyalties'].append("Disloyal")
145 |
146 | # Categories
147 | card['categories'] = []
148 | card['categoryIds'] = []
149 |
150 | # There are 2 category nodes
151 | for node in ["PrimaryCategory", "Categories"]:
152 | for multiplier in range(2):
153 | # e0, e1
154 | e = "e{0}".format(multiplier)
155 | categories_sum = int(template.find(node).find(e).attrib['V'])
156 | for category, bit in enumerate("{0:b}".format(categories_sum)[::-1]):
157 | if bit == '1':
158 | # e1 categories are off by 64.
159 | adjusted_category = category + (64 * multiplier)
160 | card['categoryIds'].append("card_category_{0}".format(adjusted_category))
161 |
162 | categories_en_us = gwent_data_helper.categories["en-US"]
163 | for category_id in card['categoryIds']:
164 | if category_id in categories_en_us:
165 | card['categories'].append(categories_en_us[category_id])
166 |
167 | # Variations no longer exist in Gwent. To maintain backwards compatability, create 1 variation.
168 | card['variations'] = {}
169 | variation = {}
170 | variation_id = card_id + "00" # Old variation id format.
171 |
172 | availability = int(template.attrib['Availability'])
173 |
174 | variation['variationId'] = variation_id
175 |
176 | variation['availability'] = CARD_SETS[availability]
177 | collectible = availability in {BASE_SET, THRONEBREAKER_SET, UNMILLABLE_SET, CRIMSONCURSE_SET, NOVIGRAD_SET, IRON_JUDGEMENT_SET, MERCHANTS_OF_OFIR_SET}
178 | variation['collectible'] = collectible
179 |
180 | # If a card is collectible, we know it has been released.
181 | # Mark Tactical Advantage as released.
182 | if collectible or card_id == "202140":
183 | card['released'] = True
184 |
185 | rarity = int(template.find('Rarity').text)
186 | variation['rarity'] = RARITIES.get(rarity)
187 |
188 | variation['craft'] = CRAFT_VALUES.get(rarity)
189 | variation['mill'] = MILL_VALUES.get(rarity)
190 |
191 | art = {}
192 | art_id = template.attrib.get('ArtId')
193 | if art_id != None:
194 | art['ingameArtId'] = art_id
195 |
196 | if collectible or card_id == "202140": # Get card art for Tactical Advantage
197 | for image_size in IMAGE_SIZES:
198 | art[image_size] = base_image_url.replace("{patch}", patch) \
199 | .replace("{cardId}", card_id) \
200 | .replace("{variationId}", variation_id) \
201 | .replace("{size}", image_size) \
202 | .replace("{artId}", art_id)
203 |
204 | variation['art'] = art
205 |
206 | card['variations'][variation_id] = variation
207 | artist = gwent_data_helper.artists.get(art_id)
208 | if artist != None:
209 | card['artist'] = artist
210 |
211 | # Add all token cards to the 'related' list.
212 | tokens = gwent_data_helper.tokens.get(card_id)
213 | card['related'] = tokens
214 |
215 | armor = gwent_data_helper.armor.get(card_id)
216 | if armor != None and TYPES.get(card_type) == "Unit":
217 | card['armor'] = int(armor)
218 |
219 | cards[card_id] = card
220 |
221 | # Check tokens are correctly marked as released.
222 | for card_id in cards:
223 | card = cards[card_id]
224 | if card['released'] and card.get('related') is not None:
225 | for token_id in card.get('related'):
226 | if token_id in cards:
227 | cards[token_id]['released'] = token_id not in INVALID_TOKENS
228 |
229 | # Remove any unreleased cards
230 | for card_id, card in list(cards.items()):
231 | if not card['released']:
232 | del cards[card_id]
233 |
234 | return cards
235 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/GwentUtils.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python3
2 | import json
3 | import re
4 | import os
5 |
6 | import xml.etree.ElementTree as xml
7 | from pprint import pprint
8 |
9 | LOCALES = ["en-US", "de-DE", "es-ES", "es-MX", "fr-FR", "it-IT", "ja-JP", "ko-KR", "pl-PL", "pt-BR", "ru-RU", "zh-CN", "zh-TW"]
10 | LOCALISATION_FILE_NAMES = {
11 | "en-US": "Localization/en-us.csv",
12 | "de-DE": "Localization/de-de.csv",
13 | "es-ES": "Localization/es-es.csv",
14 | "es-MX": "Localization/es-mx.csv",
15 | "fr-FR": "Localization/fr-fr.csv",
16 | "it-IT": "Localization/it-it.csv",
17 | "ja-JP": "Localization/ja-jp.csv",
18 | "ko-KR": "Localization/ko-kr.csv",
19 | "pl-PL": "Localization/pl-pl.csv",
20 | "pt-BR": "Localization/pt-br.csv",
21 | "ru-RU": "Localization/ru-ru.csv",
22 | "zh-CN": "Localization/zh-cn.csv",
23 | "zh-TW": "Localization/zh-tw.csv"
24 | }
25 |
26 | def save_json(filepath, data):
27 | print("Saved JSON to: %s" % filepath)
28 | with open(filepath, "w", encoding="utf-8", newline="\n") as f:
29 | json.dump(data, f, sort_keys=True, indent=2, separators=(',', ': '))
30 |
31 |
32 | def clean_html(raw_html):
33 | cleanr = re.compile('<.*?>')
34 | cleantext = re.sub(cleanr, '', raw_html)
35 | return cleantext
36 |
37 |
38 | def _is_token_valid(token, tooltips):
39 | if token is not None and token.find('Tooltip') is not None:
40 | valid = True
41 | for locale in LOCALES:
42 | tooltip = tooltips[locale].get(token.find('Tooltip').attrib['key'])
43 | if tooltip is None or tooltip == '':
44 | valid = False
45 | return valid
46 | else:
47 | return False
48 |
49 |
50 | def _get_evaluated_tooltips(raw_tooltips, card_names, card_abilities, card_templates):
51 | # Generate complete tooltips from the raw_tooltips and accompanying data.
52 | tooltips = {}
53 | for card_id in raw_tooltips:
54 |
55 | # Some cards don't have info.
56 | if raw_tooltips.get(card_id) is None or raw_tooltips.get(card_id) == "":
57 | tooltips[card_id] = ""
58 | continue
59 |
60 | # Set tooltip to be the raw tooltip string.
61 | tooltips[card_id] = raw_tooltips[card_id]
62 |
63 | template = card_templates[card_id]
64 | # First replace the MaxRange placeholder
65 | result = re.findall(r'.*?(\{Card\.MaxRange\}).*?', tooltips[card_id])
66 | for key in result:
67 | value = template.find('MaxRange').text
68 | tooltips[card_id] = tooltips[card_id].replace(key, value)
69 |
70 | # Replace provision cost placeholder
71 | result = re.findall(r'.*?(\{Template\.Provision\}).*?', tooltips[card_id])
72 | for key in result:
73 | value = template.find('Provision').text
74 | tooltips[card_id] = tooltips[card_id].replace(key, value)
75 |
76 | # https://github.com/GwentCommunityDevelopers/gwent-data/issues/38
77 | tooltips[card_id] = tooltips[card_id].replace("-B.P.BB_Hoard", "")
78 | tooltips[card_id] = tooltips[card_id].replace("Tribute-B.P.BB_Tribute", "Tribute")
79 |
80 | # Now replace all the other card abilities.
81 | # Regex. Get all strings that lie between a '{' and '}'.
82 | result = re.findall(r'.*?\{(.*?)\}.*?', tooltips[card_id])
83 | for key in result:
84 | ability_value = _get_card_ability_value(card_abilities, card_id, key)
85 | if ability_value is not None:
86 | tooltips[card_id] = tooltips[card_id].replace("{" + key + "}", ability_value)
87 |
88 | return tooltips
89 |
90 | def _get_card_ability_value(card_abilities, card_id, key):
91 | ability = card_abilities.get(card_id)
92 | if ability is None:
93 | return None
94 |
95 | lower_case_key = key.lower()
96 | ability_data = ability.find('PersistentVariables')
97 | if ability_data is not None:
98 | for value in ability_data:
99 | if value.attrib['Name'].lower() == lower_case_key:
100 | return value.attrib['V']
101 |
102 | ability_data = ability.find('TemporaryVariables')
103 | if ability_data is not None:
104 | for value in ability_data:
105 | if value.attrib['Name'].lower() == lower_case_key:
106 | return value.attrib['V']
107 |
108 | def _get_tokens(card_templates, card_abilities):
109 | tokens = {}
110 | for card_id in card_templates:
111 | tokens[card_id] = []
112 | ability = card_abilities.get(card_id)
113 | if ability is None:
114 | continue
115 | ability_data = ability.find('TemporaryVariables')
116 | if ability_data is not None:
117 | for value in ability_data.iter("V"):
118 | if value.attrib.get('Type') == "CardDefinition":
119 | token_id = value.attrib['TemplateId']
120 | if token_id not in tokens[card_id]:
121 | tokens[card_id].append(token_id)
122 |
123 | for child in value:
124 | if child.attrib.get('Type') == "CardDefinition":
125 | token_id = child.attrib['TemplateId']
126 | if token_id not in tokens[card_id]:
127 | tokens[card_id].append(token_id)
128 | return tokens
129 |
130 |
131 | def _get_keywords(tooltips):
132 | keywords_by_tooltip_id = {}
133 | for tooltip_id in tooltips:
134 | tooltip = tooltips[tooltip_id]
135 | keywords = []
136 | # Find all keywords in info string. E.g. find 'spawn' in ''
137 | # Can just use en-US here. It doesn't matter, all regions will return the same result.
138 | result = re.findall(r']+)>', tooltip)
139 | for key in result:
140 | if not key in keywords:
141 | keywords.append(key)
142 |
143 | keywords_by_tooltip_id[tooltip_id] = keywords
144 | return keywords_by_tooltip_id
145 |
146 |
147 | class GwentDataHelper:
148 | def __init__(self, raw_folder):
149 | self._folder = raw_folder
150 | self.card_templates = self.get_card_templates()
151 | raw_tooltips = {}
152 | self.card_names = {}
153 | self.flavor_strings = {}
154 | self.categories = {}
155 | for locale in LOCALES:
156 | raw_tooltips[locale] = self.get_card_tooltips(locale)
157 | self.card_names[locale] = self.get_card_names(locale)
158 | self.flavor_strings[locale] = self.get_flavor_strings(locale)
159 | self.categories[locale] = self.get_categories(locale)
160 | card_abilities = self.get_card_abilities()
161 |
162 | self.tooltips = {}
163 | for locale in LOCALES:
164 | self.tooltips[locale] = _get_evaluated_tooltips(raw_tooltips[locale], self.card_names[locale], card_abilities, self.card_templates)
165 |
166 | # Can use any locale here, all locales will return the same result.
167 | self.keywords = _get_keywords(self.tooltips[LOCALES[0]])
168 |
169 | self.tokens = _get_tokens(self.card_templates, card_abilities)
170 |
171 | self.artists = self.get_artists()
172 |
173 | self.armor = self.get_card_armor()
174 |
175 | def get_tooltips_file(self, locale):
176 | path = self._folder + LOCALISATION_FILE_NAMES[locale]
177 | if not os.path.isfile(path):
178 | print("Couldn't find " + locale + " tooltips at " + path)
179 | exit()
180 | return path
181 |
182 | def get_card_tooltips(self, locale):
183 | tooltips_file = open(self.get_tooltips_file(locale), "r", encoding="utf-8")
184 | tooltips = {}
185 | for line in tooltips_file:
186 | split = line.split(";", 1)
187 | if "tooltip" not in split[0]:
188 | continue
189 | tooltip_id = split[0].replace("_tooltip", "").replace("\"", "").lstrip("0")
190 |
191 | # Remove any weird tooltip ids e.g. 64_tooltip_lt
192 | if "_lt" in tooltip_id or "_sa" in tooltip_id or "_b" in tooltip_id or "card_in_maintenance" in tooltip_id:
193 | continue
194 |
195 | # Remove any quotation marks and new lines.
196 | tooltips[tooltip_id] = split[1].replace("\"\n", "").replace("\\n", "\n")
197 | tooltips_file.close()
198 | return tooltips
199 |
200 | def get_keyword_tooltips(self, locale):
201 | tooltips_file = open(self.get_tooltips_file(locale), "r", encoding="utf-8")
202 | keywords = {}
203 | for tooltip in tooltips_file:
204 | split = tooltip.split(";", 1)
205 | if "keyword" not in split[0]:
206 | continue
207 | keyword_id = split[0].replace("keyword_", "").replace("\"", "")
208 |
209 | keywords[keyword_id] = {}
210 | # Remove any quotation marks and new lines.
211 | keywords[keyword_id] = split[1].replace("\"", "").replace("\n", "")
212 |
213 | tooltips_file.close()
214 | return keywords
215 |
216 | def get_categories(self, locale):
217 | tooltips_file = open(self.get_tooltips_file(locale), "r", encoding="utf-8")
218 | categories = {}
219 | for line in tooltips_file:
220 | split = line.split(";", 1)
221 | if "category" not in split[0]:
222 | continue
223 | category_id = split[0]
224 |
225 | categories[category_id] = {}
226 | # Remove any quotation marks and new lines.
227 | categories[category_id] = split[1].replace("\"", "").replace("\n", "")
228 |
229 | tooltips_file.close()
230 | return categories
231 |
232 | def get_card_templates(self):
233 | path = self._folder + "Templates.xml"
234 | if not os.path.isfile(path):
235 | print("Couldn't find templates.xml at " + path)
236 | exit()
237 |
238 | card_templates = {}
239 |
240 | tree = xml.parse(path)
241 | root = tree.getroot()
242 |
243 | for template in root.iter('Template'):
244 | card_templates[template.attrib['Id']] = template
245 |
246 | return card_templates
247 |
248 | def get_artists(self):
249 | path = self._folder + "ArtDefinitions.xml"
250 | if not os.path.isfile(path):
251 | print("Couldn't find ArtDefinitions.xml at " + path)
252 | exit()
253 |
254 | artists = {}
255 |
256 | tree = xml.parse(path)
257 | root = tree.getroot()
258 |
259 | for art in root.iter('ArtDefinition'):
260 | art_id = art.attrib['ArtId']
261 | artist = art.get('ArtistName')
262 | if artist != None:
263 | artists[art_id] = artist
264 |
265 | return artists
266 |
267 | def get_card_abilities(self):
268 | path = self._folder + "Abilities.xml"
269 | if not os.path.isfile(path):
270 | print("Couldn't find abilities.xml at " + path)
271 | exit()
272 |
273 | abilities = {}
274 |
275 | tree = xml.parse(path)
276 | root = tree.getroot()
277 |
278 | for ability in root.iter('Ability'):
279 | if ability.attrib['Type'] == "CardAbility":
280 | card_id = ability.attrib['Template']
281 | abilities[card_id] = ability
282 |
283 | return abilities
284 |
285 | def get_card_armor(self):
286 | path = self._folder + "Templates.xml"
287 | if not os.path.isfile(path):
288 | print("Couldn't find templates.xml at " + path)
289 | exit()
290 |
291 | armor = {}
292 |
293 | tree = xml.parse(path)
294 | root = tree.getroot()
295 |
296 | for template in root.iter('Template'):
297 | armor_element = template.find('Armor')
298 |
299 | if armor_element != None:
300 | armor[template.attrib['Id']] = armor_element.text
301 |
302 | return armor
303 |
304 | def get_card_names(self, locale):
305 | card_name_file = open(self.get_card_names_file(locale), "r", encoding="utf8")
306 | card_names = {}
307 | for line in card_name_file:
308 | split = line.split(";", 1)
309 | if len(split) < 2:
310 | continue
311 | if "_name" in split[0]:
312 | name_id = split[0].replace("_name", "").replace("\"", "")
313 | # Remove any quotation marks and new lines.
314 | card_names[name_id] = split[1].replace("\"", "").replace("\n", "")
315 |
316 | card_name_file.close()
317 | return card_names
318 |
319 | def get_flavor_strings(self, locale):
320 | card_name_file = open(self.get_card_names_file(locale), "r", encoding="utf8")
321 | flavor_strings = {}
322 | for line in card_name_file:
323 | split = line.split(";", 1)
324 | if len(split) < 2:
325 | continue
326 | if "_fluff" in split[0]:
327 | flavor_id = split[0].replace("_fluff", "").replace("\"", "")
328 | # Remove any quotation marks and new lines.
329 | flavor_strings[flavor_id] = split[1].replace("\"", "").replace("\n", "")
330 |
331 | card_name_file.close()
332 | return flavor_strings
333 |
334 | def get_card_names_file(self, locale):
335 | path = self._folder + LOCALISATION_FILE_NAMES[locale]
336 | if not os.path.isfile(path):
337 | print("Couldn't find " + locale + " card file at " + path)
338 | exit()
339 |
340 | return path
341 |
--------------------------------------------------------------------------------