-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactoring and optimizations within the project
- Loading branch information
Showing
107 changed files
with
2,139 additions
and
317,608 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ data/* | |
!data/mws.json | ||
!data/scores.json | ||
node_modules | ||
public/lib | ||
frontend/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const {getCardByUuid, getSet} = require("./data"); | ||
const logger = require("./logger"); | ||
const boosterRules = require("../data/boosterRules.json"); | ||
const weighted = require("weighted"); | ||
const {sample, sampleSize, random, concat} = require("lodash"); | ||
|
||
const makeBoosterFromRules = (setCode) => { | ||
const set = getSet(setCode); | ||
if (!set) { | ||
throw new Error(`${setCode} does not exist`); | ||
} | ||
|
||
const setRules = boosterRules[setCode]; | ||
if (!setRules) { | ||
return getDefaultBooster(set); | ||
} | ||
|
||
try { | ||
const { boosters, totalWeight, sheets } = setRules; | ||
const boosterSheets = weighted( | ||
boosters.map(({sheets}) => sheets), | ||
boosters.map(({weight}) => weight), | ||
{total: totalWeight}); | ||
return Object.entries(boosterSheets) | ||
.flatMap(chooseCards(sheets)); | ||
} catch (error) { | ||
logger.error(`could not produce a booster of ${setCode}. Falling back to default booster. ${error.stack}`); | ||
return getDefaultBooster(set); | ||
} | ||
}; | ||
|
||
const getDefaultBooster = (set) => { | ||
let { Basic, Common, Uncommon, Rare, Mythic, size } = set; | ||
|
||
if (Mythic && !random(7)) | ||
Rare = Mythic; | ||
|
||
if (!Rare) { | ||
Rare = Uncommon; //In some sets rare didn't exist. So we replace them with uncommons | ||
} | ||
|
||
//make small sets draftable. | ||
if (size < 10) | ||
size = 10; | ||
|
||
const cardNames = concat( | ||
sampleSize(Common, size), | ||
sampleSize(Uncommon, 3), | ||
sampleSize(Rare, 1) | ||
); | ||
|
||
if (Basic) { | ||
cardNames.push(sample(Basic)); | ||
} | ||
|
||
return cardNames.map(getCardByUuid); | ||
}; | ||
|
||
const chooseCards = sheets => ([sheetCode, numberOfCardsToPick]) => { | ||
const sheet = sheets[sheetCode]; | ||
|
||
const randomCards = sheet.balance_colors | ||
? getRandomCardsWithColorBalance(sheet, numberOfCardsToPick) | ||
: getRandomCards(sheet, numberOfCardsToPick); | ||
|
||
return randomCards.map(toCard(sheetCode)); | ||
}; | ||
|
||
function getRandomCardsWithColorBalance({cardsByColor, cards}, numberOfCardsToPick) { | ||
const ret = new Set(); | ||
|
||
// Pick one card of each color | ||
["G", "U", "W", "B", "R"].forEach((color) => { | ||
ret.add(sample(cardsByColor[color])); | ||
}); | ||
|
||
const n = Object.keys(cards).length; | ||
const nums = { | ||
"W": cardsByColor["W"].length * numberOfCardsToPick - n, | ||
"B": cardsByColor["B"].length * numberOfCardsToPick - n, | ||
"U": cardsByColor["U"].length * numberOfCardsToPick - n, | ||
"R": cardsByColor["R"].length * numberOfCardsToPick - n, | ||
"G": cardsByColor["G"].length * numberOfCardsToPick - n, | ||
"c": (cardsByColor["c"] || []).length * numberOfCardsToPick, | ||
}; | ||
const total = (numberOfCardsToPick - 5) * n; | ||
while (ret.size < numberOfCardsToPick) { | ||
const randomColor = weighted.select(nums, { total }); | ||
ret.add(sample(cardsByColor[randomColor])); | ||
} | ||
return [...ret]; | ||
} | ||
|
||
function getRandomCards({cards, totalWeight: total}, numberOfCardsToPick) { | ||
const ret = new Set(); | ||
|
||
// Fast way to avoid duplicate | ||
while (ret.size < numberOfCardsToPick) { | ||
ret.add(weighted.select(cards, { total })); | ||
} | ||
|
||
return [...ret]; | ||
} | ||
|
||
const toCard = (sheetCode) => (uuid) => ({ | ||
...getCardByUuid(uuid), | ||
foil: /foil/.test(sheetCode) | ||
}); | ||
|
||
module.exports = makeBoosterFromRules; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const {describe, it} = require("mocha"); | ||
const assert = require("assert"); | ||
const boosterGenerator = require("./boosterGenerator"); | ||
const {range} = require("lodash"); | ||
|
||
describe("Acceptance tests for boosterGenerator function", () => { | ||
it("should create a MH1 booster", () => { | ||
const got = boosterGenerator("MH1"); | ||
assert(got.length > 10); | ||
got.forEach(card => assert(card.name != undefined)); | ||
}); | ||
it("should create a RNA booster", () => { | ||
const got = boosterGenerator("RNA"); | ||
got.forEach(card => assert(card.name != undefined)); | ||
}); | ||
it("should create tons of EMN booster", () => { | ||
range(1000).forEach(() => { | ||
const got = boosterGenerator("EMN"); | ||
assert(got.length > 10); | ||
got.forEach(card => assert(card.name != undefined)); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.