This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce deck builder for integration tests (#496)
* Add submodule for thronesdb-json-data. * Convert existing tests to use the deck builder. * Add deck builder helper class for tests * Update CircleCI config to support submodules. * Update player interaction to accept card pack
- Loading branch information
Showing
10 changed files
with
115 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "thronesdb-json-data"] | ||
path = thronesdb-json-data | ||
url = https://github.com/Alsciende/thronesdb-json-data.git |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
checkout: | ||
post: | ||
- git submodule sync | ||
- git submodule update --init | ||
machine: | ||
node: | ||
version: 6.1.0 | ||
|
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,17 @@ | ||
const CardUtil = { | ||
matchCardByNameAndPack(labelOrName) { | ||
var name = labelOrName; | ||
var pack; | ||
var match = labelOrName.match(/^(.*)\s\((.*)\)$/); | ||
if(match) { | ||
name = match[1]; | ||
pack = match[2]; | ||
} | ||
|
||
return function(cardData) { | ||
return cardData.name === name && (!pack || cardData.pack_code === pack); | ||
}; | ||
} | ||
}; | ||
|
||
module.exports = CardUtil; |
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,72 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const _ = require('underscore'); | ||
|
||
const {matchCardByNameAndPack} = require('./cardutil.js'); | ||
|
||
const PathToSubModulePacks = path.join(__dirname, '../../thronesdb-json-data/pack'); | ||
|
||
class DeckBuilder { | ||
constructor() { | ||
this.cards = this.loadCards(PathToSubModulePacks); | ||
} | ||
|
||
loadCards(directory) { | ||
var cards = {}; | ||
|
||
var jsonPacks = fs.readdirSync(directory).filter(file => file.endsWith('.json')); | ||
|
||
_.each(jsonPacks, file => { | ||
var cardsInPack = require(path.join(PathToSubModulePacks, file)); | ||
|
||
_.each(cardsInPack, card => { | ||
cards[card.code] = card; | ||
}); | ||
}); | ||
|
||
return cards; | ||
} | ||
|
||
buildDeck(faction, cardLabels) { | ||
var cardCounts = {}; | ||
_.each(cardLabels, label => { | ||
var cardData = this.getCard(label); | ||
if(cardCounts[cardData.code]) { | ||
cardCounts[cardData.code].count++; | ||
} else { | ||
cardCounts[cardData.code] = { | ||
count: 1, | ||
card: cardData | ||
}; | ||
} | ||
}); | ||
|
||
return { | ||
faction: { value: faction }, | ||
agenda: _.find(cardCounts, cardCount => cardCount.card.type_code === 'agenda'), | ||
drawCards: _.filter(cardCounts, cardCount => ['character', 'location', 'attachment', 'event'].includes(cardCount.card.type_code)), | ||
plotCards: _.filter(cardCounts, cardCount => cardCount.card.type_code === 'plot') | ||
}; | ||
} | ||
|
||
getCard(codeOrLabelOrName) { | ||
if(this.cards[codeOrLabelOrName]) { | ||
return this.cards[codeOrLabelOrName]; | ||
} | ||
|
||
var cardsByName = _.filter(this.cards, matchCardByNameAndPack(codeOrLabelOrName)); | ||
|
||
if(cardsByName.length === 0) { | ||
throw new Error(`Unable to find any card matching ${codeOrLabelOrName}`); | ||
} | ||
|
||
if(cardsByName.length > 1) { | ||
var matchingLabels = _.map(cardsByName, card => `${card.name} (${card.pack_code})`).join('\n'); | ||
throw new Error(`Multiple cards match the name ${codeOrLabelOrName}. Use one of these instead:\n${matchingLabels}`); | ||
} | ||
|
||
return cardsByName[0]; | ||
} | ||
} | ||
|
||
module.exports = DeckBuilder; |
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
Submodule thronesdb-json-data
added at
19ae33