· 5 years ago · Mar 11, 2021, 09:28 PM
1// Variables used by Scriptable.
2// These must be at the very top of the file. Do not edit.
3// icon-color: deep-blue; icon-glyph: sync-alt;
4/*
5
6Setup:
7
81. Save this image to Files/iCloud Drive/Scriptable/
9 https://i.imgur.com/CXzDZkD.png
10
112. Fill out the User Prefs (Scroll down a bit)
12
134. Install this shortcut:
14 https://www.icloud.com/shortcuts/ca90d07d2b9e4fc6b0ab86685038a8ca
15
165. Put in your API Key and Gamertag, then run it once.
17
186. In shortcuts go to:
19 Automations, then
20 New Personal Automation
21 Tap "Time of day"
22 (Set time to something like 2 am)
23 Tap "Next"
24 Add a "Run Shortcut" Action
25 Set it to the shurtcut from steps 4 & 5
26 Tap "Next"
27 Turn off "Ask Before Running"
28 Tap "Done"
29
307. In Scriptable go to:
31 Settings, then
32 File Bookmarks
33 New Bookmark
34 File
35 Go in the Shortcuts folder and select "H5-XP.json"
36 Leave the name as "H5-XP.json"
37 Tap "Save"
38
397. Add a medium scriptable widget to your home screen, set it to this sctipt, and you're done.
40
41Note: For the first day, your XP for "TODAY" won't be accurate. After 1 day, assuming you set it up right, it should work.
42
43Another note: I'm not sure if this widget updates automatically. The easiest way to run it quickly is to set the "When Interacting" for the widget, to "Run Script." Now every time you tap the widget, it runs the script and refreshes it.
44
45DM me at u/sac396 if you need help <3
46
47*/
48
49
50/*-----------------------------------------*/
51/* User Prefs */
52/*-----------------------------------------*/
53
54
55let apiKey = ""
56// https://developer.haloapi.com/signin > Profile > Your Subscriptions > Copy one of the API Keys
57
58let gamertag = ""
59// Include any spaces. Not case sensitive.
60
61let targetCompletionDate = "November 20, 2021"
62
63let BGImageName = "bg1.png"
64// Save to iCloud Drive/Scriptable/
65
66let font = "Halo Font"
67// Find fonts at iosfonts.com
68
69let fontSize = 18
70
71
72/*-----------------------------------------*/
73/* HTTP + iCloud Requests */
74/*-----------------------------------------*/
75
76
77// Retrieve stuff from the Halo API
78
79
80gamertag = gamertag.replace(" ", "%20")
81
82let nf = new Intl.NumberFormat
83
84
85let emblemREQ = new Request("https://www.haloapi.com/profile/h5/profiles/" + gamertag + "/emblem")
86emblemREQ.headers = { "Ocp-Apim-Subscription-Key": apiKey }
87
88let emblemImage = await emblemREQ.loadImage()
89
90
91let armorREQ = new Request("https://www.haloapi.com/profile/h5/profiles/" + gamertag + "/spartan")
92armorREQ.headers = { "Ocp-Apim-Subscription-Key": apiKey }
93
94let armorImage = await armorREQ.loadImage()
95
96
97let XPREQ = new Request("https://www.haloapi.com/stats/h5/servicerecords/campaign?players=" + gamertag)
98XPREQ.headers = { "Ocp-Apim-Subscription-Key": apiKey }
99
100let XPRES = await XPREQ.loadJSON()
101let currentXP = XPRES["Results"][0]["Result"]["Xp"]
102
103let currentLevel = XPRES["Results"][0]["Result"]["SpartanRank"]
104
105
106// Retrieve the stored XP from the shortcut, and the background image from iCloud
107
108
109let fm = FileManager.iCloud()
110
111
112let BG1 = fm.documentsDirectory() + "/" + BGImageName
113log(BG1)
114
115
116let storedXP = fm.bookmarkedPath("H5-XP.json")
117storedXP = fm.readString(storedXP)
118storedXP = JSON.parse(storedXP)
119storedXP = storedXP["Stored XP"]
120
121
122let XPGain = currentXP - storedXP
123
124let daysUntilTarget = Math.ceil( (new Date(targetCompletionDate) - new Date() ) / (1000*60*60*24) )
125
126
127/*-----------------------------------------*/
128/* Draw Contexts */
129/*-----------------------------------------*/
130
131
132// Set up draw context
133
134
135font = new Font(font, fontSize)
136
137let widget = new ListWidget()
138widget.setPadding(0, 0, 0, 0)
139
140let dc = new DrawContext()
141dc.opaque = false
142dc.size = new Size(675, 275)
143
144dc.setFont(font)
145dc.setTextColor(white)
146
147
148let leftOffset = 95
149let topOffset = 8
150let rightOffset = 452
151let rectHeight = 30
152
153
154// Right side : Armor + Emblem
155
156
157let emblemImageScale = .75
158dc.drawImageInRect(emblemImage, new Rect(455, 30, 256 * emblemImageScale, 256 * emblemImageScale))
159
160let armorImageScale = .4
161dc.drawImageInRect(armorImage, new Rect(510, 50, 512 * armorImageScale, 672 * armorImageScale))
162
163
164// Left side : Labels
165
166
167dc.setTextAlignedLeft()
168
169let textContainer1 = new Rect(leftOffset, topOffset + 10, ( rightOffset - leftOffset ), rectHeight)
170dc.drawTextInRect("SR 152", textContainer1)
171
172let textContainer3 = new Rect(leftOffset, topOffset + 40, ( rightOffset - leftOffset ), rectHeight)
173dc.drawTextInRect("TOTAL", textContainer3)
174
175let textContainer2 = new Rect(leftOffset, topOffset + 70, ( rightOffset - leftOffset ), rectHeight)
176dc.drawTextInRect("TODAY", textContainer2)
177
178let textContainer4 = new Rect(leftOffset, topOffset + 100, ( rightOffset - leftOffset ), rectHeight)
179dc.drawTextInRect("GOAL", textContainer4)
180
181let textContainer5 = new Rect(leftOffset, topOffset + 235, ( rightOffset - leftOffset ), rectHeight)
182dc.drawTextInRect("XP TO SR 152", textContainer5)
183
184
185// Left Side : Values
186
187
188dc.setTextAlignedRight()
189
190dc.drawTextInRect(nf.format(currentXP) + " (" + Math.round(currentXP / 50000) / 10 + "%)", textContainer3)
191
192dc.drawTextInRect(nf.format(XPGain), textContainer2)
193
194dc.drawTextInRect(nf.format(Math.ceil( (50000000 - currentXP) / daysUntilTarget )), textContainer4)
195
196dc.drawTextInRect(nf.format(50000000 - currentXP), textContainer5)
197
198
199/*-----------------------------------------*/
200/* Assemble Widget */
201/*-----------------------------------------*/
202
203
204widget.backgroundImage = Image.fromFile(BG1)
205
206let dcImage = dc.getImage()
207dcImage = widget.addImage(dcImage)
208dcImage = dcImage.centerAlignImage()
209
210Script.setWidget(widget)