forked from mycard/srvpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
athletic-check.js
60 lines (60 loc) · 2.37 KB
/
athletic-check.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
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AthleticChecker = void 0;
const axios_1 = __importDefault(require("axios"));
const querystring_1 = __importDefault(require("querystring"));
const moment_1 = __importDefault(require("moment"));
class AthleticChecker {
config;
athleticDeckCache;
lastAthleticDeckFetchTime;
constructor(config) {
this.config = config;
}
deckToString(deck) {
const deckText = '#ygopro-server deck log\n#main\n' + deck.main.join('\n') + '\n!side\n' + deck.side.join('\n') + '\n';
return deckText;
}
async getAthleticDecks() {
if (this.athleticDeckCache && (0, moment_1.default)().diff(this.lastAthleticDeckFetchTime, "seconds") < this.config.ttl) {
return this.athleticDeckCache;
}
const { data } = await axios_1.default.get(this.config.rankURL, {
timeout: 10000,
responseType: "json",
paramsSerializer: querystring_1.default.stringify,
params: this.config.athleticFetchParams
});
const athleticDecks = data.slice(0, this.config.rankCount).map(m => m.name);
this.athleticDeckCache = athleticDecks;
this.lastAthleticDeckFetchTime = (0, moment_1.default)();
return athleticDecks;
}
async getDeckType(deck) {
const deckString = this.deckToString(deck);
const { data } = await axios_1.default.post(this.config.identifierURL, querystring_1.default.stringify({ deck: deckString }), {
timeout: 10000,
responseType: "json"
});
return data.deck;
}
async checkAthletic(deck) {
try {
const deckType = await this.getDeckType(deck);
if (deckType === '迷之卡组') {
return { success: true, athletic: 0, message: null };
}
const athleticDecks = await this.getAthleticDecks();
const athletic = athleticDecks.findIndex(d => d === deckType) + 1;
return { success: true, athletic, message: null };
}
catch (e) {
return { success: false, message: e.toString() };
}
}
}
exports.AthleticChecker = AthleticChecker;
//# sourceMappingURL=athletic-check.js.map