refactor attendance

This commit is contained in:
xiaomlove
2022-02-25 23:13:34 +08:00
parent 57f1e92998
commit 5ed80ed637
10 changed files with 177 additions and 40 deletions
+3 -6
View File
@@ -12,6 +12,7 @@ use App\Models\SearchBox;
use App\Models\Snatch;
use App\Models\User;
use App\Repositories\AgentAllowRepository;
use App\Repositories\AttendanceRepository;
use App\Repositories\ExamRepository;
use App\Repositories\HitAndRunRepository;
use App\Repositories\SearchBoxRepository;
@@ -59,12 +60,8 @@ class Test extends Command
*/
public function handle()
{
$peerId = '-TR2920-9bqp8iu7v9se';
$agent = 'Transmission/2.92';
$rep = new AgentAllowRepository();
// $r = $rep->checkClient($peerId, $agent, true);
$r = array_slice([1,2], 1, 3);
dd($r);
$now = Carbon::today();
dd($now->toDateTimeString());
}
}
+10
View File
@@ -11,4 +11,14 @@ class Attendance extends NexusModel
protected $casts = [
'added' => 'datetime',
];
const INITIAL_BONUS = 10;
const STEP_BONUS = 5;
const MAX_BONUS = 1000;
const CONTINUOUS_BONUS = [
10 => 200,
20 => 500,
30 => 1000
];
}
+90
View File
@@ -0,0 +1,90 @@
<?php
namespace App\Repositories;
use App\Models\Attendance;
use App\Models\Setting;
use Carbon\Carbon;
class AttendanceRepository extends BaseRepository
{
public function attend($uid)
{
$attendance = $this->getAttendance($uid);
$now = Carbon::now();
$today = Carbon::today();
$settings = Setting::get('bonus');
$initialBonus = $settings['attendance_initial'] ?? Attendance::INITIAL_BONUS;
$stepBonus = $settings['attendance_step'] ?? Attendance::STEP_BONUS;
$maxBonus = $settings['attendance_max'] ?? Attendance::MAX_BONUS;
$continuousBonus = $settings['attendance_continuous'] ?? Attendance::CONTINUOUS_BONUS;
$isUpdated = 1;
$initialData = [
'uid' => $uid,
'added' => $now,
'points' => $initialBonus,
'days' => 1,
'total_days' => 1,
];
if (!$attendance) {
//first time
do_log("[DO_INSERT]: " . nexus_json_encode($initialData));
$attendance = Attendance::query()->create($initialData);
} else {
$added = $attendance->added->startOfDay();
do_log("[ORIGINAL_DATA]: " . $attendance->toJson());
if ($added->gte($today)) {
//already attended today, do nothing
$isUpdated = 0;
} else {
$diffDays = $today->diffInDays($added);
if ($diffDays == 1) {
//yesterday do it, it's continuous
do_log("[CONTINUOUS]");
$points = $this->getContinuousPoints($initialBonus, $stepBonus, $attendance->days, $maxBonus, $continuousBonus);
$update = [
'added' => $now,
'points' => $points,
'days' => $attendance->days + 1,
'total_days' => $attendance->total_days + 1,
];
} else {
//not continuous
do_log("[NOT_CONTINUOUS]");
$update = $initialData;
$update['total_days'] = $attendance->total_days + 1;
}
do_log("[DO_UPDATE]: " . nexus_json_encode($update));
$attendance->update($update);
}
}
$attendance->is_updated = $isUpdated;
do_log("[FINAL_ATTENDANCE]: " . $attendance->toJson());
return $attendance;
}
public function getAttendance($uid, $date = '')
{
$query = Attendance::query()
->where('uid', $uid)
->orderBy('id', 'desc');
if (!empty($date)) {
$query->where('added', '>=', Carbon::today())
->where('added', '<', Carbon::tomorrow());
}
return $query->first();
}
private function getContinuousPoints($initial, $step, $days, $max, $extraAwards)
{
$points = min($initial + $days * $step, $max);
krsort($extraAwards);
foreach ($extraAwards as $key => $value) {
if ($days >= $key) {
$points += $value;
break;
}
}
return $points;
}
}
+2 -2
View File
@@ -15,7 +15,7 @@ class Attendance
if($flush || ($row = $Cache->get_value($this->cachename)) === false){
$res = sql_query(sprintf('SELECT * FROM `attendance` WHERE `uid` = %u AND DATE(`added`) = %s', $this->userid, sqlesc($this->curdate.' 00:00:00'))) or sqlerr(__FILE__,__LINE__);
$row = mysql_num_rows($res) ? mysql_fetch_assoc($res) : array();
$Cache->cache_value($this->cachename, $row, 86400);
$Cache->cache_value($this->cachename, $row, 600);
}
return empty($row) ? false : $row;
}
@@ -33,7 +33,7 @@ class Attendance
$row = [0, 0, 0, 0];
}
list($id, $datediff, $days, $totalDays) = $row;
$points = min($initial + $step * $totalDays, $maximum);
$points = min($initial + $step * $days, $maximum);
$cdays = $datediff == 1 ? ++$days : 1;
if($cdays > 1){
krsort($continous);
@@ -19,7 +19,7 @@ class CreateAttendanceTable extends Migration
Schema::create('attendance', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('uid')->default(0)->index('idx_uid');
$table->dateTime('added');
$table->dateTime('added')->index();
$table->unsignedInteger('points')->default(0);
$table->unsignedInteger('days')->default(1);
});
+4 -4
View File
@@ -305,10 +305,10 @@ $basictax_bonus = $BONUS['basictax'];
$taxpercentage_bonus = $BONUS['taxpercentage'];
$prolinkpoint_bonus = $BONUS['prolinkpoint'];
$prolinktime_bonus = $BONUS['prolinktime'];
$attendance_initial_bonus = isset($BONUS['attendance_initial']) ? (int) $BONUS['attendance_initial'] : 10;
$attendance_step_bonus = isset($BONUS['attendance_step']) ? (int) $BONUS['attendance_step'] : 5;
$attendance_max_bonus = isset($BONUS['attendance_max']) ? (int) $BONUS['attendance_max'] : 1000;
$attendance_continuous_bonus = isset($BONUS['attendance_continuous']) && is_array($BONUS['attendance_continuous']) ? $BONUS['attendance_continuous'] : array(10 => 200, 20 => 500, 30 => 1000);
$attendance_initial_bonus = isset($BONUS['attendance_initial']) ? (int) $BONUS['attendance_initial'] : \App\Models\Attendance::INITIAL_BONUS;
$attendance_step_bonus = isset($BONUS['attendance_step']) ? (int) $BONUS['attendance_step'] : \App\Models\Attendance::STEP_BONUS;
$attendance_max_bonus = isset($BONUS['attendance_max']) ? (int) $BONUS['attendance_max'] : \App\Models\Attendance::MAX_BONUS;
$attendance_continuous_bonus = isset($BONUS['attendance_continuous']) && is_array($BONUS['attendance_continuous']) ? $BONUS['attendance_continuous'] : \App\Models\Attendance::CONTINUOUS_BONUS;
$neverdelete_account = $ACCOUNT['neverdelete'];
$neverdeletepacked_account = $ACCOUNT['neverdeletepacked'];
+6 -4
View File
@@ -2248,7 +2248,7 @@ function get_cat_folder($cat = 101)
if ($caticonrow['multilang'] == 'yes') {
$path .= '/' . trim($CURLANGDIR, '/');
}
do_log("cat: $cat, path: $path");
do_log("cat: $cat, path: $path", 'debug');
$catPath[$cat] = $path;
}
return $catPath[$cat] ?? '';
@@ -2465,8 +2465,10 @@ else {
}
$inboxpic = "<img class=\"".($unread ? "inboxnew" : "inbox")."\" src=\"pic/trans.gif\" alt=\"inbox\" title=\"".($unread ? $lang_functions['title_inbox_new_messages'] : $lang_functions['title_inbox_no_new_messages'])."\" />";
$attend_desk = new Attendance($CURUSER['id']);
$attendance = $attend_desk->check();
// $attend_desk = new Attendance($CURUSER['id']);
// $attendance = $attend_desk->check();
$attendanceRep = new \App\Repositories\AttendanceRepository();
$attendance = $attendanceRep->getAttendance($CURUSER['id'], date('Ymd'))
?>
<table id="info_block" cellpadding="4" cellspacing="0" border="0" width="100%"><tr>
@@ -2481,7 +2483,7 @@ else {
[<a href="torrents.php?inclbookmarked=1&amp;allsec=1&amp;incldead=0"><?php echo $lang_functions['text_bookmarks'] ?></a>]
<font class = 'color_bonus'><?php echo $lang_functions['text_bonus'] ?></font>[<a href="mybonus.php"><?php echo $lang_functions['text_use'] ?></a>]: <?php echo number_format($CURUSER['seedbonus'], 1)?>
<font class = 'color_bonus'><?php echo $lang_functions['text_seed_points'] ?></font>: <?php echo number_format($CURUSER['seed_points'], 1)?>
<?php if($attendance){ printf('&nbsp;'.$lang_functions['text_attended'], $attendance['points']); }else{ printf(' <a href="attendance.php" class="faqlink">%s</a>', $lang_functions['text_attendance']);}?>
<?php if($attendance){ printf('&nbsp;'.$lang_functions['text_attended'], $attendance->points); }else{ printf(' <a href="attendance.php" class="faqlink">%s</a>', $lang_functions['text_attendance']);}?>
<font class = 'color_invite'><?php echo $lang_functions['text_invite'] ?></font>[<a href="invite.php?id=<?php echo $CURUSER['id']?>"><?php echo $lang_functions['text_send'] ?></a>]: <?php echo $CURUSER['invites']?>
<br />
<font class="color_ratio"><?php echo $lang_functions['text_ratio'] ?></font> <?php echo $ratio?>
+1 -1
View File
@@ -620,7 +620,7 @@ function nexus_trans($key, $replace = [], $locale = null)
$search = array_map(function ($value) {return ":$value";}, array_keys($replace));
$result = str_replace($search, array_values($replace), $result);
}
do_log("key: $key, replace: " . nexus_json_encode($replace) . ", locale: $locale, getKey: $getKey, result: $result");
do_log("key: $key, replace: " . nexus_json_encode($replace) . ", locale: $locale, getKey: $getKey, result: $result", 'debug');
return $result;
}
+10
View File
@@ -145,6 +145,16 @@ class Update extends Install
$this->doLog("[INIT SEED POINTS]");
}
/**
* @since 1.6.0-beta14
*
* add id to agent_allowed_exception
*/
if (WITH_LARAVEL && !NexusDB::schema()->hasColumn('agent_allowed_exception', 'id')) {
$this->runMigrate('database/migrations/2022_02_25_021356_add_id_to_agent_allowed_exception_table.php');
$this->doLog("[ADD_ID_TO_AGENT_ALLOWED_EXCEPTION]");
}
+50 -22
View File
@@ -4,26 +4,54 @@ dbconn();
require get_langfile_path();
loggedinorreturn();
parked();
$desk = new Attendance($CURUSER['id']);
//$desk = new Attendance($CURUSER['id']);
//
//if($result = $desk->attend($attendance_initial_bonus, $attendance_step_bonus, $attendance_max_bonus, $attendance_continuous_bonus)){
// list($count, $cdays, $points) = $result;
// stdhead($lang_attendance['title']);
// begin_main_frame();
// begin_frame($lang_attendance['success']);
// printf('<p>'.$lang_attendance['attend_info'].'</p>', $count, $cdays, $points);
// end_frame();
// echo '<ul>';
// printf('<li>'.$lang_attendance['initial'].'</li>', $attendance_initial_bonus);
// printf('<li>'.$lang_attendance['steps'].'</li>', $attendance_step_bonus, $attendance_max_bonus);
// echo '<li><ol>';
// foreach($attendance_continuous_bonus as $day => $value){
// printf('<li>'.$lang_attendance['continuous'].'</li>', $day, $value);
// }
// echo '</ol></li>';
// echo '</ul>';
// end_main_frame();
// stdfoot();
//}else{
// stderr($lang_attendance['sorry'], $lang_attendance['already_attended']);
//}
if($result = $desk->attend($attendance_initial_bonus, $attendance_step_bonus, $attendance_max_bonus, $attendance_continuous_bonus)){
list($count, $cdays, $points) = $result;
stdhead($lang_attendance['title']);
begin_main_frame();
begin_frame($lang_attendance['success']);
printf('<p>'.$lang_attendance['attend_info'].'</p>', $count, $cdays, $points);
end_frame();
echo '<ul>';
printf('<li>'.$lang_attendance['initial'].'</li>', $attendance_initial_bonus);
printf('<li>'.$lang_attendance['steps'].'</li>', $attendance_step_bonus, $attendance_max_bonus);
echo '<li><ol>';
foreach($attendance_continuous_bonus as $day => $value){
printf('<li>'.$lang_attendance['continuous'].'</li>', $day, $value);
}
echo '</ol></li>';
echo '</ul>';
end_main_frame();
stdfoot();
}else{
stderr($lang_attendance['sorry'], $lang_attendance['already_attended']);
}
$rep = new \App\Repositories\AttendanceRepository();
$result = $rep->attend($CURUSER['id']);
if ($result->is_updated) {
$count = $result->total_days;
$cdays = $result->days;
$points = $result->points;
stdhead($lang_attendance['title']);
begin_main_frame();
begin_frame($lang_attendance['success']);
printf('<p>'.$lang_attendance['attend_info'].'</p>', $count, $cdays, $points);
end_frame();
echo '<ul>';
printf('<li>'.$lang_attendance['initial'].'</li>', $attendance_initial_bonus);
printf('<li>'.$lang_attendance['steps'].'</li>', $attendance_step_bonus, $attendance_max_bonus);
echo '<li><ol>';
foreach($attendance_continuous_bonus as $day => $value){
printf('<li>'.$lang_attendance['continuous'].'</li>', $day, $value);
}
echo '</ol></li>';
echo '</ul>';
end_main_frame();
stdfoot();
} else {
stderr($lang_attendance['sorry'], $lang_attendance['already_attended']);
}