add command: calculate_seed_bonus()

This commit is contained in:
xiaomlove
2022-04-21 20:02:08 +08:00
parent 1d3662af36
commit eb0c4cad8a
5 changed files with 99 additions and 15 deletions
+1 -3
View File
@@ -126,9 +126,7 @@ class Test extends Command
// $r = $rep->getContinuousDays($attendance); // $r = $rep->getContinuousDays($attendance);
// $r = $rep->getContinuousPoints(11); // $r = $rep->getContinuousPoints(11);
$rep = new ExamRepository(); $r = NexusDB::getAll('users', 'id = 1');
$exam = Exam::query()->find(6);
$r = $rep->fetchUserAndDoAssign($exam);
dd($r); dd($r);
} }
@@ -0,0 +1,40 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class TrackerCalculateSeedBonus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tracker:calculate_seed_bonus {uid}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Calculate user seed bonus.';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uid = $this->argument('uid');
$result = calculate_seed_bonus($uid);
$log = sprintf(
"[%s], %s, uid: %s, result: \n%s",
nexus()->getRequestId(), __METHOD__, $uid, var_export($result, true)
);
$this->info($log);
do_log($log);
return 0;
}
}
+1 -3
View File
@@ -1069,9 +1069,7 @@ class TrackerRepository extends BaseRepository
if (empty($update)) { if (empty($update)) {
$log .= ", no update..."; $log .= ", no update...";
} else { } else {
$user->fill($update); $user->update($update);
$log .= ", dirty: " . json_encode($user->getDirty());
$user->save();
$log .= ", query: " . last_query(); $log .= ", query: " . last_query();
} }
do_log($log, 'alert'); do_log($log, 'alert');
+24 -9
View File
@@ -5515,8 +5515,16 @@ function get_smile($num)
function calculate_seed_bonus($uid): array function calculate_seed_bonus($uid): array
{ {
global $autoclean_interval_one; $settingBonus = \App\Models\Setting::get('bonus');
global $donortimes_bonus, $perseeding_bonus, $maxseeding_bonus, $tzero_bonus, $nzero_bonus, $bzero_bonus, $l_bonus; $settingMain = \App\Models\Setting::get('main');
$autoclean_interval_one = $settingMain['autoclean_interval_one'];
$donortimes_bonus = $settingBonus['donortimes'];
$perseeding_bonus = $settingBonus['perseeding'];
$maxseeding_bonus = $settingBonus['maxseeding'];
$tzero_bonus = $settingBonus['tzero'];
$nzero_bonus = $settingBonus['nzero'];
$bzero_bonus = $settingBonus['bzero'];
$l_bonus = $settingBonus['l'];
$sqrtof2 = sqrt(2); $sqrtof2 = sqrt(2);
$logofpointone = log(0.1); $logofpointone = log(0.1);
@@ -5524,21 +5532,28 @@ function calculate_seed_bonus($uid): array
$pi = 3.141592653589793; $pi = 3.141592653589793;
$valuetwo = $bzero_bonus * ( 2 / $pi); $valuetwo = $bzero_bonus * ( 2 / $pi);
$valuethree = $logofpointone / ($nzero_bonus - 1); $valuethree = $logofpointone / ($nzero_bonus - 1);
$timenow = TIMENOW; $timenow = time();
$sectoweek = 7*24*60*60; $sectoweek = 7*24*60*60;
$A = 0; $A = 0;
$count = $torrent_count = 0; $count = $torrent_peer_count = 0;
$logPrefix = "[CALCULATE_SEED_BONUS], uid: $uid";
$torrentres = sql_query("select torrents.added, torrents.size, torrents.seeders from torrents LEFT JOIN peers ON peers.torrent = torrents.id WHERE peers.userid = $uid AND peers.seeder ='yes' group by torrents.id, peers.peer_id"); $sql = "select torrents.id, torrents.added, torrents.size, torrents.seeders, peers.id as peerID from torrents LEFT JOIN peers ON peers.torrent = torrents.id WHERE peers.userid = $uid AND peers.seeder ='yes' group by peers.torrent, peers.peer_id";
while ($torrent = mysql_fetch_array($torrentres)) $torrentResult = \Nexus\Database\NexusDB::select($sql);
do_log("$logPrefix, sql: $sql, count: " . count($torrentResult));
foreach ($torrentResult as $torrent)
{ {
$weeks_alive = ($timenow - strtotime($torrent['added'])) / $sectoweek; $weeks_alive = ($timenow - strtotime($torrent['added'])) / $sectoweek;
$gb_size = $torrent['size'] / 1073741824; $gb_size = $torrent['size'] / 1073741824;
$temp = (1 - exp($valueone * $weeks_alive)) * $gb_size * (1 + $sqrtof2 * exp($valuethree * ($torrent['seeders'] - 1))); $temp = (1 - exp($valueone * $weeks_alive)) * $gb_size * (1 + $sqrtof2 * exp($valuethree * ($torrent['seeders'] - 1)));
do_log(sprintf(
"$logPrefix, torrent: %s, peer ID: %s, weeks: %s, size: %s GB, increase A: %s",
$torrent['id'], $torrent['peerID'], $weeks_alive, $gb_size, $temp
));
$A += $temp; $A += $temp;
$count++; $count++;
$torrent_count++; $torrent_peer_count++;
} }
if ($count > $maxseeding_bonus) if ($count > $maxseeding_bonus)
$count = $maxseeding_bonus; $count = $maxseeding_bonus;
@@ -5547,12 +5562,12 @@ function calculate_seed_bonus($uid): array
$is_donor_until = $is_donor_info['donoruntil']; $is_donor_until = $is_donor_info['donoruntil'];
$is_donor = $is_donor_info['donor'] == 'yes' && ($is_donor_until === null || $is_donor_until == '0000-00-00 00:00:00' || $is_donor_until >= date('Y-m-d H:i:s')); $is_donor = $is_donor_info['donor'] == 'yes' && ($is_donor_until === null || $is_donor_until == '0000-00-00 00:00:00' || $is_donor_until >= date('Y-m-d H:i:s'));
$is_donor = intval($is_donor); $is_donor = intval($is_donor);
$log = "[CALCULATE_SEED_BONUS], user: $uid, original bonus: $all_bonus, is_donor: $is_donor, donortimes_bonus: $donortimes_bonus"; $log = "$logPrefix, original bonus: $all_bonus, is_donor: $is_donor, donortimes_bonus: $donortimes_bonus";
if ($is_donor && $donortimes_bonus > 0) { if ($is_donor && $donortimes_bonus > 0) {
$all_bonus = $all_bonus * $donortimes_bonus; $all_bonus = $all_bonus * $donortimes_bonus;
$log .= ", do multiple, all_bonus: $all_bonus"; $log .= ", do multiple, all_bonus: $all_bonus";
} }
$result = compact('seed_points','seed_bonus', 'all_bonus', 'A', 'count', 'torrent_count'); $result = compact('seed_points','seed_bonus', 'all_bonus', 'A', 'count', 'torrent_peer_count');
do_log("$log, result: " . json_encode($result)); do_log("$log, result: " . json_encode($result));
return $result; return $result;
} }
+33
View File
@@ -150,6 +150,9 @@ class NexusDB
public static function insert($table, $data) public static function insert($table, $data)
{ {
if (!IN_NEXUS) {
return DB::table($table)->insertGetId($data);
}
if (empty($table) || empty($data) || !is_array($data)) { if (empty($table) || empty($data) || !is_array($data)) {
throw new DatabaseException("require table and data(array)."); throw new DatabaseException("require table and data(array).");
} }
@@ -162,6 +165,9 @@ class NexusDB
public static function update($table, $data, $whereStr) public static function update($table, $data, $whereStr)
{ {
if (!IN_NEXUS) {
return DB::table($table)->whereRaw($whereStr)->update($data);
}
$updateArr = []; $updateArr = [];
foreach ($data as $field => $value) { foreach ($data as $field => $value) {
$updateArr[] = "`$field` = " . sqlesc($value); $updateArr[] = "`$field` = " . sqlesc($value);
@@ -173,6 +179,13 @@ class NexusDB
public static function delete($table, $whereStr, $limit = null) public static function delete($table, $whereStr, $limit = null)
{ {
if (!IN_NEXUS) {
$query = DB::table($table)->whereRaw($whereStr);
if ($limit !== null) {
$query->limit($limit);
}
return $query->delete();
}
$sql = "delete from $table where $whereStr"; $sql = "delete from $table where $whereStr";
if (!is_null($limit)) { if (!is_null($limit)) {
$sql .= " limit $limit"; $sql .= " limit $limit";
@@ -183,6 +196,10 @@ class NexusDB
public static function getOne($table, $whereStr, $fields = '*') public static function getOne($table, $whereStr, $fields = '*')
{ {
if (!IN_NEXUS) {
$result = DB::table($table)->whereRaw($whereStr)->selectRaw($fields)->first();
return $result ? json_decode(json_encode($result), true) : null;
}
if ($fields != '*') { if ($fields != '*') {
if (is_array($fields)) { if (is_array($fields)) {
$fields = implode(', ', $fields); $fields = implode(', ', $fields);
@@ -199,6 +216,13 @@ class NexusDB
public static function getAll($table, $whereStr, $fields = '*') public static function getAll($table, $whereStr, $fields = '*')
{ {
if (!IN_NEXUS) {
$result = DB::table($table)->whereRaw($whereStr)->selectRaw($fields)->get();
if ($result->isEmpty()) {
return [];
}
return json_decode(json_encode($result), true);
}
if ($fields != '*') { if ($fields != '*') {
if (is_array($fields)) { if (is_array($fields)) {
$fields = implode(', ', $fields); $fields = implode(', ', $fields);
@@ -209,6 +233,15 @@ class NexusDB
throw new DatabaseException("empty fields."); throw new DatabaseException("empty fields.");
} }
$sql = "select $fields from $table where $whereStr"; $sql = "select $fields from $table where $whereStr";
return self::select($sql);
}
public static function select(string $sql)
{
if (!IN_NEXUS) {
$result = DB::select($sql);
return json_decode(json_encode($result), true);
}
$res = sql_query($sql); $res = sql_query($sql);
$result = []; $result = [];
while ($row = mysql_fetch_assoc($res)) { while ($row = mysql_fetch_assoc($res)) {