fix division by zero when user score < rate

This commit is contained in:
printempw 2017-02-03 14:25:17 +08:00
parent 0d338ab4a7
commit c19eec5a90
2 changed files with 51 additions and 32 deletions

View File

@ -14,18 +14,49 @@ use App\Services\Repositories\UserRepository;
class UserController extends Controller 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() 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) public function handleProfile(Request $request)
{ {
switch ($this->action) { $action = $request->input('action', '');
switch ($action) {
case 'nickname': case 'nickname':
$this->validate($request, [ $this->validate($request, [
'new_nickname' => 'required|nickname|max:255' 'new_nickname' => 'required|nickname|max:255'
@ -127,7 +160,7 @@ class UserController extends Controller
break; break;
} }
event(new UserProfileUpdated($this->action, $this->user)); event(new UserProfileUpdated($action, $this->user));
} }

View File

@ -31,45 +31,31 @@
<div class="col-md-8"> <div class="col-md-8">
<div class="progress-group"> <div class="progress-group">
<span class="progress-text">{{ trans('user.used.players') }}</span> <span class="progress-text">{{ trans('user.used.players') }}</span>
<?php <span class="progress-number"><b>{{ $statistics['players']['used'] }}</b>/ {{ $statistics['players']['total'] }}</span>
// to avoid division by zero
if (option('score_per_player') == 0) {
$total = 'UNLIMITED';
$percentage = 0;
} else {
$total = $user->players->count() + floor($user->getScore() / option('score_per_player'));
$percentage = $user->players->count() / $total * 100;
}
?>
<span class="progress-number"><b>{{ $user->players->count() }}</b>/ {{ $total }}</span>
<div class="progress sm"> <div class="progress sm">
<div class="progress-bar progress-bar-aqua" style="width: {{ $percentage }}%"></div> <div class="progress-bar progress-bar-aqua" style="width: {{ $statistics['players']['percentage'] }}%"></div>
</div> </div>
</div><!-- /.progress-group --> </div><!-- /.progress-group -->
<div class="progress-group"> <div class="progress-group">
<span class="progress-text">{{ trans('user.used.storage') }}</span> <span class="progress-text">{{ trans('user.used.storage') }}</span>
<?php <?php
if (($rate = option('score_per_storage')) == 0) { $used = $statistics['storage']['used'];
$total = 'UNLIMITED'; $total = $statistics['storage']['total'];
$percentage = 0;
} else {
$total = $user->getStorageUsed() + $user->getScore() / $rate;
$percentage = $user->getStorageUsed() / ($user->getStorageUsed() + $user->getScore() / $rate) * 100;
}
?> ?>
@if ($user->getStorageUsed() > 1024)
@if ($used > 1024)
<span class="progress-number"> <span class="progress-number">
<b>{{ round($user->getStorageUsed() / 1024, 1) }}</b>/ <b>{{ round($used / 1024, 1) }}</b>/ {{ is_string($total) ? $total : round($total / 1024, 1) }} MB
{{ is_string($total) ? $total : round($total / 1024, 1) }} MB
</span> </span>
@else @else
<span class="progress-number"> <span class="progress-number">
<b>{{ $user->getStorageUsed() }}</b>/ {{ $total }} KB <b>{{ $used }}</b>/ {{ $total }} KB
</span> </span>
@endif @endif
<div class="progress sm"> <div class="progress sm">
<div class="progress-bar progress-bar-yellow" style="width: {{ $percentage }}%"></div> <div class="progress-bar progress-bar-yellow" style="width: {{ $statistics['storage']['percentage'] }}%"></div>
</div> </div>
</div><!-- /.progress-group --> </div><!-- /.progress-group -->
</div><!-- /.col --> </div><!-- /.col -->