Skip to content

Commit

Permalink
feat: update deckstrings for sideboard and Twist
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Aug 22, 2023
1 parent 4c48d04 commit 5fc6e2c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"babel-plugin-styled-components": "^1.10.7",
"clean-webpack-plugin": "^0.1.19",
"clipboard-polyfill": "^2.8.6",
"deckstrings": "^2.2.1",
"deckstrings": "^3.1.1",
"file-loader": "^2.0.0",
"hearthstonejson-client": "^1.4.0",
"html-webpack-include-sibling-chunks-plugin": "0.1.0",
Expand Down
5 changes: 0 additions & 5 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ declare module "*.otf" {
}

declare const APPLICATION_VERSION: string;

declare module "deckstrings" {
export function decode(deckString: string): object;
export function encode(deck: object): string;
}
38 changes: 36 additions & 2 deletions src/twitch-hdt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,43 @@ export interface BoardStateHand {
size: number;
}

export type BoardStateDeckCard = [number, number, number];
export type BoardStateDeckCard = [
/**
* dbf id
*/
number,
/**
* current count
*/
number,
/**
* initial count
*/
number,
];

export type SideboardDeckCard = [
/**
* owner dbf id
*/
number,
/**
* dbf id
*/
number,
/**
* current count
*/
number,
/**
* initial count
*/
number,
];

export interface BoardStateDeck {
cards?: BoardStateDeckCard[]; // [dbfId, current, initial]
cards?: BoardStateDeckCard[];
sideboard?: SideboardDeckCard[];
name?: string;
hero?: number;
format?: FormatType;
Expand All @@ -157,6 +190,7 @@ export const enum FormatType {
FT_WILD = 1,
FT_STANDARD = 2,
FT_CLASSIC = 3,
FT_TWIST = 4,
}

export const enum BnetGameType {
Expand Down
52 changes: 44 additions & 8 deletions src/utils/hearthstone.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { encode } from "deckstrings";
import { BoardStateDeckCard, FormatType } from "../twitch-hdt";
import { DeckDefinition, encode } from "deckstrings";
import {
BoardStateDeckCard,
FormatType,
SideboardDeckCard,
} from "../twitch-hdt";

export const getDeckToCopy = (
cardList: BoardStateDeckCard[],
sideboard: SideboardDeckCard[],
format: FormatType,
heroes: number[],
name?: string,
Expand All @@ -11,10 +16,9 @@ export const getDeckToCopy = (
return null;
}

const initialCards: [number, number][] = cardList
const initialCards: DeckDefinition["cards"] = cardList
.filter((card: BoardStateDeckCard) => {
const [dbfId, current, initial] = card;
return !!initial;
return !!card[2];
})
.map<[number, number]>((card: BoardStateDeckCard) => {
const [dbfId, current, initial] = card;
Expand All @@ -34,15 +38,40 @@ export const getDeckToCopy = (
},
[],
);
const deckDescription = {

const sideboardCards: DeckDefinition["sideboardCards"] = sideboard
.filter((card: SideboardDeckCard) => {
return !!card[3];
})
.map<[number, number, number]>((card: SideboardDeckCard) => {
const [owner, dbfId, current, initial] = card;
return [dbfId, initial, owner];
})
.reduce<[number, number, number][]>(
(result: [number, number, number][], card: [number, number, number]) => {
result = result.slice(0);
for (let i = 0; i < result.length; i++) {
if (result[i][0] === card[0] && result[i][2] === card[2]) {
result[i][1] += card[1];
return result;
}
}
// new card, append
return result.concat([card]);
},
[],
);

const deckDefinition: DeckDefinition = {
cards: initialCards,
sideboardCards,
format,
heroes,
};

let deckstring = null;
try {
deckstring = encode(deckDescription);
deckstring = encode(deckDefinition);
} catch (e) {
console.error(e);
return null;
Expand All @@ -54,13 +83,20 @@ export const getDeckToCopy = (

const isStandard = format === FormatType.FT_STANDARD;
const isClassic = format === FormatType.FT_CLASSIC;
const isTwist = format === FormatType.FT_TWIST;

return [
...(name ? [`### ${name}`] : []),
...(format
? [
`# Format: ${
isStandard ? "Standard" : isClassic ? "Classic" : "Wild"
isStandard
? "Standard"
: isTwist
? "Twist"
: isClassic
? "Classic"
: "Wild"
}`,
]
: []),
Expand Down
4 changes: 2 additions & 2 deletions src/viewer/CopyDeckButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ const CopyDeckButton: React.FC<Props> = ({
if (!deck) {
return null;
}
const { name, cards, format, hero } = deck;
const { name, cards, sideboard, format, hero } = deck;
if (!format || !cards || !hero) {
return null;
}
return getDeckToCopy(cards, format, [hero], name);
return getDeckToCopy(cards, sideboard || [], format, [hero], name);
}, [deck]);

const copy = useCallback(async () => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2513,10 +2513,10 @@ decamelize@^1.2.0:
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=

deckstrings@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/deckstrings/-/deckstrings-2.2.1.tgz#691fb8d8e19e77219cfd71ce56e38e1cd54d97a9"
integrity sha512-dwbSCKhmoPjT54Bs99PJB5Ur2F3vSwYKhkbiKXI2KoOmsc5orYSRfGAK10Qd9WAeUeFdPQ4PoUBrvtMuT7dveQ==
deckstrings@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/deckstrings/-/deckstrings-3.1.1.tgz#7cc60d97c9ddbd4bb22f3f1b608589cba119724b"
integrity sha512-FYrls5s83vE+y/fPVU5DjXUPh0aM77+fh52Rz/LWxZvS/fskWJ2z2JeyBTTV2y+CSq95MM/JexZeGCDYELtu6g==

decode-uri-component@^0.2.0:
version "0.2.0"
Expand Down

0 comments on commit 5fc6e2c

Please sign in to comment.