-
Notifications
You must be signed in to change notification settings - Fork 25
/
importDataFromFile.ts
421 lines (359 loc) · 12.1 KB
/
importDataFromFile.ts
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { Cube } from "@/interfaces/Cube";
import { ChangeEvent } from "react";
import genId from "./genId";
import { Solve } from "@/interfaces/Solve";
import { Event } from "@/interfaces/cubeCollection";
import { cubeCollection } from "./const/cubeCollection";
import { parse } from "papaparse";
import { clearCubes, saveBatchCubes } from "@/db/dbOperations";
export default async function importDataFromFile(
event: ChangeEvent<HTMLInputElement>
): Promise<boolean> {
try {
const selectedFile = event.target.files?.[0];
if (!selectedFile) {
throw new Error("No file selected");
}
// Check if the file is of type 'text/plain' (TXT file)
if (selectedFile.type !== "text/plain") {
throw new Error("Only .txt files are allowed.");
}
const fileContent = await readFileAsText(selectedFile);
if (!fileContent) {
throw new Error("Empty file content");
}
// Check if the data is CSV
if (fileContent.includes(";")) {
const backup = await importTwistytimerData(fileContent);
if (!backup) {
throw new Error("Invalid data in the file. (1)");
}
} else {
// Perform validation on the 'fileContent' data here
const backup = await isValidCubesData(fileContent);
if (!backup) {
throw new Error("Invalid data in the file. (2)");
}
}
// Inform the user that the import was successful
return true;
} catch (error) {
console.error(error);
return false;
}
}
async function readFileAsText(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
const fileContent = e.target?.result as string;
resolve(fileContent);
};
reader.onerror = () => {
reject(new Error("Error reading the file."));
};
reader.readAsText(file);
});
}
// Function to validate 'cubes' data
async function isValidCubesData(fileContent: string): Promise<boolean> {
const parsedData = await JSON.parse(fileContent);
// Function list
const promiseFunctions = [
importCstimerData(parsedData),
importNexusTimerData(parsedData),
importCubedeskData(parsedData),
];
// Wait for all promises
const [cstimer, nextuTimer, cubedesk] = await Promise.all(promiseFunctions);
// Verify if some has resolve
return [cstimer, nextuTimer, cubedesk].some((result) => result === true);
}
async function importTwistytimerData(parsedData: any): Promise<boolean> {
// #########################
// ## IMPORT TWISTY TIMER ##
// #########################
// CSV to JSON
parsedData = await parse(parsedData, {
dynamicTyping: true,
});
// Twisty Timer backup: Row structure
// Puzzle: 222, Category: Normal, Time: 0, Date: 1657657016937, Scramble: , Penalty: 0, Comment:
// Penalty:
// [1] - +2
// [0] - Nothing
// [2] - DNF
// List to store new cubes
const newCubeList: Cube[] = [];
for (let index = 0; index <= parsedData.data.length; index++) {
// Skip the first row which contains the headers of the chart
if (index === 0) {
continue;
}
// Extract individual data fields
const puzzle = parsedData.data[index][0];
const category = parsedData.data[index][1];
const time = parsedData.data[index][2];
const date = parsedData.data[index][3];
let scramble = parsedData.data[index][4];
const penalty = parsedData.data[index][5];
const comment = parsedData.data[index][6];
// Avoid solves with 0 time
if (time === 0) continue;
// Avoid clock category (not supported, not included in NexusTimer)
if (puzzle === "clock") {
console.warn(
"Clock, not supported on NexusTimer, backup won't restore it."
);
continue;
}
// Break loop if puzzle is null or empty
if (puzzle === null) break;
// Twisty Timer uses a different category classification
const event = cubeCollection.find((u) => u.twistyId === puzzle.toString());
if (!event) {
return false; // throw new Error("Unsupported category on Nexus Timer");
}
// Find the memory-cube corresponding to this solve
const cube = newCubeList.find(
(cube: Cube) =>
cube.name === `${puzzle}-${category}` && cube.category === event.name
);
// If the cube exists, add the solve to its session
if (cube) {
cube.solves.session.push({
id: genId(),
startTime: date - time,
endTime: date,
scramble: scramble,
bookmark: false,
time: time,
dnf: penalty === 2,
plus2: penalty === 1,
rating: scramble
? Math.floor(Math.random() * 20) +
parseInt(scramble.toString().length)
: 10,
cubeId: cube.id,
comment: comment ? comment : "",
});
continue;
}
// If the cube doesn't exist, create it and append the solve
if (!cube) {
const newCube: Cube = {
id: genId(),
name: `${puzzle}-${category}`,
category: event.name,
solves: {
session: [],
all: [],
},
createdAt: date,
favorite: false,
};
// Add the current solve
newCube.solves.session.push({
id: genId(),
startTime: date - time,
endTime: date,
scramble: scramble,
bookmark: false,
time: time,
dnf: penalty === 2,
plus2: penalty === 1,
rating: scramble
? Math.floor(Math.random() * 20) +
parseInt(scramble.toString().length)
: 10,
cubeId: newCube.id,
comment: comment ? comment : "",
});
// Add the new cube to the list
newCubeList.push(newCube);
}
}
await clearCubes();
// Update local storage with the modified list of cubes
await saveBatchCubes(newCubeList);
return true;
}
async function importCubedeskData(parsedCubeData: any): Promise<boolean> {
// #########################
// #### IMPORT CUBEDESK ####
// #########################
// Check if parsedCubeData is an object
if (typeof parsedCubeData !== "object") return false;
// Ensure that required keys are present in the Cubedesk data structure
if (!Object.keys(parsedCubeData).includes("solves")) return false;
if (!Object.keys(parsedCubeData).includes("sessions")) return false;
// Force a return of false if certain props are found in the Cubedesk backup data structure
if (Object.keys(parsedCubeData).includes("id")) return false;
if (Object.keys(parsedCubeData).includes("properties")) return false;
if (Object.keys(parsedCubeData).includes("scramble")) return false;
// List to store new cubes
const newCubeList: Cube[] = [];
// Iterate through Cubedesk sessions
parsedCubeData["sessions"].forEach(
(session: {
id: string;
name: string;
created_at: string;
order: number;
}) => {
// Create a virtual cube session
const newCube: Cube = {
id: session.id,
name: session.name,
category: "3x3", // Category not specified in Cubedesk backup -> Manual fix later by user...
solves: {
session: [],
all: [],
},
createdAt: Date.parse(session.created_at),
favorite: false,
};
// Iterate through Cubedesk solves
parsedCubeData["solves"].forEach(
(solve: {
scramble: string;
started_at: number;
ended_at: number;
time: number;
raw_time: number;
cube_type: Event;
id: string;
dnf: boolean;
plus_two: boolean;
session_id: string;
from_timer: boolean;
inspection_time: number;
is_smart_cube: boolean;
smart_put_down_time: number;
}) => {
// Check if the solve belongs to the current session
if (solve.session_id === session.id) {
// Create a new solve
const newSolve: Solve = {
id: solve.id,
startTime: solve.started_at,
endTime: solve.ended_at,
scramble: solve.scramble,
bookmark: false,
time: solve.time * 1000,
dnf: solve.dnf,
plus2: solve.plus_two,
rating: Math.floor(Math.random() * 20) + solve.scramble.length,
cubeId: session.id,
comment: "",
};
// Add the solve to the session
newCube.solves.session.push(newSolve);
}
}
);
// Add the new cube to the list
newCubeList.push(newCube);
}
);
await clearCubes();
// Update local storage with the modified list of cubes
await saveBatchCubes(newCubeList);
return true;
}
async function importNexusTimerData(parsedCubeData: Cube[]): Promise<boolean> {
// ########################
// ## IMPORT NEXUS TIMER ##
// ########################
// Verify that the backup originates from Nexustimer
// Force a return of false if certain props are found in the Nexus Timer data structure
// Ensure that the data structure contains the expected properties
if (typeof parsedCubeData !== "object") return false;
if (Object.keys(parsedCubeData).includes("properties")) return false;
if (typeof parsedCubeData[0]?.solves.all === "undefined") return false;
// No adjustments to the data structure are required;
await clearCubes();
// Update local storage with the modified list of cubes
await saveBatchCubes(parsedCubeData);
return true;
}
async function importCstimerData(parsedCubeData: any): Promise<boolean> {
// ########################
// ### IMPORT CSTIMER #####
// ########################
// Check if parsedCubeData is an object
if (typeof parsedCubeData !== "object") return false;
// Verify whether "cstimer" is the only one containing a
// property named "properties". This check helps determine
// if the parsedCubeData corresponds to CSTimer or not.
if (!Object.keys(parsedCubeData).includes("properties")) return false;
// ### validate cstimer
// Eliminate app properties section from the backup
const csTimerSessions = Object.keys(parsedCubeData).slice(
0,
Object.keys(parsedCubeData).length - 1
);
// Create a virtual object to place the new data structure
const newCubeList: Cube[] = [];
csTimerSessions.forEach((session: any) => {
// Later will be updated to the date of the first solve registered on this session
let createdAt = Date.now();
// Create a virtual cube session
const newCube: Cube = {
id: genId(),
name: session,
category: "3x3", // Not specified in CSTimer backup - Require manual fix by user later...
solves: {
session: [],
all: [],
},
createdAt: createdAt,
favorite: false,
};
// Get and convert all the solves into a compatible format
parsedCubeData[session].forEach((solve: any, index: number) => {
if (index === 0) {
createdAt = solve[3];
}
// DNF solves won't be registered in the system; they will be excluded
let hasDNF = false;
// CSTimer has a very deep array storage structure
let plus2 = false;
let solvingTime = 0;
if (solve[0][0] === -1) {
hasDNF = true;
} else if (solve[0][0] === 0) {
plus2 = false;
} else if (solve[0][0] === 2000) {
plus2 = true;
}
if (typeof solve[0][0] !== "number" || typeof solve[0][1] !== "number") {
return false; // new Error("Corrupted data type")
}
solvingTime = solve[0][1];
let scramble = solve[1];
let comment = solve[2];
let endTime = solve[3];
// Adjust the calculations trying to match the data structure
const newSolve: Solve = {
id: genId(),
startTime: endTime - solvingTime,
endTime: endTime,
scramble: scramble,
bookmark: false,
time: solvingTime,
dnf: hasDNF,
plus2: plus2,
rating: Math.floor(Math.random() * 20) + scramble.length,
cubeId: newCube.id,
comment: comment,
};
newCube.solves.session.push(newSolve);
});
newCubeList.push(newCube);
});
await clearCubes();
// Update local storage with the modified list of cubes
await saveBatchCubes(newCubeList);
return true;
}