JisuGame.php 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\JisuGameplay;
  6. use App\Services\LotteryOdds;
  7. use DomainException;
  8. class JisuGame extends Controller
  9. {
  10. public function list()
  11. {
  12. $input = request()->validate($this->scopeRules());
  13. try {
  14. $result = LotteryOdds::list($this->key($input), $input['odds_template_id'] ?? null);
  15. if (!isset($input['game'])) {
  16. // The old Jisu screen expects gameplay at the parent and type at the leaf.
  17. $result['data'] = array_map(function ($node) use ($input) {
  18. unset($node['name']);
  19. foreach ($node['children'] as &$item) {
  20. $item['type'] = (int) $input['type'];
  21. }
  22. return $node;
  23. }, $result['data']);
  24. }
  25. return $this->success($result);
  26. } catch (DomainException $e) {
  27. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  28. }
  29. }
  30. public function saveTemplate()
  31. {
  32. $input = request()->validate(array_merge($this->scopeRules(), [
  33. 'game' => ['required', 'in:jisu,lhc,canada28,jisu28'],
  34. 'names' => ['required_without:odds_template_id', 'array:zh,en,vi'],
  35. 'names.zh' => ['required_with:names', 'string', 'max:50'],
  36. 'names.en' => ['nullable', 'string', 'max:50'],
  37. 'names.vi' => ['nullable', 'string', 'max:50'],
  38. 'enabled' => ['sometimes', 'boolean'],
  39. 'make_default' => ['sometimes', 'boolean'],
  40. 'sort' => ['sometimes', 'integer', 'min:0', 'max:2147483647'],
  41. ]));
  42. try {
  43. return $this->success(LotteryOdds::saveTemplate($this->key($input), $input));
  44. } catch (DomainException $e) {
  45. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  46. }
  47. }
  48. public function setOdds()
  49. {
  50. $pc28 = in_array(request()->input('game'), ['canada28', 'jisu28'], true);
  51. $input = request()->validate(array_merge($this->scopeRules(), [
  52. 'odds_template_id' => ['required_with:game', 'nullable', 'integer', 'min:1'],
  53. 'id' => ['nullable', 'integer', 'min:1'],
  54. 'group_game' => ['nullable', 'string', 'min:1', 'max:100'],
  55. 'gameplay' => ['nullable', 'string', 'min:1', 'max:100'],
  56. 'odds' => ['required', 'regex:' . ($pc28 ? '/^\d{1,8}(\.\d{1,2})?$/' : '/^\d+(\.\d+)?$/')],
  57. 'mininum' => ['required', 'integer', 'min:0'],
  58. 'maxinum' => ['required', 'integer', 'gte:mininum'],
  59. ]));
  60. try {
  61. if (strlen((string) $input['odds']) > 10) {
  62. throw new DomainException('赔率长度超过限制');
  63. }
  64. if (!isset($input['id'])) {
  65. $groupField = ($input['game'] ?? '') === 'lhc' ? 'group_game' : 'gameplay';
  66. if (!isset($input[$groupField])) {
  67. throw new DomainException('请选择投注项或玩法分组');
  68. }
  69. }
  70. if (!isset($input['game']) && !isset($input['type']) && isset($input['id'])) {
  71. $input['type'] ??= JisuGameplay::whereKey($input['id'])->value('type');
  72. }
  73. LotteryOdds::setOdds($this->key($input), $input['odds_template_id'] ?? null, $input);
  74. return $this->success();
  75. } catch (DomainException $e) {
  76. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  77. }
  78. }
  79. private function scopeRules(): array
  80. {
  81. return [
  82. 'game' => ['nullable', 'in:jisu,lhc,canada28,jisu28'],
  83. 'type' => ['nullable', 'integer', 'min:1'],
  84. 'odds_template_id' => ['nullable', 'integer', 'min:1'],
  85. ];
  86. }
  87. private function key(array $input): string
  88. {
  89. return LotteryOdds::gameKey($input['game'] ?? 'jisu', isset($input['type']) ? (int) $input['type'] : null);
  90. }
  91. }