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
{
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));
}

View File

@ -31,45 +31,31 @@
<div class="col-md-8">
<div class="progress-group">
<span class="progress-text">{{ trans('user.used.players') }}</span>
<?php
// 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>
<span class="progress-number"><b>{{ $statistics['players']['used'] }}</b>/ {{ $statistics['players']['total'] }}</span>
<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><!-- /.progress-group -->
<div class="progress-group">
<span class="progress-text">{{ trans('user.used.storage') }}</span>
<?php
if (($rate = option('score_per_storage')) == 0) {
$total = 'UNLIMITED';
$percentage = 0;
} else {
$total = $user->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)
<span class="progress-number">
<b>{{ round($user->getStorageUsed() / 1024, 1) }}</b>/
{{ is_string($total) ? $total : round($total / 1024, 1) }} MB
<b>{{ round($used / 1024, 1) }}</b>/ {{ is_string($total) ? $total : round($total / 1024, 1) }} MB
</span>
@else
<span class="progress-number">
<b>{{ $user->getStorageUsed() }}</b>/ {{ $total }} KB
<b>{{ $used }}</b>/ {{ $total }} KB
</span>
@endif
<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><!-- /.progress-group -->
</div><!-- /.col -->