[H&R] basically

This commit is contained in:
xiaomlove
2021-06-21 02:01:26 +08:00
parent 45aba84111
commit d2a8f1a1a6
39 changed files with 966 additions and 74 deletions
@@ -0,0 +1,53 @@
<?php
namespace App\Console\Commands;
use App\Repositories\HitAndRunRepository;
use Illuminate\Console\Command;
class HitAndRunUpdateStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'hr:update_status {--uid=} {--torrent_id=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update H&R status, options: --uid, --torrent_id';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uid = $this->option('uid');
$torrentId = $this->option('torrent_id');
$rep = new HitAndRunRepository();
$result = $rep->cronjobUpdateStatus($uid, $torrentId);
$log = sprintf(
'[%s], %s, uid: %s, torrentId: %s, result: %s',
REQUEST_ID, __METHOD__, $uid, $torrentId, var_export($result, true)
);
$this->info($log);
do_log($log);
return 0;
}
}
+7 -4
View File
@@ -6,7 +6,9 @@ use App\Models\Attendance;
use App\Models\Exam;
use App\Models\ExamProgress;
use App\Models\ExamUser;
use App\Models\HitAndRun;
use App\Models\SearchBox;
use App\Models\Snatch;
use App\Models\User;
use App\Repositories\ExamRepository;
use App\Repositories\SearchBoxRepository;
@@ -53,10 +55,11 @@ class Test extends Command
*/
public function handle()
{
$time = '2021-06-16T04:03:35Z';
$date = Carbon::parse($time);
$date->tz = 'Asia/Shanghai';
dd($date->toDateTimeString());
$user = User::query()->first();
$user->update([
'page' => DB::raw('dddd')
]);
dd(last_query());
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Models;
class BonusLogs extends NexusModel
{
protected $table = 'bonus_logs';
protected $fillable = ['uid', 'business_type', 'old_total_value', 'value', 'new_total_value', 'comment'];
const DEFAULT_BONUS_CANCEL_ONE_HIT_AND_RUN = 10000;
const BUSINESS_TYPE_CANCEL_HIT_AND_RUN = 1;
public static $businessTypes = [
self::BUSINESS_TYPE_CANCEL_HIT_AND_RUN => ['text' => 'Cancel H&R'],
];
public static function getBonusForCancelHitAndRun()
{
$result = Setting::get('bonus.cancel_hr');
return $result ?? self::DEFAULT_BONUS_CANCEL_ONE_HIT_AND_RUN;
}
}
+57 -9
View File
@@ -6,25 +6,73 @@ class HitAndRun extends NexusModel
{
protected $table = 'hit_and_runs';
protected $fillable = ['uid', 'peer_id', 'torrent_id', 'status', 'comment'];
protected $fillable = ['uid', 'snatch_id', 'torrent_id', 'status', 'comment'];
public $timestamps = true;
const STATUS_INSPECTING = 1;
const STATUS_PASSED = 2;
const STATUS_NOT_PASSED = 3;
const STATUS_CANCELED = 4;
const STATUS_REACHED = 2;
const STATUS_UNREACHED = 3;
const STATUS_PARDONED = 4;
public static $status = [
self::STATUS_INSPECTING => ['text' => '考察中'],
self::STATUS_PASSED => ['text' => '已通过'],
self::STATUS_NOT_PASSED => ['text' => '未通过'],
self::STATUS_CANCELED => ['text' => '已取消'],
self::STATUS_INSPECTING => ['text' => 'Inspecting'],
self::STATUS_REACHED => ['text' => 'Reached'],
self::STATUS_UNREACHED => ['text' => 'Unreached'],
self::STATUS_PARDONED => ['text' => 'Pardoned'],
];
const MODE_DISABLED = 'disabled';
const MODE_MANUAL = 'manual';
const MODE_GLOBAL = 'global';
public static $modes = [
self::MODE_DISABLED => ['text' => 'Disabled'],
self::MODE_MANUAL => ['text' => 'Manual'],
self::MODE_GLOBAL => ['text' => 'Global'],
];
public function getStatusTextAttribute()
{
return self::$status[$this->status] ?? '';
return nexus_trans('hr.status_' . $this->status);
}
public static function listStatus(): array
{
$result = self::$status;
foreach ($result as $key => &$value) {
$value['text'] = nexus_trans('hr.status_' . $key);
}
return $result;
}
public static function listModes(): array
{
$result = self::$modes;
foreach ($result as $key => &$value) {
$value['text'] = nexus_trans('hr.mode_' . $key);
}
return $result;
}
public static function getIsEnabled(): bool
{
return Setting::get('hr.mode') != self::MODE_DISABLED;
}
public function torrent(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Torrent::class, 'torrent_id');
}
public function snatch(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Snatch::class, 'snatched_id');
}
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class, 'uid');
}
+25
View File
@@ -41,6 +41,31 @@ class Torrent extends NexusModel
self::POS_STATE_STICKY_FIRST => ['text' => 'Sticky first', 'icon_counts' => 2],
];
const HR_YES = 1;
const HR_NO = 0;
public static $hrStatus = [
self::HR_NO => ['text' => 'NO'],
self::HR_YES => ['text' => 'YES'],
];
public function getHrAttribute(): string
{
$hrMode = Setting::get('hr.mode');
if ($hrMode == HitAndRun::MODE_GLOBAL) {
return self::HR_YES;
}
if ($hrMode == HitAndRun::MODE_DISABLED) {
return self::HR_NO;
}
return $this->getRawOriginal('hr');
}
public function getHrTextAttribute()
{
return self::$hrStatus[$this->hr] ?? '';
}
public static function getBasicInfo(): array
{
$result = [];
+7 -1
View File
@@ -108,7 +108,7 @@ class User extends Authenticatable
*/
protected $fillable = [
'username', 'email', 'passhash', 'secret', 'stylesheet', 'editsecret', 'added', 'modcomment', 'enabled', 'status',
'leechwarn', 'leechwarnuntil'
'leechwarn', 'leechwarnuntil', 'page'
];
/**
@@ -238,6 +238,11 @@ class User extends Authenticatable
'torrentid');
}
public function hitAndRuns(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(HitAndRun::class, 'uid');
}
public function getAvatarAttribute($value)
{
if ($value) {
@@ -260,6 +265,7 @@ class User extends Authenticatable
//@todo how to do prepare bindings here ?
$modComment = addslashes($modComment);
$update['modcomment'] = DB::raw("concat_ws('\n', '$modComment', modcomment)");
do_log("update: " . json_encode($update) . ", modcomment: $modComment", 'notice');
return $this->update($update);
}
+68
View File
@@ -0,0 +1,68 @@
<?php
namespace App\Repositories;
use App\Models\BonusLogs;
use App\Models\HitAndRun;
use App\Models\Setting;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Query\Expression;
use Nexus\Database\NexusDB;
class BonusRepository extends BaseRepository
{
public function consumeToCancelHitAndRun($uid, $hitAndRunId)
{
$hitAndRunMode = Setting::get('hr.mode');
if ($hitAndRunMode == HitAndRun::MODE_DISABLED) {
throw new \LogicException("H&R not enabled.");
}
$user = User::query()->findOrFail($uid);
$hitAndRun = HitAndRun::query()->findOrFail($hitAndRunId);
if ($hitAndRun->uid != $uid) {
throw new \LogicException("H&R: $hitAndRunId not belongs to user: $uid.");
}
$requireBonus = BonusLogs::getBonusForCancelHitAndRun();
if ($user->seedbonus < $requireBonus) {
do_log("user: $uid, bonus: {$user->seedbonus} < requireBonus: $requireBonus", 'error');
throw new \LogicException("User bonus point not enough.");
}
$result = NexusDB::transaction(function () use ($user, $hitAndRun, $requireBonus) {
$oldUserBonus = $user->seedbonus;
$newUserBonus = bcsub($oldUserBonus, $requireBonus);
$log = "user: {$user->id}, hitAndRun: {$hitAndRun->id}, requireBonus: $requireBonus, oldUserBonus: $oldUserBonus, newUserBonus: $newUserBonus";
do_log($log);
$affectedRows = NexusDB::table($user->getTable())
->where('id', $user->id)
->where('seedbonus', $oldUserBonus)
->update(['seedbonus' => $newUserBonus]);
if ($affectedRows != 1) {
do_log("update user seedbonus affected rows != 1, query: " . last_query(), 'error');
throw new \RuntimeException("Update user seedbonus fail.");
}
$comment = nexus_trans('hr.bonus_cancel_comment', [
'now' => Carbon::now()->toDateTimeString(),
'bonus' => $requireBonus,
], $user->locale);
do_log("comment: $comment");
$hitAndRun->update([
'status' => HitAndRun::STATUS_PARDONED,
'comment' => new Expression("concat(comment, '\n$comment')"),
]);
$bonusLog = [
'business_type' => BonusLogs::BUSINESS_TYPE_CANCEL_HIT_AND_RUN,
'uid' => $user->id,
'old_total_value' => $oldUserBonus,
'value' => $requireBonus,
'new_total_value' => $newUserBonus,
'comment' => "$comment(H&R ID: {$hitAndRun->id})",
];
BonusLogs::query()->insert($bonusLog);
do_log("bonusLog: " . json_encode($bonusLog));
return true;
});
return $result;
}
}
+239
View File
@@ -0,0 +1,239 @@
<?php
namespace App\Repositories;
use App\Models\HitAndRun;
use App\Models\Message;
use App\Models\Setting;
use App\Models\User;
use App\Models\UserBanLog;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class HitAndRunRepository extends BaseRepository
{
public function cronjobUpdateStatus($uid = null, $torrentId = null)
{
$size = 1000;
$page = 1;
$setting = Setting::get('hr');
if (empty($setting['mode'])) {
do_log("H&R not set.");
return false;
}
if ($setting['mode'] == HitAndRun::MODE_DISABLED) {
do_log("H&R mode is disabled.");
return false;
}
$query = HitAndRun::query()
->where('status', HitAndRun::STATUS_INSPECTING)
->where('created_at', '<', Carbon::now()->subHours($setting['inspect_time']))
->with([
'torrent' => function ($query) {$query->select(['id', 'size', 'name']);},
'snatch',
'user' => function ($query) {$query->select(['id', 'username', 'lang']);},
'user.language',
]);
if (!is_null($uid)) {
$query->where('uid', $uid);
}
if (!is_null($torrentId)) {
$query->where('torrent_id', $torrentId);
}
$successCounts = 0;
while (true) {
$logPrefix = "page: $page, size: $size";
$rows = $query->forPage($page, $size)->get();
do_log("$logPrefix, counts: " . $rows->count());
if ($rows->isEmpty()) {
do_log("$logPrefix, no more data..." . last_query());
break;
}
foreach ($rows as $row) {
$logPrefix = "[HANDLING] " . $row->toJson();
do_log($logPrefix);
if (!$row->user) {
do_log("$logPrefix, user not exists, skip!", 'error');
continue;
}
if (!$row->snatch) {
do_log("$logPrefix, snatch not exists, skip!", 'error');
continue;
}
if (!$row->torrent) {
do_log("$logPrefix, torrent not exists, skip!", 'error');
continue;
}
//check seed time
$targetSeedTime = $row->snatch->seedtime;
$requireSeedTime = bcmul($setting['seed_time_minimum'], 3600);
$locale = $row->user->locale;
do_log("targetSeedTime: $targetSeedTime, requireSeedTime: $requireSeedTime");
if ($targetSeedTime >= $requireSeedTime) {
$result = $this->reachedBySeedTime($row);
if ($result) {
$successCounts++;
}
continue;
}
//check share ratio
$targetShareRatio = bcdiv($row->snatch->uploaded, $row->torrent->size, 4);
$requireShareRatio = $setting['ignore_when_ratio_reach'];
do_log("targetShareRatio: $targetShareRatio, requireShareRatio: $requireShareRatio");
if ($targetShareRatio >= $requireShareRatio) {
$result = $this->reachedByShareRatio($row);
if ($result) {
$successCounts++;
}
continue;
}
//unreached
$result = $this->unreached($row);
if ($result) {
$successCounts++;
}
}
$page++;
}
do_log("[CRONJOB_UPDATE_HR_DONE]");
return $successCounts;
}
private function geReachedMessage(HitAndRun $hitAndRun): array
{
return [
'receiver' => $hitAndRun->uid,
'added' => Carbon::now()->toDateTimeString(),
'subject' => nexus_trans('hr.reached_message_subject', ['hit_and_run_id' => $hitAndRun->id], $hitAndRun->user->locale),
'msg' => nexus_trans('hr.reached_message_content', [
'completed_at' => $hitAndRun->snatch->completedat->toDateTimeString(),
'torrent_id' => $hitAndRun->torrent_id,
'torrent_name' => $hitAndRun->torrent->name,
], $hitAndRun->user->locale),
];
}
private function reachedByShareRatio(HitAndRun $hitAndRun): bool
{
do_log(__METHOD__);
$comment = nexus_trans('hr.reached_by_share_ratio_comment', [
'now' => Carbon::now()->toDateTimeString(),
'seed_time_minimum' => Setting::get('hr.seed_time_minimum'),
'seed_time' => bcdiv($hitAndRun->snatch->seedtime, 3600, 1),
'share_ratio' => get_hr_ratio($hitAndRun->snatch->uploaded, $hitAndRun->snatch->downloaded),
'ignore_when_ratio_reach' => Setting::get('hr.ignore_when_ratio_reach'),
], $hitAndRun->user->locale);
$update = [
'status' => HitAndRun::STATUS_REACHED,
'comment' => $comment
];
$affectedRows = DB::table($hitAndRun->getTable())
->where('id', $hitAndRun->id)
->where('status', HitAndRun::STATUS_INSPECTING)
->update($update);
do_log("[H&R_REACHED_BY_SHARE_RATIO], " . last_query() . ", affectedRows: $affectedRows");
if ($affectedRows != 1) {
do_log($hitAndRun->toJson() . ", [H&R_REACHED_BY_SHARE_RATIO], affectedRows != 1, skip!", 'notice');
return false;
}
$message = $this->geReachedMessage($hitAndRun);
Message::query()->insert($message);
return true;
}
private function reachedBySeedTime(HitAndRun $hitAndRun): bool
{
do_log(__METHOD__);
$comment = nexus_trans('hr.reached_by_seed_time_comment', [
'now' => Carbon::now()->toDateTimeString(),
'seed_time' => bcdiv($hitAndRun->snatch->seedtime, 3600, 1),
'seed_time_minimum' => Setting::get('hr.seed_time_minimum')
], $hitAndRun->user->locale);
$update = [
'status' => HitAndRun::STATUS_REACHED,
'comment' => $comment
];
$affectedRows = DB::table($hitAndRun->getTable())
->where('id', $hitAndRun->id)
->where('status', HitAndRun::STATUS_INSPECTING)
->update($update);
do_log("[H&R_REACHED_BY_SEED_TIME], " . last_query() . ", affectedRows: $affectedRows");
if ($affectedRows != 1) {
do_log($hitAndRun->toJson() . ", [H&R_REACHED_BY_SEED_TIME], affectedRows != 1, skip!", 'notice');
return false;
}
$message = $this->geReachedMessage($hitAndRun);
Message::query()->insert($message);
return true;
}
private function unreached(HitAndRun $hitAndRun): bool
{
do_log(__METHOD__);
$comment = nexus_trans('hr.unreached_comment', [
'now' => Carbon::now()->toDateTimeString(),
'seed_time' => bcdiv($hitAndRun->snatch->seedtime, 3600, 1),
'seed_time_minimum' => Setting::get('hr.seed_time_minimum'),
'share_ratio' => get_hr_ratio($hitAndRun->snatch->uploaded, $hitAndRun->snatch->downloaded),
'torrent_size' => mksize($hitAndRun->torrent->size),
'ignore_when_ratio_reach' => Setting::get('hr.ignore_when_ratio_reach')
], $hitAndRun->user->locale);
$update = [
'status' => HitAndRun::STATUS_UNREACHED,
'comment' => $comment
];
$affectedRows = DB::table($hitAndRun->getTable())
->where('id', $hitAndRun->id)
->where('status', HitAndRun::STATUS_INSPECTING)
->update($update);
do_log("[H&R_UNREACHED], " . last_query() . ", affectedRows: $affectedRows");
if ($affectedRows != 1) {
do_log($hitAndRun->toJson() . ", [H&R_UNREACHED], affectedRows != 1, skip!", 'notice');
return false;
}
$message = [
'receiver' => $hitAndRun->uid,
'added' => Carbon::now()->toDateTimeString(),
'subject' => nexus_trans('hr.unreached_message_subject', ['hit_and_run_id' => $hitAndRun->id], $hitAndRun->user->locale),
'msg' => nexus_trans('hr.unreached_message_content', [
'completed_at' => $hitAndRun->snatch->completedat->toDateTimeString(),
'torrent_id' => $hitAndRun->torrent_id,
'torrent_name' => $hitAndRun->torrent->name,
], $hitAndRun->user->locale),
];
Message::query()->insert($message);
//disable user
/** @var User $user */
$user = $hitAndRun->user;
$counts = $user->hitAndRuns()->where('status', HitAndRun::STATUS_UNREACHED)->count();
$disableCounts = Setting::get('hr.ban_user_when_counts_reach');
do_log("user: {$user->id}, H&R counts: $counts, disableCounts: $disableCounts", 'notice');
if ($counts >= $disableCounts) {
do_log("[DISABLE_USER_DUE_TO_H&R_UNREACHED]", 'notice');
$comment = nexus_trans('hr.unreached_disable_comment', [], $user->locale);
$user->updateWithModComment(['enabled' => User::ENABLED_NO], $comment);
$message = [
'receiver' => $hitAndRun->uid,
'added' => Carbon::now()->toDateTimeString(),
'subject' => $comment,
'msg' => nexus_trans('hr.unreached_disable_message_content', [
'ban_user_when_counts_reach' => Setting::get('hr.ban_user_when_counts_reach'),
], $hitAndRun->user->locale),
];
Message::query()->insert($message);
$userBanLog = [
'uid' => $user->id,
'username' => $user->username,
'reason' => $comment
];
UserBanLog::query()->insert($userBanLog);
}
return true;
}
}
+4 -4
View File
@@ -6,7 +6,9 @@ use App\Exceptions\NexusException;
use App\Models\AudioCodec;
use App\Models\Category;
use App\Models\Codec;
use App\Models\HitAndRun;
use App\Models\Media;
use App\Models\Message;
use App\Models\Peer;
use App\Models\Processing;
use App\Models\Snatch;
@@ -16,10 +18,9 @@ use App\Models\Team;
use App\Models\Torrent;
use App\Models\TorrentSecret;
use App\Models\User;
use Carbon\Carbon;
use Hashids\Hashids;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Seeder;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class TorrentRepository extends BaseRepository
@@ -343,7 +344,7 @@ class TorrentRepository extends BaseRepository
* @date 2021/6/3
* @time 20:15
*/
public function resetTrackerReportAuthKeySecret($uid, $torrentId = 0)
public function resetTrackerReportAuthKeySecret($uid, $torrentId = 0): string
{
$insert = [
'uid' => $uid,
@@ -361,5 +362,4 @@ class TorrentRepository extends BaseRepository
}
}