From d9c449d00a801feb09b37f6027beaad669b96d36 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Wed, 8 Nov 2023 03:17:23 +0800 Subject: [PATCH] exam add index SEED_POINTS + UPLOAD_TORRENT_COUNT --- app/Models/Exam.php | 6 +++- app/Models/Torrent.php | 5 +++ app/Models/User.php | 2 +- app/Repositories/ExamRepository.php | 48 ++++++++++++++++++++--------- include/constants.php | 2 +- resources/lang/en/exam.php | 2 ++ resources/lang/zh_CN/exam.php | 2 ++ resources/lang/zh_TW/exam.php | 2 ++ 8 files changed, 52 insertions(+), 17 deletions(-) diff --git a/app/Models/Exam.php b/app/Models/Exam.php index ccdc1259..d8fe5f44 100644 --- a/app/Models/Exam.php +++ b/app/Models/Exam.php @@ -36,12 +36,16 @@ class Exam extends NexusModel const INDEX_SEED_TIME_AVERAGE = 2; const INDEX_DOWNLOADED = 3; const INDEX_SEED_BONUS = 4; + const INDEX_SEED_POINTS = 5; + const INDEX_UPLOAD_TORRENT_COUNT = 6; - public static $indexes = [ + public static array $indexes = [ self::INDEX_UPLOADED => ['name' => 'Uploaded', 'unit' => 'GB', 'source_user_field' => 'uploaded'], self::INDEX_DOWNLOADED => ['name' => 'Downloaded', 'unit' => 'GB', 'source_user_field' => 'downloaded'], self::INDEX_SEED_TIME_AVERAGE => ['name' => 'Seed time average', 'unit' => 'Hour', 'source_user_field' => 'seedtime'], self::INDEX_SEED_BONUS => ['name' => 'Bonus', 'unit' => '', 'source_user_field' => 'seedbonus'], + self::INDEX_SEED_POINTS => ['name' => 'Seed points', 'unit' => '', 'source_user_field' => ''], + self::INDEX_UPLOAD_TORRENT_COUNT => ['name' => 'Upload torrent', 'unit' => '', 'source_user_field' => ''], ]; const FILTER_USER_CLASS = 'classes'; diff --git a/app/Models/Torrent.php b/app/Models/Torrent.php index 17fa32e2..29cdce9f 100644 --- a/app/Models/Torrent.php +++ b/app/Models/Torrent.php @@ -488,6 +488,11 @@ class Torrent extends NexusModel $query->where('visible', $visible); } + public function scopeNormal($query) + { + $query->where('visible', self::VISIBLE_YES)->where('banned', self::BANNED_NO); + } + public function torrent_tags(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(TorrentTag::class, 'torrent_id'); diff --git a/app/Models/User.php b/app/Models/User.php index 64981828..241f5596 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -223,7 +223,7 @@ class User extends Authenticatable implements FilamentUser, HasName 'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime', 'invited_by', 'enabled', 'seed_points', 'last_access', 'invites', 'lang', 'attendance_card', 'privacy', 'noad', 'downloadpos', 'donoruntil', 'donor', - 'seedbonus', 'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card', + 'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card', 'seed_points_per_hour' ]; diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index e7d99285..9e9fb3bd 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -455,9 +455,9 @@ class ExamRepository extends BaseRepository * * @param $examUser * @param null $user - * @return bool + * @return ExamUser|bool */ - public function updateProgress($examUser, $user = null) + public function updateProgress($examUser, $user = null): ExamUser|bool { $beginTimestamp = microtime(true); if (!$examUser instanceof ExamUser) { @@ -490,7 +490,7 @@ class ExamRepository extends BaseRepository } $exam = $examUser->exam; if (!$user instanceof User) { - $user = $examUser->user()->select(['id', 'uploaded', 'downloaded', 'seedtime', 'leechtime', 'seedbonus'])->first(); + $user = $examUser->user()->select(['id', 'uploaded', 'downloaded', 'seedtime', 'leechtime', 'seedbonus', 'seed_points'])->first(); } $attributes = [ 'exam_user_id' => $examUser->id, @@ -522,7 +522,7 @@ class ExamRepository extends BaseRepository $attributes['index'] = $index['index']; $attributes['created_at'] = $now; $attributes['updated_at'] = $now; - $attributes['value'] = $user->{Exam::$indexes[$index['index']]['source_user_field']} ?? 0; + $attributes['value'] = $this->getProgressValue($user, $index['index']); do_log("[GET_TOTAL_VALUE]: " . $attributes['value']); $newVersionProgress = ExamProgress::query() ->where('exam_user_id', $examUser->id) @@ -541,12 +541,8 @@ class ExamRepository extends BaseRepository } $attributes['init_value'] = $newVersionProgress->init_value; } else { - //do insert. check the init value - $progressData = $this->calculateProgress($examUser, true); - $increment = $progressData[$index['index']] ?? 0; - $initValue = $attributes['value'] - $increment; - $attributes['init_value'] = max($initValue, 0); - do_log("total: {$attributes['value']}, increment: $increment, init_value: $initValue, final init_value: {$attributes['init_value']}"); + //do insert. + $attributes['init_value'] = $attributes['value']; $attributes['torrent_id'] = -1; ExamProgress::query()->insert($attributes); do_log("newVersionProgress [NOT EXISTS], doInsert with: " . json_encode($attributes)); @@ -600,6 +596,29 @@ class ExamRepository extends BaseRepository return $examUser; } + private function getProgressValue(User $user, int $index) + { + if ($index == Exam::INDEX_UPLOADED) { + return $user->uploaded; + } + if ($index == Exam::INDEX_DOWNLOADED) { + return $user->downloaded; + } + if ($index == Exam::INDEX_SEED_BONUS) { + return $user->seedbonus; + } + if ($index == Exam::INDEX_SEED_TIME_AVERAGE) { + return $user->seedtime; + } + if ($index == Exam::INDEX_SEED_POINTS) { + return $user->seed_points; + } + if ($index == Exam::INDEX_UPLOAD_TORRENT_COUNT) { + return Torrent::query()->where("owner", $user->id)->normal()->count(); + } + throw new \InvalidArgumentException("Invalid index: $index"); + } + /** * get user exam status @@ -645,11 +664,12 @@ class ExamRepository extends BaseRepository } /** + * @deprecated * @param ExamUser $examUser - * @param false $allSum + * @param bool $allSum * @return array|null */ - public function calculateProgress(ExamUser $examUser, $allSum = false) + public function calculateProgress(ExamUser $examUser, bool $allSum = false) { $logPrefix = "examUser: " . $examUser->id; $begin = $examUser->begin; @@ -685,7 +705,7 @@ class ExamRepository extends BaseRepository ->first() ->torrent_count; $progressSum[$index] = intval($progressSum[$index] / $torrentCount); - $logPrefix .= ", get torrent count: $torrentCount, from query: " . last_query(); + $logPrefix .= ", index: INDEX_SEED_TIME_AVERAGE, get torrent count: $torrentCount, from query: " . last_query(); } do_log("$logPrefix, final progressSum: " . json_encode($progressSum)); @@ -1050,7 +1070,7 @@ class ExamRepository extends BaseRepository foreach ($rows as $row) { $result = $this->updateProgress($row); do_log("$logPrefix, examUser: " . $row->toJson() . ", result type: " . gettype($result)); - if ($result != false) { + if ($result) { $success += 1; } } diff --git a/include/constants.php b/include/constants.php index e9438c66..b2449965 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ 'Seed time average', 'index_text_' . \App\Models\Exam::INDEX_DOWNLOADED => 'Download increment', 'index_text_' . \App\Models\Exam::INDEX_SEED_BONUS => 'Bonus increment', + 'index_text_' . \App\Models\Exam::INDEX_SEED_POINTS => 'Seed points increment', + 'index_text_' . \App\Models\Exam::INDEX_UPLOAD_TORRENT_COUNT => 'Upload torrent increment', 'filters' => [ \App\Models\Exam::FILTER_USER_CLASS => 'User class', \App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => 'Register time range', diff --git a/resources/lang/zh_CN/exam.php b/resources/lang/zh_CN/exam.php index fd488e0d..c68cd005 100644 --- a/resources/lang/zh_CN/exam.php +++ b/resources/lang/zh_CN/exam.php @@ -9,6 +9,8 @@ return [ 'index_text_' . \App\Models\Exam::INDEX_SEED_TIME_AVERAGE => '平均做种时间', 'index_text_' . \App\Models\Exam::INDEX_DOWNLOADED => '下载增量', 'index_text_' . \App\Models\Exam::INDEX_SEED_BONUS => '魔力增量', + 'index_text_' . \App\Models\Exam::INDEX_SEED_POINTS => '做种积分增量', + 'index_text_' . \App\Models\Exam::INDEX_UPLOAD_TORRENT_COUNT => '发种增量', 'filters' => [ \App\Models\Exam::FILTER_USER_CLASS => '用户等级', \App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => '注册时间范围', diff --git a/resources/lang/zh_TW/exam.php b/resources/lang/zh_TW/exam.php index 21490e57..d552b442 100644 --- a/resources/lang/zh_TW/exam.php +++ b/resources/lang/zh_TW/exam.php @@ -9,6 +9,8 @@ return [ 'index_text_' . \App\Models\Exam::INDEX_SEED_TIME_AVERAGE => '平均做種時間', 'index_text_' . \App\Models\Exam::INDEX_DOWNLOADED => '下載增量', 'index_text_' . \App\Models\Exam::INDEX_SEED_BONUS => '魔力增量', + 'index_text_' . \App\Models\Exam::INDEX_SEED_POINTS => '做種積分增量', + 'index_text_' . \App\Models\Exam::INDEX_UPLOAD_TORRENT_COUNT => '發種增量', 'filters' => [ \App\Models\Exam::FILTER_USER_CLASS => '用戶等級', \App\Models\Exam::FILTER_USER_REGISTER_TIME_RANGE => '註冊時間範圍',