From 36aa10f64f70b38c2b9d0fa9e1fedf6bffbba29a Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Wed, 20 Sep 2023 01:14:08 +0800 Subject: [PATCH] change announce hr handle --- app/Jobs/UpdateTorrentSeedersEtc.php | 2 +- app/Jobs/UpdateUserSeedingLeechingTime.php | 2 +- include/constants.php | 2 +- include/globalfunctions.php | 4 + public/announce.php | 167 +++++---------------- 5 files changed, 44 insertions(+), 133 deletions(-) diff --git a/app/Jobs/UpdateTorrentSeedersEtc.php b/app/Jobs/UpdateTorrentSeedersEtc.php index de1b63cf..9dc640f6 100644 --- a/app/Jobs/UpdateTorrentSeedersEtc.php +++ b/app/Jobs/UpdateTorrentSeedersEtc.php @@ -23,7 +23,7 @@ class UpdateTorrentSeedersEtc implements ShouldQueue private string $requestId; - private string $idStr; + private ?string $idStr = null; /** * Create a new job instance. diff --git a/app/Jobs/UpdateUserSeedingLeechingTime.php b/app/Jobs/UpdateUserSeedingLeechingTime.php index 9cde9c19..9b20d647 100644 --- a/app/Jobs/UpdateUserSeedingLeechingTime.php +++ b/app/Jobs/UpdateUserSeedingLeechingTime.php @@ -23,7 +23,7 @@ class UpdateUserSeedingLeechingTime implements ShouldQueue private string $requestId; - private string $idStr; + private ?string $idStr = null; /** * Create a new job instance. diff --git a/include/constants.php b/include/constants.php index 35d34178..7e9f6a88 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ 0 || $truedownthis > 0)) { $dataTraffic = getDataTraffic($torrent, $_GET, $az, $self, $snatchInfo, apply_filter('torrent_promotion', $torrent)); $USERUPDATESET[] = "uploaded = uploaded + " . $dataTraffic['uploaded_increment_for_user']; $USERUPDATESET[] = "downloaded = downloaded + " . $dataTraffic['downloaded_increment_for_user']; - -// $global_promotion_state = get_global_sp_state(); -// if (isset($torrent['__ignore_global_sp_state']) && $torrent['__ignore_global_sp_state']) { -// do_log("[IGNORE_GLOBAL_SP_STATE], sp_state: {$torrent['sp_state']}"); -// $global_promotion_state = 1; -// } -// if($global_promotion_state == 1)// Normal, see individual torrent -// { -// if($torrent['sp_state']==3) //2X -// { -// $USERUPDATESET[] = "uploaded = uploaded + 2*$trueupthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis"; -// } -// elseif($torrent['sp_state']==4) //2X Free -// { -// $USERUPDATESET[] = "uploaded = uploaded + 2*$trueupthis"; -// } -// elseif($torrent['sp_state']==6) //2X 50% -// { -// $USERUPDATESET[] = "uploaded = uploaded + 2*$trueupthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis/2"; -// } -// else{ -// if ($torrent['owner'] == $userid && $uploaderdouble_torrent > 0) -// $upthis = $trueupthis * $uploaderdouble_torrent; -// -// if($torrent['sp_state']==2) //Free -// { -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// } -// elseif($torrent['sp_state']==5) //50% -// { -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis/2"; -// } -// elseif($torrent['sp_state']==7) //30% -// { -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis*3/10"; -// } -// elseif($torrent['sp_state']==1) //Normal -// { -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis"; -// } -// } -// } -// elseif($global_promotion_state == 2) //Free -// { -// if ($torrent['owner'] == $userid && $uploaderdouble_torrent > 0) -// $upthis = $trueupthis * $uploaderdouble_torrent; -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// } -// elseif($global_promotion_state == 3) //2X -// { -// if ($uploaderdouble_torrent > 2 && $torrent['owner'] == $userid && $uploaderdouble_torrent > 0) -// $upthis = $trueupthis * $uploaderdouble_torrent; -// else $upthis = 2*$trueupthis; -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis"; -// } -// elseif($global_promotion_state == 4) //2X Free -// { -// if ($uploaderdouble_torrent > 2 && $torrent['owner'] == $userid && $uploaderdouble_torrent > 0) -// $upthis = $trueupthis * $uploaderdouble_torrent; -// else $upthis = 2*$trueupthis; -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// } -// elseif($global_promotion_state == 5){ // 50% -// if ($torrent['owner'] == $userid && $uploaderdouble_torrent > 0) -// $upthis = $trueupthis * $uploaderdouble_torrent; -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis/2"; -// } -// elseif($global_promotion_state == 6){ //2X 50% -// if ($uploaderdouble_torrent > 2 && $torrent['owner'] == $userid && $uploaderdouble_torrent > 0) -// $upthis = $trueupthis * $uploaderdouble_torrent; -// else $upthis = 2*$trueupthis; -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis/2"; -// } -// elseif($global_promotion_state == 7){ //30% -// if ($torrent['owner'] == $userid && $uploaderdouble_torrent > 0) -// $upthis = $trueupthis * $uploaderdouble_torrent; -// $USERUPDATESET[] = "uploaded = uploaded + $upthis"; -// $USERUPDATESET[] = "downloaded = downloaded + $truedownthis*3/10"; -// } } } @@ -645,47 +557,6 @@ elseif(isset($self)) if (!empty($snatchInfo)) { sql_query("UPDATE snatched SET uploaded = uploaded + $trueupthis, downloaded = downloaded + $truedownthis, to_go = $left, $announcetime, last_action = ".$dt." $finished_snatched WHERE id = {$snatchInfo['id']}") or err("SL Err 2"); do_action('snatched_saved', $torrent, $snatchInfo); - if ($az['class'] < \App\Models\HitAndRun::MINIMUM_IGNORE_USER_CLASS && !$isDonor && isset($torrent['mode'])) { - $includeRate = \App\Models\HitAndRun::getConfig('include_rate', $torrent['mode']); - if ($includeRate === "" || $includeRate === null) { - //not set yet - $includeRate = 1; - } - $hrLog = sprintf("[HR_LOG] user: %d, torrent: %d, includeRate: %s", $userid, $torrentid, $includeRate); - $includeHr = false; - if ($event == "completed") { - //event completed, download finished, ignore rate - $includeHr = true; - $hrLog .= "event = completed"; - } elseif ($seeder == "no" && ($left <= $torrent['size'] * (1 - $includeRate))) { - //no event, download not finished - $includeHr = true; - $hrLog .= "seeder = no and left lte enough"; - } - $hrLog .= ", includeHr: $includeHr"; - if ($includeHr) { - $hrMode = \App\Models\HitAndRun::getConfig('mode', $torrent['mode']); - $hrLog .= ", hrMode: $hrMode"; - if ($hrMode == \App\Models\HitAndRun::MODE_GLOBAL || ($hrMode == \App\Models\HitAndRun::MODE_MANUAL && $torrent['hr'] == \App\Models\Torrent::HR_YES)) { - $hrCacheKey = sprintf("hit_and_run:%d:%d", $userid, $torrentid); - $hrExists = \Nexus\Database\NexusDB::remember($hrCacheKey, 24*3600, function () use ($snatchInfo) { - return \App\Models\HitAndRun::query()->where("snatched_id", $snatchInfo['id'])->exists(); - }); - $hrLog .= ", hrExists: $hrExists"; - if (!$hrExists) { - $sql = "insert into hit_and_runs (uid, torrent_id, snatched_id) values ($userid, $torrentid, {$snatchInfo['id']}) on duplicate key update updated_at = " . sqlesc(date('Y-m-d H:i:s')); - $affectedRows = sql_query($sql); - do_log("$hrLog, [INSERT_H&R], sql: $sql, affectedRows: $affectedRows"); - } else { - do_log("$hrLog, already exists", "debug"); - } - } else { - do_log("$hrLog, not match", "debug"); - } - } else { - do_log($hrLog, "debug"); - } - } } } } @@ -735,6 +606,42 @@ else } +//handle hr +if ($az['class'] < \App\Models\HitAndRun::MINIMUM_IGNORE_USER_CLASS && !$isDonor && isset($torrent['mode'])) { + $hrMode = \App\Models\HitAndRun::getConfig('mode', $torrent['mode']); + $hrLog = sprintf("[HR_LOG] user: %d, torrent: %d, hrMode: %s", $userid, $torrentid, $hrMode); + if ($hrMode == \App\Models\HitAndRun::MODE_GLOBAL || ($hrMode == \App\Models\HitAndRun::MODE_MANUAL && $torrent['hr'] == \App\Models\Torrent::HR_YES)) { + $hrCacheKey = sprintf("hit_and_run:%d:%d", $userid, $torrentid); + $hrExists = \Nexus\Database\NexusDB::remember($hrCacheKey, 24*3600, function () use ($torrentid, $userid) { + return \App\Models\HitAndRun::query()->where("uid", $userid)->where("torrent_id", $torrentid)->exists(); + }); + $hrLog .= ", hrExists: $hrExists"; + if (!$hrExists) { + //last check include rate + $includeRate = \App\Models\HitAndRun::getConfig('include_rate', $torrent['mode']); + if ($includeRate === "" || $includeRate === null) { + //not set yet + $includeRate = 1; + } + $hrLog .= ", includeRate: $includeRate"; + //get newest snatch info + $snatchInfo = get_snatch_info($torrentid, $userid); + $requiredDownloaded = $torrent['size'] * $includeRate; + if ($snatchInfo['downloaded'] >= $requiredDownloaded) { + $sql = "insert into hit_and_runs (uid, torrent_id, snatched_id) values ($userid, $torrentid, {$snatchInfo['id']}) on duplicate key update updated_at = " . sqlesc(date('Y-m-d H:i:s')); + $affectedRows = sql_query($sql); + do_log("$hrLog, total downloaded: {$snatchInfo['downloaded']} > required: $requiredDownloaded, [INSERT_H&R], sql: $sql, affectedRows: $affectedRows"); + } else { + do_log("$hrLog, total downloaded: {$snatchInfo['downloaded']} <= required: $requiredDownloaded", "debug"); + } + } else { + do_log("$hrLog, already exists", "debug"); + } + } else { + do_log("$hrLog, not match", "debug"); + } +} + if (isset($event) && !empty($event)) { $updateset[] = 'seeders = ' . get_row_count("peers", "where torrent = $torrentid and to_go = 0"); $updateset[] = 'leechers = ' . get_row_count("peers", "where torrent = $torrentid and to_go > 0");