-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReplayRenamer.py
1015 lines (939 loc) · 39.7 KB
/
ReplayRenamer.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sc2reader
import PySimpleGUI as sg
from loguru import logger
from typing import List, Dict, Set, Union, Optional
import json
import getpass
import os
import shutil
import zipfile
from pathlib import Path
from sc2reader.resources import Replay
"""
Replay data variables:
build: int - the current sc2 build version
category: str - the category the game was played in, e.g. "Ladder"
clients: list - list of Participants
Participant:
clan_tag: str - the clan tag
highest_leage: int - max league the player reached in this game mode? 6 = master league
is_human: bool - if participant is human
is_observer: bool - if participant is observing
is_referee: bool - if participant is referee
name: str - sc2 ingame account name
pick_race: str - race the player picked at loading screen
play_race: str - the race the player got in game (e.g. if pick race was random)
region: str - region it was played in (e.g. "eu")
result: str - outcome of the game for participant (e.g. "Loss")
toon_handle: str - "2-S2-1-727565"
url: str - "http://eu.battle.net/sc2/en/profile/727565/1/BuRny/"
init_data: dict: {
"scaled_rating": int - the mmr
}
competetive: int - if game was in competetive? 1 for true 0 for false
computers: list - list of Computer players
cooperative: int - if game was coop? 1 for true 0 for false
date: datetime - when the game was played/started (same as "end_time"?)
expansion: str - E.g. "LotV"
filename: str - replay path
frames: int - amount of frames this game was long
game_length: Length - same as "length"
days: int
hours: int
mins: int
secs: int
seconds: int - the total amount of seconds of the game
game_type: str - e.g. "1v1"
humans: List[Participant] - list of human players
is_ladder: bool
is_private: bool
map_name: str - e.g. "Catalyst LE"
messages: list - List[ChatEvent] -
frame: int
player: Participant
second: int
text: str
to_all: bool
ranked: None ??
real_type: str - e.g. "1v1"
region: str - e.g. "eu"
release_string: str - e.g. "4.1.4.61545"
resume_from_replay: bool - if resumed from replay
speed: str - e.g. "Faster"
start_time: datetime - when the game was started
time_zone: float - GMT + X
type: str - e.g. "1v1"
unix_timestamp: int
Renaming pattern variables:
$build: replay.build, "61545"
$category: replay.category, "Ladder", "Private", "Public"
$expansion: replay.expansion, "LotV"
$EXPANSION: replay.expansion.upper(), "LOTV"
$gametype: replay.game_type, "1v1"
$playersamount: len(replay.players), 1, 2, 3
$mapname: replay.map_name
$durationmins: replay.length.mins, 11
$durationsecs: replay.length.secs, 2
$durationtotalsecs: replay.length.seconds, "661"
$version: replay.release_string.split(".")[:-1]
$region: replay.region
$REGION: replay.region.upper()
$avgmmr: average mmr of all players (only works in ranked matchmaking), 0 default
$year
$month
$day
$hour
$min
$sec
$p1name: "Burny"
$p1mmr: "2500"
$p1race: "Zerg"
$p1r: "Z"
"""
class RenamerGUI:
def __init__(self):
# Get pc username (for replay file path)
self.username = getpass.getuser()
# Change the look of PySimpleGui
sg.ChangeLookAndFeel("BrownBlue") # BrownBlue, Dark, Black
# Settings dictionary
self.settings: dict = {}
self.old_settings: dict = {}
# Program folder
self.path = Path(__file__).parent
self.settings_path = self.path / "settings.json"
# Renamer object
self.replay_renamer = ReplayRenamer()
def load_defaults(self):
"""Set default values to settings before loading settings.json file from disk"""
self.settings.update(
{
"rename_pattern": "$gametype $year-$month-$day $hour-$min-$sec $t1names($t1races) $t1mmr vs $t2mmr ($t2races)$t2names - $durationmins mins on $mapname on $version",
"source_path": f"C:/Users/{self.username}/Documents/StarCraft II/Accounts",
"target_path": f"C:/MyReplays",
"replay_file_operation": "Copy",
"sort_winner": True,
"enable_filter": True,
"exclude_matchmaking": False,
"exclude_custom_games": False,
"exclude_games_with_ai": False,
"exclude_draws": False,
"exclude_resume_from_replay": False,
"wol": True,
"hots": True,
"lotv": True,
"match_names": "",
"exclude_names": "",
"game_version_min": "",
"game_version_max": "",
"game_length_min": "",
"game_length_max": "",
"players_min": "",
"players_max": "",
"avg_mmr_min": "",
"avg_mmr_max": "",
"include_matchups": "",
"exclude_matchups": "",
"exclude_maps": "",
"show_errors": False,
"zip_replays": False,
}
)
def load_settings(self):
"""Load settings from file if it exists"""
if self.settings_path.is_file():
try:
with self.settings_path.open() as f:
saved_data: dict = json.load(f)
self.settings.update(saved_data)
except Exception as e:
logger.trace(f"Error loading settings.json")
sg.Print(f"Error loading the settings.json:\n{e}")
def save_settings(self):
"""Save settings to file"""
if self.old_settings != self.settings:
# Fix for multiline: keeps appending \n at the end
if "rename_pattern" in self.settings:
self.settings["rename_pattern"] = self.settings[
"rename_pattern"
].strip()
with self.settings_path.open("w") as f:
json.dump(self.settings, f, indent=2)
logger.info("Saved settings")
self.old_settings = self.settings
def apply_settings_to_gui(self):
"""Apply settings to GUI. Takes a dictionary with key as element names (key=...) and value as element values"""
self.window.Fill(self.settings)
# for element_id, element_value in self.settings.items():
# print(f"Updating element {element_id} with value {element_value}")
# self.window.FindElement(key=element_id).Update(value=element_value)
def load_gui_into_settings(self, values: Dict[str, str]):
"""Update settings from GUI input values"""
self.settings.update(values)
def verify_entered_data(self, values: Dict[str, str]):
"""Function to check if all the entered GUI data is valid (returns True if parsed successfully."""
comma_seperated_elements = {
"match_names",
"exclude_names",
"include_matchups",
"exclude_matchups",
"exclude_maps",
}
number_elements = {
"players_min",
"players_max",
"game_length_min",
"game_length_max",
"avg_mmr_min",
"avg_mmr_max",
}
# Check game version tuple
if values["game_version_min"] != "" and values["game_version_max"] != "":
if tuple(values["game_version_min"]) > tuple(values["game_version_max"]):
low = values["game_version_min"]
high = values["game_version_max"]
sg.Print(
f"Rightmost game version needs to be higher than the left game version, you entered '{low}' and '{high}'"
)
return False
# Check replay rename pattern
not_allowed_characters = {"\\", "/", "*", "?", '"', "<", ">", "|"}
for character in values["rename_pattern"]:
if character in not_allowed_characters:
pattern = values["rename_pattern"]
sg.Print(
f"Character '{character}' not allowed in renaming pattern, not allowed characters: {str(not_allowed_characters)}, your pattern is: {pattern}"
)
return False
# Check comma seperated elements and number elements that need to be converted from string
for element_id, element_value in values.items():
if element_value != "" and element_id in number_elements:
try:
_ = self.replay_renamer.convert_string_to_int(element_value)
except Exception as e:
sg.Print(
f"Invalid entered data in field {element_id}, needs to be a number but you entered '{element_value}'"
)
return False
if element_id in comma_seperated_elements:
try:
_ = self.replay_renamer.split_values(element_value)
except Exception as e:
sg.Print(
f"Invalid entered data in field {element_id}, unable to parse value. You entered {element_value}, error:\n{e}"
)
return False
return True
def rename_replays(self, values: Dict[str, str]):
replay_renamer = ReplayRenamer()
# replays_iterator = replay_renamer.load_replays(values["source_path"])
target_folder_path = Path(values["target_path"])
if not target_folder_path.is_dir():
try:
os.makedirs(target_folder_path, exist_ok=True)
except Exception as e:
sg.Print(
f"Could not create target folder: {target_folder_path}, error:\n{e}"
)
return
# Replays path with {replay_source_path: replay_target_path}
scheduled_replays: Dict[Path, Path] = {}
source_path = Path(values["source_path"])
replay_paths: List[Path] = [
file for file in source_path.iterdir() if file.is_file()
]
replay_paths.sort(key=lambda i: i.name)
for replay_path in replay_paths:
logger.info(f"Loading replay: {replay_path}")
try:
replay = replay_renamer.load_replay(replay_path)
except IndexError as e:
sg.Print(f"Error loading AI Replay '{replay_path}'\nError: {e}")
continue
except Exception as e:
logger.exception(f"Error loading replay")
sg.Print(f"Error loading replay '{replay_path}'\nError: {e}")
continue
filter_return_value = replay_renamer.does_replay_pass_filter(replay, values)
replay_file_name = replay_path.stem
if filter_return_value is True:
source_replay_path = replay_path
if values["replay_file_operation"] == "Rename":
target_file_name: str = replay_renamer.get_replay_rename_name(
replay, values
)
target_replay_path = source_replay_path.parent / target_file_name
else:
target_file_name = replay_renamer.get_replay_rename_name(
replay, values
)
target_replay_path = target_folder_path / target_file_name
scheduled_replays[source_replay_path] = target_replay_path
elif values["show_errors"]:
# Replay did not pass the filter
sg.Print(
f"Replay '{replay_file_name}' did not pass the filter because of filter: {filter_return_value}"
)
if scheduled_replays:
# List of replays that were copied / moved / renamed: [path1, path2, path3]
successful_target_files = []
if values["replay_file_operation"] == "Copy":
successful_target_files = self.replay_renamer.copy_replays(
scheduled_replays, values
)
elif values["replay_file_operation"] == "Move":
successful_target_files = self.replay_renamer.move_replays(
scheduled_replays, values
)
elif values["replay_file_operation"] == "Rename":
successful_target_files = self.replay_renamer.rename_replays(
scheduled_replays, values
)
# Zip succesfully copied replays
if successful_target_files and values["zip_replays"]:
zip_path = target_folder_path / "Replays.zip"
self.create_zip_archive(zip_path, successful_target_files)
logger.info("Done!")
def create_zip_archive(self, zip_path: Path, files_to_archive: List[Path]):
target_zip_path = zip_path
count = 1
# Increment zip file if it already exists
while target_zip_path.is_file():
target_zip_path = f"{zip_path} ({count})"
count += 1
# Would use LZMA for better compression but it takes too long and replays are very compressed already
# with zipfile.ZipFile(target_zip_path, "w", compression=zipfile.ZIP_LZMA) as f:
with zipfile.ZipFile(
target_zip_path, "w", compression=zipfile.ZIP_DEFLATED
) as f:
for file_path in files_to_archive:
f.write(file_path, arcname=file_path.name)
def handle_events(self, event: str, values: Dict[str, str]):
if event == "rename_replays":
self.load_gui_into_settings(values)
self.save_settings()
if self.verify_entered_data(values):
self.rename_replays(values)
def handle_exit(self, event: Optional[str], values: Optional[Dict[str, str]]):
"""If the program is abrupty closed (pressing the X or using the exit button), save settings to file"""
if values is not None:
self.load_gui_into_settings(values)
self.save_settings()
def run(self):
replay_path = f"C:/Users/{self.username}/Documents/StarCraft II/Accounts"
first_column_width = 22
two_column_width = 25
one_column_width = two_column_width * 2 + 2
self.load_defaults()
self.load_settings()
layout = [
[
sg.Text("Rename Pattern", size=(first_column_width, None)),
sg.Multiline(
"",
size=(one_column_width, 3),
do_not_clear=True,
key="rename_pattern",
),
],
[
sg.Text("Replays Folder", size=(first_column_width, None)),
sg.InputText(
f"{replay_path}",
key="source_path",
do_not_clear=True,
change_submits=True,
),
sg.FolderBrowse(
"Browse",
initial_folder=self.settings["source_path"],
target="source_path",
),
],
[
sg.Text("Target Folder", size=(first_column_width, None)),
sg.InputText(
"", do_not_clear=True, key="target_path", change_submits=True
),
sg.FolderBrowse(
"Browse", initial_folder=replay_path, target="target_path"
),
],
[
sg.Text("Replay File Operation", size=(first_column_width, None)),
sg.InputCombo(
["Copy", "Move", "Rename"],
key="replay_file_operation",
readonly=True,
),
],
[
sg.Checkbox(
"Winner is team 1 / player 1",
key="sort_winner",
change_submits=True,
)
],
[sg.Checkbox("Enable Filter", key="enable_filter", change_submits=True)],
[
sg.Checkbox(
"Exclude Matchmaking",
key="exclude_matchmaking",
change_submits=True,
)
],
[
sg.Checkbox(
"Exclude Custom Games",
key="exclude_custom_games",
change_submits=True,
)
],
[
sg.Checkbox(
"Exclude Games with AI",
key="exclude_games_with_ai",
change_submits=True,
)
],
[sg.Checkbox("Exclude Draws", key="exclude_draws", change_submits=True)],
[
sg.Checkbox(
"Exclude Resumed from Replay",
key="exclude_resume_from_replay",
change_submits=True,
)
],
[
sg.Text("Expansions", size=(first_column_width, None)),
sg.Checkbox("WoL", key="wol"),
sg.Checkbox("HotS", key="hots"),
sg.Checkbox("LotV", key="lotv"),
],
[
sg.Text(
"Match Names",
size=(first_column_width, None),
tooltip="This replay will only be renamed if one of the names is in the replay (not case sensitive)",
),
sg.Input(
"",
key="match_names",
do_not_clear=True,
change_submits=True,
size=(one_column_width, None),
tooltip="burny, brain",
),
],
[
sg.Text(
"Exclude Names",
size=(first_column_width, None),
tooltip="This replay will only be renamed if none of the names is in the replay (not case sensitive)",
),
sg.Input(
"",
key="exclude_names",
do_not_clear=True,
change_submits=True,
size=(one_column_width, None),
tooltip="burny, brain",
),
],
[
sg.Text("Game Version", size=(first_column_width, None)),
sg.Input(
"",
key="game_version_min",
do_not_clear=True,
change_submits=True,
size=(two_column_width, None),
tooltip="4.6.0",
),
sg.Input(
"",
key="game_version_max",
do_not_clear=True,
change_submits=True,
size=(two_column_width, None),
tooltip="4.8.2",
),
],
[
sg.Text("Game Length (Minutes)", size=(first_column_width, None)),
sg.Input(
"",
key="game_length_min",
do_not_clear=True,
change_submits=True,
size=(two_column_width, None),
tooltip="3",
),
sg.Input(
"",
key="game_length_max",
do_not_clear=True,
change_submits=True,
size=(two_column_width, None),
tooltip="40",
),
],
[
sg.Text("Amount of Players", size=(first_column_width, None)),
sg.Input(
"",
key="players_min",
do_not_clear=True,
change_submits=True,
size=(two_column_width, None),
tooltip="2",
),
sg.Input(
"",
key="players_max",
do_not_clear=True,
change_submits=True,
size=(two_column_width, None),
tooltip="8",
),
],
[
sg.Text("Average MMR", size=(first_column_width, None)),
sg.Input(
"",
key="avg_mmr_min",
do_not_clear=True,
change_submits=True,
size=(two_column_width, None),
tooltip="3500",
),
sg.Input(
"",
key="avg_mmr_max",
do_not_clear=True,
change_submits=True,
size=(two_column_width, None),
tooltip="4500",
),
],
[
sg.Text("Include Matchups", size=(first_column_width, None)),
sg.Input(
"",
key="include_matchups",
do_not_clear=True,
change_submits=True,
size=(one_column_width, None),
tooltip="tvz, pvx",
),
],
[
sg.Text("Exclude Matchups", size=(first_column_width, None)),
sg.Input(
"",
key="exclude_matchups",
do_not_clear=True,
change_submits=True,
size=(one_column_width, None),
tooltip="zvx, tvt",
),
],
[
sg.Text("Exclude Maps", size=(first_column_width, None)),
sg.Input(
"",
key="exclude_maps",
do_not_clear=True,
change_submits=True,
size=(one_column_width, None),
tooltip="catalyst, neon violet",
),
],
[sg.Checkbox("Show Errors", key="show_errors")],
[sg.Checkbox("Zip Replays after Renaming", key="zip_replays")],
[
sg.Button("Rename Replays", key="rename_replays", size=(10, 3)),
sg.Button("Exit", key="exit", size=(10, 3)),
],
]
self.window = sg.Window("Replay Renamer").Layout(layout)
# Required so settings can be applied (checkboxes are missing if this method is missing)
self.window.Finalize()
# Load settings before going into event loop
self.apply_settings_to_gui()
while True:
# Event Loop
event, values = self.window.Read()
logger.info(event, values)
# X was pressed
if event is None:
self.handle_exit(event, values)
break
# "Exit" was pressed
elif event == "exit":
self.handle_exit(event, values)
break
else:
self.handle_events(event, values)
self.window.Close()
class ReplayRenamer:
def __init__(self):
self.settings = {}
self.renamer_path = Path(__file__)
def load_replay(self, replay_path: Path):
"""Loads a single replay"""
replay_path = str(replay_path.resolve())
return sc2reader.load_replay(replay_path, load_level=2, load_map=False)
def load_replays(self, folder_path: Path):
"""Loads multiple replays as generator"""
folder_path = str(folder_path.resolve())
return sc2reader.load_replays(folder_path, load_level=2, load_map=False)
def convert_string_to_int(self, string: str) -> int:
return int(float(string.strip()))
def split_values(self, string: str) -> Set[str]:
"""Split values from
"my, words, comma, seperated"
to
["my", "words", "comma", "seperated"]"""
return {x.strip().lower() for x in string.strip().split(",")}
def convert_matchup_string(self, string: str) -> List[str]:
"""Input "TXvZX" becomes ["TX", "XZ"]
Input "TvX" becomes ["T", "X"]"""
split = string.lower().split("v")
first = "".join(sorted(split[0])).upper()
second = "".join(sorted(split[1])).upper()
return [first, second]
def match_matchup(
self, matchup: List[str], valid_matchups: List[List[str]]
) -> bool:
"""Matches ["T", "P"] with [["T", "X"]]
and ["TZ", "ZP"] with [["XX", "XZ"]]"""
assert len(matchup) == 2
for valid_matchup in valid_matchups:
matchup1, matchup2 = matchup
valid1, valid2 = valid_matchup
if len(matchup1) != len(valid1):
continue
if len(matchup2) != len(valid2):
continue
is_valid_match1 = all(
v == "X" or m == v for m, v in zip(matchup1, valid1)
) and all(v == "X" or m == v for m, v in zip(matchup2, valid2))
is_valid_match2 = all(
v == "X" or m == v for m, v in zip(matchup2, valid1)
) and all(v == "X" or m == v for m, v in zip(matchup1, valid2))
if is_valid_match1 or is_valid_match2:
return True
return False
def does_replay_pass_filter(
self, replay: Replay, values: Dict[str, str]
) -> Union[bool, str]:
if values["enable_filter"]:
if values["exclude_matchmaking"] and replay.is_ladder:
return "Exclude Matchmaking"
if values["exclude_custom_games"] and replay.is_private:
return "Exclude Custom Games"
if values["exclude_games_with_ai"] and len(replay.computers) > 0:
return "Exclude Games with AI"
if values["exclude_resume_from_replay"] and replay.resume_from_replay:
return "Exclude Resume from Replay"
if not values["wol"] and replay.expansion.lower() == "wol":
return "WoL"
if not values["hots"] and replay.expansion.lower() == "hots":
return "HotS"
if not values["lotv"] and replay.expansion.lower() == "lotv":
return "LotV"
if values["match_names"] != "":
split_names = self.split_values(values["match_names"])
at_least_one_player_found = any(
player.name.lower() in split_names for player in replay.players
)
if not at_least_one_player_found:
return "Match Names"
if values["exclude_names"] != "":
split_names = self.split_values(values["exclude_names"])
at_least_one_player_found = any(
player.name.lower() in split_names for player in replay.players
)
if at_least_one_player_found:
return "Exclude Names"
if values["game_version_min"] != "" or values["game_version_max"] != "":
release_tuple = tuple(replay.release_string)
min_value = values["game_version_min"]
max_value = values["game_version_max"]
if not (
(min_value == "" or tuple(min_value) <= release_tuple)
and (max_value == "" or release_tuple <= tuple(max_value))
):
return "Game Version"
conv = self.convert_string_to_int
if values["game_length_min"] != "" or values["game_length_max"] != "":
game_length = replay.length.mins + replay.length.secs / 60
min_value = values["game_length_min"]
max_value = values["game_length_max"]
if not (
(min_value == "" or conv(min_value) <= game_length)
and (max_value == "" or game_length <= conv(max_value))
):
return "Game Length"
if values["players_min"] != "" or values["players_max"] != "":
players_amount = len(replay.players)
min_value = values["players_min"]
max_value = values["players_max"]
if not (
(min_value == "" or conv(min_value) <= players_amount)
and (max_value == "" or players_amount <= conv(max_value))
):
return "Player Limit"
if (
values["avg_mmr_min"] != ""
or values["avg_mmr_max"] != ""
and len(replay.teams) >= 2
):
avg_mmr = sum(
player.init_data.get("scaled_rating", 0) or 0
if hasattr(player, "init_data")
else 0
for player in replay.players
) / len(replay.players)
min_value = values["avg_mmr_min"]
max_value = values["avg_mmr_max"]
if avg_mmr != 0 and not (
(min_value == "" or conv(min_value) <= avg_mmr)
and (max_value == "" or avg_mmr <= conv(max_value))
):
return "Average MMR"
if len(replay.teams) == 2 and len(replay.players) == 2:
races_team1 = "".join(sorted(replay.teams[0].lineup))
races_team2 = "".join(sorted(replay.teams[1].lineup))
matchup = [races_team1, races_team2]
if values["include_matchups"] != "":
# Convert "TvX, ZvX" to [["T", "X"], ["Z", "X"]]
# Or "TXvZX" to [["TX", "XZ"]] <- XZ because sorting alphabetically
comma_seperated_matchups = self.split_values(
values["include_matchups"]
)
valid_matchups = [
self.convert_matchup_string(x) for x in comma_seperated_matchups
]
is_match = self.match_matchup(matchup, valid_matchups)
if not is_match:
return "Include Matchups"
if values["exclude_matchups"] != "":
comma_seperated_matchups = self.split_values(
values["exclude_matchups"]
)
valid_matchups = [
self.convert_matchup_string(x) for x in comma_seperated_matchups
]
is_match = self.match_matchup(matchup, valid_matchups)
if is_match:
return "Exclude Matchups"
if values["exclude_maps"] != "":
comma_seperated_maps = self.split_values(values["exclude_maps"])
is_match = any(
map_name.lower() in replay.map_name.lower()
for map_name in comma_seperated_maps
)
if is_match:
return "Exclude Maps"
return True
def get_replay_values(self, replay: Replay, values: Dict[str, str]) -> dict:
# print(hasattr(replay.players[0], "init_data"))
# print(hasattr(replay.players[1], "init_data"))
player_data = []
player_data.append(
[
# Player name
replay.players[0].name,
# Player mmr, computers dont have "init_data" and it can be "None" if it wasn't a ranked game
replay.players[0].init_data.get("scaled_rating", 0) or 0
if hasattr(replay.players[0], "init_data")
else 0,
# Player race
replay.players[0].play_race,
]
)
if len(replay.players) > 1:
player_data.append(
[
replay.players[1].name,
replay.players[1].init_data.get("scaled_rating", 0) or 0
if hasattr(replay.players[1], "init_data")
else 0,
replay.players[1].play_race,
]
)
else:
player_data.append(["None", 0, "None"])
team_data = []
team_data.append(
[
# Player names
" ".join(player.name for player in replay.teams[0].players),
# Sum of player mmr
sum(
player.init_data.get("scaled_rating", 0) or 0
if hasattr(player, "init_data")
else 0
for player in replay.teams[0].players
)
// len(replay.teams[0].players),
# Races in short (e.g. "TZPT")
replay.teams[0].lineup,
]
)
if len(replay.teams) > 1:
team_data.append(
[
" ".join(player.name for player in replay.teams[1].players),
sum(
player.init_data.get("scaled_rating", 0) or 0
if hasattr(player, "init_data")
else 0
for player in replay.teams[1].players
)
// len(replay.teams[1].players),
replay.teams[1].lineup,
]
)
else:
team_data.append(["", 0, ""])
return {
"$build": replay.build,
"$category": replay.category,
"$expansion": replay.expansion,
"$EXPANSION": replay.expansion.upper(),
"$gametype": replay.real_type,
# "$gametype": replay.game_type,
"$playersamount": len(replay.players),
"$mapname": replay.map_name,
"$durationmins": replay.length.mins,
"$durationsecs": f"{replay.length.secs:02d}",
"$durationtotalsecs": replay.length.seconds,
"$version": ".".join(replay.release_string.split(".")[:-1]),
"$region": replay.region,
"$REGION": replay.region.upper(),
"$avgmmr": sum(
player.init_data.get("scaled_rating", 0) or 0
if hasattr(player, "init_data")
else 0
for player in replay.players
)
// len(replay.players)
if len(replay.players) > 0
else 0,
"$year": f"{replay.start_time.year:02d}",
"$month": f"{replay.start_time.month:02d}",
"$day": f"{replay.start_time.day:02d}",
"$hour": f"{replay.start_time.hour:02d}",
"$min": f"{replay.start_time.minute:02d}",
"$minute": f"{replay.start_time.minute:02d}",
"$sec": f"{replay.start_time.second:02d}",
"$second": f"{replay.start_time.second:02d}",
# Player data
"$p1name": player_data[replay.winner.number - 1][0]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else player_data[0][0],
"$p1mmr": player_data[replay.winner.number - 1][1]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else player_data[0][1],
"$p1race": player_data[replay.winner.number - 1][2]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else player_data[0][2],
"$p1r": player_data[replay.winner.number - 1][2][0]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else player_data[0][2][0],
"$p2name": player_data[2 - replay.winner.number][0]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else player_data[1][0],
"$p2mmr": player_data[2 - replay.winner.number][1]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else player_data[1][1],
"$p2race": player_data[2 - replay.winner.number][2]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else player_data[1][2],
"$p2r": player_data[2 - replay.winner.number][2][0]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else player_data[1][2][0],
# Team data
"$t1names": team_data[replay.winner.number - 1][0]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else team_data[0][0],
"$t1mmr": team_data[replay.winner.number - 1][1]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else team_data[0][1],
"$t1races": team_data[replay.winner.number - 1][2]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else team_data[0][2],
"$t2names": team_data[2 - replay.winner.number][0]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else team_data[1][0],
"$t2mmr": team_data[2 - replay.winner.number][1]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else team_data[1][1],
"$t2races": team_data[2 - replay.winner.number][2]
if replay.winner and replay.winner.number < 3 and values["sort_winner"]
else team_data[1][2],
}
def get_replay_rename_name(self, replay: Replay, values: Dict[str, str]) -> str:
replay_info = self.get_replay_values(replay, values)
replay_target_name = values["rename_pattern"].strip()
for variable, value in replay_info.items():
replay_target_name = replay_target_name.replace(variable, str(value))
return replay_target_name + ".SC2Replay"
def copy_replays(self, replay_path_dict: Dict[Path, Path], values: Dict[str, str]):
successfully_copied_files = []
for source_path, target_path in replay_path_dict.items():
if source_path == target_path:
successfully_copied_files.append(target_path)
continue
if target_path.is_file():
base_name = source_path.name
if values["show_errors"]:
sg.Print(
f"Could not copy file '{base_name}' because it already exists in target folder '{target_path}'"
)
continue
shutil.copy2(source_path, target_path)
successfully_copied_files.append(target_path)
return successfully_copied_files
def move_replays(self, replay_path_dict: Dict[Path, Path], values: Dict[str, str]):
successfully_moved_files = []
for source_path, target_path in replay_path_dict.items():
if source_path == target_path:
successfully_moved_files.append(target_path)
continue
if target_path.is_file():
base_name = target_path.stem
if values["show_errors"]:
sg.Print(
f"Could not move file '{base_name}' because it already exists in target folder '{target_path}'"
)
continue
shutil.move(source_path, target_path)
successfully_moved_files.append(target_path)
return successfully_moved_files
def rename_replays(
self, replay_path_dict: Dict[Path, Path], values: Dict[str, str]
):
successfully_renamed_files = []
for source_path, target_path in replay_path_dict.items():
if source_path == target_path:
successfully_renamed_files.append(target_path)
continue
if target_path.is_file():
base_name = target_path.stem
if values["show_errors"]:
sg.Print(