Skip to content

Commit

Permalink
Format generate_leaderboards.py
Browse files Browse the repository at this point in the history
  • Loading branch information
HactarCE committed Dec 14, 2024
1 parent e4163b4 commit da28be9
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions leaderboards/generate_leaderboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
'date',
'program',
]
COLUMNS_INFO = lambda event: {


def COLUMNS_INFO(event): return {
'event': ("Event", ':---'),
'rank': ("Rank", '---:'),
'solver': ("Name", ':--:'),
Expand All @@ -25,23 +27,24 @@
'program': ("Program", ':--:'),
}

# it does some funny business

def recode(s):
#return s.encode('cp1252').decode('utf8') # it worked on my machine :'
# return s.encode('cp1252').decode('utf8') # it worked on my machine :'
return s


def get_template(filename):
with open(f'leaderboards/templates/{filename}') as f:
return f.read()


def format_time(duration) -> str: # duration: timedelta | int
def format_time(duration) -> str: # duration: timedelta | int
# duration can be timedelta (time) or int (movecount for fmc)
def unit(s):
return f'<small>{s}</small>'

if isinstance(duration, int):
return f"{duration:,}".replace(',','\u2009')
return f"{duration:,}".replace(',', '\u2009')

millis = int(duration.total_seconds() * 1000)
seconds, millis = divmod(millis, 1000)
Expand All @@ -62,7 +65,7 @@ def unit(s):
return f"{hours}{unit('h')} {minutes_str} {seconds_str} {millis_str}"
hours_str = f"{minutes:02}{unit('h')}"

return f"{days:,}{unit('d')} {hours_str} {minutes_str} {seconds_str} {millis_str}".replace(',','\u2009')
return f"{days:,}{unit('d')} {hours_str} {minutes_str} {seconds_str} {millis_str}".replace(',', '\u2009')


class Solve:
Expand All @@ -80,8 +83,8 @@ def __init__(self,
else:
self.link = link
self.time = parse_time(time)
#self.puzzle = puzzles[puzzle]
#self.format = formats[format]
# self.puzzle = puzzles[puzzle]
# self.format = formats[format]
self.event = puzzles[puzzle]['events'][format]
self.solver = solvers[solver]
self.program = program
Expand Down Expand Up @@ -171,25 +174,29 @@ def relative_file_path(self):
with open('leaderboards/formats.yml') as file:
formats = yaml.load(file.read(), Loader=yaml.Loader)


def populate_puzzles(tab):
if 'name' not in tab:
raise Exception(f'tab {tab} has no name')
if 'contents' in tab:
for subtab in tab['contents']:
populate_puzzles(subtab)
elif 'puz' in tab:
puzzles[tab['puz']] = {'name':recode(tab['name'])}
puzzles[tab['puz']] = {'name': recode(tab['name'])}
else:
raise Exception(f"missing 'contents' or 'puz' in tab {tab['name']}")


# Load tabs and puzzles from YAML and create events
with open('leaderboards/tabs.yml') as tabs_file:
tab_config = yaml.load(tabs_file.read(), Loader=yaml.Loader)
puzzles = {}
for tab in tab_config:
populate_puzzles(tab)
for puzzle in puzzles:
puzzles[puzzle]['events'] = {format: Event(puzzle, format, f'{puzzles[puzzle]["name"]} {formats[format]["name"]}') for format,data in formats.items()}
puzzles[puzzle]['events'] = {format: Event(puzzle, format, f'{puzzles[puzzle]["name"]} {
formats[format]["name"]}') for format, data in formats.items()}


def parse_time(s):
if m := re.match(r'(\d+)mv', s):
Expand Down Expand Up @@ -269,7 +276,8 @@ def make_tabbed_leaderboards(tab_config, make_tab_contents, *, indent=0) -> str:
elif 'format' in tab:
tab_contents = make_tab_contents(tab, indent=indent)
elif 'puz' in tab:
subtabs = [{**tab, 'format':f, **format} for f,format in formats.items()]
subtabs = [{**tab, 'format': f, **format}
for f, format in formats.items()]
tab_contents = make_tabbed_leaderboards(
subtabs,
make_tab_contents,
Expand All @@ -278,7 +286,6 @@ def make_tabbed_leaderboards(tab_config, make_tab_contents, *, indent=0) -> str:
else:
raise Exception(f'not a valid tab {tab}')


if not tab_contents:
continue

Expand Down

0 comments on commit da28be9

Please sign in to comment.