|
// 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}` |
|
} |
|
} |