Advertisement
Guest User

Pokémon Widget

a guest
Nov 17th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const pokeAPI = "https://pokeapi.co/api/v2/pokemon/";
  2. const refreshRate = 1000;
  3.  
  4. const textColor = new Color("#FFFFFF");
  5. const backColor = new Color("#333333");
  6. const accentColor = new Color("#FF9800");
  7.  
  8. const allowedPokemon = [
  9.     "pikachu", "pichu", "charmander", "squirtle", "ditto", "ekans", "clefairy", "jigglypuff", "oddish", "paras", "meowth", "psyduck", "cubone", "koffing", "snorlax",
  10. ];
  11.  
  12. const getRandomPokemon = async () => {
  13.     const randomIndex = Math.floor(Math.random() * allowedPokemon.length);
  14.     const pokemonName = allowedPokemon[randomIndex];
  15.     const response = await new Request(`${pokeAPI}${pokemonName}`).loadJSON();
  16.     return response;
  17. };
  18.  
  19. const createWidget = async (pokemon) => {
  20.     const list = new ListWidget();
  21.     list.backgroundColor = backColor;
  22.     list.setPadding(12, 12, 12, 12);
  23.  
  24.     const mainStack = list.addStack();
  25.     mainStack.layoutVertically();
  26.     mainStack.centerAlignContent();
  27.  
  28.     // Image
  29.     const imageUrl = pokemon.sprites.other["official-artwork"].front_default;
  30.     const imageRequest = new Request(imageUrl);
  31.     const image = await imageRequest.loadImage();
  32.     const imageStack = mainStack.addStack();
  33.     imageStack.layoutHorizontally();
  34.     imageStack.addSpacer();
  35.     const imageItem = imageStack.addImage(image);
  36.     imageItem.imageSize = new Size(75, 75);
  37.     imageItem.cornerRadius = 10;
  38.     imageStack.addSpacer();
  39.  
  40.     // Name
  41.     const nameStack = mainStack.addStack();
  42.     nameStack.layoutHorizontally();
  43.     nameStack.addSpacer();
  44.     const nameText = nameStack.addText(pokemon.name.charAt(0).toUpperCase() + pokemon.name.slice(1).toLowerCase());
  45.     nameText.font = Font.boldSystemFont(18);
  46.     nameText.textColor = textColor;
  47.     nameText.centerAlignText();
  48.  
  49.     nameStack.addSpacer();
  50.  
  51.     // Abilities (Name and damage only, smaller font)
  52.     const abilitiesStack = mainStack.addStack();
  53.     abilitiesStack.layoutVertically();
  54.  
  55.     for (let i = 0; i < 2 && i < pokemon.abilities.length; i++) {
  56.         const abilityName = pokemon.abilities[i].ability.name;
  57.         const abilityUrl = pokemon.abilities[i].ability.url;
  58.         const abilityResponse = await new Request(abilityUrl).loadJSON();
  59.         const abilityDamageString = abilityResponse.effect_entries.find(entry => entry.language.name === 'en')?.short_effect;
  60.         const abilityDamage = abilityDamageString ? extractDamageNumber(abilityDamageString) : "N/A";
  61.  
  62.         const abilitySubStack = abilitiesStack.addStack();
  63.         abilitySubStack.layoutHorizontally();
  64.         abilitySubStack.addSpacer();
  65.         const abilityText = abilitySubStack.addText(`${abilityName} `);
  66.         abilityText.font = Font.regularSystemFont(13);
  67.         abilityText.textColor = accentColor;
  68.         abilityText.centerAlignText();
  69.         abilitySubStack.addSpacer();
  70.     }
  71.  
  72.     return list;
  73. };
  74.  
  75. // Helper function to extract damage number (if present)
  76. function extractDamageNumber(text) {
  77.     const match = text.match(/(\d+) damage/i);
  78.     return match ? match[1] : "";
  79. }
  80.  
  81. const updateWidget = async () => {
  82.     const pokemon = await getRandomPokemon();
  83.     const widget = await createWidget(pokemon);
  84.  
  85.     if (!config.runsInWidget) {
  86.         await widget.presentSmall();
  87.     }
  88.  
  89.     Script.setWidget(widget);
  90.     Script.complete();
  91. };
  92.  
  93. (async () => {
  94.     await updateWidget();
  95.  
  96.     const timer = new Timer();
  97.     timer.timeInterval = refreshRate;
  98.     timer.schedule({
  99.         repeating: true,
  100.         behavior: Timer.Behavior.ResetAfterScheduled,
  101.     });
  102.     timer.onFired = updateWidget;
  103. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement