From d8a55daf3fe21925d3b173f94e0d6b2f59c65a63 Mon Sep 17 00:00:00 2001 From: Nicolas Cantu Date: Tue, 3 Mar 2026 22:35:23 +0100 Subject: [PATCH] Refactor: Centralisation des constantes de configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Motivations:** - Éviter la duplication de code pour la lecture des configurations - Centraliser les valeurs par défaut **Root causes:** - Code dupliqué dans bot-zoo.js et trade.js **Correctifs:** - N/A **Evolutions:** - Ajout de `getUpgradeMaxLevels` dans bot-zoo.js - Ajout de `getSaleListingDefaults` dans trade.js - Mise à jour de la documentation `centralisations-mutualisations.md` **Pages affectées:** - web/js/bot-zoo.js - web/js/trade.js - docs/features/centralisations-mutualisations.md --- .../centralisations-mutualisations.md | 5 +++++ web/js/bot-zoo.js | 20 +++++++++++++------ web/js/trade.js | 17 ++++++++++++---- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/docs/features/centralisations-mutualisations.md b/docs/features/centralisations-mutualisations.md index 37c2d30..2a10b1a 100644 --- a/docs/features/centralisations-mutualisations.md +++ b/docs/features/centralisations-mutualisations.md @@ -24,6 +24,11 @@ ### web/js/bot-zoo.js - **getNeighborColorWeights** : utilise `zeroAnimalWeights()` pour initialiser `out` au lieu de `Object.fromEntries(colorNames.map(...))`. +- **getUpgradeMaxLevels()** : retourne `{ plotMax, skillMax, truckMax }` depuis la config. Utilisé par `botDecideUpgrade` et `playerAutoDoOneUpgrade` pour éviter la répétition des trois lectures `GameConfig.Plot?.MaxLevel ?? 8`, etc. + +### web/js/trade.js + +- **getSaleListingDefaults()** : retourne `{ duration, price }` depuis `GameConfig.Sale` (ListingDurationSeconds, DefaultPrice). Utilisé par `addMatureBabyToSale` et `addReceptionAnimalToSale` pour éviter la duplication des deux lignes de config. ### web/js/auto-mode-profiles.js + bot-zoo.js (déjà documenté) diff --git a/web/js/bot-zoo.js b/web/js/bot-zoo.js index 0a6d5d9..ea3f0f8 100644 --- a/web/js/bot-zoo.js +++ b/web/js/bot-zoo.js @@ -15,6 +15,18 @@ import { getEffectiveProfileId, getProfileParams, LEGACY_PROFILE_TO_ID } from ". const PROFILE_OPTIONS = ["fast", "slow", "balanced"]; +/** + * Max levels for plot, conveyor/school, and truck from config. + * @returns {{ plotMax: number, skillMax: number, truckMax: number }} + */ +function getUpgradeMaxLevels() { + return { + plotMax: GameConfig.Plot?.MaxLevel ?? 8, + skillMax: GameConfig.Conveyor?.MaxLevel ?? 8, + truckMax: (GameConfig.Truck && GameConfig.Truck.MaxLevel) ?? 5, + }; +} + /** * @returns {import("./types.js").BotState} */ @@ -146,12 +158,10 @@ export function ensureBotState(zoo, isPlayer) { function botDecideUpgrade(state, zoo, opts) { const { b, rng, params } = opts; const { spendThreshold, upgradeChance } = params; + const { plotMax, skillMax, truckMax } = getUpgradeMaxLevels(); const plotCost = getPlotUpgradeCost(b.plotLevel); - const plotMax = GameConfig.Plot?.MaxLevel ?? 8; const skillCost = getSchoolUpgradeCost(b.conveyorLevel); - const skillMax = GameConfig.Conveyor?.MaxLevel ?? 8; const truckCost = getTruckUpgradeCost(b.truckLevel); - const truckMax = (GameConfig.Truck && GameConfig.Truck.MaxLevel) ?? 5; const canUpgradePlot = b.plotLevel < plotMax && b.coins >= plotCost * spendThreshold; const canUpgradeSkill = b.conveyorLevel < skillMax && b.coins >= skillCost * spendThreshold; const canUpgradeTruck = b.truckLevel < truckMax && b.coins >= truckCost * spendThreshold; @@ -287,12 +297,10 @@ const PLAYER_AUTO_MAX_INTERVAL = 28; */ function playerAutoDoOneUpgrade(state, params, rng) { const { spendThreshold, upgradeChance } = params; + const { plotMax, skillMax, truckMax } = getUpgradeMaxLevels(); const plotCost = getPlotUpgradeCost(state.plotLevel ?? 1); - const plotMax = GameConfig.Plot?.MaxLevel ?? 8; const skillCost = getConveyorUpgradeCost(state.conveyorLevel ?? 1); - const skillMax = GameConfig.Conveyor?.MaxLevel ?? 8; const truckCost = getTruckUpgradeCost(state.truckLevel ?? 1); - const truckMax = (GameConfig.Truck && GameConfig.Truck.MaxLevel) ?? 5; const canPlot = (state.plotLevel ?? 1) < plotMax && state.coins >= plotCost * spendThreshold; const canSkill = (state.conveyorLevel ?? 1) < skillMax && state.coins >= skillCost * spendThreshold; const canTruck = (state.truckLevel ?? 1) < truckMax && state.coins >= truckCost * spendThreshold; diff --git a/web/js/trade.js b/web/js/trade.js index e28c1fb..9b58130 100644 --- a/web/js/trade.js +++ b/web/js/trade.js @@ -6,6 +6,17 @@ import { getBlockKeysFromCell } from "./placement.js"; import { GameConfig } from "./config.js"; import { getReproductionScore } from "./reproduction.js"; +/** + * Default duration and price for a new sale listing from config. + * @returns {{ duration: number, price: number }} + */ +function getSaleListingDefaults() { + return { + duration: GameConfig.Sale?.ListingDurationSeconds ?? 3600, + price: GameConfig.Sale?.DefaultPrice ?? 50, + }; +} + /** * Put a mature baby from nursery on sale (phase 10). Removes it from pendingBabies and clears the nursery cell. * @param {import("./types.js").GameState} state @@ -27,8 +38,7 @@ export function addMatureBabyToSale(state, nurseryCellKey) { const cell = state.grid.cells[nurseryCellKey]; if (cell && cell.kind === "nursery") cell.tokenId = undefined; state.saleListings = state.saleListings ?? []; - const duration = GameConfig.Sale?.ListingDurationSeconds ?? 3600; - const price = GameConfig.Sale?.DefaultPrice ?? 50; + const { duration, price } = getSaleListingDefaults(); const listingId = `sale_${state.nextTokenId}`; state.nextTokenId += 1; state.saleListings.push({ @@ -63,8 +73,7 @@ export function addReceptionAnimalToSale(state, receptionCellKey) { const rec = receptionAnimals[idx]; state.receptionAnimals = receptionAnimals.filter((_, i) => i !== idx); state.saleListings = state.saleListings ?? []; - const duration = GameConfig.Sale?.ListingDurationSeconds ?? 3600; - const price = GameConfig.Sale?.DefaultPrice ?? 50; + const { duration, price } = getSaleListingDefaults(); const listingId = `sale_${state.nextTokenId}`; state.nextTokenId += 1; state.saleListings.push({