From c19eec5a904e1e5a56de32bff8a571b0cc3a7e34 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 3 Feb 2017 14:25:17 +0800 Subject: [PATCH] fix division by zero when user score < rate --- app/Http/Controllers/UserController.php | 49 +++++++++++++++++++++---- resources/views/user/index.tpl | 34 +++++------------ 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index f5658645..6e058203 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -14,18 +14,49 @@ use App\Services\Repositories\UserRepository; class UserController extends Controller { - private $action = ""; - private $user = null; + /** + * Current user instance. + * + * @var App\Models\User + */ + private $user = null; - public function __construct(Request $request, UserRepository $users) + public function __construct(UserRepository $users) { - $this->action = $request->input('action', ''); - $this->user = $users->get(session('uid')); + $this->user = $users->get(session('uid')); } public function index() { - return view('user.index')->with('user', $this->user); + return view('user.index')->with([ + 'user' => $this->user, + 'statistics' => [ + 'players' => $this->calculatePercentageUsed($this->user->players->count(), option('score_per_player')), + 'storage' => $this->calculatePercentageUsed($this->user->getStorageUsed(), option('score_per_storage')) + ] + ]); + } + + /** + * Calculate percentage of resources used by user. + * + * @param int $used + * @param int $rate + * @return array + */ + protected function calculatePercentageUsed($used, $rate) + { + // init default value to avoid division by zero + $result['used'] = $used; + $result['total'] = 'UNLIMITED'; + $result['percentage'] = 0; + + if ($rate != 0) { + $result['total'] = $used + floor($this->user->getScore() / $rate); + $result['percentage'] = $result['total'] ? $used / $result['total'] * 100 : 100; + } + + return $result; } /** @@ -62,7 +93,9 @@ class UserController extends Controller */ public function handleProfile(Request $request) { - switch ($this->action) { + $action = $request->input('action', ''); + + switch ($action) { case 'nickname': $this->validate($request, [ 'new_nickname' => 'required|nickname|max:255' @@ -127,7 +160,7 @@ class UserController extends Controller break; } - event(new UserProfileUpdated($this->action, $this->user)); + event(new UserProfileUpdated($action, $this->user)); } diff --git a/resources/views/user/index.tpl b/resources/views/user/index.tpl index 4a73669b..e7dac682 100644 --- a/resources/views/user/index.tpl +++ b/resources/views/user/index.tpl @@ -31,45 +31,31 @@
{{ trans('user.used.players') }} - players->count() + floor($user->getScore() / option('score_per_player')); - $percentage = $user->players->count() / $total * 100; - } - ?> - {{ $user->players->count() }}/ {{ $total }} + {{ $statistics['players']['used'] }}/ {{ $statistics['players']['total'] }}
-
+
{{ trans('user.used.storage') }} + getStorageUsed() + $user->getScore() / $rate; - $percentage = $user->getStorageUsed() / ($user->getStorageUsed() + $user->getScore() / $rate) * 100; - } + $used = $statistics['storage']['used']; + $total = $statistics['storage']['total']; ?> - @if ($user->getStorageUsed() > 1024) + + @if ($used > 1024) - {{ round($user->getStorageUsed() / 1024, 1) }}/ - {{ is_string($total) ? $total : round($total / 1024, 1) }} MB + {{ round($used / 1024, 1) }}/ {{ is_string($total) ? $total : round($total / 1024, 1) }} MB @else - {{ $user->getStorageUsed() }}/ {{ $total }} KB + {{ $used }}/ {{ $total }} KB @endif
-
+