Skip to content

Instantly share code, notes, and snippets.

@armstnp
Last active December 23, 2020 06:06
Show Gist options
  • Save armstnp/99e6f9a82ab470eec7757e1c0b31c603 to your computer and use it in GitHub Desktop.
Save armstnp/99e6f9a82ab470eec7757e1c0b31c603 to your computer and use it in GitHub Desktop.
Scriptable: GW2 Random Item Widget
let item = await randomItem()
let size = await getSize()
if(size === null) { Script.complete() }
let widget = await createWidget(item, size)
if (config.runsInWidget) {
Script.setWidget(widget)
} else {
switch(size) {
case "small":
widget.presentSmall()
break
case "medium":
widget.presentMedium()
break
case "large":
widget.presentLarge()
break
}
}
Script.complete()
async function createWidget(item, size) {
let itemIcon = await loadItemIcon(item)
let [rarityFore, rarityBack] = rarityColors(item)
let widget = new ListWidget()
widget.setPadding(0, 0, 0, 0)
let bg = new LinearGradient()
bg.colors = [Color.black(), Color.black(), rarityBack]
bg.locations = [0.0, 0.4, 2.0]
bg.startPoint = new Point(0.2, 0)
bg.endPoint = new Point(0.8, 1)
widget.backgroundGradient = bg
let vstack = widget.addStack()
vstack.layoutVertically()
vstack.topAlignContent()
vstack.setPadding(0, 0, 0, 0)
let stack = vstack.addStack()
stack.setPadding(0, 0, 0, 0.8)
stack.spacing = 15
stack.centerAlignContent()
let image = stack.addImage(itemIcon)
image.containerRelativeShape = true
if(size === "medium") { image.applyFillingContentMode() }
image.imageOpacity = 0.8
if(size === "medium" || size === "large") {
let name = stack.addText(item["name"])
name.leftAlignText()
name.font = Font.title2()
name.shadowOffset = new Point(-2, 2)
name.shadowRadius = 3
name.textColor = rarityFore
name.shadowColor = rarityBack
}
let description = cleanDescription(item)
if(size === "large" && description.length > 0) {
vstack.addSpacer(30)
let rawItem = vstack.addText(description)
rawItem.font = Font.caption1()
rawItem.centerAlignText()
}
widget.url = buildItemUrl(item)
return widget
}
async function randomItem() {
let allIds = await loadItemIds()
let num = Math.round(Math.random() * allIds.length)
let id = allIds[num]
let item = await loadItem(id)
return item
}
async function loadItemIds() {
let url = "https://api.guildwars2.com/v2/items"
let req = new Request(url)
return await req.loadJSON()
}
async function loadItem(id) {
let url = "https://api.guildwars2.com/v2/items/" + id
let req = new Request(url)
return await req.loadJSON()
}
async function loadItemIcon(item) {
let url = item["icon"]
let req = new Request(url)
return req.loadImage()
}
function buildItemUrl(item) {
let chatCode = item["chat_link"]
return "https://wiki.guildwars2.com/index.php?search=%5B%26" + chatCode.substring(2, chatCode.length - 1) + "%5D"
}
async function getSize() {
return config.runsInWidget
? config.widgetFamily
: await askSize()
}
async function askSize() {
const choices = ["Small", "Medium", "Large"]
let prompt = new Alert()
prompt.title = "Select Preview Size"
choices.forEach(c => prompt.addAction(c))
prompt.addCancelAction("Cancel")
var choice = await prompt.present()
if(choice === -1) { return null }
return choices[choice].toLowerCase()
}
function rarityColors(item) {
return ((!"rarity" in item)
? [Color.white(), new Color("888")]
: {
"Junk": [new Color("AAA"), new Color("555")],
"Basic": [Color.white(), new Color("888")],
"Fine": [new Color("62A4DA"), new Color("31526D")],
"Masterwork": [new Color("1A9306"), new Color("0D4B03")],
"Rare": [new Color("FCD00B"), new Color("7E6806")],
"Exotic": [new Color("FFA405"), new Color("805203")],
"Ascended": [new Color("FB3E8D"), new Color("7E1F47")],
"Legendary": [new Color("4C139D"), new Color("260A4F")]
}[item["rarity"]])
}
function cleanDescription(item) {
let description = item["description"] || ""
description = description.replace(/<\/?c(=[^>]*)?>/g, "")
description = description.replace("<br>", "\n")
return description
}
@armstnp
Copy link
Author

armstnp commented Dec 23, 2020

Small Size

C2EC320A-7CB4-49FD-A65A-93770ED95259

Medium Size

5D67EAB6-1689-4AE5-BD20-72F4DD462BE8

Large Size

87961FB3-53DC-4009-A1EB-1AB60863B077

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment