commit 78ba0bc69805ee3af483bf42e18412fa944d8133 Author: legonzaur Date: Thu Aug 1 11:54:34 2024 +0200 initial commit diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b8afc8f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cards-json"] + path = cards-json + url = git@git.legonzaur.fr:heronfief/cards-json.git diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a51561d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM docker.io/mongo:7 + +COPY ./mongo-init.js /docker-entrypoint-initdb.d/ +COPY ./cards-json /cards-json +CMD ["mongod", "--replSet", "rs0"] \ No newline at end of file diff --git a/cards-json b/cards-json new file mode 160000 index 0000000..78a7282 --- /dev/null +++ b/cards-json @@ -0,0 +1 @@ +Subproject commit 78a72827859f784583b120a515916e790e641f1d diff --git a/mongo-init.js b/mongo-init.js new file mode 100644 index 0000000..7a72dc3 --- /dev/null +++ b/mongo-init.js @@ -0,0 +1,153 @@ +rs.initiate(); + +const EffectType = { + CHAMPION_ACTION: "champion_action", + SUICIDE: "suicide", + FACTION_COMBO: "faction_combo", + + CHAMPION_AMOUNT: "champion_amount", + TOTAL_DAMAGE: "total_damage", + CARD_AMOUNT: "card_amount", + ACTION_AMOUNT: "action_amount", +}; + +const CardRole = { + PERSONAL: "personal", + MARKET: "market", + FIRE_GEM: "fire_gem", + + HUNTER: "hunter", + TRAVELER: "traveler", + CLERIC: "cleric", + FIGHTER: "fighter", + RANGER: "ranger", + THIEF: "thief", + WIZARD: "wizard", + + HALF_DEMON: "half-demon", + DWARF: "dwarf", + ELF: "elf", + OGRE: "ogre", + ORC: "orc", + SMALLFOLK: "smallfolk", +}; + +function createEffectCondition(json, cardUUID) { + const createdCondition = {}; + if (typeof json === "string") { + createdCondition.effectId = json; + if (json == EffectType.FACTION_COMBO) { + createdCondition.faction = cardUUID.faction; + } + createdCondition.amount = 1; + } else { + createdCondition.effectId = json.id; + createdCondition.amount = json.amount ?? 1; + createdCondition.cardId = json.card_id; + if (json.id == EffectType.FACTION_COMBO) { + createdCondition.faction = json.faction ?? cardUUID.faction; + } + } + return createdCondition; +} + +function createEffects(json, cardUUID) { + return json.map((e) => { + const createdEffect = { + card: cardUUID, + effect: e.effect, + amount: e.amount ?? 1, + subEffects: [], + times: e.times ?? 1, + per: e.per, + faction: e.faction, + }; + if (e.sub_effects) { + createdEffect.subEffects = createEffects(e.sub_effects, cardUUID); + } + + if (e.effect_type) { + createdEffect.condition = createEffectCondition(e.effect_type, cardUUID); + } + + if (e.mutex) { + createdEffect.mutex = e.mutex.toString(); + } + + const { insertedId } = db.effects.insertOne(createdEffect); + + return insertedId; + }); +} + +function createCard(json, pack, playerIndex) { + const createdCard = { + cardId: json.id, + role: json.role, + cardType: json.card_type, + faction: json.faction, + name: json.name, + pack: pack, + playerIndex, + effects: [], + }; + + if (json.cost) { + createdCard.cost = json.cost; + } + + if (json.defense) { + createdCard.defense = json.defense; + } + + if (json.guard !== undefined) { + createdCard.guard = json.guard; + } + + const { insertedId } = db.cards.insertOne(createdCard); + + if (json.effects) { + db.cards.updateOne( + { _id: insertedId }, + { $set: { effects: createEffects(json.effects, insertedId) } } + ); + } +} + +async function loadCards() { + const cardPath = "/cards-json"; + + const files = fs.readdirSync(cardPath).filter((e) => e.endsWith(".json")); + + const processed = await Promise.all( + files.map(async (f) => { + return [ + path.parse(f).name, + JSON.parse(fs.readFileSync(path.join(cardPath, f), "utf8")).cards, + ]; + }) + ); + + Object.entries(processed) + .filter((d) => d[1] && d[1][1]) + .forEach( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ([_, data]) => { + data[1].forEach((c) => { + if (c.role == CardRole.FIRE_GEM || c.role == CardRole.MARKET) { + for (let i = 0; i < (c.init_amount ?? 1); i++) { + createCard(c, data[0]); + } + } else { + for (let j = 0; j < 4; j++) { + for (let i = 0; i < (c.init_amount ?? 1); i++) { + createCard(c, data[0], j); + } + } + } + }); + } + ); +} + +loadCards();