about summary refs log tree commit diff
path: root/compose/cs/cs.py
blob: fdb32fd522294489198e0f4f663fd63daa0585fd (plain) (blame)
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
import calendar
import datetime
import random
from typing import List, Tuple
from flask import Flask, render_template
app = Flask("Today's maps")
all_maps = [ "mirage 🇲🇦",
         "inferno 🔥",
         "overpass 🌉",
         "vertigo 🏗️",
         "nuke ☢",
         "ancient 🐉",
         "dust 🏜",
         "train 🚆",
         #"anubis ☥",
         "cache ☭",
         "agency 🏢",
         "office 🖥",
         "grind 🚛",
         "mocha ☕"]
#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 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)