diff options
Diffstat (limited to 'js/scripting-lang/web/src/api.js')
-rw-r--r-- | js/scripting-lang/web/src/api.js | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/js/scripting-lang/web/src/api.js b/js/scripting-lang/web/src/api.js new file mode 100644 index 0000000..cf43178 --- /dev/null +++ b/js/scripting-lang/web/src/api.js @@ -0,0 +1,183 @@ +// api.js +// API fetch logic + +/** + * Fetch a Pokémon by name from the PokéAPI + * @param {string} name + * @returns {Promise<object>} Pokémon data + */ +export async function fetchPokemon(name) { + const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${encodeURIComponent(name.toLowerCase())}`); + if (!res.ok) { + throw new Error('Pokémon not found'); + } + return await res.json(); +} + +/** + * Fetch a Pokémon species by name or ID from the PokéAPI + * @param {string|number} nameOrId + * @returns {Promise<object>} Pokémon species data + */ +export async function fetchPokemonSpecies(nameOrId) { + const res = await fetch(`https://pokeapi.co/api/v2/pokemon-species/${encodeURIComponent(nameOrId)}`); + if (!res.ok) { + throw new Error('Pokémon species not found'); + } + return await res.json(); +} + +/** + * Fetch an evolution chain by ID from the PokéAPI + * @param {number} id + * @returns {Promise<object>} Evolution chain data + */ +export async function fetchEvolutionChain(id) { + const res = await fetch(`https://pokeapi.co/api/v2/evolution-chain/${id}`); + if (!res.ok) { + throw new Error('Evolution chain not found'); + } + return await res.json(); +} + +/** + * Get evolution chain ID for a Pokémon + * @param {string|number} pokemonNameOrId + * @returns {Promise<number>} Evolution chain ID + */ +export async function getEvolutionChainId(pokemonNameOrId) { + try { + // First try to get the species data + const species = await fetchPokemonSpecies(pokemonNameOrId); + return species.evolution_chain.url.split('/').slice(-2, -1)[0]; + } catch (error) { + throw new Error(`Could not find evolution chain for ${pokemonNameOrId}: ${error.message}`); + } +} + +/** + * Fetch complete evolution data for a Pokémon + * @param {string|number} pokemonNameOrId + * @returns {Promise<object>} Complete evolution data + */ +export async function fetchEvolutionData(pokemonNameOrId) { + try { + // Get the evolution chain ID + const chainId = await getEvolutionChainId(pokemonNameOrId); + + // Fetch the evolution chain + const evolutionChain = await fetchEvolutionChain(chainId); + + return { + chainId, + evolutionChain, + pokemonName: pokemonNameOrId + }; + } catch (error) { + throw new Error(`Failed to fetch evolution data: ${error.message}`); + } +} + +// Baba Yaga harness integration +import { FunctionalHarness } from '../../scripting-harness/core/harness.js'; + +let harness = null; + +/** + * Initialize Baba Yaga harness + */ +async function initBabaYaga() { + // Harness will be created when we have a script + return true; +} + +/** + * Get the current harness instance for dev mode integration + */ +export function getCurrentHarness() { + console.log('[API] getCurrentHarness called, harness available:', !!harness); + return harness; +} + +/** + * Execute a Baba Yaga script with evolution data using the harness + * @param {string} script - Baba Yaga script to execute + * @param {object} evolutionData - Evolution chain data to work with + * @returns {Promise<object>} Script execution results + */ +export async function executeBabaYagaScript(script, evolutionData) { + try { + // Create harness with the script + harness = new FunctionalHarness(script, { + logStateChanges: false, + logCommands: false, + debug: false + }); + + // IMPORTANT: Initialize the harness before use + await harness.initialize(); + + // Process the evolution data through the harness + const result = await harness.update(evolutionData); + + // Extract emitted values from commands + const emittedValues = result.commands + .filter(cmd => cmd.type === 'emit') + .map(cmd => cmd.value); + + + + return { + result: result.model, + emitted: emittedValues.length > 0 ? emittedValues : {}, + evolutionData + }; + + } catch (error) { + throw new Error(`Baba Yaga script error: ${error.message}`); + } +} + +/** + * Get example Baba Yaga scripts for evolution data + */ +export function getExampleScripts() { + return { + 'Basic Evolution Stages': ` +/* Get evolution stages from the chain */ +state : ..listen; +/* Extract the evolution chain for easier access */ +chain : state.evolutionChain.chain; +getSpeciesName : stage -> stage.species.name; +evolutionStages : map @getSpeciesName chain.evolves_to; +..emit evolutionStages; +`, + 'Evolution Methods': ` +/* Get evolution methods and requirements */ +state : ..listen; +/* Extract the evolution chain for easier access */ +chain : state.evolutionChain.chain; +getEvolutionInfo : evo -> { + species: evo.species.name, + method: evo.evolution_details[0].trigger.name, + level: evo.evolution_details[0].min_level +}; +evolutionMethods : map @getEvolutionInfo chain.evolves_to; +..emit evolutionMethods; +`, + 'Filter by Evolution Method': ` +/* Filter evolutions by method (e.g., level-up only) */ +state : ..listen; +/* Extract the evolution chain for easier access */ +chain : state.evolutionChain.chain; +isLevelUp : evo -> + when evo.evolution_details[0].trigger.name is + "level-up" then true + _ then false; +levelEvolutions : filter @isLevelUp chain.evolves_to; +getSpeciesName : evo -> evo.species.name; +levelEvolutionNames : map @getSpeciesName levelEvolutions; +..emit levelEvolutionNames; +` + }; +} \ No newline at end of file |