r/hearthstone • u/ziphion • Jun 03 '17
Discussion How to encode/decode deck codes
Hi guys,
Since posting my random deck builder yesterday, I've been getting a few questions on how to work with deck codes. I'm kind of a newbie programmer so it took a lot of work for me to figure it out myself, and so I thought I'd make a post explaining the process. I'll focus on encoding using Java in this post, since that's what I'm working with primarily, but decoding (and using other languages) should be easy to figure out if you know how to create the codes. Here we go!
First things first: check out this documentation from HearthSim. It's got an explanation of the deck code format, as well as links to actual modules in Python and Javascript for generating/decoding. I needed to implement them in Java in order to have them work with my app, but this was my starting place. Don't worry if you're confused after reading that; I'll go into more detail below.
So what is a deck code?
A deck code (or deckstring) is simply a string of bytes that have been encoded into Base64. The bytes themselves represent integers, each of which are encoded as "varints".
What is a varint, you ask? If you're a programmer, you may be already familiar with the primitive data types "int" and "long"; an "int" is an integer that always uses 4 bytes (so "30" would be encoded as 0x0000001E, where each "00" is one byte in hexidecimal), while a "long" is an integer that is too big for 4 bytes, so it always uses 8 bytes (so "30" would be encoded as 0x000000000000001E). A varint is simply a data type that uses exactly as many bytes as it needs (so "30" would just be 0x1E).
How do we make a deck code?
So we need a way to make varints. I was able to find a Java method here for encoding an integer as a varint, and I'm sure you can find similar functions for whichever language your using. I'll reproduce the code I used below.
We also need a full list of DBF IDs for all collectible cards and heroes. These are unique ID numbers that Hearthstone uses to identify pretty much every object in the game. Check out this site and click on cards.collectible.json. To get you started, here are the dbfIDs for each of the starting heroes:
- Malfurion: 274
- Rexxar: 31
- Jaina: 637
- Uther: 671
- Anduin: 813
- Valeera: 930
- Thrall: 1066
- Gul'Dan: 893
- Garrosh: 7
Finally, you need to separate all the cards in the deck into two piles: single-copy cards, and double-copy cards. In the below example, I've placed all cards with only one copy in the deck into an ArrayList called "deck1", and all cards with exactly two copies in the deck into "deck2". I also sorted the cards by DBF ID, though that's not entirely necessary.
Could you just show me the code you used?
Yep, here it is:
public static void writeVarInt(DataOutputStream dos, int value) throws IOException {
//taken from http://wiki.vg/Data_types, altered slightly
do {
byte temp = (byte) (value & 0b01111111);
// Note: >>> means that the sign bit is shifted with the rest of the
// number rather than being left alone
value >>>= 7;
if (value != 0) {
temp |= 0b10000000;
}
dos.writeByte(temp);
} while (value != 0);
}
// ...
ByteArrayOutputStream baos = null;
DataOutputStream dos = null;
try {
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
writeVarInt(dos, 0); // always zero
writeVarInt(dos, 1); // encoding version number
writeVarInt(dos, format); // standard = 2, wild = 1
writeVarInt(dos, 1); // number of heroes in heroes array, always 1
writeVarInt(dos, dbfHero); // DBF ID of hero
writeVarInt(dos, deck1.size()); // number of 1-quantity cards
for (int i = 0; i < deck1.size(); i++) {
writeVarInt(dos, deck1.get(i).dbfID);
}
writeVarInt(dos, deck2.size()); // number of 2-quantity cards
for (int i = 0; i < deck2.size(); i++) {
writeVarInt(dos, deck2.get(i).dbfID);
}
writeVarInt(dos, 0); //the number of cards that have quantity greater than 2. Always 0 for constructed
dos.flush(); //flushes the output stream to the byte array output stream
if (baos != null)
baos.close();
if (dos != null)
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
String deckString = Base64.getEncoder().encodeToString(baos.toByteArray()); //encode the byte array to a base64 string
return deckString;Note that you can add lines to the deck code that begin with # and the Hearthstone client will ignore those lines, like comments. However, if you precede a line with ###, the Hearthstone client will actually use whatever follows as the name of the deck.
Decoding a deck code is pretty much the opposite of encoding: go through the string with a "readVarInt" function/method and interpret the results using the structure I laid out above. If you run into trouble, go back and consult HearthSim's page, or comment below with your questions.
Enjoy!
-Ziphion
33
u/_Journey_ Jun 03 '17
9
u/essayelynch Jun 04 '17
Since you're a self-proclaimed newbie, I hope a bit of advice is well received from someone who isn't ;)
You should look into autoclosables for your I/O streams... https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
...and collection streams for looping through the cards in the deck. https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
1
u/FlaivoZ Jul 16 '17
I'm trying to do something similar but in pure javascript. I've converted all the dbfid in varint (so I will not give the raw int to the function but directly the final varint to the function), then can I build a string with 1x cards and 2x cards and then convert it in Base64?
1
u/eduardo_a2j Sep 05 '17
I still stuck and I cannot understand how decode this deckcodes... For example, Each code start with "AAE", where the first character "A", means = 0, the second "A", the version always is 1, but my question is, if both characters are the same, why at decode this return two differents values???
3
u/Mya__ Jun 04 '17
Here is also someone who helped me learn how to manage bytestreams and varints in this context using psuedo code, for those like me who learn best that way.
https://www.reddit.com/r/hearthstone/comments/6eumkt/so_i_tried_the_new_deck_sharing_feature/diews1s/?context=1
Also, here are some of the resources I used while learning this:
And I used this Binary Calculator to help with debugging.
3
u/Nordic_Marksman Jun 03 '17
Does this have any other uses than being able to use 3rd party apps to build deck lists? If not it's cool and a nice post, just seems to have very little practical use for this information.
1
u/eduardo_a2j Jul 02 '17
There is something that I don't understand... For this deck string:
AAECAR8GxwPJBLsFmQfZB/gIDI0B2AGoArUDhwSSBe0G6wfbCe0JgQr+DAA=
The byte number 4 is for hero, in this case "C"
How it is convert to Integer 31, wich is ID_card for Hunter??
Somebody would explain to me, please, like if was first grade.
1
u/0x75 Aug 15 '17
And the Hearthstone client was ready to pick data from the clipboard in that particular format ?
I guess that was reversed from the game, rather than official documentation but I am still checking the links.
1
u/[deleted] Sep 15 '17
I'm confused on how to read a deck string. You decode the base64 string and then you have an array of bytes which represent varints, but by definition, a varint can be any number of bytes. So how do you know how many bytes to read as being part of each number?
0
u/Phil_1988 Jun 04 '17
Wish I understood this, but I'm working with php and this is just gibberish to me. :/ I downloaded netbeans 8 and copied into it, to no avail. I added format and dbfHero variables, but the deck1 array is way too complicated to me. Not to mention I'm unable to call this method from the main.
0
u/[deleted] Jun 03 '17
[deleted] — view deleted comment