summaryrefslogtreecommitdiff
path: root/autoschedule/main.php
blob: 892ea024789cd5599ecbdae68f4b6480dcb21073 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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");