summaryrefslogtreecommitdiff
path: root/pages/api/evening.php
blob: 9884e057a7024bf9b15c19c8698f73fb75bb01b3 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/functions.inc";
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/session.inc"; global $isLoggedIn; global $isLowerLoggedIn;
if (!$isLoggedIn || $isLowerLoggedIn) {
    header("Location: /-/login");
    die();
}

$pairs = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/pairs.json"), true);
$ignored = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/evening/ignored.json"), true);
$data = [];

$members = $members = [
    ...array_map(function ($i) {
        $system = "ynmuc";
        $i["_lastFronted"] = -1;
        $id = $i["id"];
        $memberData = $i;

        $fronters = array_map(function ($item) {
            return $item["id"];
        }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/fronters.json"), true)["members"]);

        if (in_array($id, $fronters)) {
            $i["_lastFronted"] = time();
        } else {
            $switches = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/switches.json"), true);

            $thisMember = array_filter($switches, function ($item) use ($memberData) {
                return in_array($memberData["id"], $item["members"]);
            });

            $thisMember = array_values($thisMember);
            $frontingEnd = null;

            if (count($thisMember) > 0) {
                $thisIndex = array_search($thisMember[0], $switches);

                $frontingStart = $thisMember[0];
                $frontingEnd = $switches[$thisIndex - 1];
            }

            if ($frontingEnd !== null && isset($frontingStart)) {
                $i["_lastFronted"] = strtotime($frontingEnd["timestamp"]);
            }
        }

        return $i;
    }, array_values(array_filter(scoreOrderGlobal(), function ($i) {
        return $i["_system"] === "ynmuc";
    }))),
    ...array_map(function ($i) {
        $system = "gdapd";
        $i["_lastFronted"] = -1;
        $id = $i["id"];
        $memberData = $i;

        $fronters = array_map(function ($item) {
            return $item["id"];
        }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/fronters.json"), true)["members"]);

        if (in_array($id, $fronters)) {
            $i["_lastFronted"] = time();
        } else {
            $switches = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system/switches.json"), true);

            $thisMember = array_filter($switches, function ($item) use ($memberData) {
                return in_array($memberData["id"], $item["members"]);
            });

            $thisMember = array_values($thisMember);
            $frontingEnd = null;

            if (count($thisMember) > 0) {
                $thisIndex = array_search($thisMember[0], $switches);

                $frontingStart = $thisMember[0];
                $frontingEnd = $switches[$thisIndex - 1];
            }

            if ($frontingEnd !== null && isset($frontingStart)) {
                $i["_lastFronted"] = strtotime($frontingEnd["timestamp"]);
            }
        }

        return $i;
    }, array_values(array_filter(scoreOrderGlobal(), function ($i) {
        return $i["_system"] === "gdapd";
    })))
];

usort($pairs, function ($a, $b) use ($members) {
    $times = [];

    foreach ($a[0] as $id) {
        $times[] = getLastFronted($members, $id);
    }
    foreach ($a[1] as $id) {
        $times[] = getLastFronted($members, $id);
    }

    $timeA = time() - min(...$times);
    $times = [];

    foreach ($b[0] as $id) {
        $times[] = getLastFronted($members, $id);
    }
    foreach ($b[1] as $id) {
        $times[] = getLastFronted($members, $id);
    }

    $timeB = time() - min(...$times);

    return $timeB - $timeA;
}); $pairs = array_values($pairs); foreach ($pairs as $pair): $times = [];
    foreach ($pair[0] as $id): $times[] = getLastFronted($members, $id); endforeach;
    foreach ($pair[1] as $id): $times[] = getLastFronted($members, $id); endforeach;
endforeach;

$listI = 0; for ($i = 0; $i < 2; $i++): $pair = $pairs[$listI];
    $data[$i] = [];
    if (!in_array(date('Y-m-d', time() + 86400 * $i), $ignored)) {
        $data[$i][0] = [];
        $data[$i][1] = [];
        foreach ($pair[0] as $id):
            $data[$i][0][] = getMemberWithoutSystem($id)["display_name"] ?? getMemberWithoutSystem($id)["name"];
        endforeach;
        foreach ($pair[1] as $id):
            $data[$i][1][] = getMemberWithoutSystem($id)["display_name"] ?? getMemberWithoutSystem($id)["name"];
        endforeach;
    } else {
        $data[$i] = null;
    }
if (!in_array(date('Y-m-d', time() + 86400 * $i), $ignored)) $listI++; if ($listI === count($pairs)) $listI = 0; endfor;

header("Content-Type: application/json");
die(json_encode($data, JSON_PRETTY_PRINT));