Skip to content

Commit

Permalink
change announce hr handle
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Sep 19, 2023
1 parent 6128c17 commit 36aa10f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 133 deletions.
2 changes: 1 addition & 1 deletion app/Jobs/UpdateTorrentSeedersEtc.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class UpdateTorrentSeedersEtc implements ShouldQueue

private string $requestId;

private string $idStr;
private ?string $idStr = null;

/**
* Create a new job instance.
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/UpdateUserSeedingLeechingTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class UpdateUserSeedingLeechingTime implements ShouldQueue

private string $requestId;

private string $idStr;
private ?string $idStr = null;

/**
* Create a new job instance.
Expand Down
2 changes: 1 addition & 1 deletion include/constants.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.7');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-09-14');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-09-20');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
Expand Down
4 changes: 4 additions & 0 deletions include/globalfunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1205,3 +1205,7 @@ function is_danger_url($url): bool
return false;
}

function get_snatch_info($torrentId, $userId)
{
return mysql_fetch_assoc(sql_query(sprintf('select * from snatched where torrentid = %s and userid = %s order by id desc limit 1', $torrentId, $userId)));
}
167 changes: 37 additions & 130 deletions public/announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@
}
else // continue an existing session
{
$snatchInfo = mysql_fetch_assoc(sql_query(sprintf('select * from snatched where torrentid = %s and userid = %s order by id desc limit 1', $torrentid, $userid)));
$upthis = $trueupthis = max(0, $uploaded - $self["uploaded"]);
$downthis = $truedownthis = max(0, $downloaded - $self["downloaded"]);
$announcetime = ($self["seeder"] == "yes" ? "seedtime = seedtime + {$self['announcetime']}" : "leechtime = leechtime + {$self['announcetime']}");
Expand All @@ -511,99 +510,12 @@
}

do_log("upthis: $upthis, downthis: $downthis, announcetime: $announcetime, is_cheater: $is_cheater");

$snatchInfo = get_snatch_info($torrentid, $userid);
if (!$is_cheater && ($trueupthis > 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";
// }
}
}

Expand Down Expand Up @@ -645,47 +557,6 @@
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");
}
}
}
}
}
Expand Down Expand Up @@ -735,6 +606,42 @@

}

//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");
Expand Down

0 comments on commit 36aa10f

Please sign in to comment.