summaryrefslogtreecommitdiff
path: root/autoschedule
diff options
context:
space:
mode:
Diffstat (limited to 'autoschedule')
-rw-r--r--autoschedule/main.php280
-rw-r--r--autoschedule/ponies.php99
2 files changed, 379 insertions, 0 deletions
diff --git a/autoschedule/main.php b/autoschedule/main.php
new file mode 100644
index 0000000..892ea02
--- /dev/null
+++ b/autoschedule/main.php
@@ -0,0 +1,280 @@
+<?php
+
+$_SERVER['DOCUMENT_ROOT'] = "..";
+
+require_once "../includes/score.php";
+require_once "../includes/bitset.php";
+require_once "../includes/functions.php";
+
+$days = [];
+$today_date = date('Y-m-d', time() + 86400);
+$today_weekday = (int)date('N', time() + 86400);
+$today = [];
+$weekend = $today_weekday > 5;
+
+require_once "./ponies.php"; global $ponies_raindrops; global $ponies_cloudburst;
+
+$candidates_cloudburst = [];
+$candidates_cloudburst_sorted = [
+ "night" => [[], [], [], []],
+ "morning" => [[], [], [], []],
+ "afternoon" => [[], [], [], []],
+ "evening" => [[], [], [], []]
+];
+$candidates_cloudburst_best = [];
+
+foreach ($ponies_cloudburst as $pony) {
+ $dates = $pony["_dates"];
+ echo($pony["name"] . ": " . $dates[0] . ", " . $dates[1] . "\n");
+
+ $candidates_cloudburst[] = $pony["id"];
+ if (isset($dates[0])) $candidates_cloudburst_sorted[$dates[1]][1][] = $pony["id"];
+ if (isset($dates[1])) $candidates_cloudburst_sorted[$dates[0]][0][] = $pony["id"];
+ if (isset($dates[2])) $candidates_cloudburst_sorted[$dates[2]][2][] = $pony["id"];
+ if (isset($dates[3])) $candidates_cloudburst_sorted[$dates[3]][3][] = $pony["id"];
+}
+
+unset($candidates_cloudburst_sorted[""]);
+
+$candidates_cloudburst_sorted["night"] = [
+ ...$candidates_cloudburst_sorted["night"][0],
+ ...$candidates_cloudburst_sorted["night"][1],
+ ...$candidates_cloudburst_sorted["night"][2],
+ ...$candidates_cloudburst_sorted["night"][3]
+];
+$candidates_cloudburst_sorted["morning"] = [
+ ...$candidates_cloudburst_sorted["morning"][0],
+ ...$candidates_cloudburst_sorted["morning"][1],
+ ...$candidates_cloudburst_sorted["morning"][2],
+ ...$candidates_cloudburst_sorted["morning"][3]
+];
+$candidates_cloudburst_sorted["afternoon"] = [
+ ...$candidates_cloudburst_sorted["afternoon"][0],
+ ...$candidates_cloudburst_sorted["afternoon"][1],
+ ...$candidates_cloudburst_sorted["afternoon"][2],
+ ...$candidates_cloudburst_sorted["afternoon"][3]
+];
+$candidates_cloudburst_sorted["evening"] = [
+ ...$candidates_cloudburst_sorted["evening"][0],
+ ...$candidates_cloudburst_sorted["evening"][1],
+ ...$candidates_cloudburst_sorted["evening"][2],
+ ...$candidates_cloudburst_sorted["evening"][3]
+];
+
+$candidates_cloudburst_id = [];
+$candidates_cloudburst_id["night"] = 0;
+$candidates_cloudburst_id["morning"] = 0;
+$candidates_cloudburst_id["afternoon"] = 0;
+$candidates_cloudburst_id["evening"] = 0;
+
+$candidates_cloudburst_best["night"] = $candidates_cloudburst_sorted["night"][$candidates_cloudburst_id["night"]];
+$candidates_cloudburst_best["morning"] = $candidates_cloudburst_sorted["morning"][$candidates_cloudburst_id["morning"]];
+$candidates_cloudburst_best["afternoon"] = $candidates_cloudburst_sorted["afternoon"][$candidates_cloudburst_id["afternoon"]];
+$candidates_cloudburst_best["evening"] = $candidates_cloudburst_sorted["evening"][$candidates_cloudburst_id["evening"]];
+
+$candidates_raindrops = [];
+$candidates_raindrops_sorted = [
+ "night" => [[], [], [], []],
+ "morning" => [[], [], [], []],
+ "afternoon" => [[], [], [], []],
+ "evening" => [[], [], [], []]
+];
+$candidates_raindrops_best = [];
+
+foreach ($ponies_raindrops as $pony) {
+ $dates = $pony["_dates"];
+ echo($pony["name"] . ": " . implode(", ", $dates) . "\n");
+
+ $add = false;
+
+ if ($weekend && $pony["_metadata"]["shared_memory"] < 2) {
+ $add = true;
+ } else if ($weekend && $pony["_metadata"]["little"] === 2) {
+ $add = true;
+ } else if ($pony["_metadata"]["little"] !== 2 && !$pony["_metadata"]["shared_memory"] < 2) {
+ $add = true;
+ }
+
+ if ($add) {
+ $candidates_raindrops[] = $pony["id"];
+ if (isset($dates[0])) $candidates_raindrops_sorted[$dates[0]][0][] = $pony["id"];
+ if (isset($dates[1])) $candidates_raindrops_sorted[$dates[1]][1][] = $pony["id"];
+ if (isset($dates[2])) $candidates_raindrops_sorted[$dates[2]][2][] = $pony["id"];
+ if (isset($dates[3])) $candidates_raindrops_sorted[$dates[3]][3][] = $pony["id"];
+ }
+}
+
+unset($candidates_raindrops_sorted[""]);
+
+$candidates_raindrops_sorted["night"] = [
+ ...$candidates_raindrops_sorted["night"][0],
+ ...$candidates_raindrops_sorted["night"][1],
+ ...$candidates_raindrops_sorted["night"][2],
+ ...$candidates_raindrops_sorted["night"][3]
+];
+$candidates_raindrops_sorted["morning"] = [
+ ...$candidates_raindrops_sorted["morning"][0],
+ ...$candidates_raindrops_sorted["morning"][1],
+ ...$candidates_raindrops_sorted["morning"][2],
+ ...$candidates_raindrops_sorted["morning"][3]
+];
+$candidates_raindrops_sorted["afternoon"] = [
+ ...$candidates_raindrops_sorted["afternoon"][0],
+ ...$candidates_raindrops_sorted["afternoon"][1],
+ ...$candidates_raindrops_sorted["afternoon"][2],
+ ...$candidates_raindrops_sorted["afternoon"][3]
+];
+$candidates_raindrops_sorted["evening"] = [
+ ...$candidates_raindrops_sorted["evening"][0],
+ ...$candidates_raindrops_sorted["evening"][1],
+ ...$candidates_raindrops_sorted["evening"][2],
+ ...$candidates_raindrops_sorted["evening"][3]
+];
+
+$candidates_raindrops_id = [];
+$candidates_raindrops_id["night"] = 0;
+$candidates_raindrops_id["morning"] = 0;
+$candidates_raindrops_id["afternoon"] = 0;
+$candidates_raindrops_id["evening"] = 0;
+
+$candidates_raindrops_best["night"] = $candidates_raindrops_sorted["night"][$candidates_raindrops_id["night"]];
+$candidates_raindrops_best["morning"] = $candidates_raindrops_sorted["morning"][$candidates_raindrops_id["morning"]];
+$candidates_raindrops_best["afternoon"] = $candidates_raindrops_sorted["afternoon"][$candidates_raindrops_id["afternoon"]];
+$candidates_raindrops_best["evening"] = $candidates_raindrops_sorted["evening"][$candidates_raindrops_id["evening"]];
+
+echo("--------------\n");
+
+var_dump($candidates_cloudburst_best);
+var_dump($candidates_raindrops_best);
+
+echo("--------------\n");
+
+$correct = false;
+$relationships = [];
+
+foreach (scoreOrderGlobal() as $pony) {
+ $relationships[$pony["id"]] = [
+ ...array_map(function ($i) {
+ return explode("/", $i)[1];
+ }, array_values(array_filter($pony["_metadata"]["marefriends"], function ($i) use ($pony) {
+ return explode("/", $i)[0] !== $pony["_system"];
+ }))),
+ ...array_map(function ($i) {
+ return explode("/", $i)[1];
+ }, array_values(array_filter($pony["_metadata"]["sisters"], function ($i) use ($pony) {
+ return explode("/", $i)[0] !== $pony["_system"];
+ }))),
+ ...array_map(function ($i) {
+ return explode("/", $i)[1];
+ }, array_values(array_filter($pony["_metadata"]["caretakers"], function ($i) use ($pony) {
+ return explode("/", $i)[0] !== $pony["_system"];
+ })))
+ ];
+}
+
+while (!$correct) {
+ $ponies = [];
+ $fixed = false;
+
+ foreach (["morning", "afternoon", "evening", "night"] as $period) {
+ if (in_array($candidates_raindrops_best[$period], $ponies)) {
+ echo("Saw " . $candidates_raindrops_best[$period] . " (raindrops) more than once\n");
+
+ if (count($candidates_raindrops_sorted[$period]) > $candidates_raindrops_id[$period]) {
+ $candidates_raindrops_id[$period]++;
+
+ if (isset($candidates_raindrops_sorted[$period][$candidates_raindrops_id[$period]])) {
+ $candidates_raindrops_best[$period] = $candidates_raindrops_sorted[$period][$candidates_raindrops_id[$period]];
+ echo(" Conflict resolved\n");
+ $fixed = true;
+ } else {
+ echo(" Unable to resolve conflict\n");
+ }
+ } else {
+ echo(" Unable to resolve conflict\n");
+ }
+ } else {
+ $ponies[] = $candidates_raindrops_best[$period];
+ }
+
+ if (in_array($candidates_cloudburst_best[$period], $ponies)) {
+ echo("Saw " . $candidates_cloudburst_best[$period] . " (cloudburst) more than once\n");
+
+ if (count($candidates_cloudburst_sorted[$period]) > $candidates_cloudburst_id[$period]) {
+ $candidates_cloudburst_id[$period]++;
+
+ if (isset($candidates_cloudburst_sorted[$period][$candidates_cloudburst_id[$period]])) {
+ $candidates_cloudburst_best[$period] = $candidates_cloudburst_sorted[$period][$candidates_cloudburst_id[$period]];
+ echo(" Conflict resolved\n");
+ $fixed = true;
+ } else {
+ echo(" Unable to resolve conflict\n");
+ }
+ } else {
+ echo(" Unable to resolve conflict\n");
+ }
+ } else {
+ $ponies[] = $candidates_cloudburst_best[$period];
+ }
+
+ $relations_cloudburst = $relationships[$candidates_cloudburst_best[$period]];
+ $relations_raindrops = $relationships[$candidates_raindrops_best[$period]];
+
+ if (!in_array($candidates_cloudburst_best[$period], $relations_raindrops)) {
+ echo($candidates_cloudburst_best[$period] . " (cloudburst) is not in a relationship with " . $candidates_raindrops_best[$period] . " (raindrops)\n");
+
+ $fixable = false;
+ $fixRelation = null;
+
+ foreach ($relationships[$candidates_cloudburst_best[$period]] as $relationship) {
+ if (in_array($relationship, $candidates_raindrops_sorted[$period])) {
+ $fixable = true;
+ $fixRelation = $relationship;
+ break;
+ }
+ }
+
+ if (!$fixable) {
+ echo(" Unable to resolve conflict\n");
+ } else {
+ $fixed = true;
+ $candidates_raindrops_id[$period] = array_search($fixRelation, $candidates_raindrops_sorted[$period]);
+ $candidates_raindrops_best[$period] = $fixRelation;
+ echo(" Conflict resolved\n");
+ }
+ }
+
+ if (!in_array($candidates_raindrops_best[$period], $relations_cloudburst)) {
+ echo($candidates_raindrops_best[$period] . " (raindrops) is not in a relationship with " . $candidates_cloudburst_best[$period] . " (cloudburst)\n");
+
+ $fixable = false;
+ $fixRelation = null;
+
+ foreach ($relationships[$candidates_raindrops_best[$period]] as $relationship) {
+ if (in_array($relationship, $candidates_cloudburst_sorted[$period])) {
+ $fixable = true;
+ $fixRelation = $relationship;
+ break;
+ }
+ }
+
+ if (!$fixable) {
+ echo(" Unable to resolve conflict\n");
+ } else {
+ $fixed = true;
+ $candidates_cloudburst_id[$period] = array_search($fixRelation, $candidates_cloudburst_sorted[$period]);
+ $candidates_cloudburst_best[$period] = $fixRelation;
+ echo(" Conflict resolved\n");
+ }
+ }
+ }
+
+ if (!$fixed) $correct = true;
+}
+
+echo("--------------\n");
+
+echo("Morning: " . getSystemMember("ynmuc", $candidates_cloudburst_best["morning"])["display_name"] . " with " . getSystemMember("gdapd", $candidates_raindrops_best["morning"])["display_name"] . "\n");
+echo("Afternoon: " . getSystemMember("ynmuc", $candidates_cloudburst_best["afternoon"])["display_name"] . " with " . getSystemMember("gdapd", $candidates_raindrops_best["afternoon"])["display_name"] . "\n");
+echo("Evening: " . getSystemMember("ynmuc", $candidates_cloudburst_best["evening"])["display_name"] . " with " . getSystemMember("gdapd", $candidates_raindrops_best["evening"])["display_name"] . "\n");
+echo("Night: " . getSystemMember("ynmuc", $candidates_cloudburst_best["night"])["display_name"] . " with " . getSystemMember("gdapd", $candidates_raindrops_best["night"])["display_name"] . "\n"); \ No newline at end of file
diff --git a/autoschedule/ponies.php b/autoschedule/ponies.php
new file mode 100644
index 0000000..3346452
--- /dev/null
+++ b/autoschedule/ponies.php
@@ -0,0 +1,99 @@
+<?php
+
+global $ponies_raindrops;
+global $ponies_cloudburst;
+global $weekend;
+
+$travelling = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/travelling.json"), true);
+
+function ponies(string $system): array {
+ global $travelling;
+ global $weekend;
+
+ return array_map(function ($i) use ($system, $weekend) {
+ $switches = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/$system-switches.json"), true);
+ $memberData = $i;
+
+ $i["_lastFronted"] = -1;
+ $i["_datesRaw"] = array_values(array_map(function ($i) {
+ return strtotime("1970-01-01T" . date("H:i:s", strtotime($i["timestamp"])) . ".000Z");
+ }, array_filter($switches, function ($item) use ($memberData) {
+ return in_array($memberData["id"], $item["members"]);
+ })));
+
+ $i["_datesValues"] = [
+ "night" => 0,
+ "morning" => 0,
+ "afternoon" => 0,
+ "evening" => 0
+ ];
+
+ foreach ($i["_datesRaw"] as $date) {
+ if ($date < 16200 || $date >= 75600) $i["_datesValues"]["night"]++;
+ if ($date >= 61200 && $date < 75600) $i["_datesValues"]["evening"]++;
+ if ($date >= 39600 && $date < 61200) $i["_datesValues"]["afternoon"]++;
+ if ($date >= 16200 && $date < 39600) $i["_datesValues"]["morning"]++;
+ }
+
+ if (($i["_metadata"]["little"] === 2 || $i["_metadata"]["shared_memory"] < 2) && !$weekend) {
+ $i["_datesValues"]["morning"] = 0;
+ $i["_datesValues"]["afternoon"] = 0;
+ }
+
+ $i["_dates"] = array_values(array_filter(array_keys($i["_datesValues"]), function ($j) use ($i) {
+ return $i["_datesValues"][$j] > 0;
+ }));
+
+ uasort($i["_dates"], function ($a, $b) use ($i) {
+ return $i["_datesValues"][$b] - $i["_datesValues"][$a];
+ });
+ $i["_dates"] = array_values($i["_dates"]);
+
+ $id = $i["id"];
+
+ $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 {
+ $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) use ($system, $travelling) {
+ return $i["_system"] === $system && (!in_array($i["id"], array_keys($travelling)) || !$travelling[$i["id"]]["travelling"]) && !isset($i["_metadata"]["median"]) && $i["name"] !== "unknown" && $i["name"] !== "fusion";
+ })));
+}
+
+$ponies_cloudburst = ponies("ynmuc");
+uasort($ponies_cloudburst, function ($a, $b) {
+ return $b["_lastFronted"] - $a["_lastFronted"];
+});
+$ponies_cloudburst = array_filter($ponies_cloudburst, function ($i) { return $i["_lastFronted"] > 0; });
+
+$ponies_raindrops = ponies("gdapd");
+uasort($ponies_raindrops, function ($a, $b) {
+ return $b["_lastFronted"] - $a["_lastFronted"];
+});
+$ponies_raindrops = array_filter($ponies_raindrops, function ($i) { return $i["_lastFronted"] > 0; });
+
+$ponies_cloudburst = array_reverse($ponies_cloudburst);
+$ponies_raindrops = array_reverse($ponies_raindrops); \ No newline at end of file