This repository was archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
418 lines (412 loc) · 10.5 KB
/
index.js
File metadata and controls
418 lines (412 loc) · 10.5 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.ts
var pokemonAutoChessEloDecay_exports = {};
__export(pokemonAutoChessEloDecay_exports, {
default: () => main
});
module.exports = __toCommonJS(pokemonAutoChessEloDecay_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_relativeTime = __toESM(require("dayjs/plugin/relativeTime"));
var import_dotenv = __toESM(require("dotenv"));
var import_firebase_admin = __toESM(require("firebase-admin"));
var import_mongoose5 = require("mongoose");
// models/detailled-statistic.ts
var import_mongoose = require("mongoose");
var pokemon = new import_mongoose.Schema({
name: {
type: String
},
avatar: {
type: String
},
items: [
{
type: String
}
]
});
var statisticSchema = new import_mongoose.Schema({
playerId: {
type: String
},
elo: {
type: Number
},
time: {
type: Number
},
name: {
type: String
},
rank: {
type: Number
},
avatar: {
type: String
},
pokemons: [pokemon]
});
var detailled_statistic_default = (0, import_mongoose.model)(
"DetailledStatisticV2",
statisticSchema
);
// models/history.ts
var import_mongoose2 = require("mongoose");
var historyPokemon = new import_mongoose2.Schema({
name: {
type: String
},
avatar: {
type: String
},
inventory: [
{
type: String
}
]
});
var history = new import_mongoose2.Schema(
{
id: { type: String },
name: { type: String },
startTime: { type: Number },
endTime: { type: Number },
players: [
{
id: { type: String },
avatar: { type: String },
name: { type: String },
elo: { type: Number },
rank: { type: Number },
pokemons: [historyPokemon]
}
]
},
{
toJSON: {
transform: function(doc, ret) {
delete ret._id;
delete ret.__v;
ret.players.forEach((p) => {
p.pokemons.forEach((po) => {
po.inventory.forEach((i) => {
delete i._id;
});
delete po._id;
});
delete p._id;
});
}
}
}
);
var history_default = (0, import_mongoose2.model)("History", history);
// models/title-statistic.ts
var import_mongoose3 = require("mongoose");
var titleSchema = new import_mongoose3.Schema({
name: {
type: String
},
rarity: {
type: Number
}
});
var title_statistic_default = (0, import_mongoose3.model)("TitleStatistic", titleSchema);
// models/user-metadata.ts
var import_mongoose4 = require("mongoose");
var userMetadataSchema = new import_mongoose4.Schema({
uid: {
type: String
},
displayName: {
type: String
},
langage: {
type: String,
default: "eng"
},
avatar: {
type: String,
default: "0019/Normal"
},
wins: {
type: Number,
default: 0
},
exp: {
type: Number,
default: 0
},
level: {
type: Number,
default: 0
},
elo: {
type: Number,
default: 1e3
},
donor: {
type: Boolean,
default: false
},
booster: {
type: Number,
default: 0
},
mapWin: {
ICE: {
type: Number,
default: 0
},
FIRE: {
type: Number,
default: 0
},
GROUND: {
type: Number,
default: 0
},
NORMAL: {
type: Number,
default: 0
},
GRASS: {
type: Number,
default: 0
},
WATER: {
type: Number,
default: 0
}
},
map: {
ICE: {
type: String,
default: "ICE0"
},
FIRE: {
type: String,
default: "FIRE0"
},
GROUND: {
type: String,
default: "GROUND0"
},
NORMAL: {
type: String,
default: "NORMAL0"
},
GRASS: {
type: String,
default: "GRASS0"
},
WATER: {
type: String,
default: "WATER0"
}
},
title: {
type: String
},
role: {
type: String
},
honors: [
{
type: String
}
],
titles: [
{
type: String
}
],
pokemonCollection: {
type: Map,
of: {
dust: {
type: Number
},
selectedEmotion: {
type: String
},
emotions: [
{
type: String
}
],
shinyEmotions: [
{
type: String
}
],
selectedShiny: {
type: Boolean
},
id: {
type: String
}
}
}
});
var user_metadata_default = (0, import_mongoose4.model)("UserMetadata", userMetadataSchema);
// index.ts
import_dayjs.default.extend(import_relativeTime.default);
async function main() {
import_dotenv.default.config();
import_firebase_admin.default.initializeApp({
credential: import_firebase_admin.default.credential.cert({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
projectId: process.env.FIREBASE_PROJECT_ID,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, "\n")
})
});
const db = await (0, import_mongoose5.connect)(process.env.MONGO_URI);
try {
await deleteAnonymous();
await eloDecay();
await titleStats();
await deleteOldHistory();
} catch (error) {
console.error(error);
throw error;
} finally {
console.log("disconnect db");
await db.disconnect();
}
}
async function deleteAnonymous() {
console.log("deleting anonymous accounts...");
const currentDate = (0, import_dayjs.default)();
const oneMonthLimit = currentDate.subtract(1, "month");
const anonymousAccounts = new Array();
await listAllUsers();
async function listAllUsers(nextPageToken) {
const listUsersResult = await import_firebase_admin.default.auth().listUsers(1e3, nextPageToken);
console.log(nextPageToken);
listUsersResult.users.forEach((userRecord) => {
const lastSignInDate = (0, import_dayjs.default)(userRecord.metadata.lastSignInTime);
if (userRecord.email === void 0 && userRecord.photoURL === void 0 && userRecord.metadata.lastSignInTime && lastSignInDate.isBefore(oneMonthLimit)) {
anonymousAccounts.push(userRecord);
}
});
if (listUsersResult.pageToken) {
await listAllUsers(listUsersResult.pageToken);
}
}
console.log(
"deleting ",
anonymousAccounts.length,
" 1 month inactive anonymous accounts"
);
while (anonymousAccounts.length > 0) {
const batchDeletion = new Array();
for (let i = 0; i < 999; i++) {
const account = anonymousAccounts.pop();
account && batchDeletion.push(account.uid);
}
const firebaseDeletion = await import_firebase_admin.default.auth().deleteUsers(batchDeletion);
console.log("firebase deletion result ", firebaseDeletion);
const pacDeletion = await user_metadata_default.deleteMany({
uid: { $in: batchDeletion }
});
console.log("pac deletion result ", pacDeletion);
}
}
async function eloDecay() {
console.log("computing elo decay...");
const users = await user_metadata_default.find({ elo: { $gt: 1100 } }, [
"uid",
"elo",
"displayName"
]);
if (users && users.length > 0) {
console.log("Checking activity of ", users.length, " users");
for (let i = 0; i < users.length; i++) {
const u = users[i];
const stats = await detailled_statistic_default.find(
{ playerId: u.uid },
["time"],
{
limit: 1,
sort: { time: -1 }
}
);
if (stats && stats.length > 0) {
const time = stats[0].time;
if (time) {
const lastGame = new Date(time);
const now = new Date(Date.now());
if (now.getTime() - lastGame.getTime() > 86400 * 1e3 * 10) {
const decay = Math.max(1100, u.elo - 10);
console.log(
`user ${u.displayName} (${u.elo}) will decay to ${decay}`
);
u.elo = decay;
await u.save();
}
}
}
}
} else {
console.log("No users to check");
}
}
async function titleStats() {
console.log("count the numbers of users...");
const count = await user_metadata_default.countDocuments();
console.log(count, " users found");
const titleEnum = await (await fetch("https://pokemon-auto-chess.com/title-names")).json();
for (let i = 0; i < Object.values(titleEnum).length; i++) {
const title = Object.values(titleEnum)[i];
const titleCount = await user_metadata_default.countDocuments({
titles: { $in: title }
});
await title_statistic_default.deleteMany({ name: title });
await title_statistic_default.create({ name: title, rarity: titleCount / count });
}
}
async function deleteOldHistory() {
console.log("deleting 4 weeks old games...");
const deleteResults = await detailled_statistic_default.deleteMany({
time: { $lt: Date.now() - 86400 * 1e3 * 30 }
});
const allGames = await detailled_statistic_default.countDocuments();
console.log(deleteResults, allGames);
const historyResults = await history_default.deleteMany({
startTime: { $lt: Date.now() - 86400 * 1e3 * 30 }
});
const histories = await history_default.countDocuments();
console.log(historyResults, histories);
}
main();