mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-28 06:57:22 +08:00
move cleanup: seed bonus and seeding leeching time to job
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class CalculateSeedBonus implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
private int $beginUid;
|
||||
|
||||
private int $endUid;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $beginUid, int $endUid)
|
||||
{
|
||||
$this->beginUid = $beginUid;
|
||||
$this->endUid = $endUid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$beginTimestamp = time();
|
||||
$logPrefix = sprintf("[CLEANUP_CLI_CALCULATE_SEED_BONUS], beginUid: %s, endUid: %s", $this->beginUid, $this->endUid);
|
||||
$sql = sprintf("select userid from peers where userid > %s and userid <= %s and seeder = 'yes' group by userid", $this->beginUid, $this->endUid);
|
||||
$results = NexusDB::select($sql);
|
||||
$count = count($results);
|
||||
do_log("$logPrefix, [GET_UID], sql: $sql, count: " . count($results));
|
||||
if ($count == 0) {
|
||||
do_log("$logPrefix, no user...");
|
||||
return;
|
||||
}
|
||||
$haremAdditionFactor = Setting::get('bonus.harem_addition');
|
||||
$officialAdditionFactor = Setting::get('bonus.official_addition');
|
||||
$donortimes_bonus = Setting::get('bonus.donortimes');
|
||||
$autoclean_interval_one = Setting::get('main.autoclean_interval_one');
|
||||
$sql = sprintf("select %s from users where id in (%s)", implode(',', User::$commonFields), implode(',', array_column($results, 'userid')));
|
||||
$results = NexusDB::select($sql);
|
||||
do_log("$logPrefix, [GET_UID_REAL], count: " . count($results));
|
||||
foreach ($results as $userInfo)
|
||||
{
|
||||
$uid = $userInfo['id'];
|
||||
$isDonor = is_donor($userInfo);
|
||||
$seedBonusResult = calculate_seed_bonus($uid);
|
||||
$bonusLog = "[CLEANUP_CALCULATE_SEED_BONUS], user: $uid, seedBonusResult: " . nexus_json_encode($seedBonusResult);
|
||||
$all_bonus = $seedBonusResult['seed_bonus'];
|
||||
$bonusLog .= ", all_bonus: $all_bonus";
|
||||
if ($isDonor) {
|
||||
$all_bonus = $all_bonus * $donortimes_bonus;
|
||||
$bonusLog .= ", isDonor, donortimes_bonus: $donortimes_bonus, all_bonus: $all_bonus";
|
||||
}
|
||||
if ($officialAdditionFactor > 0) {
|
||||
$officialAddition = $seedBonusResult['official_bonus'] * $officialAdditionFactor;
|
||||
$all_bonus += $officialAddition;
|
||||
$bonusLog .= ", officialAdditionFactor: $officialAdditionFactor, official_bonus: {$seedBonusResult['official_bonus']}, officialAddition: $officialAddition, all_bonus: $all_bonus";
|
||||
}
|
||||
if ($haremAdditionFactor > 0) {
|
||||
$haremBonus = calculate_harem_addition($uid);
|
||||
$haremAddition = $haremBonus * $haremAdditionFactor;
|
||||
$all_bonus += $haremAddition;
|
||||
$bonusLog .= ", haremAdditionFactor: $haremAdditionFactor, haremBonus: $haremBonus, haremAddition: $haremAddition, all_bonus: $all_bonus";
|
||||
}
|
||||
$dividend = 3600 / $autoclean_interval_one;
|
||||
$all_bonus = $all_bonus / $dividend;
|
||||
$seed_points = $seedBonusResult['seed_points'] / $dividend;
|
||||
$sql = "update users set seed_points = ifnull(seed_points, 0) + $seed_points, seedbonus = seedbonus + $all_bonus where id = $uid limit 1";
|
||||
do_log("$bonusLog, query: $sql");
|
||||
NexusDB::statement($sql);
|
||||
}
|
||||
$costTime = time() - $beginTimestamp;
|
||||
do_log("$logPrefix, [DONE], cost time: $costTime seconds");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Plugin;
|
||||
use App\Repositories\PluginRepository;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ManagePlugin implements ShouldQueue, ShouldBeUnique
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
private Plugin $plugin;
|
||||
|
||||
private string $action;
|
||||
|
||||
public int $uniqueFor = 600;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Plugin $plugin, string $action)
|
||||
{
|
||||
$this->plugin = $plugin;
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
public function uniqueId()
|
||||
{
|
||||
return $this->plugin->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(PluginRepository $pluginRepository)
|
||||
{
|
||||
match ($this->action) {
|
||||
'install' => $pluginRepository->doInstall($this->plugin),
|
||||
'update' => $pluginRepository->doUpdate($this->plugin),
|
||||
'delete' => $pluginRepository->doDelete($this->plugin),
|
||||
default => throw new \InvalidArgumentException("Invalid action: " . $this->action)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class UpdateSeedingLeechingTime implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
private int $beginUid;
|
||||
|
||||
private int $endUid;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $beginUid, int $endUid)
|
||||
{
|
||||
$this->beginUid = $beginUid;
|
||||
$this->endUid = $endUid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$beginTimestamp = time();
|
||||
$logPrefix = sprintf("[CLEANUP_CLI_UPDATE_SEEDING_LEECHING_TIME], beginUid: %s, endUid: %s", $this->beginUid, $this->endUid);
|
||||
$sql = sprintf("select id from users where id > %s and id <= %s and enabled = 'yes' and status = 'confirmed'", $this->beginUid, $this->endUid);
|
||||
$results = NexusDB::select($sql);
|
||||
do_log("$logPrefix, [GET_UID], sql: $sql, count: " . count($results));
|
||||
foreach ($results as $arr) {
|
||||
$uid = $arr['id'];
|
||||
$sql = sprintf('select sum(seedtime) as st, sum(leechtime) as lt from snatched where userid = %s limit 1', $uid);
|
||||
$row = NexusDB::select($sql);
|
||||
if (is_numeric($row[0]['st'])) {
|
||||
$sql = sprintf('update users set seedtime = %s, leechtime = %s where id = %s limit 1', $row[0]['st'], $row[0]['lt'], $uid);
|
||||
NexusDB::statement($sql);
|
||||
}
|
||||
}
|
||||
$costTime = time() - $beginTimestamp;
|
||||
do_log("$logPrefix, [DONE], cost time: $costTime seconds");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user