-
Notifications
You must be signed in to change notification settings - Fork 76
/
athletic-check.ts
75 lines (70 loc) · 2.06 KB
/
athletic-check.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
import axios from "axios";
import qs from "querystring";
import moment, {Moment} from "moment";
interface Deck {
main: number[];
side: number[];
}
interface Config {
rankURL: string;
identifierURL: string;
athleticFetchParams: any;
rankCount: number;
ttl: number;
}
interface AthleticDecksReturnData {
name: string
}
interface ReturnMessage {
success: boolean;
athletic?: number;
message: string;
}
export class AthleticChecker {
config: Config;
athleticDeckCache: string[];
lastAthleticDeckFetchTime: Moment;
constructor(config: Config) {
this.config = config;
}
deckToString(deck: 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(): Promise<string[]> {
if (this.athleticDeckCache && moment().diff(this.lastAthleticDeckFetchTime, "seconds") < this.config.ttl) {
return this.athleticDeckCache;
}
const { data } = await axios.get(this.config.rankURL, {
timeout: 10000,
responseType: "json",
paramsSerializer: qs.stringify,
params: this.config.athleticFetchParams
});
const athleticDecks = (data as AthleticDecksReturnData[]).slice(0, this.config.rankCount).map(m => m.name);
this.athleticDeckCache = athleticDecks;
this.lastAthleticDeckFetchTime = moment();
return athleticDecks;
}
async getDeckType(deck: Deck): Promise<string> {
const deckString = this.deckToString(deck);
const { data } = await axios.post(this.config.identifierURL, qs.stringify({ deck: deckString }), {
timeout: 10000,
responseType: "json"
});
return data.deck;
}
async checkAthletic(deck: Deck): Promise<ReturnMessage> {
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() };
}
}
}