| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace App\Module\Game\Logics;
- use App\Module\Game\Models\GameUserSkin;
- use App\Module\Game\Dto\UserSkinDto;
- use UCore\Dto\Res;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 皮肤逻辑类
- *
- * 处理皮肤相关的内部业务逻辑
- */
- class SkinLogic
- {
- /**
- * 获取用户皮肤信息
- *
- * @param int $userId 用户ID
- * @return UserSkinDto|null
- */
- public function getUserSkinInfo(int $userId): ?UserSkinDto
- {
- try {
- $userSkin = GameUserSkin::where('user_id', $userId)->first();
-
- if (!$userSkin) {
- // 如果用户没有皮肤记录,自动初始化
- $initResult = $this->initUserSkin($userId);
- if (!$initResult->success) {
- return null;
- }
- $userSkin = GameUserSkin::where('user_id', $userId)->first();
- }
-
- return UserSkinDto::fromModel($userSkin);
- } catch (\Exception $e) {
- Log::error('获取用户皮肤信息失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
- /**
- * 切换用户皮肤
- *
- * @param int $userId 用户ID
- * @param int $skinId 皮肤ID
- * @return Res
- */
- public function changeSkin(int $userId, int $skinId): Res
- {
- try {
- // 验证皮肤ID
- if (!GameUserSkin::isValidSkinId($skinId)) {
- return Res::error('无效的皮肤ID');
- }
- $userSkin = GameUserSkin::where('user_id', $userId)->first();
-
- if (!$userSkin) {
- // 如果用户没有皮肤记录,先初始化
- $initResult = $this->initUserSkin($userId);
- if (!$initResult->success) {
- return $initResult;
- }
- $userSkin = GameUserSkin::where('user_id', $userId)->first();
- }
- // 检查是否拥有该皮肤
- if (!$userSkin->hasSkin($skinId)) {
- return Res::error('您还没有拥有该皮肤');
- }
- // 切换皮肤
- $userSkin->current_skin_id = $skinId;
- $userSkin->save();
- return Res::success('皮肤切换成功');
- } catch (\Exception $e) {
- Log::error('切换皮肤失败', [
- 'user_id' => $userId,
- 'skin_id' => $skinId,
- 'error' => $e->getMessage()
- ]);
- return Res::error('切换皮肤失败');
- }
- }
- /**
- * 为用户添加皮肤
- *
- * @param int $userId 用户ID
- * @param int $skinId 皮肤ID
- * @return Res
- */
- public function addSkin(int $userId, int $skinId): Res
- {
- try {
- // 验证皮肤ID
- if (!GameUserSkin::isValidSkinId($skinId)) {
- return Res::error('无效的皮肤ID');
- }
- $userSkin = GameUserSkin::where('user_id', $userId)->first();
-
- if (!$userSkin) {
- // 如果用户没有皮肤记录,先初始化
- $initResult = $this->initUserSkin($userId);
- if (!$initResult->success) {
- return $initResult;
- }
- $userSkin = GameUserSkin::where('user_id', $userId)->first();
- }
- // 添加皮肤
- if (!$userSkin->addSkin($skinId)) {
- return Res::error('您已经拥有该皮肤');
- }
- $userSkin->save();
- return Res::success('皮肤获得成功');
- } catch (\Exception $e) {
- Log::error('添加皮肤失败', [
- 'user_id' => $userId,
- 'skin_id' => $skinId,
- 'error' => $e->getMessage()
- ]);
- return Res::error('添加皮肤失败');
- }
- }
- /**
- * 检查用户是否拥有指定皮肤
- *
- * @param int $userId 用户ID
- * @param int $skinId 皮肤ID
- * @return bool
- */
- public function hasSkin(int $userId, int $skinId): bool
- {
- try {
- $userSkin = GameUserSkin::where('user_id', $userId)->first();
-
- if (!$userSkin) {
- return $skinId === 1; // 默认拥有1号皮肤
- }
- return $userSkin->hasSkin($skinId);
- } catch (\Exception $e) {
- Log::error('检查皮肤拥有状态失败', [
- 'user_id' => $userId,
- 'skin_id' => $skinId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 初始化用户皮肤数据
- *
- * @param int $userId 用户ID
- * @return Res
- */
- public function initUserSkin(int $userId): Res
- {
- try {
- // 检查是否已存在
- $existing = GameUserSkin::where('user_id', $userId)->first();
- if ($existing) {
- return Res::success('用户皮肤数据已存在');
- }
- // 创建默认皮肤数据
- GameUserSkin::create([
- 'user_id' => $userId,
- 'current_skin_id' => 1,
- 'owned_skins' => '1',
- ]);
- return Res::success('用户皮肤数据初始化成功');
- } catch (\Exception $e) {
- Log::error('初始化用户皮肤数据失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return Res::error('初始化用户皮肤数据失败');
- }
- }
- }
|