Skip to content

Instantly share code, notes, and snippets.

@grantjbutler
Last active December 31, 2020 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantjbutler/ef52bb35ebbc014709d94c07b1b87d92 to your computer and use it in GitHub Desktop.
Save grantjbutler/ef52bb35ebbc014709d94c07b1b87d92 to your computer and use it in GitHub Desktop.
Scriptable Widgets for Side Quest from the Zeldathon team

This is a script for Scriptable for iOS that displays stats widgets for Side Quest: Take to the Seas, presented by the Zeldathon Team.

To use the widgets, create a new script in Scriptable with the contents of scripts.js. Once you've added the script, add a Scriptable widget to the home screen. In the configuration screen of the widget, select the script you created in Scriptable.

There are currently three types of widgets you can display. You can change which type of widget is displayed by changing the value of the "Parameter" parameter when configuring the widget.

  • Total: This widget displays the current total raised during Side Quest. This is the default widget if no value for "Parameter" is provided. You can also get this widget by specifying total for the value.
  • Projects: This widget shows the number of charity: water projects that have been funded through Side Quest. You can get this widget by specifying project or projects for the value of "Parameter".
  • People: This widget shows the number of people who will now have access to clean water as a result of Side Quest. You can get this widget by specifying people or person for the value of "Parameter".

All widgets support the different sizes. At the medium and large sizes, the projects and people widgets are combined into one, showing both stats side-by-side.

TODO:

  • Add widget for the schedule
  • Support opening widgets with Siri ("Hey, Siri, show the Side Quest total")
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: hand-holding-heart;
if (config.runsInWidget) {
let widget = await createWidget(args.widgetParameter, config.widgetFamily)
Script.setWidget(widget)
} else {
let widget = await createWidget("person", "medium")
widget.presentMedium()
Script.complete()
}
async function createWidget(type, size) {
switch (type) {
case "person":
case "people":
case "project":
case "projects":
if (size == "small") {
return await createSmallStatWidget(type)
} else {
return await createMediumStatWidget()
}
case "total":
default:
if (size == "small") {
return await createSmallTotalWidget()
} else {
return await createMediumTotalWidget()
}
}
}
// --------------
// TOTAL STATS WIDGETS
// --------------
async function createSmallStatWidget(type) {
let request = new Request("https://donate.zeldathon.net/total?unformatted=true")
let totalString = await request.loadString()
let total = parseFloat(totalString)
switch (type) {
case "person":
case "people":
let people = Math.floor(total / 40)
return createBasicWidget(`${people}`, "people given clean water")
case "project":
case "projects":
let rounded = Math.floor(total / 1000)
let projects = rounded / 10
return createBasicWidget(`${projects}`, "charity: water projects funded")
break
}
}
async function createMediumStatWidget() {
let request = new Request("https://donate.zeldathon.net/total?unformatted=true")
let totalString = await request.loadString()
let total = parseFloat(totalString)
let people = Math.floor(total / 40)
let rounded = Math.floor(total / 1000)
let projects = rounded / 10
return createTwoColumnWidget(
{ bigText: `${people}`, bottomText: "people granted access to clean water" },
{ bigText: `${projects}`, bottomText: "charity: water projects funded" }
)
}
// --------------
// TOTAL WIDGETS
// --------------
async function createSmallTotalWidget() {
let request = new Request("https://donate.zeldathon.net/total?unformatted=true")
let totalString = await request.loadString()
let total = parseFloat(totalString)
return createBasicWidget('$' + formatNumber(total), "raised for charity: water")
}
async function createMediumTotalWidget() {
let request = new Request("https://donate.zeldathon.net/total")
let total = await request.loadString()
return createBasicWidget(total, "raised for charity: water")
}
// ---------------
// WIDGETS
// ---------------
function createBasicWidget(bigText, bottomText) {
let widget = new ListWidget()
widget.backgroundColor = new Color("#407cc9")
widget.url = "https://twitch.tv/zeldathon"
widget.refreshAfterDate = new Date((new Date()).getTime() + 1000 * 60 * 5)
let campaignName = widget.addText("Side Quest: Take to the Seas")
campaignName.font = Font.caption1()
campaignName.lineLimit = 0
widget.addSpacer(null)
let totalText = widget.addText(bigText)
totalText.font = Font.largeTitle()
widget.addSpacer(null)
let charity = widget.addText(bottomText)
charity.font = Font.caption1()
return widget
}
function createTwoColumnWidget(col1, col2) {
let widget = new ListWidget()
widget.backgroundColor = new Color("#407cc9")
widget.url = "https://twitch.tv/zeldathon"
widget.refreshAfterDate = new Date((new Date()).getTime() + 1000 * 60 * 5)
let campaignName = widget.addText("Side Quest: Take to the Seas")
campaignName.font = Font.caption1()
widget.addSpacer(null)
let row = widget.addStack()
row.layoutHorizontally()
row.spacing = 16
let firstColumn = row.addStack()
firstColumn.layoutVertically()
createColumn(col1.bigText, col1.bottomText, firstColumn)
let secondColumn = row.addStack()
secondColumn.layoutVertically()
createColumn(col2.bigText, col2.bottomText, secondColumn)
widget.addSpacer(null)
return widget
}
function createColumn(bigText, bottomText, source) {
let totalText = source.addText(bigText)
totalText.font = Font.largeTitle()
let charity = source.addText(bottomText)
charity.font = Font.caption1()
}
function formatNumber(number) {
if (number >= 1000000) {
let rounded = Math.floor(number / 100000)
return `${rounded / 10}m`
} else if (number >= 1000) {
let rounded = Math.floor(number / 100)
return `${rounded / 10}k`
} else {
return `${number}`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment