Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
challonge-migrator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
challonge-migrator
Commits
15108e61
Commit
15108e61
authored
Jan 31, 2021
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix
parent
5316d049
Pipeline
#2294
passed with stages
in 32 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
16 deletions
+16
-16
migrate.ts
migrate.ts
+16
-16
No files found.
migrate.ts
View file @
15108e61
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
)));
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment