Commit 15108e61 authored by nanahira's avatar nanahira

fix

parent 5316d049
Pipeline #2294 passed with stages
in 32 seconds
import {axiosPostConfig, Config, loadConfig, Match, MatchData, Participant, ParticipantData} from "./src/config";
import { axiosPostConfig, Config, loadConfig, Match, MatchData, Participant, ParticipantData } from "./src/config";
import axios from "axios";
import qs from "querystring";
......@@ -11,7 +11,7 @@ let newParticipantNameMap: Map<string, Participant> = new Map();
let newParticipantIdMap: Map<number, Participant> = new Map();
async function getTournamentData(tournamentUrl: string) {
const {data: {tournament}} = await axios.get(` https://api.challonge.com/v1/tournaments/${tournamentUrl}.json`, {
const { data: { tournament } } = await axios.get(` https://api.challonge.com/v1/tournaments/${tournamentUrl}.json`, {
responseType: "json",
paramsSerializer: qs.stringify,
params: {
......@@ -22,14 +22,14 @@ async function getTournamentData(tournamentUrl: string) {
});
const participants = (tournament.participants as ParticipantData[]).map(pd => pd.participant);
const matches = (tournament.matches as MatchData[]).map(pd => pd.match);
return {participants, matches}
return { participants, matches }
}
async function loadOldTournamentData() {
const {participants, matches} = await getTournamentData(config.sourceTournament);
const { participants, matches } = await getTournamentData(config.sourceTournament);
oldParticipantNameMap.clear();
oldParticipantIdMap.clear();
for(let participant of participants) {
for (let participant of participants) {
//console.log(`Inserting old participant ${participant.id} ${participant.name}`);
oldParticipantNameMap.set(participant.name, participant);
oldParticipantIdMap.set(participant.id, participant);
......@@ -38,10 +38,10 @@ async function loadOldTournamentData() {
}
async function loadNewTournamentData() {
const {participants, matches} = await getTournamentData(config.targetTournament);
const { participants, matches } = await getTournamentData(config.targetTournament);
newParticipantIdMap.clear();
newParticipantNameMap.clear();
for(let participant of participants) {
for (let participant of participants) {
//console.log(`Inserting new participant ${participant.id} ${participant.name}`);
newParticipantNameMap.set(participant.name, participant);
newParticipantIdMap.set(participant.id, participant);
......@@ -51,11 +51,11 @@ async function loadNewTournamentData() {
async function checkQuit(round: number, oldPlayerId: number) {
const oldPlayer = oldParticipantIdMap.get(oldPlayerId);
if(oldPlayer.active) {
if (oldPlayer.active) {
return;
}
const nextRoundMatch = oldMatches.find(m => m.round === round + 1 && (m.player1_id === oldPlayerId || m.player2_id === oldPlayerId));
if(!nextRoundMatch) {
if (!nextRoundMatch) {
const newPlayerId = newParticipantNameMap.get(oldPlayer.name).id;
console.log(`Player ${oldPlayer.name}: ${oldPlayerId}/${newPlayerId} quitted in round ${round}.`);
try {
......@@ -73,19 +73,19 @@ async function migrateMatch(match: Match) {
const currentMatchPlayer1Name = newParticipantIdMap.get(match.player1_id).name;
const currentMatchPlayer2Name = newParticipantIdMap.get(match.player2_id).name;
const oldMatch = oldMatches.find(m => {
if(m.round !== match.round) {
if (m.round !== match.round) {
return false;
}
const oldMatchPlayer1Name = oldParticipantIdMap.get(m.player1_id).name;
const oldMatchPlayer2Name = oldParticipantIdMap.get(m.player2_id).name;
return currentMatchPlayer1Name === oldMatchPlayer1Name && oldMatchPlayer2Name === currentMatchPlayer2Name;
});
if(!oldMatch) {
if (!oldMatch) {
console.error(`Match for ${currentMatchPlayer1Name} vs ${currentMatchPlayer2Name} not found.`);
return;
}
let winner_id: string | number;
if (typeof(oldMatch.winner_id) === "number" && oldParticipantIdMap.has(oldMatch.winner_id)) {
if (typeof (oldMatch.winner_id) === "number" && oldParticipantIdMap.has(oldMatch.winner_id)) {
const winnerName = oldParticipantIdMap.get(oldMatch.winner_id as number).name;
winner_id = newParticipantNameMap.get(winnerName).id;
console.log(`Migrating match ${currentMatchPlayer1Name} vs ${currentMatchPlayer2Name} with winner ${winnerName}.`);
......@@ -116,13 +116,13 @@ async function main() {
config = await loadConfig();
console.log(`Reading old tournament ${config.sourceTournament}.`);
await loadOldTournamentData();
for(let round = 1; ;++round) {
for (let round = 1; round < config.maxRounds; ++round) {
console.log(`Handling round ${round}.`);
await loadNewTournamentData();
const matches = newMatches.filter(m => m.round === round && m.state === "open");
if(!matches.length) {
console.log(`No available matches in round ${round}. exiting.`);
break;
if (!matches.length) {
console.log(`No available matches in round ${round}.`);
continue;
}
await Promise.all(matches.map(match => migrateMatch(match)));
}
......
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