2017-07-04 14:05:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
use App\Facades\Utils;
|
2017-07-04 14:05:37 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Rank;
|
2017-08-12 09:51:55 +08:00
|
|
|
use App\Models\Role;
|
2017-12-23 06:32:21 +08:00
|
|
|
use App\Events\UserRegistered;
|
2017-12-03 00:55:17 +08:00
|
|
|
use App\Events\UserStateChanged;
|
2017-12-23 06:32:21 +08:00
|
|
|
use App\Events\UserStatsChanged;
|
|
|
|
use App\Models\Enums\PilotState;
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-13 04:48:03 +08:00
|
|
|
class UserService extends BaseService
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2017-12-15 13:51:57 +08:00
|
|
|
/**
|
2017-12-23 06:32:21 +08:00
|
|
|
* Register a pilot. Also attaches the initial roles
|
|
|
|
* required, and then triggers the UserRegistered event
|
2017-12-23 02:46:46 +08:00
|
|
|
* @param User $user
|
2017-12-15 13:51:57 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-12-23 02:46:46 +08:00
|
|
|
public function createPilot(User $user)
|
2017-12-15 13:51:57 +08:00
|
|
|
{
|
|
|
|
# Determine if we want to auto accept
|
2017-12-23 03:54:25 +08:00
|
|
|
if(setting('pilot.auto_accept') === true) {
|
2017-12-23 02:46:46 +08:00
|
|
|
$user->state = PilotState::ACTIVE;
|
2017-12-15 13:51:57 +08:00
|
|
|
} else {
|
2017-12-23 02:46:46 +08:00
|
|
|
$user->state = PilotState::PENDING;
|
2017-12-23 03:54:25 +08:00
|
|
|
}
|
2017-12-15 13:51:57 +08:00
|
|
|
|
2017-12-23 02:46:46 +08:00
|
|
|
$user->save();
|
2017-12-15 13:51:57 +08:00
|
|
|
|
|
|
|
# Attach the user roles
|
|
|
|
$role = Role::where('name', 'user')->first();
|
|
|
|
$user->attachRole($role);
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
# Let's check their rank and where they should start
|
2017-12-15 13:51:57 +08:00
|
|
|
$this->calculatePilotRank($user);
|
|
|
|
|
2017-12-23 02:46:46 +08:00
|
|
|
$user->refresh();
|
|
|
|
|
2017-12-15 13:51:57 +08:00
|
|
|
event(new UserRegistered($user));
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* Change the user's state. PENDING to ACCEPTED, etc
|
|
|
|
* Send out an email
|
|
|
|
* @param User $user
|
|
|
|
* @param $old_state
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function changeUserState(User $user, $old_state): User
|
|
|
|
{
|
|
|
|
if($user->state === $old_state) {
|
2017-12-23 06:55:47 +08:00
|
|
|
return $user;
|
2017-12-23 06:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('User ' . $user->pilot_id . ' state changing from '
|
|
|
|
. PilotState::label($old_state) . ' to '
|
|
|
|
. PilotState::label($user->state));
|
|
|
|
|
|
|
|
event(new UserStateChanged($user, $old_state));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust the number of flights a user has. Triggers
|
|
|
|
* UserStatsChanged event
|
|
|
|
* @param User $user
|
|
|
|
* @param int $count
|
|
|
|
* @return User
|
|
|
|
*/
|
2017-12-13 06:58:27 +08:00
|
|
|
public function adjustFlightCount(User $user, int $count): User
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->refresh();
|
2017-12-23 06:32:21 +08:00
|
|
|
$old_value = $user->flights;
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->flights += $count;
|
|
|
|
$user->save();
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
event(new UserStatsChanged($user, 'flights', $old_value));
|
2017-12-03 00:55:17 +08:00
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
return $user;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* Update a user's flight times
|
|
|
|
* @param User $user
|
|
|
|
* @param int $minutes
|
|
|
|
* @return User
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
public function adjustFlightTime(User $user, int $minutes): User
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->refresh();
|
2017-12-14 00:56:26 +08:00
|
|
|
$user->flight_time += $minutes;
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->save();
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
return $user;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See if a pilot's rank has change. Triggers the UserStatsChanged event
|
|
|
|
* @param User $user
|
|
|
|
* @return User
|
|
|
|
*/
|
2017-12-13 06:58:27 +08:00
|
|
|
public function calculatePilotRank(User $user): User
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->refresh();
|
2017-12-23 06:32:21 +08:00
|
|
|
$old_rank = $user->rank;
|
|
|
|
$original_rank_id = $user->rank_id;
|
2017-12-14 00:56:26 +08:00
|
|
|
$pilot_hours = Utils::minutesToHours($user->flight_time);
|
2017-07-04 14:05:37 +08:00
|
|
|
|
|
|
|
# TODO: Cache
|
2017-12-23 06:32:21 +08:00
|
|
|
$ranks = Rank::where('auto_promote', true)
|
|
|
|
->orderBy('hours', 'asc')->get();
|
2017-07-04 14:05:37 +08:00
|
|
|
|
|
|
|
foreach ($ranks as $rank) {
|
|
|
|
if($rank->hours > $pilot_hours) {
|
|
|
|
break;
|
|
|
|
} else {
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->rank_id = $rank->id;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
// Only trigger the event/update if there's been a change
|
|
|
|
if($user->rank_id !== $original_rank_id) {
|
|
|
|
$user->save();
|
|
|
|
$user->refresh();
|
|
|
|
event(new UserStatsChanged($user, 'rank', $old_rank));
|
|
|
|
}
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
return $user;
|
|
|
|
}
|
2017-12-03 00:55:17 +08:00
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* Recount/update all of the stats for a user
|
|
|
|
* @param User $user
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function recalculateStats(User $user): User
|
|
|
|
{
|
2017-12-13 06:58:27 +08:00
|
|
|
return $user;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
}
|