| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- namespace App\Module\Game\Services;
- use App\Module\Game\Dtos\TaskChangeTempDto;
- use App\Module\Game\Logics\TaskTemp;
- use Illuminate\Support\Facades\Log;
- /**
- * 任务暂存服务类
- *
- * 提供任务暂存相关的服务方法,用于外部调用
- */
- class TaskTempService
- {
- /**
- * 获取用户的任务变更数据
- *
- * @param int $userId 用户ID
- * @return array 任务变更数据列表
- */
- public static function getUserTaskChanges(int $userId): array
- {
- try {
- return TaskTemp::getUserTaskChanges($userId);
- } catch (\Exception $e) {
- Log::error('获取用户任务变更数据失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 获取用户指定任务的变更数据
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @return TaskChangeTempDto|null 任务变更数据
- */
- public static function getUserTaskChange(int $userId, int $taskId): ?TaskChangeTempDto
- {
- try {
- return TaskTemp::getUserTaskChange($userId, $taskId);
- } catch (\Exception $e) {
- Log::error('获取用户指定任务变更数据失败', [
- 'user_id' => $userId,
- 'task_id' => $taskId,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
- /**
- * 记录任务进度更新
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @param string $taskName 任务名称
- * @param int $progress 进度百分比
- * @param array $progressDetails 进度详情
- * @return bool 是否成功
- */
- public static function recordTaskProgressUpdate(int $userId, int $taskId, string $taskName, int $progress, array $progressDetails = []): bool
- {
- try {
- TaskTemp::handleTaskProgressUpdated($userId, $taskId, $taskName, $progress, $progressDetails);
- return true;
- } catch (\Exception $e) {
- Log::error('记录任务进度更新失败', [
- 'user_id' => $userId,
- 'task_id' => $taskId,
- 'progress' => $progress,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 清理用户的任务变更数据
- *
- * @param int $userId 用户ID
- * @return bool 是否成功
- */
- public static function clearUserTaskChanges(int $userId): bool
- {
- try {
- TaskTemp::clearUserTaskChanges($userId);
- return true;
- } catch (\Exception $e) {
- Log::error('清理用户任务变更数据失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 清理用户指定任务的变更数据
- *
- * @param int $userId 用户ID
- * @param int $taskId 任务ID
- * @return bool 是否成功
- */
- public static function clearUserTaskChange(int $userId, int $taskId): bool
- {
- try {
- TaskTemp::clearUserTaskChange($userId, $taskId);
- return true;
- } catch (\Exception $e) {
- Log::error('清理用户指定任务变更数据失败', [
- 'user_id' => $userId,
- 'task_id' => $taskId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 获取用户任务变更统计信息
- *
- * @param int $userId 用户ID
- * @return array 统计信息
- */
- public static function getUserTaskChangeStats(int $userId): array
- {
- try {
- $changes = TaskTemp::getUserTaskChanges($userId);
- $stats = [
- 'total_changes' => count($changes),
- 'progress_updates' => 0,
- 'completed_tasks' => 0,
- 'reward_claims' => 0,
- 'latest_update' => null,
- ];
- foreach ($changes as $change) {
- switch ($change->changeType) {
- case 'progress_updated':
- $stats['progress_updates']++;
- break;
- case 'completed':
- $stats['completed_tasks']++;
- break;
- case 'reward_claimed':
- $stats['reward_claims']++;
- break;
- }
- if ($stats['latest_update'] === null || $change->updatedAt > $stats['latest_update']) {
- $stats['latest_update'] = $change->updatedAt;
- }
- }
- return $stats;
- } catch (\Exception $e) {
- Log::error('获取用户任务变更统计信息失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return [
- 'total_changes' => 0,
- 'progress_updates' => 0,
- 'completed_tasks' => 0,
- 'reward_claims' => 0,
- 'latest_update' => null,
- ];
- }
- }
- /**
- * 检查用户是否有任务变更数据
- *
- * @param int $userId 用户ID
- * @return bool 是否有变更数据
- */
- public static function hasUserTaskChanges(int $userId): bool
- {
- try {
- $changes = TaskTemp::getUserTaskChanges($userId);
- return !empty($changes);
- } catch (\Exception $e) {
- Log::error('检查用户任务变更数据失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 获取用户最近的任务变更
- *
- * @param int $userId 用户ID
- * @param int $limit 限制数量
- * @return array 最近的任务变更
- */
- public static function getUserRecentTaskChanges(int $userId, int $limit = 10): array
- {
- try {
- $changes = TaskTemp::getUserTaskChanges($userId);
- // 按更新时间倒序排序
- usort($changes, function ($a, $b) {
- return $b->updatedAt - $a->updatedAt;
- });
- // 限制数量
- return array_slice($changes, 0, $limit);
- } catch (\Exception $e) {
- Log::error('获取用户最近任务变更失败', [
- 'user_id' => $userId,
- 'limit' => $limit,
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- }
|