import calendar import datetime import random from typing import List, Tuple from flask import Flask, render_template, redirect, url_for app = Flask("Today's maps") all_maps = [ "mirage 🇲🇦", "inferno 🔥", "overpass 🌉", "vertigo 🏗️", "nuke ☢", "ancient 🐉", "dust 🏜", "train 🚆", #"anubis ☥", "cache ☭", "agency 🏢", "office 🖥", "basalt 🌈⛰️", "insertion 🚓"] #maps = [ all_maps[i] for i in [0,1,2,3,4,5,6]] maps = all_maps LOOKAHEAD=7 # Rafal's method def maps_of_day(day: datetime.date) -> Tuple[str, str]: l = len(maps) i = (day-day.replace(day=1, month=1)).days+1 return maps[(2*i)%l], maps[(2*i+1)%l] def maps_of_current_week() -> List[Tuple[str, str, str]]: today = datetime.date.today() days = [today+datetime.timedelta(days=i) for i in range(LOOKAHEAD)] return [(day.strftime("%A"), *maps_of_day(day)) for day in days] # Kacper's method def date_to_day_no(dt: datetime.date) -> int: return int(calendar.timegm(dt.timetuple()) / (60 * 60 * 24)) def day_no_to_date(day_no: int) -> datetime.date: return datetime.date.fromtimestamp(day_no * (60 * 60 * 24)) def get_map_schedule(from_date: datetime.date, to_date: datetime.date, number_of_maps: int) \ -> List[Tuple[datetime.date, Tuple[int, int]]]: assert number_of_maps > 2 assert from_date <= to_date from_day_no = date_to_day_no(from_date) to_day_no = date_to_day_no(to_date) from_map_no = from_day_no * 2 to_map_no = to_day_no * 2 from_permutation_no = int(from_map_no / number_of_maps) to_permutation_no = int(to_map_no / number_of_maps) sequence_count = to_permutation_no - from_permutation_no + 1 # generate random permutations # (1 additional for preventing the 2 same maps in a row) permutations = list(range(number_of_maps)) * (sequence_count + 1) for i in range(sequence_count + 1): # the permutation is generated using its number as a seed, so for the # same date, maps will be always the same (as long as random # implementation in python won't change) random.seed(from_permutation_no + i) for x in range(number_of_maps): y = random.randint(x, number_of_maps - 1) ix = i * number_of_maps + x iy = i * number_of_maps + y permutations[ix], permutations[iy] = \ permutations[iy], permutations[ix] # prevent 2 the same maps in a row: # if first element of current permutation is equal to the last of # previous permutation, then swap 2 last elements of previous # permutation first_el = i * number_of_maps if i != 0 and permutations[first_el] == permutations[first_el - 1]: permutations[first_el - 1], permutations[first_el - 2] = \ permutations[first_el - 2], permutations[first_el - 1] output = [] perm_iter = from_map_no - from_permutation_no * number_of_maps for day_no in range(from_day_no, to_day_no + 1): maps_for_day = permutations[perm_iter], permutations[perm_iter + 1] assert permutations[perm_iter] != permutations[perm_iter + 1] perm_iter += 2 output.append((day_no_to_date(day_no), maps_for_day)) return output @app.route("/") def main(): return redirect(url_for('r3pack')) @app.route("/cielak") def cielak(): return render_template("main.html", maps=maps_of_current_week()) @app.route("/r3pack") def r3pack(): today = datetime.date.today() map_schedule = get_map_schedule( today, today+datetime.timedelta(days=LOOKAHEAD-1), len(maps)) render_data = [(day.strftime("%A"), *[maps[i] for i in mapsi]) for (day, mapsi) in map_schedule] return render_template("main.html", maps=render_data)