-
Notifications
You must be signed in to change notification settings - Fork 2
/
slp_rename.js
executable file
·153 lines (132 loc) · 4.17 KB
/
slp_rename.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const slp = require('slp-parser-js');
const { default: SlippiGame } = require('slp-parser-js');
const argv = require('yargs')
.usage('Usage $0 [options] <directories>')
.demandCommand(1, 'You must provide directories to rename.')
.boolean('n')
.describe('n', 'perform a trial run without renaming')
.boolean('r')
.describe('r', 'rename in subdirectories too')
.help('h')
.argv
/** Returns character with their tag or color in parentheses (if they have either). */
function playerName(player, metadata) {
const character = slp.characters.getCharacterName(player.characterId);
const color = slp.characters.getCharacterColorName(player.characterId, player.characterColor);
let playerIds = [];
if (player.nametag) {
playerIds.push(player.nametag);
} else if (color !== 'Default') {
playerIds.push(color);
}
if (metadata && metadata.names.netplay !== 'Player' && metadata.names.netplay !== undefined) {
playerIds.push(metadata.names.netplay);
}
if (playerIds.length > 0) {
return `${character} (${playerIds.join(',')})`;
} else {
return character;
}
}
function prettyPrintTeams(settings, metadata) {
const stage = slp.stages.getStageName(settings.stageId);
const teams = new Map();
for (let i = 0; i < settings.players.length; i++) {
let player = settings.players[i];
if (!teams.has(player.teamId)) {
teams.set(player.teamId, []);
}
if (metadata) {
teams.get(player.teamId).push(playerName(player, metadata.players[i]));
} else {
teams.get(player.teamId).push(playerName(player));
}
}
const pretty = Array.from(teams.values())
.map(team => team.join(' & '))
.join(' vs ');
return `${pretty} - ${stage}`;
}
function prettyPrintSingles(settings, metadata) {
// kind of annoying that some games don't have metadata
let player1, player2;
if (metadata) {
player1 = playerName(settings.players[0], metadata.players[0]);
player2 = playerName(settings.players[1], metadata.players[1]);
} else {
player1 = playerName(settings.players[0]);
player2 = playerName(settings.players[1]);
}
const stage = slp.stages.getStageName(settings.stageId);
return `${player1} vs ${player2} - ${stage}`;
}
function parsedFilename(settings, metadata, file) {
const dateRegex = file.match('_([^\.]+)');
let datePrefix = null;
if (!dateRegex) {
if (!metadata) {
return null;
}
const dateStr = metadata.startAt.replace(/[-:]/g, '');
datePrefix = dateStr.substring(0, dateStr.length - 1);
} else {
datePrefix = dateRegex[1];
}
let pretty = null;
if (settings.isTeams) {
pretty = prettyPrintTeams(settings, metadata);
} else {
pretty = prettyPrintSingles(settings, metadata);
}
if (!pretty) {
return null;
}
return `${datePrefix} - ${pretty}.slp`
}
function isDirectory(dir) {
const stats = fs.lstatSync(dir);
return stats && stats.isDirectory();
}
const directories = argv._;
while (directories.length > 0) {
const dir = directories.pop();
if (!isDirectory(dir)) {
console.log(`${dir} is not a directory, skipping.`);
continue;
}
console.log(`Searching ${dir} for slp files.`);
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
if (argv.r && isDirectory(filePath)) {
directories.push(filePath);
continue;
} else if (!file.match('\.slp$')) {
console.log(`'${file}' skipped.`);
continue;
}
const game = new SlippiGame(filePath);
const settings = game.getSettings();
const metadata = game.getMetadata();
const newName = parsedFilename(settings, metadata, file);
if (!newName) {
console.log(`Error parsing '${file}'`);
continue;
}
const newPath = path.join(dir, newName);
if (!argv.n) {
fs.rename(filePath, newPath, err => {
if (err) {
console.log(`Error renaming ${filePath}: ${err}`);
} else {
console.log(`Renamed: ${file} -> ${newName}`);
}
});
} else {
console.log(`${file} -> ${newName}`);
}
}
}