Trying to parse a collection that has been extracted with foilers will contain unrecognized cards (e.g. ALT_ALIZE_B_NE_FOILER_C).
In that case, this exception will be thrown because the reference will not pass this regex validation:
const match = id.match(/^ALT_(\w+)_(A|B|P)_(\w{2})_(\d{1,2})_(C|R1|R2|U)(?:_(\d+))?$/)
if (!match) { throw "unrecognized card id" }
My proposal is that this type of card can be expected in a collection, but it shouldn't be encoded (thus, not decoded either), nor cause the encoding to fail.
I lack Typescript knowledge to work on it myself, but how I'd do it in another language would be dealing with custom exceptions:
class BaseDeckFmtException(Exception):
pass
class DeckEncodingException(BaseDeckFmtException):
pass
class RecoverableDeckEncodingException(DeckEncodingException):
pass
And then:
const match = id.match(/^ALT_(\w+)_(A|B|P)_(\w{2})_(\d{1,2})_(C|R1|R2|U)(?:_(\d+))?$/)
if (!match) {
const foilerMatch = id.match(/^ALT_(\w+)_B_NE_FOILER_(C|R|U)$/)
if (!!foilerMatch) {throw RecoverableDeckEncodingException("foiler card")}
throw DeckEncodingException("unrecognized card id")
}
However, that's where things get a little difficult because you need to reduce by one the value in .SetGroup.refs_count. I believe that with the current implementation of the library, it's not feasible to do. Basically because the encoded string is generated as the collection is parsed.
To mitigate this there are three workarounds that could be followed:
- Run an initial verification of the references. While the quick fix would have an impact on the performance as it'd mean applying two regex on a card, we could also store the card elements already separated and skip the second one when processing a card. My understanding is that this would be done here:
private static groupedBySet(refQtyList: Array<CardRefQty>): Array<Array<CardRefQty>> {
let groups = new Map<RefSetCode, Array<CardRefQty>>()
for (let rq of refQtyList) {
const code = new CardRefElements(rq.id).set_code
let g = groups.get(code)
if (!g) {
g = []
groups.set(code, g)
}
g.push(rq)
}
return Array.from(groups, ([_, v]) => v)
}
With this approach there wouldn't be a need to use custom exceptions as described above.
- The alternative would be to only write the whole encoding once everything has been parsed. This is a more complex and requires more memory. And whilst it would be the most flexible and would allow to catch the most errors and deal with them elegantly, we might be introducing a level of complexity that might simply not be necessary.
- Encode foilers alongside the rest of the collection.
As a fast workaround to this issue, I'd be inclined to use the first approach but with a quick if "_FOILER_" in rq: continue; but in the long run I think we could consider something more akin to storing a CardRefElements object instead of the card reference rq.
Trying to parse a collection that has been extracted with foilers will contain unrecognized cards (e.g. ALT_ALIZE_B_NE_FOILER_C).
In that case, this exception will be thrown because the reference will not pass this regex validation:
My proposal is that this type of card can be expected in a collection, but it shouldn't be encoded (thus, not decoded either), nor cause the encoding to fail.
I lack Typescript knowledge to work on it myself, but how I'd do it in another language would be dealing with custom exceptions:
And then:
However, that's where things get a little difficult because you need to reduce by one the value in
.SetGroup.refs_count. I believe that with the current implementation of the library, it's not feasible to do. Basically because the encoded string is generated as the collection is parsed.To mitigate this there are three workarounds that could be followed:
As a fast workaround to this issue, I'd be inclined to use the first approach but with a quick
if "_FOILER_" in rq: continue; but in the long run I think we could consider something more akin to storing aCardRefElementsobject instead of the card referencerq.