Repository navigation
Expand file tree
/
Copy pathResinModel.mc
More file actions
335 lines (271 loc) · 11.1 KB
/
Copy pathResinModel.mc
File metadata and controls
335 lines (271 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright (C) 2025- EvilSquirrelGuy. All rights reserved.
*
* Licensed under the EvilSquirrelGuy Protective Licence.
* Personal use and modifications allowed. Redistribution requires full source,
* a changelog, and clear attribution. No commercial use or patenting.
*
* Full licence terms: https://github.com/EvilSquirrelGuy/GarminResinWidget/blob/main/LICENCE.md
*/
using Toybox.Communications as Communications;
using Toybox.Application.Properties as Properties;
using Toybox.Application.Storage as Storage;
using Toybox.Lang;
using Toybox.Time;
using Toybox.Math as Maths;
(:glance)
class ResinData {
// constant value
static var TIME_PER_RESIN = 8 * Time.Gregorian.SECONDS_PER_MINUTE; // 8 minutes per resin
var createdTime; // createdTime
var maxResin; // max_resin
var fullTime; // now + resin_recovery_time
function initialize() {
// set createdTime
createdTime = Time.now().value();
}
function getString() as Lang.String {
return getCurrentResin().toString() + "/" + maxResin.toString();
}
function getRemainingSeconds() {
// get the remaining seconds
var remainingSeconds = 0;
// if fullTime isn't set to 0, calculate difference
if (fullTime > 0) {
remainingSeconds = fullTime - Time.now().value();
}
// if we calculate as 0, it's full so we don't care
if (remainingSeconds <= 0) {
remainingSeconds = 0;
}
return remainingSeconds;
}
function setFullTimeOffset(offset) {
if (offset != 0) {
fullTime = Time.now().value() + offset;
} else { // it's full, discard
fullTime = 0;
}
}
function getCurrentResin() {
var remainingTime = getRemainingSeconds();
var currResin = 0;
if (remainingTime <= 0) { // it's full already
currResin = maxResin;
} else {
// calculate current resin
// i wrote this on 5h of sleep and ungodly amounts of caffeine so the logic might be flawed... oops?
/*
* I feel like i need to explain what's going on here or I'll forget so:
* (assuming max resin = 200, time per resin = 480s, or 8min)
*
* If there's 20s left until full, that means we have 199 resin, and 200 in 20s.
* Therefore, we floor the result, as we have the same amout of resin as we had time remaining was 480s.
* Now extrapolate that to, for example 3600s left. 3600s = 7,5 resin, or rather: 200-7,5 = 192,5. However since
* we can't have fractional resin, and it's not generated 193 resin yet, we simply floor the result to get our
* current resin count!
*
* And as we all know, that condenses down into a single line of code!
*/
// convert to float cuz otherwise it truncates, and we get inaccurate resin count
currResin = Maths.floor(maxResin.toFloat() - (remainingTime.toFloat()/TIME_PER_RESIN.toFloat())).toNumber();
}
return currResin;
}
function getDuration() as Lang.String {
var remainingSeconds = getRemainingSeconds();
var abbrDays = WatchUi.loadResource(Rez.Strings.abbr_days);
var abbrHours = WatchUi.loadResource(Rez.Strings.abbr_hours);
var abbrMins = WatchUi.loadResource(Rez.Strings.abbr_minutes);
var abbrSecs = WatchUi.loadResource(Rez.Strings.abbr_seconds);
var left = WatchUi.loadResource(Rez.Strings.time_left);
if (remainingSeconds == 0) {
return "Full";
}
var durationString = "";
if (remainingSeconds > Time.Gregorian.SECONDS_PER_DAY) {
durationString += (remainingSeconds/(24*60*60)).toNumber().toString() + abbrDays + " ";
}
if (remainingSeconds > Time.Gregorian.SECONDS_PER_HOUR) {
durationString += (remainingSeconds%(24*60*60)/(60*60)).toNumber().toString() + abbrHours + " ";
}
if (remainingSeconds > Time.Gregorian.SECONDS_PER_MINUTE) {
durationString += (remainingSeconds%(60*60)/(60)).toNumber().toString() + abbrMins + " ";
}
durationString += (remainingSeconds%(60)).toNumber().toString() + abbrSecs + " " + left;
return durationString;
}
}
(:glance)
class ResinModel {
/* This URL is taken from https://github.com/iseizuu/node-hoyolab/blob/main/src/routes/routes.ts
* using variables BBS_API and GENSHIN_RECORD_DAILY_NOTE_API
* If this starts raising 404s then those prolly need to be checked...
*/
const URL = "https://bbs-api-os.hoyolab.com/game_record/genshin/api/dailyNote";
hidden var ltoken_v2;
hidden var ltuid_v2;
hidden var UID;
hidden var region;
var resinData = null;
var callback = null;
function initialize(cb) {
System.println("Initialising ResinModel...");
callback = cb;
loadConfigData();
// update the resin data
updateResinData();
}
function loadConfigData() {
// logs logs logs
System.println("Reading data from config file...");
// load the data
ltoken_v2 = Properties.getValue("ltoken_v2");
ltuid_v2 = Properties.getValue("ltuid_v2");
UID = Properties.getValue("game_uid");
region = Properties.getValue("region");
}
function invalidateCache() {
// log
System.println("Invalidating cache...");
// remove our cached version of the resinData
resinData = null;
// invalidate the cache so we don't read from storage
Storage.setValue("lastCacheTime", -1);
// call update method
updateResinData();
}
function updateResinData() {
// updates the resin data
// get last cache time
var lastCacheTime = Storage.getValue("lastCacheTime");
// get the cache invalidation period
var cacheRetention = Properties.getValue("cache_retention") * Time.Gregorian.SECONDS_PER_MINUTE;
// get difference
var diff = -1;
if (lastCacheTime != null && lastCacheTime != -1) {
diff = Time.now().value() - lastCacheTime; // diff in seconds
}
// check if resin data is still stored
// only rely on memory data for max 60s
if (resinData != null && Time.now().value() - resinData.createdTime < 60) {
System.println(Lang.format("Used in-memory data: $1$; $2$; $3$s", [resinData.getCurrentResin(), resinData.maxResin, resinData.getRemainingSeconds()]));
callback.invoke(resinData);
// if we last cached 2h+ ago, load the data from cache, otherwise, fetch it from API again
} else if (diff > cacheRetention || diff == -1) {
fetchResinData();
} else {
generateResinData();
}
}
function generateResinData() {
System.println("Generating resin data from cache...");
// generate resin data from cached values
var fullTime = Storage.getValue("resinFullTimestamp");
var maxResin = Storage.getValue("maxResin");
// time stuff
var lastCached = Storage.getValue("lastCacheTime");
var currTime = Time.now().value();
if (lastCached == null) {
// assume that if this is null, then the other values aren't set
System.println("Error: no cached data found!");
resinData = null;
return;
} else if (lastCached == -1) {
System.println("Error: cache was manually invalidated");
resinData = null;
return;
} else if (currTime - lastCached > 1.5 * maxResin * ResinData.TIME_PER_RESIN) {
// if the data hasn't been updated in 1,5x resin cycles we give up, since it's very unlikely to be accurate
System.println("Error: cached data is too old!");
resinData = null;
return;
}
// var remainingTime = fullTime - currTime;
// var currResin = 0; // init to 0
resinData = new ResinData();
// resinData.currentResin = currResin;
resinData.maxResin = maxResin;
resinData.fullTime = fullTime;
System.println(Lang.format("Calculated cached data: $1$; $2$; $3$s", [resinData.getCurrentResin(), resinData.maxResin, resinData.getRemainingSeconds()]));
callback.invoke(resinData);
}
function fetchResinData() {
if (!(Toybox has :Communications)) {
System.println("Error: Communications module unavailable");
return;
}
// censor uid and log that data was read from config, should give format: 74*****07
var uid_string = UID.toString();
var censored_uid = "unset";
if (UID != 0 && UID != null) {
censored_uid = uid_string.substring(0, 2)
+ (uid_string.length() == 9 ? "*****" : "******")
+ uid_string.substring(-2,null);
}
// log data that we have
System.println("Using stored account data: game_uid=" + censored_uid + "; region=" + (region == null ? "unset" : region));
// url paramters
var params = {
"server" => region,
"role_id" => UID
};
// censor the cookies and log that we have them
var censored_ltoken_v2 = "unset";
if (ltoken_v2.length() != 0 && ltoken_v2 != null) {
censored_ltoken_v2 = ltoken_v2.substring(0,2) + "***...***" + ltoken_v2.substring(-2,null);
}
var censored_ltuid_v2 = "unset";
if (ltuid_v2 != 0) {
censored_ltuid_v2 = ltuid_v2.toString().substring(0,2) + "*****" + ltuid_v2.toString().substring(-2,null);
}
// log bit
System.println("Using cookies: ltoken_v2=" + censored_ltoken_v2 + "; ltuid_v2=" + censored_ltuid_v2);
// parse the actual auth cookies into cookiestring
var cookieString = "ltoken_v2=" + ltoken_v2 + "; ltuid_v2=" + ltuid_v2.toString() + ";";
// headers (here, cookies for auth)
var headers = {
"Cookie" => cookieString,
};
// finalise the options
var options = {
:method => Communications.HTTP_REQUEST_METHOD_GET,
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
:headers => headers
};
System.println("Querying API: " + URL);
Communications.makeWebRequest(URL, params, options, method(:onResponse));
}
function onResponse(responseCode as Lang.Number, data as Lang.Dictionary) as Void {
System.println("Received code " + responseCode.toString() + " from API: ");
if (responseCode == 200) {
System.println("Got internal status code " + data.get("retcode").toString());
if (data.get("retcode") == 0) {
handleData(data.get("data"));
return;
} else {
System.println("Error - API reported an error: " + data.get("message"));
}
} else {
System.println("Error - Unexpected URL response code: " + responseCode.toString());
}
// attempt to generate resin data from cached values
System.println("Attempting to recover - trying cached data...");
generateResinData();
}
function handleData(data) {
//System.println(data);
// this only runs if we didn't load data from memory
resinData = new ResinData();
// resinData.currentResin = data.get("current_resin").toNumber(); // technically we don't need this field
resinData.maxResin = data.get("max_resin").toNumber();
resinData.setFullTimeOffset(data.get("resin_recovery_time").toNumber());
callback.invoke(resinData);
System.println(Lang.format("Received data from API: $1$; $2$; $3$s", [resinData.getCurrentResin(), resinData.maxResin, resinData.getRemainingSeconds()]));
// cache data to persistent storage
Storage.setValue("resinFullTimestamp", resinData.fullTime);
Storage.setValue("maxResin", resinData.maxResin);
// set cache time to now so we can invalidate it after some amount of time
Storage.setValue("lastCacheTime", Time.now().value());
}
}