-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist.py
More file actions
47 lines (35 loc) · 1.6 KB
/
Copy pathplaylist.py
File metadata and controls
47 lines (35 loc) · 1.6 KB
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
import os
class MapPlaylist:
"""Performs file index navigation in the directory"""
def __init__(self, root_dir: str = "maps") -> None:
"""Initializes the class object"""
self.root_dir: str = root_dir
self.playlist: list[str] = []
self.current_index: int = 0
self._scan_directories()
def _scan_directories(self) -> None:
"""Recursively parses valid targets into a list"""
categories: list[str] = ["easy", "medium", "hard", "challenger"]
for category in categories:
cat_path: str = os.path.join(self.root_dir, category)
if not os.path.exists(cat_path):
continue
files: list[str] = sorted(os.listdir(cat_path))
for file in files:
if file.endswith(".txt"):
self.playlist.append(os.path.join(cat_path, file))
@property
def current_map_path(self) -> str:
"""Returns filepath of the current index pointer"""
if not self.playlist:
raise FileNotFoundError("Directory scan yielded "
"empty simulation map pool.")
return self.playlist[self.current_index]
def next_map(self) -> str:
"""Increments state index pointer sequentially and loops"""
self.current_index = (self.current_index + 1) % len(self.playlist)
return self.current_map_path
def previous_map(self) -> str:
"""Decrements state index pointer and loops backwards"""
self.current_index = (self.current_index - 1) % len(self.playlist)
return self.current_map_path