2025-02-16 04:37:18 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controllers\Exec\Tasks;
|
|
|
|
require_once __DIR__ . '/../../../../vendor/autoload.php';
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
define("NGALLERY", Yaml::parse(file_get_contents(__DIR__ . '/../../../../ngallery.yaml'))['ngallery']);
|
|
|
|
use App\Services\{Router, Auth, DB, Json, Date};
|
|
|
|
use App\Controllers\ExceptionRegister;
|
|
|
|
use App\Core\Page;
|
|
|
|
|
|
|
|
class ExecContests
|
|
|
|
{
|
|
|
|
public static function run()
|
|
|
|
{
|
|
|
|
if (NGALLERY['root']['contests']['enabled'] != true) {
|
|
|
|
echo "Contests on this server disabled. Skip...";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$contests = DB::query('SELECT * FROM contests WHERE status < 3');
|
|
|
|
foreach ($contests as $contest) {
|
|
|
|
self::processContest($contest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function processContest(array $contest)
|
|
|
|
{
|
|
|
|
echo "Checking contest ID {$contest['id']}\n";
|
|
|
|
|
|
|
|
switch ($contest['status']) {
|
|
|
|
case 0:
|
|
|
|
self::handleOpenPretends($contest);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
self::handleClosePretends($contest);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
self::handleClosingContest($contest);
|
|
|
|
break;
|
2025-02-17 22:10:46 +03:00
|
|
|
case 02:
|
|
|
|
self::handleClosePretendsByTime($contest);
|
|
|
|
break;
|
2025-02-16 04:37:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function handleOpenPretends(array $contest)
|
|
|
|
{
|
|
|
|
if (self::isAnotherContestInStatus(1)) {
|
|
|
|
echo "[{$contest['id']}] Waiting for another contest to complete dialing. Skip...\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($contest['openpretendsdate'] <= time()) {
|
|
|
|
DB::query('UPDATE contests SET status = 1 WHERE id = :id', [':id' => $contest['id']]);
|
|
|
|
echo "[{$contest['id']}] Opened for pretends.\n";
|
|
|
|
} else {
|
|
|
|
echo "[{$contest['id']}] Not ready for open pretends. Skip...\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function handleClosePretends(array $contest)
|
|
|
|
{
|
2025-02-17 22:10:46 +03:00
|
|
|
if (self::isAnotherContestInStatus(2) || self::isAnotherContestInStatus(02)) {
|
2025-02-16 04:37:18 +03:00
|
|
|
echo "[{$contest['id']}] Waiting for another contest to end. Skip...\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-17 22:10:46 +03:00
|
|
|
if ($contest['closepretendsdate'] <= time()) {
|
2025-02-16 06:18:56 +03:00
|
|
|
DB::query('UPDATE photos SET on_contest=2 WHERE on_contest=1 AND contest_id=:id', array(':id'=>$contest['id']));
|
2025-02-17 22:10:46 +03:00
|
|
|
if ($contest['opendate'] <= time()) {
|
|
|
|
DB::query('UPDATE contests SET status = 2 WHERE id = :id', [':id' => $contest['id']]);
|
|
|
|
echo "[{$contest['id']}] Opened.\n";
|
|
|
|
} else {
|
|
|
|
DB::query('UPDATE contests SET status = 02 WHERE id = :id', [':id' => $contest['id']]);
|
|
|
|
}
|
2025-02-16 04:37:18 +03:00
|
|
|
echo "[{$contest['id']}] Closed for pretends.\n";
|
|
|
|
} else {
|
|
|
|
echo "[{$contest['id']}] Not closed for pretends. Skip...\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-17 22:10:46 +03:00
|
|
|
private static function handleClosePretendsByTime(array $contest)
|
|
|
|
{
|
|
|
|
echo "[{$contest['id']}] Cheking for Open by time...\n";
|
|
|
|
if ($contest['opendate'] <= time()) {
|
|
|
|
DB::query('UPDATE contests SET status = 2 WHERE id = :id', [':id' => $contest['id']]);
|
|
|
|
echo "[{$contest['id']}] .\n";
|
|
|
|
} else {
|
|
|
|
echo "[{$contest['id']}] not opened by time. Skip...\n";
|
|
|
|
}
|
|
|
|
echo "[{$contest['id']}] Opened.\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-02-16 04:37:18 +03:00
|
|
|
private static function handleClosingContest(array $contest)
|
|
|
|
{
|
|
|
|
if ($contest['closedate'] > time()) {
|
|
|
|
echo "[{$contest['id']}] Waiting for end time. Skip...\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "[{$contest['id']}] Ready for closing!\n";
|
|
|
|
self::processVotes($contest);
|
|
|
|
DB::query('UPDATE contests SET status = 3 WHERE id = :id', [':id' => $contest['id']]);
|
2025-02-17 22:10:46 +03:00
|
|
|
DB::query('UPDATE photos SET contest_id = 0, on_contest = 0 WHERE contest_id = :id', [':id' => $contest['id']]);
|
2025-02-16 04:37:18 +03:00
|
|
|
echo "[{$contest['id']}] Closed.\n";
|
|
|
|
if (NGALLERY['root']['contests']['autonew']['enabled'] === true) {
|
|
|
|
echo "Creating new contest...";
|
|
|
|
$theme = DB::query('SELECT * FROM contests_themes WHERE status=1 ORDER BY RAND() LIMIT 1')[0];
|
2025-02-17 22:10:46 +03:00
|
|
|
if (count($theme) <= 0) {
|
|
|
|
echo "Not found themes for autocreating Contest. Skip...\n";
|
|
|
|
return;
|
|
|
|
}
|
2025-02-16 04:37:18 +03:00
|
|
|
$time = time();
|
2025-02-17 22:10:46 +03:00
|
|
|
if (NGALLERY['root']['contests']['autonew']['times']['pretendsopen'] === 'now') {
|
2025-02-16 04:37:18 +03:00
|
|
|
$pretendsopen = $time;
|
2025-02-17 22:10:46 +03:00
|
|
|
$status = 1;
|
2025-02-16 04:37:18 +03:00
|
|
|
} else {
|
2025-02-17 22:10:46 +03:00
|
|
|
$status = 0;
|
|
|
|
$pretendsopen = Date::addTime(NGALLERY['root']['contests']['autonew']['times']['pretendsopen']);
|
2025-02-16 04:37:18 +03:00
|
|
|
}
|
|
|
|
|
2025-02-17 22:10:46 +03:00
|
|
|
$pretendsclose = Date::addTime(NGALLERY['root']['contests']['autonew']['times']['pretendsclose']);
|
|
|
|
if (NGALLERY['root']['contests']['autonew']['times']['open'] === 'now') {
|
2025-02-16 04:37:18 +03:00
|
|
|
$contestopen = $pretendsclose;
|
|
|
|
} else {
|
2025-02-17 22:10:46 +03:00
|
|
|
$contestopen = Date::addTime(NGALLERY['root']['contests']['autonew']['times']['open']);
|
2025-02-16 04:37:18 +03:00
|
|
|
}
|
2025-02-17 22:10:46 +03:00
|
|
|
$contestclose = Date::addTime(NGALLERY['root']['contests']['autonew']['times']['close']);
|
|
|
|
DB::query('INSERT INTO contests VALUES (\'0\', :themeid, :openprdate, :closeprdate, :opendate, :closedate, :status)', array(':themeid'=>$theme['id'], ':openprdate'=>$pretendsopen, ':closeprdate'=>$pretendsclose, ':opendate'=>$contestopen, ':closedate'=>$contestclose, ':status'=>$status));
|
2025-02-16 04:37:18 +03:00
|
|
|
echo "Contest created! Continue...";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function processVotes(array $contest)
|
|
|
|
{
|
|
|
|
$votes = DB::query(
|
|
|
|
'SELECT user_id, photo_id, COUNT(*) AS vote_count
|
2025-02-17 22:10:46 +03:00
|
|
|
FROM contests_rates WHERE contest_id = :id
|
2025-02-16 04:37:18 +03:00
|
|
|
GROUP BY user_id ORDER BY vote_count DESC LIMIT 3',
|
|
|
|
[':id' => $contest['id']]
|
|
|
|
);
|
|
|
|
|
|
|
|
$place = 1;
|
|
|
|
foreach ($votes as $vote) {
|
|
|
|
self::updatePhotoContent($vote, $contest, $place);
|
|
|
|
$place++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function updatePhotoContent(array $vote, array $contest, int $place)
|
|
|
|
{
|
|
|
|
$photo = DB::query('SELECT * FROM photos WHERE id = :id', [':id' => $vote['photo_id']])[0];
|
|
|
|
$photoData = json_decode($photo['content'], true);
|
2025-02-17 22:10:46 +03:00
|
|
|
|
|
|
|
if (!isset($photoData['contests']) || !is_array($photoData['contests'])) {
|
|
|
|
$photoData['contests'] = [];
|
|
|
|
}
|
|
|
|
|
2025-02-16 04:37:18 +03:00
|
|
|
$theme = DB::query('SELECT title FROM contests_themes WHERE id = :id', [':id' => $contest['themeid']])[0]['title'];
|
2025-02-17 22:10:46 +03:00
|
|
|
|
|
|
|
$photoData['contests'][] = [
|
2025-02-16 04:37:18 +03:00
|
|
|
'id' => $contest['id'],
|
|
|
|
'contesttheme' => $theme,
|
|
|
|
'votenum' => $vote['vote_count'],
|
|
|
|
'place' => $place
|
|
|
|
];
|
2025-02-17 22:10:46 +03:00
|
|
|
|
2025-02-17 23:10:16 +03:00
|
|
|
|
|
|
|
DB::query('INSERT INTO contests_winners VALUES (\'0\', :photo_id, :place, :contest_id, :date)', array(':photo_id'=>$vote['photo_id'], ':place'=>$place, ':contest_id'=>$contest['id'], ':date'=>time()));
|
|
|
|
|
2025-02-16 06:18:56 +03:00
|
|
|
DB::query('UPDATE photos SET content = :content, on_contest=0, contest_id=0 WHERE id = :id', [
|
2025-02-16 04:37:18 +03:00
|
|
|
':id' => $vote['photo_id'],
|
2025-02-17 22:10:46 +03:00
|
|
|
':content' => json_encode($photoData, JSON_UNESCAPED_UNICODE)
|
2025-02-16 04:37:18 +03:00
|
|
|
]);
|
2025-02-17 22:10:46 +03:00
|
|
|
|
2025-02-16 04:37:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function isAnotherContestInStatus(int $status): bool
|
|
|
|
{
|
|
|
|
return !empty(DB::query('SELECT status FROM contests WHERE status = :status', [':status' => $status]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (php_sapi_name() === 'cli') {
|
|
|
|
ExecContests::run();
|
|
|
|
}
|