Commit c16d5980 authored by nanahira's avatar nanahira

update challonge

parent 75b91036
Pipeline #33625 passed with stages
in 10 minutes and 7 seconds
...@@ -22,7 +22,7 @@ class Challonge { ...@@ -22,7 +22,7 @@ class Challonge {
return this.previous; return this.previous;
} }
try { try {
const { data: { tournament } } = await axios_1.default.get(`https://api.challonge.com/v1/tournaments/${this.config.tournament_id}.json`, { const { data: { tournament } } = await axios_1.default.get(`${this.config.challonge_url}/v1/tournaments/${this.config.tournament_id}.json`, {
params: { params: {
api_key: this.config.api_key, api_key: this.config.api_key,
include_participants: 1, include_participants: 1,
...@@ -35,7 +35,7 @@ class Challonge { ...@@ -35,7 +35,7 @@ class Challonge {
return tournament; return tournament;
} }
catch (e) { catch (e) {
this.log.error(`Failed to get tournament ${this.config.tournament_id}`, e); this.log.error(`Failed to get tournament ${this.config.tournament_id}: ${e}`);
return; return;
} }
} }
...@@ -47,7 +47,7 @@ class Challonge { ...@@ -47,7 +47,7 @@ class Challonge {
} }
async putScore(matchId, match, retried = 0) { async putScore(matchId, match, retried = 0) {
try { try {
await axios_1.default.put(`https://api.challonge.com/v1/tournaments/${this.config.tournament_id}/matches/${matchId}.json`, { await axios_1.default.put(`${this.config.challonge_url}/v1/tournaments/${this.config.tournament_id}/matches/${matchId}.json`, {
api_key: this.config.api_key, api_key: this.config.api_key,
match: match, match: match,
}); });
...@@ -56,7 +56,7 @@ class Challonge { ...@@ -56,7 +56,7 @@ class Challonge {
return true; return true;
} }
catch (e) { catch (e) {
this.log.error(`Failed to put score for match ${matchId}`, e); this.log.error(`Failed to put score for match ${matchId}: ${e}`);
if (retried < 5) { if (retried < 5) {
this.log.info(`Retrying match ${matchId}`); this.log.info(`Retrying match ${matchId}`);
return this.putScore(matchId, match, retried + 1); return this.putScore(matchId, match, retried + 1);
...@@ -67,6 +67,34 @@ class Challonge { ...@@ -67,6 +67,34 @@ class Challonge {
} }
} }
} }
async clearParticipants() {
try {
await axios_1.default.delete(`${this.config.challonge_url}/v1/tournaments/${this.config.tournament_id}/participants/clear.json`, {
params: {
api_key: this.config.api_key
},
validateStatus: () => true,
});
return true;
}
catch (e) {
this.log.error(`Failed to clear participants for tournament ${this.config.tournament_id}: ${e}`);
return false;
}
}
async uploadParticipants(participantNames) {
try {
await axios_1.default.post(`${this.config.challonge_url}/v1/tournaments/${this.config.tournament_id}/participants/bulk_add.json`, {
api_key: this.config.api_key,
participants: participantNames.map(name => ({ name })),
});
return true;
}
catch (e) {
this.log.error(`Failed to upload participants for tournament ${this.config.tournament_id}: ${e}`);
return false;
}
}
} }
exports.Challonge = Challonge; exports.Challonge = Challonge;
//# sourceMappingURL=challonge.js.map //# sourceMappingURL=challonge.js.map
\ No newline at end of file
...@@ -2,34 +2,15 @@ import axios from 'axios'; ...@@ -2,34 +2,15 @@ import axios from 'axios';
import { createLogger } from 'bunyan'; import { createLogger } from 'bunyan';
import moment, { Moment } from 'moment'; import moment, { Moment } from 'moment';
import PQueue from 'p-queue'; import PQueue from 'p-queue';
import _ from 'underscore';
export interface Match { export interface Match {
attachment_count?: any;
created_at: string;
group_id?: any;
has_attachment: boolean;
id: number; id: number;
identifier: string; state: 'pending' | 'open' | 'complete'; // pending: 还未开始,open: 进行中,complete: 已结束
location?: any;
loser_id?: any;
player1_id: number; player1_id: number;
player1_is_prereq_match_loser: boolean;
player1_prereq_match_id?: any;
player1_votes?: any;
player2_id: number; player2_id: number;
player2_is_prereq_match_loser: boolean; winner_id?: number | 'tie'; // 如果存在,则代表该比赛已经结束
player2_prereq_match_id?: any; scores_csv: string; // 2-1
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 { export interface MatchWrapper {
...@@ -37,34 +18,8 @@ export interface MatchWrapper { ...@@ -37,34 +18,8 @@ export interface MatchWrapper {
} }
export interface Participant { export interface Participant {
active: boolean;
checked_in_at?: any;
created_at: string;
final_rank?: any;
group_id?: any;
icon?: any;
id: number; id: number;
invitation_id?: any;
invite_email?: any;
misc?: any;
name: string; 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 { export interface ParticipantWrapper {
...@@ -72,69 +27,7 @@ export interface ParticipantWrapper { ...@@ -72,69 +27,7 @@ export interface ParticipantWrapper {
} }
export interface Tournament { 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; 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[]; participants: ParticipantWrapper[];
matches: MatchWrapper[]; matches: MatchWrapper[];
} }
...@@ -145,13 +38,14 @@ export interface TournamentWrapper { ...@@ -145,13 +38,14 @@ export interface TournamentWrapper {
export interface MatchPost { export interface MatchPost {
scores_csv: string; scores_csv: string;
winner_id: number; winner_id?: number | 'tie';
} }
export interface ChallongeConfig { export interface ChallongeConfig {
api_key: string; api_key: string;
tournament_id: string; tournament_id: string;
cache_ttl: number; cache_ttl: number;
challonge_url: string;
} }
export class Challonge { export class Challonge {
...@@ -169,7 +63,7 @@ export class Challonge { ...@@ -169,7 +63,7 @@ export class Challonge {
} }
try { try {
const { data: { tournament } } = await axios.get<TournamentWrapper>( const { data: { tournament } } = await axios.get<TournamentWrapper>(
`https://api.challonge.com/v1/tournaments/${this.config.tournament_id}.json`, `${this.config.challonge_url}/v1/tournaments/${this.config.tournament_id}.json`,
{ {
params: { params: {
api_key: this.config.api_key, api_key: this.config.api_key,
...@@ -183,7 +77,7 @@ export class Challonge { ...@@ -183,7 +77,7 @@ export class Challonge {
this.previousTime = moment(); this.previousTime = moment();
return tournament; return tournament;
} catch (e) { } catch (e) {
this.log.error(`Failed to get tournament ${this.config.tournament_id}`, e); this.log.error(`Failed to get tournament ${this.config.tournament_id}: ${e}`);
return; return;
} }
} }
...@@ -198,7 +92,7 @@ export class Challonge { ...@@ -198,7 +92,7 @@ export class Challonge {
async putScore(matchId: number, match: MatchPost, retried = 0) { async putScore(matchId: number, match: MatchPost, retried = 0) {
try { try {
await axios.put( await axios.put(
`https://api.challonge.com/v1/tournaments/${this.config.tournament_id}/matches/${matchId}.json`, `${this.config.challonge_url}/v1/tournaments/${this.config.tournament_id}/matches/${matchId}.json`,
{ {
api_key: this.config.api_key, api_key: this.config.api_key,
match: match, match: match,
...@@ -208,7 +102,7 @@ export class Challonge { ...@@ -208,7 +102,7 @@ export class Challonge {
this.previousTime = undefined; this.previousTime = undefined;
return true; return true;
} catch (e) { } catch (e) {
this.log.error(`Failed to put score for match ${matchId}`, e); this.log.error(`Failed to put score for match ${matchId}: ${e}`);
if (retried < 5) { if (retried < 5) {
this.log.info(`Retrying match ${matchId}`); this.log.info(`Retrying match ${matchId}`);
return this.putScore(matchId, match, retried + 1); return this.putScore(matchId, match, retried + 1);
...@@ -218,4 +112,32 @@ export class Challonge { ...@@ -218,4 +112,32 @@ export class Challonge {
} }
} }
} }
async clearParticipants() {
try {
await axios.delete(`${this.config.challonge_url}/v1/tournaments/${this.config.tournament_id}/participants/clear.json`, {
params: {
api_key: this.config.api_key
},
validateStatus: () => true,
})
return true;
} catch (e) {
this.log.error(`Failed to clear participants for tournament ${this.config.tournament_id}: ${e}`);
return false;
}
}
async uploadParticipants(participantNames: string[]) {
try {
await axios.post(`${this.config.challonge_url}/v1/tournaments/${this.config.tournament_id}/participants/bulk_add.json`, {
api_key: this.config.api_key,
participants: participantNames.map(name => ({ name })),
});
return true;
} catch (e) {
this.log.error(`Failed to upload participants for tournament ${this.config.tournament_id}: ${e}`);
return false;
}
}
} }
...@@ -142,7 +142,8 @@ ...@@ -142,7 +142,8 @@
"cache_ttl": 60000, "cache_ttl": 60000,
"no_match_mode": false, "no_match_mode": false,
"api_key": "123", "api_key": "123",
"tournament_id": "456" "tournament_id": "456",
"challonge_url": "https://api.challonge.com"
}, },
"deck_log": { "deck_log": {
"enabled": false, "enabled": false,
......
...@@ -23,6 +23,8 @@ const auth = require('./ygopro-auth.js'); ...@@ -23,6 +23,8 @@ const auth = require('./ygopro-auth.js');
const settings = loadJSON('./config/config.json'); const settings = loadJSON('./config/config.json');
config = settings.modules.tournament_mode; config = settings.modules.tournament_mode;
challonge_config = settings.modules.challonge; challonge_config = settings.modules.challonge;
const { Challonge } = require('./challonge');
const challonge = new Challonge(challonge_config);
ssl_config = settings.modules.http.ssl; ssl_config = settings.modules.http.ssl;
const _async = require("async"); const _async = require("async");
...@@ -156,19 +158,11 @@ const UploadToChallonge = async function () { ...@@ -156,19 +158,11 @@ const UploadToChallonge = async function () {
sendResponse("读取玩家列表完毕,共有" + player_list.length + "名玩家。"); sendResponse("读取玩家列表完毕,共有" + player_list.length + "名玩家。");
try { try {
sendResponse("开始清空 Challonge 玩家列表。"); sendResponse("开始清空 Challonge 玩家列表。");
await axios.delete(`https://api.challonge.com/v1/tournaments/${challonge_config.tournament_id}/participants/clear.json`, { await challonge.clearParticipants();
params: {
api_key: challonge_config.api_key
},
validateStatus: () => true,
});
sendResponse("开始上传玩家列表至 Challonge。"); sendResponse("开始上传玩家列表至 Challonge。");
for (const chunk of _.chunk(player_list, 10)) { for (const chunk of _.chunk(player_list, 10)) {
sendResponse(`开始上传玩家 ${chunk.join(', ')} 至 Challonge。`); sendResponse(`开始上传玩家 ${chunk.join(', ')} 至 Challonge。`);
await axios.post(`https://api.challonge.com/v1/tournaments/${challonge_config.tournament_id}/participants/bulk_add.json`, { await challonge.uploadParticipants(chunk);
api_key: challonge_config.api_key,
participants: chunk.map(name => ({ name })),
});
} }
sendResponse("玩家列表上传完成。"); sendResponse("玩家列表上传完成。");
} catch (e) { } catch (e) {
......
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