| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace App\Module\Task\Services;
- use App\Module\Task\Models\Task;
- use App\Module\Task\Models\TaskCategory;
- use App\Module\Task\Models\TaskCondition;
- use App\Module\Task\Models\TaskConditionType;
- use App\Module\Task\Models\TaskCost;
- use App\Module\Task\Models\TaskReward;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Schema;
- class TaskConfigService
- {
- /**
- * 配置文件路径
- *
- * @var string
- */
- protected $configPath = 'public/json';
- /**
- * 获取任务基础配置
- *
- * @return array
- */
- public function getTaskConfig()
- {
- $configFile = "{$this->configPath}/task_config.json";
- if (File::exists($configFile)) {
- return json_decode(File::get($configFile), true);
- }
- return [
- 'tasks' => [],
- 'categories' => []
- ];
- }
- /**
- * 获取任务条件配置
- *
- * @return array
- */
- public function getTaskConditionConfig()
- {
- $configFile = "{$this->configPath}/task_condition_config.json";
- if (File::exists($configFile)) {
- return json_decode(File::get($configFile), true);
- }
- return [
- 'conditions' => [],
- 'condition_types' => []
- ];
- }
- /**
- * 获取任务奖励配置
- *
- * @return array
- */
- public function getTaskRewardConfig()
- {
- $configFile = "{$this->configPath}/task_reward_config.json";
- if (File::exists($configFile)) {
- return json_decode(File::get($configFile), true);
- }
- return [
- 'rewards' => []
- ];
- }
- /**
- * 获取任务消耗配置
- *
- * @return array
- */
- public function getTaskCostConfig()
- {
- $configFile = "{$this->configPath}/task_cost_config.json";
- if (File::exists($configFile)) {
- return json_decode(File::get($configFile), true);
- }
- return [
- 'costs' => []
- ];
- }
- /**
- * 导出任务基础配置表
- *
- * @return string
- */
- public function exportTaskConfig()
- {
- $taskConfig = [
- 'tasks' => [],
- 'categories' => []
- ];
- try {
- // 尝试获取任务数据
- if (Schema::hasTable('task_tasks')) {
- $tasks = Task::all();
- foreach ($tasks as $task) {
- $taskConfig['tasks'][] = [
- 'id' => $task->id,
- 'name' => $task->name,
- 'description' => $task->description,
- 'category_id' => $task->category_id,
- 'type' => $task->type,
- 'reset_type' => $task->reset_type,
- 'min_level' => $task->min_level ?? $task->level_required ?? 0,
- 'max_level' => $task->max_level ?? 0,
- 'time_limit' => $task->time_limit,
- 'start_time' => $task->start_time ? $task->start_time->toDateTimeString() : null,
- 'end_time' => $task->end_time ? $task->end_time->toDateTimeString() : null,
- 'sort_order' => $task->sort_order,
- 'is_active' => $task->is_active
- ];
- }
- }
- // 尝试获取分类数据
- if (Schema::hasTable('task_categories')) {
- $categories = TaskCategory::all();
- foreach ($categories as $category) {
- $taskConfig['categories'][] = [
- 'id' => $category->id,
- 'name' => $category->name,
- 'code' => $category->code,
- 'parent_id' => $category->parent_id ?? 0,
- 'sort_order' => $category->sort_order
- ];
- }
- }
- } catch (\Exception $e) {
- Log::warning('Error exporting task config: ' . $e->getMessage());
- }
- return json_encode($taskConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- }
- /**
- * 导出任务条件配置表
- *
- * @return string
- */
- public function exportTaskConditionConfig()
- {
- $conditionConfig = [
- 'conditions' => [],
- 'condition_types' => []
- ];
- try {
- // 尝试获取条件数据
- if (Schema::hasTable('task_conditions')) {
- $conditions = TaskCondition::all();
- foreach ($conditions as $condition) {
- $conditionConfig['conditions'][] = [
- 'id' => $condition->id,
- 'task_id' => $condition->task_id,
- 'condition_type_id' => $condition->condition_type_id,
- 'param1' => $condition->param1,
- 'param2' => $condition->param2,
- 'target_value' => $condition->target_value,
- 'is_prerequisite' => $condition->is_prerequisite,
- 'extra_data' => $condition->extra_data,
- 'sort_order' => $condition->sort_order
- ];
- }
- }
- // 尝试获取条件类型数据
- if (Schema::hasTable('task_condition_types')) {
- $conditionTypes = TaskConditionType::all();
- foreach ($conditionTypes as $type) {
- $conditionConfig['condition_types'][] = [
- 'id' => $type->id,
- 'name' => $type->name,
- 'code' => $type->code,
- 'description' => $type->description
- ];
- }
- }
- } catch (\Exception $e) {
- Log::warning('Error exporting task condition config: ' . $e->getMessage());
- }
- return json_encode($conditionConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- }
- /**
- * 导出任务奖励配置表
- *
- * @return string
- */
- public function exportTaskRewardConfig()
- {
- $rewardConfig = [
- 'rewards' => []
- ];
- try {
- // 尝试获取奖励数据
- if (Schema::hasTable('task_rewards')) {
- $rewards = TaskReward::all();
- foreach ($rewards as $reward) {
- $rewardConfig['rewards'][] = [
- 'id' => $reward->id,
- 'task_id' => $reward->task_id,
- 'reward_type' => $reward->reward_type,
- 'reward_param1' => $reward->reward_param1,
- 'reward_param2' => $reward->reward_param2,
- 'quantity' => $reward->quantity,
- 'extra_data' => $reward->extra_data,
- 'sort_order' => $reward->sort_order
- ];
- }
- }
- } catch (\Exception $e) {
- Log::warning('Error exporting task reward config: ' . $e->getMessage());
- }
- return json_encode($rewardConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- }
- /**
- * 导出任务消耗配置表
- *
- * @return string
- */
- public function exportTaskCostConfig()
- {
- $costConfig = [
- 'costs' => []
- ];
- try {
- // 尝试获取消耗数据
- if (Schema::hasTable('task_costs')) {
- $costs = TaskCost::all();
- foreach ($costs as $cost) {
- $costConfig['costs'][] = [
- 'id' => $cost->id,
- 'task_id' => $cost->task_id,
- 'cost_type' => $cost->cost_type,
- 'cost_param1' => $cost->cost_param1,
- 'cost_param2' => $cost->cost_param2,
- 'quantity' => $cost->quantity,
- 'extra_data' => $cost->extra_data,
- 'sort_order' => $cost->sort_order
- ];
- }
- }
- } catch (\Exception $e) {
- Log::warning('Error exporting task cost config: ' . $e->getMessage());
- }
- return json_encode($costConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- }
- }
|