forked from patapancakes/iidx-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.ts
197 lines (167 loc) · 4.26 KB
/
util.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
import { profile } from "./models/profile";
import { settings } from "./models/settings";
import { pc_data } from "./models/pc_data";
export function IDToCode(id: number) {
const padded = _.padStart(id.toString(), 8);
return `${padded.slice(0, 4)}-${padded.slice(4)}`;
}
export function base64decode(s: string) {
const base64list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
let p = -8,
a = 0,
c: number,
d: number,
buffer: number[] = [];
for (let i = 0; i < s.length; i++) {
if ((c = base64list.indexOf(s.charAt(i))) < 0) continue;
a = (a << 6) | (c & 63);
if ((p += 6) >= 0) {
d = (a >> p) & 255;
if (c !== 64) buffer.push(d);
a &= 63;
p -= 8;
}
}
return buffer;
}
export function GetVersion(info: EamuseInfo) {
return parseInt(info.module.substr(4, 2));
}
export async function IIDXidTorefid(iidxid: number) {
const profile = await DB.FindOne<profile>(null, {
collection: "profile",
iidxid: iidxid,
});
if (_.isNil(profile)) return null;
return profile.refid;
}
export function ClidToRank(clid: number) {
let style: number, rank: number;
if (clid >= 5) {
style = 1;
rank = clid - 5;
} else {
style = 0;
rank = clid;
}
return {
style: style,
rank: rank,
};
}
export function AppendSettingConverter(
sf: boolean,
cf: boolean,
df: boolean,
af: boolean,
rf: boolean,
rr: boolean,
rs: boolean,
hp: boolean,
dg: boolean,
ch: boolean,
rp: boolean,
hi: boolean,
) {
const result =
Number(sf) << 0 |
Number(cf) << 1 |
Number(df) << 2 |
Number(af) << 3 |
Number(rf) << 4 |
Number(false) << 5 | // UNK //
Number(rr) << 6 |
Number(rs) << 7 |
Number(hp) << 8 |
Number(dg) << 9 |
Number(ch) << 10 |
Number(rp) << 11 |
Number(hi) << 12;
return result;
}
export function DateToName(now: number, score_time: number) {
const msPerMinute = 60 * 1000;
const msPerHour = msPerMinute * 60;
const msPerDay = msPerHour * 24;
const msPerMonth = msPerDay * 30;
const msPerYear = msPerDay * 365;
const elapsed = now - score_time;
if (elapsed < msPerMinute) {
return "-" + Math.round(elapsed / 1000) + "s";
} else if (elapsed < msPerHour) {
return "-" + Math.round(elapsed / msPerMinute) + "m";
} else if (elapsed < msPerDay) {
return "-" + Math.round(elapsed / msPerHour) + "h";
} else if (elapsed < msPerMonth) {
return "-" + Math.round(elapsed / msPerDay) + "d";
} else if (elapsed < msPerYear) {
return "-" + Math.round(elapsed / msPerMonth) + "mo";
} else {
return "-" + Math.round(elapsed / msPerYear) + "yr";
}
}
export function buffToHex(buff: number[]) {
let hexString = "";
for (let i = 0; i < buff.length; i++) {
const hex = buff[i].toString(16);
const paddedHex = hex.length % 2 ? "0" + hex : hex;
hexString += paddedHex;
}
return hexString;
}
export function randomIntRange(min, max) {
return Math.floor(Math.random() * max) + min;
}
export async function refidToProfile(refid: string) {
const profile = await DB.FindOne<profile>(refid, {
collection: "profile",
});
let profile_data = [];
try {
profile_data = [
profile.name,
profile.pid,
profile.iidxid,
profile.iidxidstr,
];
} catch {
profile_data = ["", 0, 0, ""];
}
return profile_data;
}
export async function refidToQpro(refid: string) {
const setting = await DB.FindOne<settings>(refid, {
collection: "settings",
});
let qpro_data = [];
try {
qpro_data = [
setting.qpro_hair,
setting.qpro_head,
setting.qpro_face,
setting.qpro_body,
setting.qpro_hand,
setting.qpro_back,
];
} catch {
qpro_data = [0, 0, 0, 0, 0, 0];
}
return qpro_data;
}
export async function refidToPd(refid: string) {
const pc_data = await DB.FindOne<pc_data>(refid, {
collection: "pc_data",
});
let p_data = [];
try {
p_data = [
pc_data.sgid,
pc_data.dgid,
pc_data.sach,
pc_data.dach,
];
} catch {
p_data = [0, 0, 0, 0];
}
return p_data;
}