Skip to content

Commit

Permalink
Some random cleanups while I'm there.
Browse files Browse the repository at this point in the history
  • Loading branch information
Senryoku committed Oct 19, 2023
1 parent 72f9473 commit 8a503b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
33 changes: 15 additions & 18 deletions src/BoosterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class ColorBalancedSlotCache {

constructor(cardPool: CardPool, options: Options = {}) {
const localGetCard = options.getCard ?? getCard;
for (const cid of cardPool.keys()) {
for (const [cid, count] of cardPool) {
if (!(localGetCard(cid).colors.join() in this.byColor))
this.byColor[localGetCard(cid).colors.join()] = new CardPool();
this.byColor[localGetCard(cid).colors.join()].set(cid, cardPool.get(cid) as number);
this.byColor[localGetCard(cid).colors.join()].set(cid, count);
}

this.monocolored = new CardPool();
Expand Down Expand Up @@ -242,9 +242,9 @@ function filterCardPool(slotedCardPool: SlotedCardPool, predicate: (cid: CardID)
for (const slot in slotedCardPool) {
specialCards[slot] = new CardPool();
filteredCardPool[slot] = new CardPool();
for (const cid of slotedCardPool[slot].keys()) {
if (predicate(cid)) specialCards[slot].set(cid, slotedCardPool[slot].get(cid) as number);
else filteredCardPool[slot].set(cid, slotedCardPool[slot].get(cid) as number);
for (const [cid, count] of slotedCardPool[slot]) {
if (predicate(cid)) specialCards[slot].set(cid, count);
else filteredCardPool[slot].set(cid, count);
}
}
return [specialCards, filteredCardPool];
Expand Down Expand Up @@ -532,13 +532,13 @@ class STXBoosterFactory extends BoosterFactory {
// Filter STA cards according to session collections
if (options.session && !options.session.unrestrictedCardPool()) {
const STACards: CardPool = options.session.restrictedCollection(["sta"]);
for (const cid of STACards.keys()) {
for (const [cid, count] of STACards) {
const card = getCard(cid);
// Remove Japanese versions
if (parseInt(card.collector_number) <= 63)
this.mysticalArchiveByRarity[card.rarity].set(
cid,
Math.min(options.maxDuplicates?.[card.rarity] ?? 99, STACards.get(cid) as number)
Math.min(options.maxDuplicates?.[card.rarity] ?? 99, count)
);
}
} else {
Expand Down Expand Up @@ -1012,10 +1012,10 @@ class YDMUBoosterFactory extends BoosterFactory {
if (options.session && !options.session.unrestrictedCardPool()) {
const YDMUCards: CardPool = options.session.restrictedCollection(["ydmu"]);
this.alchemyCards = { uncommon: new CardPool(), rare: new CardPool(), mythic: new CardPool() };
for (const cid of YDMUCards.keys())
for (const [cid, count] of YDMUCards)
this.alchemyCards[getCard(cid).rarity].set(
cid,
Math.min(options.maxDuplicates?.[getCard(cid).rarity] ?? 99, YDMUCards.get(cid) as number)
Math.min(options.maxDuplicates?.[getCard(cid).rarity] ?? 99, count)
);
} else {
this.alchemyCards = { uncommon: new CardPool(), rare: new CardPool(), mythic: new CardPool() };
Expand Down Expand Up @@ -1150,12 +1150,12 @@ class BROBoosterFactory extends BoosterFactory {

if (options.session && !options.session.unrestrictedCardPool()) {
const BRRCards: CardPool = options.session.restrictedCollection(["brr"]);
for (const cid of BRRCards.keys()) {
for (const [cid, count] of BRRCards) {
const card = getCard(cid);
if (parseInt(card.collector_number) <= 63)
this.retroArtifacts[card.rarity].set(
cid,
Math.min(options.maxDuplicates?.[card.rarity] ?? 99, BRRCards.get(cid) as number)
Math.min(options.maxDuplicates?.[card.rarity] ?? 99, count)
);
}
} else {
Expand Down Expand Up @@ -1391,12 +1391,12 @@ class MOMBoosterFactory extends BoosterFactory {

if (options.session && !options.session.unrestrictedCardPool()) {
const MULCards: CardPool = options.session.restrictedCollection(["mul"]);
for (const cid of MULCards.keys()) {
for (const [cid, count] of MULCards) {
const card = getCard(cid);
if (parseInt(card.collector_number) <= 65)
this.multiverseLegend[card.rarity].set(
cid,
Math.min(options.maxDuplicates?.[card.rarity] ?? 99, MULCards.get(cid) as number)
Math.min(options.maxDuplicates?.[card.rarity] ?? 99, count)
);
}
} else {
Expand Down Expand Up @@ -1626,13 +1626,10 @@ class WOEBoosterFactory extends BoosterFactory {
super(cardPool, landSlot, options);
if (options.session && !options.session.unrestrictedCardPool()) {
const WOTCards: CardPool = options.session.restrictedCollection(["wot"]);
for (const cid of WOTCards.keys()) {
for (const [cid, count] of WOTCards) {
const card = getCard(cid);
if (parseInt(card.collector_number) <= WOEBoosterFactory.MaxWOTCollectorNumber)
this.wotPool[card.rarity].set(
cid,
Math.min(options.maxDuplicates?.[card.rarity] ?? 99, WOTCards.get(cid) as number)
);
this.wotPool[card.rarity].set(cid, Math.min(options.maxDuplicates?.[card.rarity] ?? 99, count));
}
} else {
for (const cid of CardsBySet["wot"]) {
Expand Down
19 changes: 8 additions & 11 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export class Session implements IIndexable {
for (const s of sets)
if (s in CardsBySet)
for (const cid of CardsBySet[s].filter((cid) => cardPool.has(cid)))
restricted.set(cid, cardPool.get(cid) as number);
restricted.set(cid, cardPool.get(cid)!);
else console.error(`Session.restrictedCollection Error: '${s}' not in CardsBySet.`);
return restricted;
} else return cardPool;
Expand Down Expand Up @@ -561,13 +561,10 @@ export class Session implements IIndexable {

// Compute the minimum count of each remaining card
for (const c of intersection) {
collection.set(c, useCollection[0] ? (Connections[user_list[0]].collection.get(c) as number) : 4);
collection.set(c, useCollection[0] ? Connections[user_list[0]].collection.get(c)! : 4);
for (let i = 1; i < user_list.length; ++i)
if (useCollection[i])
collection.set(
c,
Math.min(collection.get(c) as number, Connections[user_list[i]].collection.get(c) as number)
);
collection.set(c, Math.min(collection.get(c)!, Connections[user_list[i]].collection.get(c)!));
}
return collection;
}
Expand Down Expand Up @@ -920,10 +917,10 @@ export class Session implements IIndexable {

// Add a new card to skipped pile. (Make sure there's enough cards for the player to draw if this is the last pile)
if (s.cardPool.length > 1 || (s.currentPile < 2 && s.cardPool.length > 0))
s.piles[s.currentPile].push(s.cardPool.pop() as UniqueCard);
s.piles[s.currentPile].push(s.cardPool.pop()!);
// Give a random card from the card pool if this was the last pile
if (s.currentPile === 2) {
const card = s.cardPool.pop() as UniqueCard;
const card = s.cardPool.pop()!;
Connections[s.currentPlayer()].pickedCards.main.push(card);
Connections[s.currentPlayer()].socket.emit("winstonDraftRandomCard", card);
this.draftLog?.users[s.currentPlayer()].picks.push({
Expand All @@ -948,7 +945,7 @@ export class Session implements IIndexable {
piles: [...s.piles.map((p, idx) => p.slice(0, idx < s.currentPile ? -1 : undefined).map((c) => c.id))],
});
Connections[s.currentPlayer()].pickedCards.main.push(...s.piles[s.currentPile]);
if (s.cardPool.length > 0) s.piles[s.currentPile] = [s.cardPool.pop() as UniqueCard];
if (s.cardPool.length > 0) s.piles[s.currentPile] = [s.cardPool.pop()!];
else s.piles[s.currentPile] = [];
this.winstonNextRound();
return true;
Expand Down Expand Up @@ -1250,8 +1247,8 @@ export class Session implements IIndexable {
// Column Row
const idx = choice < 3 ? 3 * i + choice : 3 * (choice - 3) + i;
if (s.boosters[0][idx] !== null) {
Connections[s.currentPlayer()].pickedCards.main.push(s.boosters[0][idx] as UniqueCard);
pickedCards.push(s.boosters[0][idx] as UniqueCard);
Connections[s.currentPlayer()].pickedCards.main.push(s.boosters[0][idx]!);
pickedCards.push(s.boosters[0][idx]!);
log.pick.push(idx);
s.boosters[0][idx] = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function pickCard(
);
if (candidates.length > 0) {
const tmpPool = new CardPool();
for (const cid of candidates) tmpPool.set(cid, cardPool.get(cid) as number);
for (const cid of candidates) tmpPool.set(cid, cardPool.get(cid)!);
cid = randomFunc(tmpPool);
}
}
Expand Down

0 comments on commit 8a503b4

Please sign in to comment.