TaskConfigService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace App\Module\Task\Services;
  3. use App\Module\Task\Models\Task;
  4. use App\Module\Task\Models\TaskCategory;
  5. use App\Module\Task\Models\TaskCondition;
  6. use App\Module\Task\Models\TaskConditionType;
  7. use App\Module\Task\Models\TaskCost;
  8. use App\Module\Task\Models\TaskReward;
  9. use Illuminate\Support\Facades\File;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Schema;
  12. class TaskConfigService
  13. {
  14. /**
  15. * 配置文件路径
  16. *
  17. * @var string
  18. */
  19. protected $configPath = 'public/json';
  20. /**
  21. * 获取任务基础配置
  22. *
  23. * @return array
  24. */
  25. public function getTaskConfig()
  26. {
  27. $configFile = "{$this->configPath}/task_config.json";
  28. if (File::exists($configFile)) {
  29. return json_decode(File::get($configFile), true);
  30. }
  31. return [
  32. 'tasks' => [],
  33. 'categories' => []
  34. ];
  35. }
  36. /**
  37. * 获取任务条件配置
  38. *
  39. * @return array
  40. */
  41. public function getTaskConditionConfig()
  42. {
  43. $configFile = "{$this->configPath}/task_condition_config.json";
  44. if (File::exists($configFile)) {
  45. return json_decode(File::get($configFile), true);
  46. }
  47. return [
  48. 'conditions' => [],
  49. 'condition_types' => []
  50. ];
  51. }
  52. /**
  53. * 获取任务奖励配置
  54. *
  55. * @return array
  56. */
  57. public function getTaskRewardConfig()
  58. {
  59. $configFile = "{$this->configPath}/task_reward_config.json";
  60. if (File::exists($configFile)) {
  61. return json_decode(File::get($configFile), true);
  62. }
  63. return [
  64. 'rewards' => []
  65. ];
  66. }
  67. /**
  68. * 获取任务消耗配置
  69. *
  70. * @return array
  71. */
  72. public function getTaskCostConfig()
  73. {
  74. $configFile = "{$this->configPath}/task_cost_config.json";
  75. if (File::exists($configFile)) {
  76. return json_decode(File::get($configFile), true);
  77. }
  78. return [
  79. 'costs' => []
  80. ];
  81. }
  82. /**
  83. * 导出任务基础配置表
  84. *
  85. * @return string
  86. */
  87. public function exportTaskConfig()
  88. {
  89. $taskConfig = [
  90. 'tasks' => [],
  91. 'categories' => []
  92. ];
  93. try {
  94. // 尝试获取任务数据
  95. if (Schema::hasTable('task_tasks')) {
  96. $tasks = Task::all();
  97. foreach ($tasks as $task) {
  98. $taskConfig['tasks'][] = [
  99. 'id' => $task->id,
  100. 'name' => $task->name,
  101. 'description' => $task->description,
  102. 'category_id' => $task->category_id,
  103. 'type' => $task->type,
  104. 'reset_type' => $task->reset_type,
  105. 'min_level' => $task->min_level ?? $task->level_required ?? 0,
  106. 'max_level' => $task->max_level ?? 0,
  107. 'time_limit' => $task->time_limit,
  108. 'start_time' => $task->start_time ? $task->start_time->toDateTimeString() : null,
  109. 'end_time' => $task->end_time ? $task->end_time->toDateTimeString() : null,
  110. 'sort_order' => $task->sort_order,
  111. 'is_active' => $task->is_active
  112. ];
  113. }
  114. }
  115. // 尝试获取分类数据
  116. if (Schema::hasTable('task_categories')) {
  117. $categories = TaskCategory::all();
  118. foreach ($categories as $category) {
  119. $taskConfig['categories'][] = [
  120. 'id' => $category->id,
  121. 'name' => $category->name,
  122. 'code' => $category->code,
  123. 'parent_id' => $category->parent_id ?? 0,
  124. 'sort_order' => $category->sort_order
  125. ];
  126. }
  127. }
  128. } catch (\Exception $e) {
  129. Log::warning('Error exporting task config: ' . $e->getMessage());
  130. }
  131. return json_encode($taskConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  132. }
  133. /**
  134. * 导出任务条件配置表
  135. *
  136. * @return string
  137. */
  138. public function exportTaskConditionConfig()
  139. {
  140. $conditionConfig = [
  141. 'conditions' => [],
  142. 'condition_types' => []
  143. ];
  144. try {
  145. // 尝试获取条件数据
  146. if (Schema::hasTable('task_conditions')) {
  147. $conditions = TaskCondition::all();
  148. foreach ($conditions as $condition) {
  149. $conditionConfig['conditions'][] = [
  150. 'id' => $condition->id,
  151. 'task_id' => $condition->task_id,
  152. 'condition_type_id' => $condition->condition_type_id,
  153. 'param1' => $condition->param1,
  154. 'param2' => $condition->param2,
  155. 'target_value' => $condition->target_value,
  156. 'is_prerequisite' => $condition->is_prerequisite,
  157. 'extra_data' => $condition->extra_data,
  158. 'sort_order' => $condition->sort_order
  159. ];
  160. }
  161. }
  162. // 尝试获取条件类型数据
  163. if (Schema::hasTable('task_condition_types')) {
  164. $conditionTypes = TaskConditionType::all();
  165. foreach ($conditionTypes as $type) {
  166. $conditionConfig['condition_types'][] = [
  167. 'id' => $type->id,
  168. 'name' => $type->name,
  169. 'code' => $type->code,
  170. 'description' => $type->description
  171. ];
  172. }
  173. }
  174. } catch (\Exception $e) {
  175. Log::warning('Error exporting task condition config: ' . $e->getMessage());
  176. }
  177. return json_encode($conditionConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  178. }
  179. /**
  180. * 导出任务奖励配置表
  181. *
  182. * @return string
  183. */
  184. public function exportTaskRewardConfig()
  185. {
  186. $rewardConfig = [
  187. 'rewards' => []
  188. ];
  189. try {
  190. // 尝试获取奖励数据
  191. if (Schema::hasTable('task_rewards')) {
  192. $rewards = TaskReward::all();
  193. foreach ($rewards as $reward) {
  194. $rewardConfig['rewards'][] = [
  195. 'id' => $reward->id,
  196. 'task_id' => $reward->task_id,
  197. 'reward_type' => $reward->reward_type,
  198. 'reward_param1' => $reward->reward_param1,
  199. 'reward_param2' => $reward->reward_param2,
  200. 'quantity' => $reward->quantity,
  201. 'extra_data' => $reward->extra_data,
  202. 'sort_order' => $reward->sort_order
  203. ];
  204. }
  205. }
  206. } catch (\Exception $e) {
  207. Log::warning('Error exporting task reward config: ' . $e->getMessage());
  208. }
  209. return json_encode($rewardConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  210. }
  211. /**
  212. * 导出任务消耗配置表
  213. *
  214. * @return string
  215. */
  216. public function exportTaskCostConfig()
  217. {
  218. $costConfig = [
  219. 'costs' => []
  220. ];
  221. try {
  222. // 尝试获取消耗数据
  223. if (Schema::hasTable('task_costs')) {
  224. $costs = TaskCost::all();
  225. foreach ($costs as $cost) {
  226. $costConfig['costs'][] = [
  227. 'id' => $cost->id,
  228. 'task_id' => $cost->task_id,
  229. 'cost_type' => $cost->cost_type,
  230. 'cost_param1' => $cost->cost_param1,
  231. 'cost_param2' => $cost->cost_param2,
  232. 'quantity' => $cost->quantity,
  233. 'extra_data' => $cost->extra_data,
  234. 'sort_order' => $cost->sort_order
  235. ];
  236. }
  237. }
  238. } catch (\Exception $e) {
  239. Log::warning('Error exporting task cost config: ' . $e->getMessage());
  240. }
  241. return json_encode($costConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  242. }
  243. }