Commit 09131e88 authored by nanahira's avatar nanahira

rework challonge

parent bb5f09a8
Pipeline #13284 passed with stages
in 14 minutes and 46 seconds
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Challonge = void 0;
const axios_1 = __importDefault(require("axios"));
const bunyan_1 = require("bunyan");
const moment_1 = __importDefault(require("moment"));
const p_queue_1 = __importDefault(require("p-queue"));
class Challonge {
config;
constructor(config) {
this.config = config;
}
queue = new p_queue_1.default({ concurrency: 1 });
log = (0, bunyan_1.createLogger)({ name: 'challonge' });
previous;
previousTime;
async getTournamentProcess(noCache = false) {
if (!noCache && this.previous && this.previousTime.isAfter((0, moment_1.default)().subtract(this.config.cache_ttl, 'ms'))) {
return this.previous;
}
try {
const { data: { tournament } } = await axios_1.default.get(`https://api.challonge.com/v1/tournaments/${this.config.tournament_id}.json`, {
params: {
api_key: this.config.api_key,
include_participants: 1,
include_matches: 1,
},
});
this.previous = tournament;
this.previousTime = (0, moment_1.default)();
return tournament;
}
catch (e) {
this.log.error(`Failed to get tournament ${this.config.tournament_id}`, e);
return;
}
}
async getTournament(noCache = false) {
if (noCache) {
return this.getTournamentProcess(noCache);
}
return this.queue.add(() => this.getTournamentProcess());
}
async putScore(matchId, match, retried = 0) {
try {
await axios_1.default.put(`https://api.challonge.com/v1/tournaments/${this.config.tournament_id}/matches/${matchId}.json`, {
api_key: this.config.api_key,
match: match,
});
this.previous = undefined;
this.previousTime = undefined;
return true;
}
catch (e) {
this.log.error(`Failed to put score for match ${matchId}`, e);
if (retried < 5) {
this.log.info(`Retrying match ${matchId}`);
return this.putScore(matchId, match, retried + 1);
}
else {
this.log.error(`Failed to put score for match ${matchId} after 5 retries`);
return false;
}
}
}
}
exports.Challonge = Challonge;
//# sourceMappingURL=challonge.js.map
\ No newline at end of file
import axios from 'axios';
import { createLogger } from 'bunyan';
import moment, { Moment } from 'moment';
import PQueue from 'p-queue';
export interface Match {
attachment_count?: any;
created_at: string;
group_id?: any;
has_attachment: boolean;
id: number;
identifier: string;
location?: any;
loser_id?: any;
player1_id: number;
player1_is_prereq_match_loser: boolean;
player1_prereq_match_id?: any;
player1_votes?: any;
player2_id: number;
player2_is_prereq_match_loser: boolean;
player2_prereq_match_id?: any;
player2_votes?: any;
round: number;
scheduled_time?: any;
started_at: string;
state: string;
tournament_id: number;
underway_at?: any;
updated_at: string;
winner_id?: any;
prerequisite_match_ids_csv: string;
scores_csv: string;
}
export interface MatchWrapper {
match: Match;
}
export interface Participant {
active: boolean;
checked_in_at?: any;
created_at: string;
final_rank?: any;
group_id?: any;
icon?: any;
id: number;
invitation_id?: any;
invite_email?: any;
misc?: any;
name: string;
on_waiting_list: boolean;
seed: number;
tournament_id: number;
updated_at: string;
challonge_username?: any;
challonge_email_address_verified?: any;
removable: boolean;
participatable_or_invitation_attached: boolean;
confirm_remove: boolean;
invitation_pending: boolean;
display_name_with_invitation_email_address: string;
email_hash?: any;
username?: any;
attached_participatable_portrait_url?: any;
can_check_in: boolean;
checked_in: boolean;
reactivatable: boolean;
}
export interface ParticipantWrapper {
participant: Participant;
}
export interface Tournament {
accept_attachments: boolean;
allow_participant_match_reporting: boolean;
anonymous_voting: boolean;
category?: any;
check_in_duration?: any;
completed_at?: any;
created_at: string;
created_by_api: boolean;
credit_capped: boolean;
description: string;
game_id: number;
group_stages_enabled: boolean;
hide_forum: boolean;
hide_seeds: boolean;
hold_third_place_match: boolean;
id: number;
max_predictions_per_user: number;
name: string;
notify_users_when_matches_open: boolean;
notify_users_when_the_tournament_ends: boolean;
open_signup: boolean;
participants_count: number;
prediction_method: number;
predictions_opened_at?: any;
private: boolean;
progress_meter: number;
pts_for_bye: string;
pts_for_game_tie: string;
pts_for_game_win: string;
pts_for_match_tie: string;
pts_for_match_win: string;
quick_advance: boolean;
ranked_by: string;
require_score_agreement: boolean;
rr_pts_for_game_tie: string;
rr_pts_for_game_win: string;
rr_pts_for_match_tie: string;
rr_pts_for_match_win: string;
sequential_pairings: boolean;
show_rounds: boolean;
signup_cap?: any;
start_at?: any;
started_at: string;
started_checking_in_at?: any;
state: string;
swiss_rounds: number;
teams: boolean;
tie_breaks: string[];
tournament_type: string;
updated_at: string;
url: string;
description_source: string;
subdomain?: any;
full_challonge_url: string;
live_image_url: string;
sign_up_url?: any;
review_before_finalizing: boolean;
accepting_predictions: boolean;
participants_locked: boolean;
game_name: string;
participants_swappable: boolean;
team_convertable: boolean;
group_stages_were_started: boolean;
participants: ParticipantWrapper[];
matches: MatchWrapper[];
}
export interface TournamentWrapper {
tournament: Tournament;
}
export interface MatchPost {
scores_csv: string;
winner_id: number;
}
export interface ChallongeConfig {
api_key: string;
tournament_id: string;
cache_ttl: number;
}
export class Challonge {
constructor(private config: ChallongeConfig) { }
private queue = new PQueue({ concurrency: 1 })
private log = createLogger({ name: 'challonge' });
private previous: Tournament;
private previousTime: Moment;
private async getTournamentProcess(noCache = false) {
if(!noCache && this.previous && this.previousTime.isAfter(moment().subtract(this.config.cache_ttl, 'ms'))) {
return this.previous;
}
try {
const { data: { tournament } } = await axios.get<TournamentWrapper>(
`https://api.challonge.com/v1/tournaments/${this.config.tournament_id}.json`,
{
params: {
api_key: this.config.api_key,
include_participants: 1,
include_matches: 1,
},
},
);
this.previous = tournament;
this.previousTime = moment();
return tournament;
} catch (e) {
this.log.error(`Failed to get tournament ${this.config.tournament_id}`, e);
return;
}
}
async getTournament(noCache = false) {
if (noCache) {
return this.getTournamentProcess(noCache);
}
return this.queue.add(() => this.getTournamentProcess())
}
async putScore(matchId: number, match: MatchPost, retried = 0) {
try {
await axios.put(
`https://api.challonge.com/v1/tournaments/${this.config.tournament_id}/matches/${matchId}.json`,
{
api_key: this.config.api_key,
match: match,
},
);
this.previous = undefined;
this.previousTime = undefined;
return true;
} catch (e) {
this.log.error(`Failed to put score for match ${matchId}`, e);
if (retried < 5) {
this.log.info(`Retrying match ${matchId}`);
return this.putScore(matchId, match, retried + 1);
} else {
this.log.error(`Failed to put score for match ${matchId} after 5 retries`);
return false;
}
}
}
}
......@@ -140,11 +140,8 @@
"post_score_midduel": true,
"cache_ttl": 60000,
"no_match_mode": false,
"options": {
"apiKey": "123"
},
"tournament_id": "456",
"use_custom_module": false
"api_key": "123",
"tournament_id": "456"
},
"deck_log": {
"enabled": false,
......
......@@ -12,7 +12,6 @@
"async": "^3.2.0",
"axios": "^0.19.2",
"bunyan": "^1.8.14",
"challonge": "^2.2.0",
"deepmerge": "^4.2.2",
"formidable": "^1.2.6",
"geoip-country-lite": "^1.0.0",
......@@ -22,6 +21,7 @@
"moment": "^2.29.1",
"mysql": "^2.18.1",
"node-os-utils": "^1.3.2",
"p-queue": "6.6.2",
"pg": "^6.4.2",
"q": "^1.5.1",
"querystring": "^0.2.0",
......@@ -380,11 +380,6 @@
"node": ">=4"
}
},
"node_modules/challonge": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/challonge/-/challonge-2.2.0.tgz",
"integrity": "sha512-7OhS4yZiUWCU8CBoadlB5Vb5EMb1mQuEVXkE9P8f5YYCd0LC4z3WhDN5uCx0swdpltZhPStxFY+2ZztWiN696Q=="
},
"node_modules/chownr": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
......@@ -789,6 +784,11 @@
"node": ">=4"
}
},
"node_modules/eventemitter3": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz",
"integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw=="
},
"node_modules/extend": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
......@@ -1785,6 +1785,14 @@
"node": "*"
}
},
"node_modules/p-finally": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz",
"integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=",
"engines": {
"node": ">=4"
}
},
"node_modules/p-limit": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
......@@ -1810,6 +1818,32 @@
"node": ">=8"
}
},
"node_modules/p-queue": {
"version": "6.6.2",
"resolved": "https://registry.npmjs.org/p-queue/-/p-queue-6.6.2.tgz",
"integrity": "sha512-RwFpb72c/BhQLEXIZ5K2e+AhgNVmIejGlTgiB9MzZ0e93GRvqZ7uSi0dvRF7/XIXDeNkra2fNHBxTyPDGySpjQ==",
"dependencies": {
"eventemitter3": "^4.0.4",
"p-timeout": "^3.2.0"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/p-timeout": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-3.2.0.tgz",
"integrity": "sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg==",
"dependencies": {
"p-finally": "^1.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/p-try": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
......@@ -3299,11 +3333,6 @@
"supports-color": "^5.3.0"
}
},
"challonge": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/challonge/-/challonge-2.2.0.tgz",
"integrity": "sha512-7OhS4yZiUWCU8CBoadlB5Vb5EMb1mQuEVXkE9P8f5YYCd0LC4z3WhDN5uCx0swdpltZhPStxFY+2ZztWiN696Q=="
},
"chownr": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
......@@ -3602,6 +3631,11 @@
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A=="
},
"eventemitter3": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz",
"integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw=="
},
"extend": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
......@@ -4406,6 +4440,11 @@
"resolved": "https://registry.npmjs.org/over/-/over-0.0.5.tgz",
"integrity": "sha1-8phS5w/X4l82DgE6jsRMgq7bVwg="
},
"p-finally": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz",
"integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4="
},
"p-limit": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
......@@ -4422,6 +4461,23 @@
"p-limit": "^2.2.0"
}
},
"p-queue": {
"version": "6.6.2",
"resolved": "https://registry.npmjs.org/p-queue/-/p-queue-6.6.2.tgz",
"integrity": "sha512-RwFpb72c/BhQLEXIZ5K2e+AhgNVmIejGlTgiB9MzZ0e93GRvqZ7uSi0dvRF7/XIXDeNkra2fNHBxTyPDGySpjQ==",
"requires": {
"eventemitter3": "^4.0.4",
"p-timeout": "^3.2.0"
}
},
"p-timeout": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-3.2.0.tgz",
"integrity": "sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg==",
"requires": {
"p-finally": "^1.0.0"
}
},
"p-try": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
......
......@@ -14,7 +14,6 @@
"async": "^3.2.0",
"axios": "^0.19.2",
"bunyan": "^1.8.14",
"challonge": "^2.2.0",
"deepmerge": "^4.2.2",
"formidable": "^1.2.6",
"geoip-country-lite": "^1.0.0",
......@@ -24,6 +23,7 @@
"moment": "^2.29.1",
"mysql": "^2.18.1",
"node-os-utils": "^1.3.2",
"p-queue": "6.6.2",
"pg": "^6.4.2",
"q": "^1.5.1",
"querystring": "^0.2.0",
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment