paid torrent

This commit is contained in:
xiaomlove
2023-02-11 16:08:48 +08:00
parent 43b35b4e77
commit 3c64e76011
50 changed files with 556 additions and 87 deletions
@@ -0,0 +1,111 @@
<?php
namespace App\Filament\Resources\User;
use App\Filament\Resources\User\TorrentBuyLogResource\Pages;
use App\Filament\Resources\User\TorrentBuyLogResource\RelationManagers;
use App\Models\TorrentBuyLog;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class TorrentBuyLogResource extends Resource
{
protected static ?string $model = TorrentBuyLog::class;
protected static ?string $navigationIcon = 'heroicon-o-collection';
protected static ?string $navigationGroup = 'User';
protected static ?int $navigationSort = 10;
protected static function getNavigationLabel(): string
{
return __('admin.sidebar.torrent_buy_log');
}
public static function getBreadcrumb(): string
{
return self::getNavigationLabel();
}
public static function form(Form $form): Form
{
return $form
->schema([
//
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id')->sortable(),
Tables\Columns\TextColumn::make('uid')
->formatStateUsing(fn ($state) => username_for_admin($state))
->label(__('label.username'))
,
Tables\Columns\TextColumn::make('torrent_id')
->formatStateUsing(fn ($record) => torrent_name_for_admin($record->torrent))
->label(__('label.torrent.label'))
,
Tables\Columns\TextColumn::make('price')
->formatStateUsing(fn ($state) => number_format($state))
->label(__('label.price'))
,
Tables\Columns\TextColumn::make('created_at')
->formatStateUsing(fn ($state) => format_datetime($state))
->label(__('label.created_at'))
,
])
->filters([
Tables\Filters\Filter::make('uid')
->form([
Forms\Components\TextInput::make('uid')
->label(__('label.username'))
->placeholder('UID')
,
])->query(function (Builder $query, array $data) {
return $query->when($data['uid'], fn (Builder $query, $value) => $query->where("uid", $value));
})
,
Tables\Filters\Filter::make('torrent_id')
->form([
Forms\Components\TextInput::make('torrent_id')
->label(__('label.torrent.label'))
->placeholder('Torrent ID')
,
])->query(function (Builder $query, array $data) {
return $query->when($data['torrent_id'], fn (Builder $query, $value) => $query->where("torrent_id", $value));
})
,
])
->actions([
// Tables\Actions\EditAction::make(),
])
->bulkActions([
// Tables\Actions\DeleteBulkAction::make(),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListTorrentBuyLogs::route('/'),
'create' => Pages\CreateTorrentBuyLog::route('/create'),
'edit' => Pages\EditTorrentBuyLog::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,12 @@
<?php
namespace App\Filament\Resources\User\TorrentBuyLogResource\Pages;
use App\Filament\Resources\User\TorrentBuyLogResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\CreateRecord;
class CreateTorrentBuyLog extends CreateRecord
{
protected static string $resource = TorrentBuyLogResource::class;
}
@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\User\TorrentBuyLogResource\Pages;
use App\Filament\Resources\User\TorrentBuyLogResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\EditRecord;
class EditTorrentBuyLog extends EditRecord
{
protected static string $resource = TorrentBuyLogResource::class;
protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
@@ -0,0 +1,27 @@
<?php
namespace App\Filament\Resources\User\TorrentBuyLogResource\Pages;
use App\Filament\PageList;
use App\Filament\Resources\User\TorrentBuyLogResource;
use App\Models\TorrentBuyLog;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
class ListTorrentBuyLogs extends PageList
{
protected static string $resource = TorrentBuyLogResource::class;
protected function getActions(): array
{
return [
// Actions\CreateAction::make(),
];
}
protected function getTableQuery(): Builder
{
return TorrentBuyLog::query()->with(['user', 'torrent']);
}
}
+8 -2
View File
@@ -30,8 +30,14 @@ class LatestTorrents extends BaseWidget
protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('name')->limit(30)->label(__('label.name')),
Tables\Columns\TextColumn::make('user.username')->label(__('label.torrent.owner')),
Tables\Columns\TextColumn::make('name')
->label(__('label.name'))
->formatStateUsing(fn ($record) => torrent_name_for_admin($record, false, 30))
,
Tables\Columns\TextColumn::make('owner')
->label(__('label.torrent.owner'))
->formatStateUsing(fn ($state) => username_for_admin($state))
,
Tables\Columns\TextColumn::make('size')->formatStateUsing(fn ($state) => mksize($state))->label(__('label.torrent.size')),
Tables\Columns\TextColumn::make('added')->dateTime()->label(__('label.added')),
];
+4 -1
View File
@@ -30,7 +30,10 @@ class LatestUsers extends BaseWidget
protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('username')->label(__('label.user.username')),
Tables\Columns\TextColumn::make('id')
->label(__('label.user.username'))
->formatStateUsing(fn ($state) => username_for_admin($state))
,
Tables\Columns\TextColumn::make('email')->label(__('label.email')),
Tables\Columns\BadgeColumn::make('status')->colors(['success' => 'confirmed', 'danger' => 'pending'])->label(__('label.status')),
Tables\Columns\TextColumn::make('added')->dateTime()->label(__('label.added')),
+4
View File
@@ -35,8 +35,10 @@ class BonusLogs extends NexusModel
const BUSINESS_TYPE_BUY_RAINBOW_ID = 16;
const BUSINESS_TYPE_BUY_CHANGE_USERNAME_CARD = 17;
const BUSINESS_TYPE_GIFT_MEDAL = 18;
const BUSINESS_TYPE_BUY_TORRENT = 19;
const BUSINESS_TYPE_ROLE_WORK_SALARY = 1000;
const BUSINESS_TYPE_TORRENT_BE_DOWNLOADED = 1001;
public static array $businessTypes = [
self::BUSINESS_TYPE_CANCEL_HIT_AND_RUN => ['text' => 'Cancel H&R'],
@@ -57,8 +59,10 @@ class BonusLogs extends NexusModel
self::BUSINESS_TYPE_BUY_RAINBOW_ID => ['text' => 'Buy rainbow ID'],
self::BUSINESS_TYPE_BUY_CHANGE_USERNAME_CARD => ['text' => 'Buy change username card'],
self::BUSINESS_TYPE_GIFT_MEDAL => ['text' => 'Gift medal to someone'],
self::BUSINESS_TYPE_BUY_TORRENT => ['text' => 'Buy torrent'],
self::BUSINESS_TYPE_ROLE_WORK_SALARY => ['text' => 'Role work salary'],
self::BUSINESS_TYPE_TORRENT_BE_DOWNLOADED => ['text' => 'Torrent be downloaded'],
];
public function getBusinessTypeTextAttribute()
+3 -3
View File
@@ -14,7 +14,7 @@ class Torrent extends NexusModel
'size', 'added', 'type', 'numfiles', 'owner', 'nfo', 'sp_state', 'promotion_time_type',
'promotion_until', 'anonymous', 'url', 'pos_state', 'cache_stamp', 'picktype', 'picktime',
'last_reseed', 'pt_gen', 'technical_info', 'leechers', 'seeders', 'cover', 'last_action',
'times_completed', 'approval_status', 'banned', 'visible', 'pos_state_until',
'times_completed', 'approval_status', 'banned', 'visible', 'pos_state_until', 'price',
];
const VISIBLE_YES = 'yes';
@@ -33,7 +33,7 @@ class Torrent extends NexusModel
public static $commentFields = [
'id', 'name', 'added', 'visible', 'banned', 'owner', 'sp_state', 'pos_state', 'hr', 'picktype', 'picktime',
'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover', 'anonymous', 'approval_status',
'pos_state_until', 'category', 'source', 'medium', 'codec', 'standard', 'processing', 'team', 'audiocodec',
'pos_state_until', 'category', 'source', 'medium', 'codec', 'standard', 'processing', 'team', 'audiocodec', 'price'
];
public static $basicRelations = [
@@ -240,7 +240,7 @@ class Torrent extends NexusModel
public static function getFieldsForList($appendTableName = false): array|bool
{
$fields = 'id, sp_state, promotion_time_type, promotion_until, banned, picktype, pos_state, category, source, medium, codec, standard, processing, team, audiocodec, leechers, seeders, name, small_descr, times_completed, size, added, comments,anonymous,owner,url,cache_stamp, pt_gen, hr, approval_status, cover';
$fields = 'id, sp_state, promotion_time_type, promotion_until, banned, picktype, pos_state, category, source, medium, codec, standard, processing, team, audiocodec, leechers, seeders, name, small_descr, times_completed, size, added, comments,anonymous,owner,url,cache_stamp, pt_gen, hr, approval_status, cover, price';
$fields = preg_split('/[,\s]+/', $fields);
if ($appendTableName) {
foreach ($fields as &$value) {
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Nexus\Database\NexusDB;
class TorrentBuyLog extends NexusModel
{
public $timestamps = true;
protected $fillable = ['uid', 'torrent_id', 'price', 'channel'];
public function user()
{
return $this->belongsTo(User::class, 'uid');
}
public function torrent()
{
return $this->belongsTo(Torrent::class, 'torrent_id');
}
}
+53
View File
@@ -8,6 +8,8 @@ use App\Models\Invite;
use App\Models\Medal;
use App\Models\Message;
use App\Models\Setting;
use App\Models\Torrent;
use App\Models\TorrentBuyLog;
use App\Models\User;
use App\Models\UserMedal;
use App\Models\UserMeta;
@@ -250,6 +252,57 @@ class BonusRepository extends BaseRepository
}
public function consumeToBuyTorrent($uid, $torrentId, $channel = 'Web'): bool
{
$user = User::query()->findOrFail($uid);
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
$requireBonus = $torrent->price;
NexusDB::transaction(function () use ($user, $requireBonus, $torrent, $channel) {
$comment = nexus_trans('bonus.comment_buy_torrent', [
'bonus' => $requireBonus,
'torrent_id' => $torrent->id,
], $user->locale);
do_log("comment: $comment");
$this->consumeUserBonus($user, $requireBonus, BonusLogs::BUSINESS_TYPE_BUY_TORRENT, $comment);
TorrentBuyLog::query()->create([
'uid' => $user->id,
'torrent_id' => $torrent->id,
'price' => $requireBonus,
'channel' => $channel,
]);
//increment owner bonus
$taxFactor = Setting::get('torrent.tax_factor');
if (!is_numeric($taxFactor) || $taxFactor < 0 || $taxFactor > 1) {
throw new \RuntimeException("Invalid tax_factor: $taxFactor");
}
$increaseBonus = $requireBonus * (1 - $taxFactor);
$owner = $torrent->user;
if ($owner->id) {
$nowStr = now()->toDateTimeString();
$businessType = BonusLogs::BUSINESS_TYPE_TORRENT_BE_DOWNLOADED;
$owner->increment('seedbonus', $increaseBonus);
$comment = nexus_trans('bonus.comment_torrent_be_downloaded', [
'username' => $user->username,
'uid' => $user->id,
], $owner->locale);
$bonusLog = [
'business_type' => $businessType,
'uid' => $owner->id,
'old_total_value' => $owner->seedbonus,
'value' => $increaseBonus,
'new_total_value' => bcadd($owner->seedbonus, $increaseBonus),
'comment' => sprintf('[%s] %s', BonusLogs::$businessTypes[$businessType]['text'], $comment),
'created_at' => $nowStr,
'updated_at' => $nowStr,
];
BonusLogs::query()->insert($bonusLog);
}
});
return true;
}
public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logComment = '', array $userUpdates = [])
{
if (!isset(BonusLogs::$businessTypes[$logBusinessType])) {
+8
View File
@@ -711,4 +711,12 @@ HTML;
}
}
public function getPaidIcon(array $torrentInfo, $size = 16, $verticalAlign = 'sub')
{
if (!isset($torrentInfo['price']) || $torrentInfo['price'] <= 0) {
return '';
}
return sprintf('<span title="%s" style="vertical-align: %s"><svg t="1676058062789" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3406" width="%s" height="%s"><path d="M554.666667 810.666667v42.666666h-85.333334v-42.666666c-93.866667 0-170.666667-76.8-170.666666-170.666667h85.333333c0 46.933333 38.4 85.333333 85.333333 85.333333v-170.666666c-93.866667 0-170.666667-76.8-170.666666-170.666667s76.8-170.666667 170.666666-170.666667V170.666667h85.333334v42.666666c93.866667 0 170.666667 76.8 170.666666 170.666667h-85.333333c0-46.933333-38.4-85.333333-85.333333-85.333333v170.666666h17.066666c29.866667 0 68.266667 17.066667 98.133334 42.666667 34.133333 29.866667 59.733333 76.8 59.733333 128-4.266667 93.866667-81.066667 170.666667-174.933333 170.666667z m0-85.333334c46.933333 0 85.333333-38.4 85.333333-85.333333s-38.4-85.333333-85.333333-85.333333v170.666666zM469.333333 298.666667c-46.933333 0-85.333333 38.4-85.333333 85.333333s38.4 85.333333 85.333333 85.333333V298.666667z" fill="#CD7F32" p-id="3407"></path></svg></span>', nexus_trans('torrent.paid_torrent'), $verticalAlign, $size, $size);
}
}