diff options
author | Paweł Dybiec <pawel@dybiec.info> | 2020-08-10 19:45:22 +0200 |
---|---|---|
committer | Paweł Dybiec <pawel@dybiec.info> | 2020-08-10 19:45:22 +0200 |
commit | 9deb8544c61be867a4b7b9a994800bff997d542b (patch) | |
tree | 1300a8c33d41098a664033485028cb8a31ba05b4 | |
parent | Add my website (diff) |
Update cs website
-rw-r--r-- | compose/cs/__pycache__/cs.cpython-38.pyc | bin | 1474 -> 0 bytes | |||
-rw-r--r-- | compose/cs/cs.py | 70 |
2 files changed, 66 insertions, 4 deletions
diff --git a/compose/cs/__pycache__/cs.cpython-38.pyc b/compose/cs/__pycache__/cs.cpython-38.pyc Binary files differdeleted file mode 100644 index 9d63e21..0000000 --- a/compose/cs/__pycache__/cs.cpython-38.pyc +++ /dev/null diff --git a/compose/cs/cs.py b/compose/cs/cs.py index b455af2..1774ccc 100644 --- a/compose/cs/cs.py +++ b/compose/cs/cs.py @@ -1,4 +1,6 @@ +import calendar import datetime +import random from typing import List, Tuple from flask import Flask, render_template app = Flask("Today's maps") @@ -13,7 +15,8 @@ maps = [ "mirage 🇲🇦", "cache ☭", "agency 🏢", "office 🖥"] - +LOOKAHEAD=6 +# 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 @@ -21,10 +24,69 @@ def maps_of_day(day: datetime.date) -> Tuple[str, str]: def maps_of_current_week() -> List[Tuple[str, str, str]]: today = datetime.date.today() - days = [today+datetime.timedelta(days=i) for i in range(6)] + days = [today+datetime.timedelta(days=i) for i in range(LOOKAHEAD)] return [(day.strftime("%A"), *maps_of_day(day)) for day in days] -@app.route("/") -def main(): +# 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("/cielak") +def cielak(): return render_template("main.html", maps=maps_of_current_week()) +@app.route("/") +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) |