From 6d90a259697839b6d7d7b69663aa0ba00fdc9729 Mon Sep 17 00:00:00 2001 From: Ziven <> Date: Tue, 1 Nov 2022 09:04:16 +0800 Subject: [PATCH] v0.3.1 --- extend.php | 3 +- src/Listeners/doCheckin.php | 60 +++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/extend.php b/extend.php index 1c31503..2fd06ad 100644 --- a/extend.php +++ b/extend.php @@ -14,8 +14,7 @@ (new Extend\Locales(__DIR__ . '/locale')), - (new Extend\Policy()) - ->modelPolicy(User::class, UserPolicy::class), + (new Extend\Policy())->modelPolicy(User::class, UserPolicy::class), (new Extend\Event())->listen(Saving::class, [doCheckin::class, 'checkinSaved']), (new Extend\ApiSerializer(UserSerializer::class))->attributes(AddUserCheckinAttributes::class), diff --git a/src/Listeners/doCheckin.php b/src/Listeners/doCheckin.php index a7a1d8b..3f27aad 100644 --- a/src/Listeners/doCheckin.php +++ b/src/Listeners/doCheckin.php @@ -21,43 +21,45 @@ public function __construct(SettingsRepositoryInterface $settings, Dispatcher $e public function checkinSaved(Saving $event){ $actor = $event->actor; $user = $event->user; - $actor->assertCan('checkin.allowCheckIn', $user); + $allowCheckIn = $actor->can('checkin.allowCheckIn', $user); - $attributes = Arr::get($event->data, 'attributes', []); + if($allowCheckIn){ + $attributes = Arr::get($event->data, 'attributes', []); - if (array_key_exists('canCheckin', $attributes)) { - $canCheckin = true; - $timezone = $this->settings->get('ziven-forum-checkin.checkinTimeZone', 0); - $current_timestamp = time()+$timezone*60*60; - $current_data_at_midnight = strtotime(date('Y-m-d', $current_timestamp)." 00:00:00"); - - $last_checkin_time = $user->last_checkin_time; + if (array_key_exists('canCheckin', $attributes)) { + $canCheckin = true; + $timezone = $this->settings->get('ziven-forum-checkin.checkinTimeZone', 0); + $current_timestamp = time()+$timezone*60*60; + $current_data_at_midnight = strtotime(date('Y-m-d', $current_timestamp)." 00:00:00"); + + $last_checkin_time = $user->last_checkin_time; - if($last_checkin_time!==null){ - $explode_checkin_data = explode(' ', $last_checkin_time); - $checkin_date_at_midnight = strtotime($explode_checkin_data[0]." 00:00:00"); - $canCheckin = $current_data_at_midnight>$checkin_date_at_midnight; - } - - if($canCheckin){ - if(($current_timestamp-$checkin_date_at_midnight)/3600<48){ - $user->total_continuous_checkin_count+=1; - }else{ - $user->total_continuous_checkin_count=1; + if($last_checkin_time!==null){ + $explode_checkin_data = explode(' ', $last_checkin_time); + $checkin_date_at_midnight = strtotime($explode_checkin_data[0]." 00:00:00"); + $canCheckin = $current_data_at_midnight>$checkin_date_at_midnight; } + + if($canCheckin){ + if(($current_timestamp-$checkin_date_at_midnight)/3600<48){ + $user->total_continuous_checkin_count+=1; + }else{ + $user->total_continuous_checkin_count=1; + } - $user->last_checkin_time = date('Y-m-d H:i:s', $current_timestamp); - $user->total_checkin_count+=1; + $user->last_checkin_time = date('Y-m-d H:i:s', $current_timestamp); + $user->total_checkin_count+=1; - // app("log")->error($canCheckin); - // app("log")->error($user->total_continuous_checkin_count); + // app("log")->error($canCheckin); + // app("log")->error($user->total_continuous_checkin_count); - if(isset($user->money)===true){ - $checkinRewardMoney = (float)$this->settings->get('ziven-forum-checkin.checkinRewardMoney', 0); - $user->money+=$checkinRewardMoney; - } + if(isset($user->money)===true){ + $checkinRewardMoney = (float)$this->settings->get('ziven-forum-checkin.checkinRewardMoney', 0); + $user->money+=$checkinRewardMoney; + } - $this->events->dispatch(new checkinUpdated($user)); + $this->events->dispatch(new checkinUpdated($user)); + } } } }