Skip to content

Instantly share code, notes, and snippets.

@mudkipme
Last active October 28, 2020 04:00
Show Gist options
  • Save mudkipme/5731190e38ef591ff2861408129be07e to your computer and use it in GitHub Desktop.
Save mudkipme/5731190e38ef591ff2861408129be07e to your computer and use it in GitHub Desktop.
const typeTextMap = {
normal: '一般',
fire: '火',
water: '水',
grass: '草',
electric: '電',
fighting: '格鬥',
flying: '飛行',
poison: '毒',
bug: '蟲',
rock: '岩石',
ground: '地面',
psychic: '超能力',
ice: '冰',
dragon: '龍',
ghost: '幽靈',
dark: '惡',
steel: '鋼',
fairy: '妖精'
}
const typeLightColorMap = {
'一般': '#E7E7D8',
'火': '#FF927D',
'水': '#77BBFF',
'草': '#BDFFA3',
'電': '#FAE078',
'格鬥': '#DD9988',
'飛行': '#99BBFF',
'毒': '#C689BA',
'蟲': '#DAEC44',
'岩石': '#E1D08C',
'地面': '#F1DDA0',
'超能力': '#FF9CC4',
'冰': '#DBF6FF',
'龍': '#A194FF',
'幽靈': '#9F9FEC',
'惡': '#BDA396',
'鋼': '#DFDFE1',
'妖精': '#FBCBFB'
}
const typeDarkColorMap = {
'一般': '#8A8A7B',
'火': '#BA1F00',
'水': '#0D6AC8',
'草': '#40C60A',
'電': '#BD8E00',
'格鬥': '#912E1E',
'飛行': '#3678FF',
'毒': '#792F6A',
'蟲': '#849400',
'岩石': '#88762C',
'地面': '#B59226',
'超能力': '#D00053',
'冰': '#13A8D9',
'龍': '#31229D',
'幽靈': '#42428E',
'惡': '#442C21',
'鋼': '#74747B',
'妖精': '#EC67EA'
}
async function pickPokemon() {
const url = "https://pokeapi.co/api/v2/pokemon/?limit=2000"
const req = new Request(url)
const json = await req.loadJSON()
const pokemonUrl = json.results[Math.floor(Math.random() * json.results.length)].url
const pokemonReq = new Request(pokemonUrl)
return await pokemonReq.loadJSON()
}
async function getSpecies(pokemon) {
const req = new Request(pokemon.species.url)
return await req.loadJSON()
}
async function loadNewData() {
const pokemon = await pickPokemon()
const species = await getSpecies(pokemon)
const imgReq = new Request(pokemon.sprites.front_default)
const { name } = species.names.find(item => item.language.name.toLowerCase() === 'zh-hant')
if (!name) {
throw new Error('invalid species')
}
const image = await imgReq.loadImage()
return {
pokemon,
species,
image
}
}
async function loadData() {
const fm = FileManager.local()
const dataPath = fm.joinPath(fm.documentsDirectory(), 'pokemon.json')
const imagePath = fm.joinPath(fm.documentsDirectory(), 'pokemon.png')
if (fm.fileExists(dataPath) && fm.fileExists(imagePath)) {
const json = JSON.parse(fm.readString(dataPath))
// not expired
if (Date.now() - new Date(json.date).getTime() < 864e5) {
return {
pokemon: json.pokemon,
species: json.species,
image: Image.fromFile(imagePath)
}
}
try {
const data = await loadNewData()
fm.writeImage(imagePath, data.image)
fm.writeString(dataPath, JSON.stringify({
pokemon: data.pokemon,
species: data.species,
date: new Date()
}))
return data
} catch (e) {
console.log(e)
return {
pokemon: json.pokemon,
species: json.species,
image: Image.fromFile(imagePath)
}
}
}
return await loadNewData()
}
const data = await loadData()
const widget = await createWidget(data)
// Check if the script is running in
// a widget. If not, show a preview of
// the widget to easier debug it.
if (!config.runsInWidget) {
await widget.presentMedium()
}
// Tell the system to show the widget.
Script.setWidget(widget)
Script.complete()
async function createWidget({ pokemon, species, image }) {
const widget = new ListWidget()
widget.addImage(image)
widget.addSpacer()
const { name } = species.names.find(item => item.language.name.toLowerCase() === 'zh-hant')
const titleText = widget.addText(name)
titleText.font = Font.boldSystemFont(16)
const typeStack = widget.addStack()
typeStack.setPadding(5, 0, 5, 0)
for (const item of pokemon.types) {
const type = typeTextMap[item.type.name]
const stack = typeStack.addStack()
stack.backgroundColor = new Color(Device.isUsingDarkAppearance() ? typeDarkColorMap[type] : typeLightColorMap[type])
stack.cornerRadius = 4
stack.setPadding(2, 5, 2, 5)
const typeText = stack.addText(type)
typeText.font = Font.lightSystemFont(12)
typeStack.addSpacer(5)
}
widget.url = `https://wiki.52poke.com/wiki/${encodeURIComponent(name)}`
return widget
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment