From 2162eaa06f7e4764eb3dcfe130ec2c711d0c62ab Mon Sep 17 00:00:00 2001 From: Minteck Date: Thu, 22 Sep 2022 19:47:05 +0200 Subject: Cofronting support and stuff like that --- api/fronter.php | 30 +++- api/pluralkit-integration.php | 76 +++++---- includes/planner.php | 379 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 436 insertions(+), 49 deletions(-) diff --git a/api/fronter.php b/api/fronter.php index e74d34a..f962625 100644 --- a/api/fronter.php +++ b/api/fronter.php @@ -31,7 +31,22 @@ switch ($type) { if ($member !== null && $member !== "null") header("Location: /?error=System member not found") and die(); if (!isset($list[$date])) $list[$date] = []; - $list[$date][] = $member; + $list[$date][] = [$member, null]; + break; + + case "cofront": + if (!isset($index) || trim($index) === "" || is_integer($index)) + if ($index !== null && $index !== "null") header("Location: /?error=Invalid index") and die(); + + $day = $list[$date]; + + if (!isset($day[$index])) + if ($index !== null && $index !== "null") header("Location: /?error=Index not found") and die(); + + if (!isset($member) || trim($member) === "" || strlen($member) !== 5 || !preg_match("/[a-z]/i", $member)) + if ($member !== null && $member !== "null") header("Location: /?error=System member not found") and die(); + + $list[$date][$index][1] = $member; break; case "delete": @@ -48,6 +63,19 @@ switch ($type) { break; + case "codelete": + if (!isset($index) || trim($index) === "" || is_integer($index)) + if ($index !== null && $index !== "null") header("Location: /?error=Invalid index") and die(); + + $day = $list[$date]; + + if (!isset($day[$index])) + if ($index !== null && $index !== "null") header("Location: /?error=Index not found") and die(); + + $list[$date][$index][1] = null; + + break; + case "down": if (!isset($index) || trim($index) === "" || is_integer($index)) if ($index !== null && $index !== "null") header("Location: /?error=Invalid index") and die(); diff --git a/api/pluralkit-integration.php b/api/pluralkit-integration.php index 98f4cb9..3582df1 100644 --- a/api/pluralkit-integration.php +++ b/api/pluralkit-integration.php @@ -39,19 +39,51 @@ if ($input["type"] === "CREATE_SWITCH" || $input["type"] === "UPDATE_SWITCH" || $ntfy = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true)["ntfy"]; - file_get_contents('https://' . $ntfy["server"] . '/public-switches', false, stream_context_create([ - 'http' => [ - 'method' => 'POST', - 'header' => - "Content-Type: text/plain\r\n" . - "Title: 🐴 Switch occurred in the $name\r\n" . - "Priority: default\r\n" . - "Tags: switch\r\n" . - "Actions: view, Open on Cold Haze, https://ponies.equestria.horse/" . $fronters["members"][0]["name"] . "/, clear=true\r\n" . - "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]), - 'content' => ($fronters["members"][0]["display_name"] ?? $fronters["members"][0]["name"]) . " switched in just now" - ] - ])); + if (count($fronters["members"]) > 1) { + $context = stream_context_create([ + 'http' => [ + 'method' => 'POST', + 'header' => + "Content-Type: text/plain\r\n" . + "Title: 🐴 Switch occurred in the $name\r\n" . + "Priority: default\r\n" . + "Tags: switch\r\n" . + "Actions: view, Open " . $fronters["members"][0]["display_name"] . " on Cold Haze, https://ponies.equestria.horse/" . $fronters["members"][0]["name"] . "/, clear=true;view, Open " . $fronters["members"][1]["display_name"] . " on Cold Haze, https://ponies.equestria.horse/" . $fronters["members"][1]["name"] . "/, clear=true\r\n" . + "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]), + 'content' => ($fronters["members"][0]["display_name"] ?? $fronters["members"][0]["name"]) . " and " . ($fronters["members"][1]["display_name"] ?? $fronters["members"][1]["name"]) . " switched in just now" + ] + ]); + } else if (count($fronters["members"]) > 0) { + $context = stream_context_create([ + 'http' => [ + 'method' => 'POST', + 'header' => + "Content-Type: text/plain\r\n" . + "Title: 🐴 Switch occurred in the $name\r\n" . + "Priority: default\r\n" . + "Tags: switch\r\n" . + "Actions: view, Open on Cold Haze, https://ponies.equestria.horse/" . $fronters["members"][0]["name"] . "/, clear=true\r\n" . + "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]), + 'content' => ($fronters["members"][0]["display_name"] ?? $fronters["members"][0]["name"]) . " switched in just now" + ] + ]); + } else { + $context = stream_context_create([ + 'http' => [ + 'method' => 'POST', + 'header' => + "Content-Type: text/plain\r\n" . + "Title: 🐴 Switch occurred in the $name\r\n" . + "Priority: default\r\n" . + "Tags: switch\r\n" . + "Actions: view, Open on Cold Haze, https://ponies.equestria.horse/, clear=true\r\n" . + "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]), + 'content' => "The fallback pony switched in just now" + ] + ]); + } + + file_get_contents('https://' . $ntfy["server"] . '/public-switches', false, $context); if ($system === "gdapd") { $topic = "public-switches-raindrops"; @@ -59,21 +91,5 @@ if ($input["type"] === "CREATE_SWITCH" || $input["type"] === "UPDATE_SWITCH" || $topic = "public-switches-cloudburst"; } - file_get_contents('https://' . $ntfy["server"] . '/' . $topic, false, stream_context_create([ - 'http' => [ - 'method' => 'POST', - 'header' => - "Content-Type: text/plain\r\n" . - "Title: 🐴 Switch occurred in the $name\r\n" . - "Priority: default\r\n" . - "Tags: switch\r\n" . - "Actions: view, Open on Cold Haze, https://ponies.equestria.horse/" . $fronters["members"][0]["name"] . "/, clear=true\r\n" . - "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]), - 'content' => ($fronters["members"][0]["display_name"] ?? $fronters["members"][0]["name"]) . " switched in just now" - ] - ])); - - file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/data.json-res", "affected: " . $input["type"] . " (" . gettype($input["type"]) . ")") and die(); -} else { - file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/data.json-res", "invalid method: " . $input["type"] . " (" . gettype($input["type"]) . ")") and die(); + file_get_contents('https://' . $ntfy["server"] . '/' . $topic, false, $context); } \ No newline at end of file diff --git a/includes/planner.php b/includes/planner.php index 8644b25..f98d83b 100644 --- a/includes/planner.php +++ b/includes/planner.php @@ -12,12 +12,70 @@ require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/functions.php"; $cloudburst = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/ynmuc-planner.json"), true); $raindrops = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/gdapd-planner.json"), true); +foreach ($cloudburst as $id => $day) { + foreach ($day as $index => $fronter) { + if (is_string($fronter)) { + $cloudburst[$id][$index] = [$fronter]; + } else if (is_array($fronter)) { + if (count($fronter) < 2) { + if (!isset($cloudburst[$id][$index][0])) $cloudburst[$id][$index][0] = null; + $cloudburst[$id][$index][1] = null; + } else if (count($fronter) > 2) { + $array = []; + + if (isset($cloudburst[$id][$index][0])) { + $array[0] = $cloudburst[$id][$index][0]; + } else { + $array[0] = null; + } + + if (isset($cloudburst[$id][$index][1])) { + $array[1] = $cloudburst[$id][$index][1]; + } else { + $array[1] = null; + } + + $cloudburst[$id][$index] = $array; + } + } + } +} + +foreach ($raindrops as $id => $day) { + foreach ($day as $index => $fronter) { + if (is_string($fronter)) { + $raindrops[$id][$index] = [$fronter]; + } else if (is_array($fronter)) { + if (count($fronter) < 2) { + if (!isset($raindrops[$id][$index][0])) $raindrops[$id][$index][0] = null; + $raindrops[$id][$index][1] = null; + } else if (count($fronter) > 2) { + $array = []; + + if (isset($raindrops[$id][$index][0])) { + $array[0] = $raindrops[$id][$index][0]; + } else { + $array[0] = null; + } + + if (isset($raindrops[$id][$index][1])) { + $array[1] = $raindrops[$id][$index][1]; + } else { + $array[1] = null; + } + + $raindrops[$id][$index] = $array; + } + } + } +} + function day($display, $diff): void { global $cloudburst; global $raindrops; ?> - - + + ?> - Cloudburst System - Raindrops System + Cloudburst System + Raindrops System @@ -42,52 +100,84 @@ function day($display, $diff): void { global $cloudburst; global $raindrops; ?> - - + colspan="3" colspan="2"> + Multiple merged membersMerge .png" style="width:24px;"> + + + + + + + + + + + Multiple merged membersMerge + + .png" style="width:24px;"> + + + - - + + Add new fronter - + - - + + colspan="3" colspan="2"> Multiple merged membersMerge .png" style="width:24px;"> + + + + + + + + + + + Multiple merged membersMerge + + .png" style="width:24px;"> + + + - + Add new fronter - + - + 0 && count($dayRaindrops) > 0): ?> - will sleep with + will sleep with Unable to calculate who will sleep with who @@ -312,10 +402,10 @@ function day($display, $diff): void { global $cloudburst; global $raindrops; ?> index: id } - if (fronting[system][date][id] === "lzlaq" || fronting[system][date][id] === "irxyh") { + if (fronting[system][date][id][0] === "lzlaq" || fronting[system][date][id][0] === "irxyh") { document.getElementById("edit-fronter-name").innerText = "merged members"; } else { - document.getElementById("edit-fronter-name").innerText = names[fronting[system][date][id]] ?? fronting[system][date][id]; + document.getElementById("edit-fronter-name").innerText = names[fronting[system][date][id][0]] ?? fronting[system][date][id][0]; } document.getElementById("edit-fronter-date").innerText = display; @@ -326,6 +416,123 @@ function day($display, $diff): void { global $cloudburst; global $raindrops; ?> modal.show(); } + function openEditCofronter(system, id, date) { + let display; + + switch (date) { + case "": + display = "today"; + break; + + case "": + display = "tomorrow"; + break; + + case "": + display = "on "; + break; + + case "": + display = "on "; + break; + + case "": + display = "on "; + break; + + case "": + display = "on "; + break; + + case "": + display = "on "; + break; + } + + window.selectedFronting = { + system: system === "cloudburst" ? "ynmuc" : "gdapd", + date: date, + index: id + } + + document.getElementById("edit-cofronter-name").innerText = names[fronting[system][date][id][1]] ?? fronting[system][date][id][1]; + document.getElementById("edit-cofronter-name2").innerText = names[fronting[system][date][id][0]] ?? fronting[system][date][id][0]; + + document.getElementById("edit-cofronter-date").innerText = display; + document.getElementById("edit-cofronter-pos").innerText = ordinal(id + 1); + document.getElementById("edit-cofronter-system").innerText = system === "cloudburst" ? "Cloudburst System" : "Raindrops System"; + + let modal = new bootstrap.Modal(document.getElementById('edit-cofronter')); + modal.show(); + } + + function addCofronter(system, offset, index) { + let date; + let display; + + switch (offset) { + case 0: + date = ""; + display = "today"; + break; + + case 1: + date = ""; + display = "tomorrow"; + break; + + case 2: + date = ""; + display = "on "; + break; + + case 3: + date = ""; + display = "on "; + break; + + case 4: + date = ""; + display = "on "; + break; + + case 5: + date = ""; + display = "on "; + break; + + case 6: + date = ""; + display = "on "; + break; + } + + window.selectedFronting = { + system: system === "cloudburst" ? "ynmuc" : "gdapd", + date: date, + index: index + } + + window.currentWorkingDate = date; + document.getElementById("new-cofronter-date").innerText = display; + document.getElementById("new-cofronter-system").innerText = system === "cloudburst" ? "Cloudburst System" : "Raindrops System"; + window.addSystem = system === "raindrops" ? "gdapd" : "ynmuc"; + + if (fronting[system][date][index][0] === "lzlaq" || fronting[system][date][index][0] === "irxyh") { + document.getElementById("new-cofronter-main").innerText = "merged members"; + } else { + document.getElementById("new-cofronter-main").innerText = names[fronting[system][date][index][0]] ?? fronting[system][date][index][0]; + } + + document.getElementById("list2").style.display = "block"; + document.getElementById("search2-results").style.display = "none"; + document.getElementById("search2").value = ""; + + let modal = new bootstrap.Modal(document.getElementById('new-cofronter')); + modal.show(); + document.getElementById("search2").focus(); + } + function addFronter(system, offset) { let date; let display; @@ -393,6 +600,18 @@ function day($display, $diff): void { global $cloudburst; global $raindrops; ?> }); } + function confirmCofronterAdd(system, id) { + Array.from(document.getElementsByClassName("new-cofronter-link")).forEach((i) => { + i.classList.add("disabled"); + }); + + document.getElementById("new-cofronter-close").classList.add("disabled"); + + window.fetch("/api/fronter?t=cofront&d=" + window.selectedFronting["date"] + "&m=" + id + "&s=" + system + "&i=" + window.selectedFronting["index"]).then(() => { + location.reload(); + }); + } + function deleteFronter() { Array.from(document.getElementsByClassName("edit-fronter-link")).forEach((i) => { i.classList.add("disabled"); @@ -405,6 +624,18 @@ function day($display, $diff): void { global $cloudburst; global $raindrops; ?> }); } + function deleteCofronter() { + Array.from(document.getElementsByClassName("edit-cofronter-link")).forEach((i) => { + i.classList.add("disabled"); + }); + + document.getElementById("edit-cofronter-close").classList.add("disabled"); + + window.fetch("/api/fronter?t=codelete&d=" + window.selectedFronting["date"] + "&i=" + window.selectedFronting["index"] + "&s=" + window.selectedFronting["system"]).then(() => { + location.reload(); + }); + } + function moveFronterDown() { Array.from(document.getElementsByClassName("edit-fronter-link")).forEach((i) => { i.classList.add("disabled"); @@ -418,7 +649,12 @@ function day($display, $diff): void { global $cloudburst; global $raindrops; ?> } function viewFronterPage() { - let id = fronting[window.selectedFronting["system"] === "ynmuc" ? "cloudburst" : "raindrops"][window.selectedFronting["date"]][window.selectedFronting["index"]]; + let id = fronting[window.selectedFronting["system"] === "ynmuc" ? "cloudburst" : "raindrops"][window.selectedFronting["date"]][window.selectedFronting["index"]][0]; + location.href = "/" + id; + } + + function viewCofronterPage() { + let id = fronting[window.selectedFronting["system"] === "ynmuc" ? "cloudburst" : "raindrops"][window.selectedFronting["date"]][window.selectedFronting["index"]][1]; location.href = "/" + id; } @@ -465,6 +701,35 @@ function day($display, $diff): void { global $cloudburst; global $raindrops; ?> + +