summaryrefslogtreecommitdiff
path: root/daemons/next.php
blob: c004a6610fce04c7d12c2a8d6819bb93c030c7e4 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

$_SERVER['DOCUMENT_ROOT'] = "..";

$ntfy = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/app.json"), true)["ntfy"];
$pairs = [];

$pairs2 = [
    ["jybgq"],
    ["ujcmc"],
    ["vzyai"],
    ["alqer"]
];

// --------------------

if (!function_exists("formatPonypush")) {
    function formatPonypush($message) {
        return "Update to Ponypush 3.1.0 or later — (\$PA1$\$" . base64_encode($message) . "\$\$)";
    }
}

function getLastFronted($members, $id) {
    foreach ($members as $member) {
        if ($member["id"] === $id) {
            return $member["_lastFronted"];
        }
    }

    return -1;
}

function getMemberWithoutSystem(string $id) {
    global $isLowerLoggedIn; global $isLoggedIn;
    $member = null;

    $members2 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/gdapd/members.json"), true);
    foreach ($members2 as $m) {
        $m["_system"] = "gdapd";
        $m["system"] = "gdapd";
        if ($m["id"] === $id) $member = $m;
    }

    $members3 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/hrbom/members.json"), true);
    foreach ($members3 as $m) {
        $m["_system"] = "hrbom";
        $m["system"] = "hrbom";
        if ($m["id"] === $id) $member = $m;
    }

    if ($isLowerLoggedIn || $isLoggedIn) {
        $app = $GLOBALS["ColdHazeApp"] ?? json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/app.json"), true);
        $members4 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/other/members.json"), true);

        foreach ($members4 as $m) {
            $m["_system"] = $app["other"]["id"];
            $m["system"] = $app["other"]["id"];
            if ($m["id"] === $id) $member = $m;
        }
    }

    return $member;
}

$members = 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'] . "/data/$system/fronters.json"), true)["members"]);

    if (in_array($id, $fronters)) {
        $i["_lastFronted"] = time();
    } else {
        $switches = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/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;
}, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/gdapd/members.json"), true));

$requested = []; foreach ([...$pairs, ...$pairs2] as $pair) {
    array_push($requested, ...$pair);
} $requested = array_unique($requested);

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

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

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

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

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

    if ($timeA < 5 || $timeB < 5) {
        return -INF;
    } else {
        return $timeB - $timeA;
    }
}); $pairsM = array_values($pairs2);

$requested = array_map(function ($i) { return $i[0]; }, $pairsM);

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

$requested = array_values(array_filter(array_unique($requested), function ($i) use ($fronters) {
    return !in_array($i, $fronters);
}));

$i = 0;
$list = [$requested[$i]];

if (count($list) > 1) {
    $str = implode(" or ", array_map(function ($i) {
        return (getMemberWithoutSystem($i)["display_name"] ?? getMemberWithoutSystem($i)["name"]);
    }, $list)) . " is fronting today.";
} elseif (count($list) > 0) {
    $str = (getMemberWithoutSystem($list[0])["display_name"] ?? getMemberWithoutSystem($list[0])["name"]) . " is fronting today.";
} else {
    $str = "We couldn't tell who will front today, sorry.";
}

$context = stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' =>
            "Content-Type: text/plain\r\n" .
            "Title: " . formatPonypush("🎠 Pony fronting today") . "\r\n" .
            "Priority: high\r\n" .
            "Tags: schedule\r\n" .
            "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]),
        'content' => formatPonypush($str)
    ]
]);

file_get_contents('https://' . $ntfy["server"] . '/main', false, $context);