Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better bots #1701

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ You can debug this application by adding the following configuration to your `la
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run", "start-debug"
"run", "start-debug"
],
"port": 1338
}
Expand Down
79 changes: 74 additions & 5 deletions backend/player/bot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,70 @@
const {sample, pull, times} = require("lodash");
const { sample, pull, times, shuffle } = require("lodash");
const pileSort = require("pile-sort");

const RARITY_ORDER = ["Mythic", "Rare", "Uncommon", "Common", "Basic"];
function sortByRarity (cards) {
return shuffle(cards).sort((a, b) => {
return RARITY_ORDER.indexOf(a.rarity) - RARITY_ORDER.indexOf(b.rarity);
});
}

const decidingPicksNumber = 6;
function samplePick (pack, pool, myColors) {
if (pool.length === decidingPicksNumber) {
const colorCount = pool.reduce(
(acc, pack) => {
pack.colorIdentity.forEach(color => { acc[color] += 1; });
return acc;
},
{
W: 0,
U: 0,
B: 0,
R: 0,
G: 0
// TODO count colourless?
}
);

const countColors = Object.keys(colorCount).reduce(
(acc, color) => {
const count = colorCount[color];
if (!acc[count]) {
acc[count] = [];
}
acc[count].push(color);
return acc;
},
{}
);
const orderedColors = Object.values(countColors)
.reduce(
(acc, next) => {
return [...acc, ...shuffle(next)];
},
[]
);

myColors.clear();
orderedColors.slice(0, 2).forEach(color => myColors.add(color));
}

const [cardsInMyColors, cardsNotInMyColors] = pileSort(pack, [
card => {
return (
card.colorIdentity.length === 0 ||
card.colorIdentity.some(color => myColors.has(color))
// NOTE: this means if you if your colors are WU, you can find your bot
// picks a WG card (they match on W and are splashing?)
);
}
]);

const orderedPicks = sortByRarity(cardsInMyColors);
if (orderedPicks.length) return orderedPicks[0];

return sortByRarity(cardsNotInMyColors)[0];
}

const Player = require("./index");
const logger = require("../logger");
Expand All @@ -14,15 +80,18 @@ module.exports = class Bot extends Player {
this.gameId= gameId;
this.picksPerPack = picksPerPack;
this.burnsPerPack = burnsPerPack;

this.myColors = new Set(["W", "U", "B", "R", "G"]); // bot chooses 2 colors at some point
}

getPack(pack) {
const cardsToPick = Math.min(this.picksPerPack, pack.length);
times(cardsToPick, () => {
const randomPick = sample(pack);
logger.info(`GameID: ${this.gameId}, Bot, picked: ${randomPick.name}`);
this.picks.push(randomPick.name);
pull(pack, randomPick);
const card = samplePick(pack, this.pool, this.myColors);
logger.info(`GameID: ${this.gameId}, Bot, picked: ${card.name}`);
this.pool.push(card);
this.picks.push(card.name);
pull(pack, card);
});

// burn cards
Expand Down
4 changes: 2 additions & 2 deletions backend/player/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class Player extends EventEmitter {
picks: [],
burns: []
},
pool: [],
pool: [], // cards you have picks from packs
picks: [], // string names of picked cards
cap: {
packs: {}
},
picks: [],
draftLog: {
round : {},
pack: []
Expand Down
18 changes: 12 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"jsonfile": "^6.1.0",
"lodash": "^4.17.21",
"node-schedule": "^2.1.1",
"pile-sort": "^1.0.2",
"prop-types": "^15.8.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
Loading