Skip to content

Commit

Permalink
Fix typeString ordering to reflect real cards
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaKretin committed Dec 16, 2018
1 parent fc20352 commit 879f261
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions dist/class/CardData.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface ICardDataNames {
typeString: string;
}
export declare class CardData {
private static generateTypeString;
readonly ot: number;
readonly alias: number;
readonly setcode: number;
Expand Down
31 changes: 28 additions & 3 deletions dist/class/CardData.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions lib/class/CardData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CardAttribute, CardCategory, CardLinkMarker, CardOT, CardRace, CardType } from "../module/enums";
import { setcodes } from "../module/setcodes";
import { translations } from "../module/translations";
import { Translation } from "./Translation";

export interface ICardDataRaw {
ot: number;
Expand Down Expand Up @@ -55,6 +56,32 @@ async function getSetcodeNames(setcode: number, lang: string): Promise<string[]>
}

export class CardData {
private static generateTypeString(type: number, race: number, trans: Translation): string {
// list of types to defer in order they should appear
const deferred = [CardType.TYPE_TUNER, CardType.TYPE_NORMAL, CardType.TYPE_EFFECT];
let i = 1;
const names = [];
const defNames: { [type: number]: string } = {};
while (i <= type) {
if ((i & type) === i) {
const name = trans.getType(i);
if (deferred.indexOf(i) > -1) {
defNames[i] = name;
} else {
names.push(name);
}
}
i = i * 2;
}
for (const def of deferred) {
if (def in defNames) {
names.push(defNames[def]);
}
}
return names
.join("/")
.replace(trans.getType(CardType.TYPE_MONSTER), getNames(race, v => trans.getRace(v)).join("|"));
}
public readonly ot: number;
public readonly alias: number;
public readonly setcode: number;
Expand Down Expand Up @@ -90,12 +117,7 @@ export class CardData {
race: getNames(this.race, v => trans.getRace(v)),
setcode: getSetcodeNames(this.setcode, lang),
type: getNames(this.type, v => trans.getType(v)),
typeString: getNames(this.type, v => trans.getType(v))
.join("/")
.replace(
trans.getType(CardType.TYPE_MONSTER),
getNames(this.race, v => trans.getRace(v)).join("|")
)
typeString: CardData.generateTypeString(this.type, this.race, trans)
};
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,21 @@ describe("Testing prices", function() {
expect(price.hi).to.be.a("number");
});
});
describe("Testing type string", function() {
it("Effect should come after Synchro", async function() {
const card = await index.getCard("Junk Warrior", "en");
expect(card.data.names.en.typeString).to.equal("Warrior/Synchro/Effect");
});
it("Normal should come after Tuner", async function() {
const card = await index.getCard("Tune Warrior", "en");
expect(card.data.names.en.typeString).to.equal("Warrior/Tuner/Normal");
});
it("Tuner should come after Synchro", async function() {
const card = await index.getCard("Coral Dragon", "en");
expect(card.data.names.en.typeString).to.equal("Dragon/Synchro/Tuner/Effect");
});
it("Nomi Tuner should be Special Summon/Tuner/Effect", async function() {
const card = await index.getCard("Blackwing - Gofu the Vague Shadow", "en");
expect(card.data.names.en.typeString).to.equal("Winged Beast/Special Summon/Tuner/Effect");
});
});

0 comments on commit 879f261

Please sign in to comment.