-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
364 lines (348 loc) · 12.2 KB
/
convert.js
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
function changeInputStyle(element) {
const addedFilesDiv = document.getElementById("addedFiles");
const files = element.files;
const numOfFiles = files.length;
let fileNames = "";
for (let index = 0; index < numOfFiles; index++) {
fileNames += `<span> ${files[index].name} </span>`;
}
addedFilesDiv.innerHTML = fileNames;
addedFilesDiv.style.display = "flex";
}
async function convertFiles() {
const files = document.getElementById("file").files;
const numOfFiles = files.length;
if (numOfFiles == 0) {
alert("Insert some file first!");
return;
}
for (let index = 0; index < numOfFiles; index++) {
await convert(index);
console.log("Converted " + files[index].name);
}
}
//APIs or such for conversion
const API = {
characters:
"https://raw.githubusercontent.com/MadeBaruna/paimon-moe/main/src/data/furnishing/en.json",
weapons:
"https://raw.githubusercontent.com/MadeBaruna/paimon-moe/main/src/data/weapons/en.json",
uigf: "https://api.uigf.org/dict/genshin/en.json",
};
const getIdsFromApi = async () => {
const response = await fetch(API.uigf);
console.log("Success!")
return (await response.json());
}
//I will refer to "paimon.moe" as "PMOE" for the sake of convenience
async function convert(index) {
const weapons = await getWeapons();
const characters = await getCharacters();
const PMOE_Dict = { ...weapons, ...characters };
const fileData = await getFileData(index);
const accounts = getAccounts(fileData);
const idsFromApi = await getIdsFromApi();
console.log("Acquired Data:\n", fileData, accounts, PMOE_Dict, "\n");
const accountsUIGF = [];
accounts.forEach((accountString) => {
let account = new Account(accountString, fileData, PMOE_Dict, idsFromApi);
accountsUIGF.push(account);
let confirmDownload = confirm(`The data for UID ${account.UID} has been processed. Click Ok to download the file or Cancel to leave it!`);
if(confirmDownload){
downloadData(account.UIGFRoot);
}
});
console.log(accountsUIGF);
}
function downloadData(data) {
const dataStr =
"data:text/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(data));
const dlAnchorElem = document.createElement("a");
const monthsExcluded = document.querySelector(
".removeDuplicateRadio:checked"
).value;
dlAnchorElem.setAttribute("href", dataStr);
dlAnchorElem.setAttribute(
"download",
data.info.uid + `(excluding_${monthsExcluded}_months)` + ".json"
);
dlAnchorElem.click();
dlAnchorElem.remove();
}
//Functions to get the data to convert PMOE id -> name;
//1. Function to get the weapons' data from the raw data
async function getWeapons() {
const fetchedPromise = await fetch(API.weapons);
const data = await fetchedPromise.json();
return data;
}
//2. Function to get the Characters' (Furnishing to be precise, because MadeBaruna didn't provide a characters API)
async function getCharacters() {
const fetchedPromise = await fetch(API.characters);
const data = await fetchedPromise.json();
return data;
}
//Function to get the file from user
async function getFileData(index) {
return new Promise((resolve, reject) => {
const file = document.getElementById("file").files[index];
if (!file) {
alert("Please insert a file first!");
reject("No file selected");
return;
}
if (file.type !== "application/json") {
alert("Wrong file type! (.json Expected)");
reject("Invalid file type");
return;
}
if (!file.name.includes("paimon-moe-local-data")) {
alert("Provide correct file or do not rename the exported file!");
reject("Invalid file type");
return;
}
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
try {
const result = JSON.parse(reader.result);
resolve(result);
} catch (error) {
reject("Error parsing JSON data: " + error);
}
};
reader.onerror = () => {
reject("Error reading file");
};
});
}
//Function to get all the related accounts from PMOE as as array (i.e String concatenated infront of the main keys like uid, wish-counter, etc)
function getAccounts(fileData) {
const accArray = Object.keys(fileData).filter((key) =>
key.includes("wish-uid")
);
return accArray.map((accountUID) => {
return accountUID.replace("wish-uid", "");
});
}
//Classes required for an account
class UIGFRoot {
constructor(uid, currentDate, allPulls, idsFromApi) {
this.info = new UIGFInfo(
uid,
"en-us",
currentDate,
"PMOE-Converter",
"1.0.0",
"v3.0"
);
this.list = this.getList(allPulls, currentDate, idsFromApi);
}
getList(allPulls, currentDate, idsFromApi) {
let uigfPulls = [];
let currentPullId = parseInt(String(this.info.uid) + "000000");
const removeDuplicateMonth = parseInt(
document.querySelector(".removeDuplicateRadio:checked").value
);
const filteredPulls = allPulls.map((singlePull) => {
const pullTime = new Date(singlePull.time);
let timeToRemoveInMs;
if (removeDuplicateMonth == 6) {
timeToRemoveInMs = 15552000000;
} else if (removeDuplicateMonth == 12) {
timeToRemoveInMs = 31536000000;
} else {
timeToRemoveInMs = 0;
}
if (currentDate.getTime() - pullTime.getTime() > timeToRemoveInMs) {
return singlePull;
}
});
filteredPulls.forEach((singlePull) => {
if (singlePull != undefined) {
let listItem = new UIGFListItem(
singlePull,
singlePull.gachaType.toString(),
(currentPullId++).toString(),
idsFromApi
);
uigfPulls.push(listItem);
}
});
return uigfPulls;
}
convertToList() { }
}
class UIGFInfo {
constructor(uid, lang, currentDate, appName, appVersion, uigfVersion) {
this.uid = uid;
this.lang = lang;
this.export_timestamp = Math.floor(currentDate.getTime() / 1000);
this.export_time = this.getTimeInFormat(currentDate);
this.export_app = appName;
this.region_time_zone = this.getRegionTimeZone(this.uid[0]);
this.export_app_version = appVersion;
this.uigf_version = uigfVersion;
}
getTimeInFormat(currentDate) {
const date = currentDate.toISOString().split("T")[0];
const time = currentDate.toTimeString().split(" ")[0];
return date + " " + time;
}
getRegionTimeZone(firstUIDCharacter) {
switch (firstUIDCharacter) {
case "6":
return -5;
case "7":
return 1;
default:
return 8;
}
}
}
class UIGFListItem {
constructor(pmoePull, gachaType, id, idsFromApi) {
this.uigf_gacha_type = gachaType == 400 ? 301 : gachaType;
this.gacha_type = gachaType;
this.count = "1";
this.time = pmoePull["time"];
this.item_id = this.idInString(pmoePull, idsFromApi);
this.id = id;
}
idInString(pmoePull, idsFromApi) {
console.log(idsFromApi);
const id = idsFromApi[pmoePull.itemName];
// console.log(String(id))
return String(id);
}
}
class Account {
constructor(accountName, fileData, PMOE_Dict, idsFromApi) {
this.accountName = accountName;
this.UID = this.getUID(fileData);
this.currentDate = new Date();
this.allPulls = this.getPulls(fileData, PMOE_Dict);
this.UIGFRoot = new UIGFRoot(this.UID, this.currentDate, this.allPulls, idsFromApi);
}
getUID(fileData) {
return fileData[this.accountName + "wish-uid"].toString();
}
getName(PMOEId, PMOE_Dict) {
if (PMOE_Dict[PMOEId] != undefined) return PMOE_Dict[PMOEId].name;
return PMOEId[0].toUpperCase() + PMOEId.slice(1);
}
getPulls(fileData, PMOE_Dict) {
let allPulls = [];
try {
fileData[this.accountName + "wish-counter-beginners"].pulls.forEach(
(beginnerPull) => {
let itemName = this.getName(beginnerPull.id, PMOE_Dict);
let pullObject = {
gachaType: 100,
itemName,
time: beginnerPull.time,
itemType: beginnerPull.type,
pity: beginnerPull.pity,
};
allPulls.push(pullObject);
}
);
} catch (err) {
if (err.name == "TypeError") {
console.log("Couldn't find any beginners wishes");
} else {
console.log(err);
}
}
try {
fileData[
this.accountName + "wish-counter-character-event"
].pulls.forEach((characterPull) => {
let itemName = this.getName(characterPull.id, PMOE_Dict);
let pullObject = {
gachaType: 301,
itemName,
time: characterPull.time,
itemType: characterPull.type,
pity: characterPull.pity,
};
allPulls.push(pullObject);
});
} catch (err) {
if (err.name == "TypeError") {
console.log("Couldn't find any character wishes");
console.log(err);
} else {
console.log(err);
}
}
try {
fileData[this.accountName + "wish-counter-standard"].pulls.forEach(
(standardPull) => {
let itemName = this.getName(standardPull.id, PMOE_Dict);
let pullObject = {
gachaType: 200,
itemName,
time: standardPull.time,
itemType: standardPull.type,
pity: standardPull.pity,
};
allPulls.push(pullObject);
}
);
} catch (err) {
if (err.name == "TypeError") {
console.log("Couldn't find any standard wishes");
console.log(err);
} else {
console.log(err);
}
}
try {
fileData[
this.accountName + "wish-counter-weapon-event"
].pulls.forEach((weaponPull) => {
let itemName = this.getName(weaponPull.id, PMOE_Dict);
let pullObject = {
gachaType: 302,
itemName,
time: weaponPull.time,
itemType: weaponPull.type,
pity: weaponPull.pity,
};
allPulls.push(pullObject);
});
} catch (err) {
if (err.name == "TypeError") {
console.log("Couldn't find any weapon wishes");
} else {
console.log(err);
}
}
try {
fileData[
this.accountName + "wish-counter-chronicled"
].pulls.forEach((chronicledPull) => {
let itemName = this.getName(chronicledPull.id, PMOE_Dict);
let pullObject = {
gachaType: 500,
itemName,
time: chronicledPull.time,
itemType: chronicledPull.type,
pity: chronicledPull.pity,
};
allPulls.push(pullObject);
});
} catch (err) {
if (err.name == "TypeError") {
console.log(
"Couldn't find any chronicled wishes, for UID: " + this.UID
);
} else {
console.log(err);
}
}
return allPulls;
}
}