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