2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
namespace App\Http\Controllers;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
use App;
|
2016-09-04 15:35:12 +08:00
|
|
|
use View;
|
|
|
|
use Utils;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Texture;
|
2016-09-04 15:35:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2016-11-18 23:06:15 +08:00
|
|
|
use App\Events\UserProfileUpdated;
|
2016-08-29 19:47:30 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-10-23 11:41:52 +08:00
|
|
|
use App\Services\Repositories\UserRepository;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
class UserController extends Controller
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-02-03 14:25:17 +08:00
|
|
|
/**
|
|
|
|
* Current user instance.
|
|
|
|
*
|
|
|
|
* @var App\Models\User
|
|
|
|
*/
|
|
|
|
private $user = null;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-02-03 14:25:17 +08:00
|
|
|
public function __construct(UserRepository $users)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-02-03 14:25:17 +08:00
|
|
|
$this->user = $users->get(session('uid'));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2017-02-03 14:25:17 +08:00
|
|
|
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;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
2017-07-14 09:17:42 +08:00
|
|
|
* Handle user signing.
|
2016-09-04 15:35:12 +08:00
|
|
|
*
|
2017-11-18 13:25:08 +08:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-09-04 15:35:12 +08:00
|
|
|
*/
|
2017-07-14 09:17:42 +08:00
|
|
|
public function sign()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-07-14 09:17:42 +08:00
|
|
|
if ($this->user->canSign()) {
|
2017-11-18 13:25:08 +08:00
|
|
|
$acquiredScore = $this->user->sign();
|
2017-01-17 22:57:16 +08:00
|
|
|
|
2016-09-10 21:39:45 +08:00
|
|
|
return json([
|
2016-07-24 12:33:11 +08:00
|
|
|
'errno' => 0,
|
2017-11-18 13:25:08 +08:00
|
|
|
'msg' => trans('user.sign-success', ['score' => $acquiredScore]),
|
2016-07-24 12:33:11 +08:00
|
|
|
'score' => $this->user->getScore(),
|
2017-04-21 00:05:45 +08:00
|
|
|
'storage' => $this->calculatePercentageUsed($this->user->getStorageUsed(), option('score_per_storage')),
|
2017-08-05 23:43:35 +08:00
|
|
|
'remaining_time' => $this->getUserSignRemainingTimeWithPrecision()
|
2016-07-21 22:01:57 +08:00
|
|
|
]);
|
|
|
|
} else {
|
2017-08-06 17:05:04 +08:00
|
|
|
$remaining_time = $this->getUserSignRemainingTimeWithPrecision();
|
2017-08-05 23:43:35 +08:00
|
|
|
return json(trans('user.cant-sign-until', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'time' => $remaining_time >= 1
|
|
|
|
? $remaining_time : round($remaining_time * 60),
|
2017-08-06 17:05:04 +08:00
|
|
|
'unit' => $remaining_time >= 1
|
|
|
|
? trans('user.time-unit-hour') : trans('user.time-unit-min')
|
2017-08-05 23:43:35 +08:00
|
|
|
]), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 23:43:35 +08:00
|
|
|
public function getUserSignRemainingTimeWithPrecision()
|
|
|
|
{
|
|
|
|
$hours = $this->user->getSignRemainingTime() / 3600;
|
|
|
|
|
2017-08-06 17:05:04 +08:00
|
|
|
return $hours > 1 ? round($hours) : $hours;
|
2017-08-05 23:43:35 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
public function profile()
|
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
return view('user.profile')->with('user', $this->user);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
2017-01-17 22:57:16 +08:00
|
|
|
* Handle changing user profile.
|
2016-09-04 15:35:12 +08:00
|
|
|
*
|
|
|
|
* @param Request $request
|
2017-11-18 13:25:08 +08:00
|
|
|
* @param UserRepository $users
|
2017-04-27 09:25:42 +08:00
|
|
|
* @return mixed
|
2016-09-04 15:35:12 +08:00
|
|
|
*/
|
2017-04-27 09:25:42 +08:00
|
|
|
public function handleProfile(Request $request, UserRepository $users)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-02-03 14:25:17 +08:00
|
|
|
$action = $request->input('action', '');
|
|
|
|
|
|
|
|
switch ($action) {
|
2016-09-04 15:35:12 +08:00
|
|
|
case 'nickname':
|
|
|
|
$this->validate($request, [
|
|
|
|
'new_nickname' => 'required|nickname|max:255'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$nickname = $request->input('new_nickname');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
if ($this->user->setNickName($nickname)) {
|
|
|
|
event(new UserProfileUpdated($action, $this->user));
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.nickname.success', ['nickname' => $nickname]), 0);
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
break; // @codeCoverageIgnore
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
case 'password':
|
|
|
|
$this->validate($request, [
|
2017-07-22 11:12:49 +08:00
|
|
|
'current_password' => 'required|min:6|max:16',
|
2016-09-04 15:35:12 +08:00
|
|
|
'new_password' => 'required|min:8|max:16'
|
|
|
|
]);
|
|
|
|
|
2017-01-08 12:49:32 +08:00
|
|
|
if (!$this->user->verifyPassword($request->input('current_password')))
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.password.wrong-password'), 1);
|
2016-09-04 15:35:12 +08:00
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
if ($this->user->changePasswd($request->input('new_password'))) {
|
|
|
|
event(new UserProfileUpdated($action, $this->user));
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.password.success'), 0);
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
break; // @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
case 'email':
|
|
|
|
$this->validate($request, [
|
|
|
|
'new_email' => 'required|email',
|
2017-07-22 11:12:49 +08:00
|
|
|
'password' => 'required|min:6|max:16'
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-04-27 09:25:42 +08:00
|
|
|
if ($users->get($request->input('new_email'), 'email')) {
|
|
|
|
return json(trans('user.profile.email.existed'), 1);
|
|
|
|
}
|
|
|
|
|
2017-01-08 12:49:32 +08:00
|
|
|
if (!$this->user->verifyPassword($request->input('password')))
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.email.wrong-password'), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
if ($this->user->setEmail($request->input('new_email'))) {
|
|
|
|
event(new UserProfileUpdated($action, $this->user));
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.email.success'), 0);
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
break; // @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
case 'delete':
|
|
|
|
$this->validate($request, [
|
2017-07-22 11:12:49 +08:00
|
|
|
'password' => 'required|min:6|max:16'
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-01-08 12:49:32 +08:00
|
|
|
if (!$this->user->verifyPassword($request->input('password')))
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.delete.wrong-password'), 1);
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
if ($this->user->delete()) {
|
2016-11-17 17:32:12 +08:00
|
|
|
session()->flush();
|
2016-09-04 15:35:12 +08:00
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
return response()
|
|
|
|
->json([
|
|
|
|
'errno' => 0,
|
|
|
|
'msg' => trans('user.profile.delete.success')
|
|
|
|
])
|
|
|
|
->cookie('uid', '', time() - 3600, '/')
|
|
|
|
->cookie('token', '', time() - 3600, '/');
|
2016-09-04 15:35:12 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
break; // @codeCoverageIgnore
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
default:
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('general.illegal-parameters'), 1);
|
2016-09-04 15:35:12 +08:00
|
|
|
break;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2017-11-18 13:25:08 +08:00
|
|
|
} // @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
2017-01-17 22:57:16 +08:00
|
|
|
* Set user avatar.
|
2016-09-04 15:35:12 +08:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
public function setAvatar(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$result = Texture::find($request->input('tid'));
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
if ($result) {
|
2016-09-04 15:35:12 +08:00
|
|
|
if ($result->type == "cape")
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.avatar.wrong-type'), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if ($this->user->setAvatar($request->input('tid'))) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.profile.avatar.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-07 13:48:20 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2017-11-18 13:25:08 +08:00
|
|
|
} // @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
}
|