-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredeem-runner.js
More file actions
403 lines (351 loc) · 11.9 KB
/
redeem-runner.js
File metadata and controls
403 lines (351 loc) · 11.9 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
const DEFAULT_MAX_RETRY_ROUNDS = 3;
const DEFAULT_REWARDS_URL = "https://shift.gearboxsoftware.com/rewards";
function createRedemptionController() {
let stopRequested = false;
let skipRequested = false;
return {
requestStop() {
stopRequested = true;
},
requestSkip() {
skipRequested = true;
},
isStopRequested() {
return stopRequested;
},
consumeSkip() {
if (!skipRequested) {
return false;
}
skipRequested = false;
return true;
},
reset() {
stopRequested = false;
skipRequested = false;
}
};
}
function createRedeemRunner(deps) {
const {
browserApi,
injectContentScript,
evaluateInTab,
checkFinalResult,
isLoginRequired,
setCodeState,
updateCodeOverview = async () => {},
incrementRetryCount = async () => {},
sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
states
} = deps;
if (!browserApi || !injectContentScript || !checkFinalResult || !setCodeState || !states) {
throw new Error("Missing required redeem runner dependencies.");
}
const handleControlAction = async ({
code,
game,
platform,
controller,
setStatus
}) => {
if (controller?.isStopRequested()) {
await setCodeState(code, states.NEW, game, platform);
await updateCodeOverview();
return { success: false, state: "stopped", error: "Stopped by user" };
}
if (controller?.consumeSkip()) {
await setCodeState(code, states.INVALID, game, platform);
await updateCodeOverview();
setStatus?.(`Skipped ${code}`);
return { success: false, state: "invalid", error: "Skipped by user", skipped: true };
}
return null;
};
const waitForReloadAndInject = async (tabId) => {
await new Promise((resolve) => {
const listener = (updatedTabId, changeInfo) => {
if (updatedTabId === tabId && changeInfo.status === "complete") {
browserApi.tabs.onUpdated.removeListener(listener);
resolve();
}
};
browserApi.tabs.onUpdated.addListener(listener);
});
let response = null;
try {
response = await browserApi.tabs.sendMessage(tabId, { action: "heartbeat" });
} catch (error) {
response = null;
}
if (response && response.status === "alive") {
return;
}
await injectContentScript(tabId, "shift-config.js");
await injectContentScript(tabId, "shift-handler.js");
await sleep(1000);
};
const processCode = async ({
code,
game,
platform,
tabId,
controller,
retryCount,
setStatus
}) => {
await setCodeState(code, states.CHECKING, game, platform);
try {
const controlBefore = await handleControlAction({ code, game, platform, controller, setStatus });
if (controlBefore) {
return controlBefore;
}
let redemptionResult = null;
try {
redemptionResult = await browserApi.tabs.sendMessage(tabId, {
action: "redeemCode",
code: code,
game: game,
platforms: [platform],
maxRetries: 0,
currentRetry: retryCount
});
} catch (messageError) {
const controlAfterMessage = await handleControlAction({
code,
game,
platform,
controller,
setStatus
});
if (controlAfterMessage) {
return controlAfterMessage;
}
}
if (redemptionResult && !redemptionResult.success && redemptionResult.state !== "submitted") {
if (redemptionResult.state === "checked") {
await setCodeState(code, states.VALIDATED, game, platform);
return { success: true, state: "validated", alreadyRedeemed: true };
}
if (redemptionResult.state === "expired") {
await setCodeState(code, states.EXPIRED, game, platform);
return { success: false, state: "expired" };
}
if (redemptionResult.state === "invalid") {
await setCodeState(code, states.INVALID, game, platform);
return { success: false, state: "invalid" };
}
await setCodeState(code, states.ERROR, game, platform);
return { success: false, state: "error", error: redemptionResult.error };
}
await waitForReloadAndInject(tabId);
const controlAfterReload = await handleControlAction({
code,
game,
platform,
controller,
setStatus
});
if (controlAfterReload) {
return controlAfterReload;
}
await sleep(500);
const controlAfterDelay = await handleControlAction({
code,
game,
platform,
controller,
setStatus
});
if (controlAfterDelay) {
return controlAfterDelay;
}
const finalResult = await checkFinalResult(tabId, code);
const controlAfterFinal = await handleControlAction({
code,
game,
platform,
controller,
setStatus
});
if (controlAfterFinal) {
return controlAfterFinal;
}
if (finalResult.state === "redeemed") {
await setCodeState(code, states.REDEEMED, game, platform);
return { success: true, state: "redeemed" };
}
if (finalResult.state === "checked") {
await setCodeState(code, states.VALIDATED, game, platform);
return { success: true, state: "validated", alreadyRedeemed: true };
}
if (finalResult.state === "expired") {
await setCodeState(code, states.EXPIRED, game, platform);
return { success: false, state: "expired" };
}
if (finalResult.state === "invalid") {
await setCodeState(code, states.INVALID, game, platform);
return { success: false, state: "invalid" };
}
await setCodeState(code, states.ERROR, game, platform);
return { success: false, state: "error", error: finalResult.error || "Unknown error" };
} catch (error) {
await setCodeState(code, states.ERROR, game, platform);
return { success: false, state: "error", error: error.message };
}
};
const run = async ({
codesToProcess,
game,
platform,
timingSettings,
controller,
setStatus,
onCodeStart,
onCodeComplete,
onTabReady,
rewardsUrl = DEFAULT_REWARDS_URL,
maxRetryRounds = DEFAULT_MAX_RETRY_ROUNDS
}) => {
const reportStatus = typeof setStatus === "function" ? setStatus : () => {};
const timing = timingSettings || { codeDelay: 5, retryDelay: 15 };
let tab = (await browserApi.tabs.query({ url: rewardsUrl })).pop();
if (!tab) {
tab = await browserApi.tabs.create({ url: rewardsUrl });
}
onTabReady?.(tab);
await browserApi.tabs.update(tab.id, { url: rewardsUrl });
await waitForReloadAndInject(tab.id);
const tabInfo = await browserApi.tabs.get(tab.id);
if (isLoginRequired(tabInfo.url)) {
reportStatus("Not logged in! Please log in to SHIFT in the opened tab and try again");
return { success: false, state: "login_required" };
}
let totalProcessed = 0;
let redeemedCount = 0;
let finalErrorCount = 0;
let codeQueue = [...codesToProcess];
let erroredCodes = [];
let retryRound = 0;
while (codeQueue.length > 0 && retryRound <= maxRetryRounds) {
if (controller?.isStopRequested()) {
reportStatus("Redemption stopped by user");
return { success: false, state: "stopped" };
}
if (retryRound > 0) {
reportStatus(`Retry round ${retryRound}/${maxRetryRounds} - ${codeQueue.length} codes remaining`);
await updateCodeOverview();
const retryDelay = timing.retryDelay * 1000 + (retryRound * 5000);
reportStatus(`Waiting ${retryDelay / 1000}s before retry round ${retryRound}...`);
await sleep(retryDelay);
await browserApi.tabs.update(tab.id, { url: rewardsUrl });
await waitForReloadAndInject(tab.id);
await sleep(3000);
const retryTabInfo = await browserApi.tabs.get(tab.id);
if (isLoginRequired(retryTabInfo.url)) {
reportStatus("Session expired! Please log in to SHIFT in the opened tab and try again");
return { success: false, state: "login_required" };
}
}
const currentBatch = [...codeQueue];
codeQueue = [];
erroredCodes = [];
for (let i = 0; i < currentBatch.length; i++) {
if (controller?.isStopRequested()) {
reportStatus("Redemption stopped by user");
return { success: false, state: "stopped" };
}
const { code } = currentBatch[i];
onCodeStart?.(code);
totalProcessed++;
reportStatus(`Round ${retryRound + 1}: Processing ${i + 1}/${currentBatch.length}: ${code}`);
const result = await processCode({
code,
game,
platform,
tabId: tab.id,
controller,
retryCount: retryRound,
setStatus: reportStatus
});
onCodeComplete?.(code, result);
if (!result.success) {
const reason = result.error || result.state || "unknown";
console.warn(`Redemption failed for ${code} (${platform}/${game}): ${reason}`);
}
if (result.state === "stopped") {
reportStatus("Redemption stopped by user");
return result;
}
if (result.success || result.state === "validated" || result.state === "redeemed" || result.state === "checked") {
redeemedCount++;
await updateCodeOverview();
} else if (result.state === "error" && retryRound < maxRetryRounds) {
erroredCodes.push({ code, state: result });
await incrementRetryCount(code, game);
await updateCodeOverview();
} else {
if (result.state === "error") {
finalErrorCount++;
}
await updateCodeOverview();
}
try {
await updateCodeOverview();
} catch (overviewError) {
console.error("Error updating overview:", overviewError);
}
if (i < currentBatch.length - 1) {
const baseDelay = timing.codeDelay * 1000;
const extraDelayForValidation = result.validated ? 2000 : 0;
const delay = retryRound === 0
? baseDelay + extraDelayForValidation
: baseDelay + extraDelayForValidation + (retryRound * 2000);
reportStatus(`Round ${retryRound + 1}: Waiting ${delay / 1000}s before next code...`);
await sleep(delay);
}
if (evaluateInTab) {
try {
const message = await evaluateInTab(tab.id, () => {
const notice = document.getElementsByClassName("alert notice")[0];
return notice ? notice.innerHTML : null;
});
if (message && message.includes("To continue to redeem SHiFT codes, please launch a SHiFT-enabled title first!")) {
reportStatus("Rate limited - please launch a SHiFT-enabled game first!");
codeQueue = [];
erroredCodes = [];
retryRound = maxRetryRounds + 1;
break;
}
} catch (error) {
// Ignore script execution errors
}
}
}
if (erroredCodes.length > 0 && retryRound < maxRetryRounds) {
codeQueue = erroredCodes;
retryRound++;
} else {
break;
}
}
const expiredCount = totalProcessed - redeemedCount - finalErrorCount;
reportStatus(`Completed! Redeemed: ${redeemedCount}, Errors: ${finalErrorCount}, Other: ${expiredCount}, Total: ${totalProcessed}`);
await updateCodeOverview();
return {
success: true,
state: "completed",
redeemedCount,
finalErrorCount,
totalProcessed
};
};
return { run };
}
const RedeemRunner = { createRedeemRunner, createRedemptionController };
if (typeof module !== "undefined" && module.exports) {
module.exports = RedeemRunner;
}
if (typeof globalThis !== "undefined") {
globalThis.RedeemRunner = RedeemRunner;
}