diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index cc5889fd..b81ddb9b 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -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()); } } diff --git a/app/Models/Attendance.php b/app/Models/Attendance.php index 50715cf9..001e8482 100644 --- a/app/Models/Attendance.php +++ b/app/Models/Attendance.php @@ -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 + ]; + } diff --git a/app/Repositories/AttendanceRepository.php b/app/Repositories/AttendanceRepository.php new file mode 100644 index 00000000..f1ab7481 --- /dev/null +++ b/app/Repositories/AttendanceRepository.php @@ -0,0 +1,90 @@ +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; + } +} diff --git a/classes/class_attendance.php b/classes/class_attendance.php index d22759ae..fa4c7b3a 100644 --- a/classes/class_attendance.php +++ b/classes/class_attendance.php @@ -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); diff --git a/database/migrations/2021_06_08_113437_create_attendance_table.php b/database/migrations/2021_06_08_113437_create_attendance_table.php index ebf3d16e..0926f278 100644 --- a/database/migrations/2021_06_08_113437_create_attendance_table.php +++ b/database/migrations/2021_06_08_113437_create_attendance_table.php @@ -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); }); diff --git a/include/config.php b/include/config.php index 6fbb8e38..e89ec744 100644 --- a/include/config.php +++ b/include/config.php @@ -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']; diff --git a/include/functions.php b/include/functions.php index f5b8c954..76f12874 100644 --- a/include/functions.php +++ b/include/functions.php @@ -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 = "\"inbox\""; - $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')) ?> @@ -2481,7 +2483,7 @@ else { [] []: : - %s', $lang_functions['text_attendance']);}?> + points); }else{ printf(' %s', $lang_functions['text_attendance']);}?> []:
diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 71cb21f8..31261b4a 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -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; } diff --git a/nexus/Install/Update.php b/nexus/Install/Update.php index bbeaeadb..5918c6a2 100644 --- a/nexus/Install/Update.php +++ b/nexus/Install/Update.php @@ -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]"); + } + diff --git a/public/attendance.php b/public/attendance.php index 473bafd0..ab67dee5 100644 --- a/public/attendance.php +++ b/public/attendance.php @@ -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('

'.$lang_attendance['attend_info'].'

', $count, $cdays, $points); +// end_frame(); +// echo ''; +// 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('

'.$lang_attendance['attend_info'].'

', $count, $cdays, $points); - end_frame(); - echo ''; - end_main_frame(); - stdfoot(); -}else{ - stderr($lang_attendance['sorry'], $lang_attendance['already_attended']); -} \ No newline at end of file +$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('

'.$lang_attendance['attend_info'].'

', $count, $cdays, $points); + end_frame(); + echo ''; + end_main_frame(); + stdfoot(); + +} else { + stderr($lang_attendance['sorry'], $lang_attendance['already_attended']); +}