diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 88f0462a..bc857834 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -126,9 +126,7 @@ class Test extends Command // $r = $rep->getContinuousDays($attendance); // $r = $rep->getContinuousPoints(11); - $rep = new ExamRepository(); - $exam = Exam::query()->find(6); - $r = $rep->fetchUserAndDoAssign($exam); + $r = NexusDB::getAll('users', 'id = 1'); dd($r); } diff --git a/app/Console/Commands/TrackerCalculateSeedBonus.php b/app/Console/Commands/TrackerCalculateSeedBonus.php new file mode 100644 index 00000000..f9985013 --- /dev/null +++ b/app/Console/Commands/TrackerCalculateSeedBonus.php @@ -0,0 +1,40 @@ +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; + } +} diff --git a/app/Repositories/TrackerRepository.php b/app/Repositories/TrackerRepository.php index 972eb6fe..a9595cc4 100644 --- a/app/Repositories/TrackerRepository.php +++ b/app/Repositories/TrackerRepository.php @@ -1069,9 +1069,7 @@ class TrackerRepository extends BaseRepository if (empty($update)) { $log .= ", no update..."; } else { - $user->fill($update); - $log .= ", dirty: " . json_encode($user->getDirty()); - $user->save(); + $user->update($update); $log .= ", query: " . last_query(); } do_log($log, 'alert'); diff --git a/include/functions.php b/include/functions.php index 0ddb270b..8916cbb5 100644 --- a/include/functions.php +++ b/include/functions.php @@ -5515,8 +5515,16 @@ function get_smile($num) function calculate_seed_bonus($uid): array { - global $autoclean_interval_one; - global $donortimes_bonus, $perseeding_bonus, $maxseeding_bonus, $tzero_bonus, $nzero_bonus, $bzero_bonus, $l_bonus; + $settingBonus = \App\Models\Setting::get('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); $logofpointone = log(0.1); @@ -5524,21 +5532,28 @@ function calculate_seed_bonus($uid): array $pi = 3.141592653589793; $valuetwo = $bzero_bonus * ( 2 / $pi); $valuethree = $logofpointone / ($nzero_bonus - 1); - $timenow = TIMENOW; + $timenow = time(); $sectoweek = 7*24*60*60; $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"); - while ($torrent = mysql_fetch_array($torrentres)) + $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"; + $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; $gb_size = $torrent['size'] / 1073741824; $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; $count++; - $torrent_count++; + $torrent_peer_count++; } if ($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 = $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); - $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) { $all_bonus = $all_bonus * $donortimes_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)); return $result; } diff --git a/nexus/Database/NexusDB.php b/nexus/Database/NexusDB.php index 1e520260..055a5877 100644 --- a/nexus/Database/NexusDB.php +++ b/nexus/Database/NexusDB.php @@ -150,6 +150,9 @@ class NexusDB public static function insert($table, $data) { + if (!IN_NEXUS) { + return DB::table($table)->insertGetId($data); + } if (empty($table) || empty($data) || !is_array($data)) { throw new DatabaseException("require table and data(array)."); } @@ -162,6 +165,9 @@ class NexusDB public static function update($table, $data, $whereStr) { + if (!IN_NEXUS) { + return DB::table($table)->whereRaw($whereStr)->update($data); + } $updateArr = []; foreach ($data as $field => $value) { $updateArr[] = "`$field` = " . sqlesc($value); @@ -173,6 +179,13 @@ class NexusDB 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"; if (!is_null($limit)) { $sql .= " limit $limit"; @@ -183,6 +196,10 @@ class NexusDB 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 (is_array($fields)) { $fields = implode(', ', $fields); @@ -199,6 +216,13 @@ class NexusDB 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 (is_array($fields)) { $fields = implode(', ', $fields); @@ -209,6 +233,15 @@ class NexusDB throw new DatabaseException("empty fields."); } $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); $result = []; while ($row = mysql_fetch_assoc($res)) {