SimulationTest.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\Simulation as SimulationController;
  4. use App\Models\User;
  5. use App\Services\LotteryOdds;
  6. use App\Services\Simulation\SimulationBetService;
  7. use App\Services\Simulation\SimulationFundService;
  8. use App\Services\Simulation\SimulationMemberService;
  9. use App\Services\Simulation\SimulationResetService;
  10. use App\Services\Simulation\SimulationRunner;
  11. use App\Services\Simulation\SimulationSettings;
  12. use App\Services\Simulation\SimulationTaskService;
  13. use App\Services\UserService;
  14. use App\Support\MemberAudience;
  15. use App\Support\StatsScope;
  16. use Illuminate\Config\Repository;
  17. use Illuminate\Database\Capsule\Manager;
  18. use Illuminate\Database\Schema\Blueprint;
  19. use Illuminate\Events\Dispatcher;
  20. use Illuminate\Foundation\Application;
  21. use Illuminate\Http\Request;
  22. use Illuminate\Support\Facades\DB;
  23. use Illuminate\Support\Facades\Facade;
  24. use Illuminate\Support\Facades\Schema;
  25. use PHPUnit\Framework\TestCase;
  26. /**
  27. * 模拟管理:模拟会员的充值、下注、结算、提现写真实业务表,但正式口径的统计和列表完全看不到;
  28. * 模拟口径只看到模拟会员;自动任务能跑完并可重复调度;重置只删模拟数据。
  29. */
  30. class SimulationTest extends TestCase
  31. {
  32. private Application $app;
  33. protected function setUp(): void
  34. {
  35. parent::setUp();
  36. Facade::clearResolvedInstances();
  37. $this->app = $app = new Application(dirname(__DIR__, 2));
  38. $app->instance('config', new Repository(['app' => ['timezone' => 'Asia/Shanghai', 'locale' => 'zh'], 'admin_realtime' => ['enabled' => false],
  39. 'cache' => ['default' => 'array', 'stores' => ['array' => ['driver' => 'array']], 'prefix' => '']]));
  40. $app->instance('files', new \Illuminate\Filesystem\Filesystem());
  41. $app->instance('path.lang', dirname(__DIR__, 2) . '/lang');
  42. $app->register(\Illuminate\Translation\TranslationServiceProvider::class);
  43. $app->register(\Illuminate\Validation\ValidationServiceProvider::class);
  44. $app->register(\Illuminate\Cache\CacheServiceProvider::class);
  45. $app->instance('log', new \Psr\Log\NullLogger());
  46. $app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, (new \ReflectionClass(\Illuminate\Routing\ResponseFactory::class))->newInstanceWithoutConstructor());
  47. Request::macro('validate', function (array $rules) { return validator()->validate($this->all(), $rules); });
  48. Facade::setFacadeApplication($app);
  49. $db = new Manager($app);
  50. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  51. $db->setEventDispatcher(new Dispatcher($app));
  52. $db->setAsGlobal();
  53. $db->bootEloquent();
  54. $app->instance('db.transactions', new \Illuminate\Database\DatabaseTransactionsManager());
  55. $app->instance('db', $db->getDatabaseManager());
  56. $app->bind('db.schema', fn () => $db->schema());
  57. $this->request();
  58. $this->schema();
  59. $this->seedRealMember();
  60. }
  61. protected function tearDown(): void
  62. {
  63. DB::disconnect();
  64. Facade::clearResolvedInstances();
  65. parent::tearDown();
  66. }
  67. private function request(array $input = [], string $method = 'GET'): void
  68. {
  69. $request = Request::create('/admin/simulation', $method, $input);
  70. $request->user = (object) ['id' => 1, 'username' => 'root'];
  71. $this->app->instance('request', $request);
  72. }
  73. private function schema(): void
  74. {
  75. Schema::create('users', function (Blueprint $t) {
  76. $t->id(); $t->string('member_id')->nullable(); $t->string('user_id')->nullable(); $t->string('account')->nullable();
  77. $t->string('first_name')->nullable(); $t->string('username')->default(''); $t->string('from', 20)->nullable()->default('-1');
  78. $t->unsignedBigInteger('agent_id')->nullable(); $t->string('agent_user_code')->nullable(); $t->string('user_code')->nullable();
  79. $t->integer('is_banned')->default(0); $t->integer('status')->default(0); $t->integer('level')->default(0);
  80. $t->string('register_ip')->default(''); $t->string('register_domain')->default(''); $t->integer('last_active_time')->default(0);
  81. $t->string('language')->default('zh'); $t->integer('recharge_channel_group_id')->default(1); $t->string('admin_note')->default('');
  82. $t->timestamps();
  83. });
  84. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_11_120000_add_user_type_to_users.php')->up();
  85. Schema::create('wallets', function (Blueprint $t) {
  86. $t->id(); $t->unsignedBigInteger('user_id')->default(0); $t->string('member_id'); $t->string('coin')->nullable(); $t->string('net')->nullable();
  87. $t->decimal('available_balance', 30, 10)->default(0); $t->decimal('frozen_balance', 30, 10)->default(0); $t->timestamps();
  88. });
  89. Schema::create('balance_logs', function (Blueprint $t) {
  90. $t->id(); $t->string('member_id'); $t->decimal('amount', 30, 10); $t->decimal('before_balance', 30, 10)->default(0);
  91. $t->decimal('after_balance', 30, 10)->default(0); $t->string('change_type'); $t->string('related_id')->nullable(); $t->string('remark')->nullable();
  92. $t->integer('type')->default(1); $t->string('room_id')->nullable(); $t->timestamps();
  93. });
  94. Schema::create('payment_orders', function (Blueprint $t) {
  95. $t->id(); $t->integer('type'); $t->string('order_no')->nullable(); $t->string('member_id'); $t->decimal('amount', 24, 10);
  96. $t->string('channel')->nullable(); $t->string('bank_name')->nullable(); $t->string('account')->nullable(); $t->string('card_no')->nullable();
  97. $t->integer('status'); $t->integer('state')->nullable(); $t->text('callback_data')->nullable(); $t->string('remark')->nullable();
  98. $t->decimal('fee', 24, 8)->default(0); $t->decimal('fee_rate', 12, 6)->default(0); $t->decimal('fee_fixed', 12, 4)->default(0);
  99. $t->decimal('frozen_amount', 24, 10)->default(0); $t->text('recharge_bonus_snapshot')->nullable(); $t->timestamps();
  100. });
  101. Schema::create('level', function (Blueprint $t) { $t->id(); $t->integer('level'); $t->decimal('recharge', 20, 2); });
  102. Schema::create('config', function (Blueprint $t) { $t->id(); $t->string('field'); $t->string('val')->nullable(); });
  103. foreach (['issues', 'pc_issues'] as $table) {
  104. Schema::create($table, function (Blueprint $t) {
  105. $t->id(); $t->string('issue_no'); $t->dateTime('start_time'); $t->dateTime('end_time'); $t->integer('status'); $t->timestamps();
  106. });
  107. }
  108. Schema::create('gameplay_rules', function (Blueprint $t) {
  109. $t->id(); $t->string('keywords'); $t->string('groups'); $t->decimal('odds', 10, 2); $t->decimal('mininum', 12, 2); $t->decimal('maxinum', 12, 2); $t->timestamps();
  110. });
  111. foreach (['jisu_gameplay', 'lhc_number'] as $table) {
  112. Schema::create($table, function (Blueprint $t) {
  113. $t->id(); $t->integer('type')->nullable(); $t->string('odds'); $t->bigInteger('mininum'); $t->bigInteger('maxinum');
  114. });
  115. }
  116. Schema::create('lhc_order', function (Blueprint $t) {
  117. $t->id(); $t->string('issue'); $t->string('ordernum'); $t->string('member_id'); $t->decimal('odds', 10, 2)->default(0);
  118. $t->decimal('amount', 22, 2); $t->decimal('total_amount', 22, 2)->default(0); $t->integer('lottery_status')->default(0);
  119. $t->decimal('win_amount', 22, 2)->default(0); $t->decimal('profit_and_loss', 22, 2)->nullable(); $t->integer('type')->default(5);
  120. $t->integer('is_faker')->default(0); $t->string('bet_source', 16)->nullable(); $t->integer('created_at')->nullable(); $t->integer('updated_at')->nullable();
  121. });
  122. Schema::create('lhc_order_item', function (Blueprint $t) {
  123. $t->id(); $t->string('ordernum'); $t->string('member_id'); $t->string('number'); $t->integer('lottery_status')->default(0);
  124. $t->timestamps();
  125. });
  126. Schema::create('sport_order', function (Blueprint $t) {
  127. $t->id(); $t->string('user_id'); $t->string('order_id'); $t->string('issue'); $t->text('detail')->nullable(); $t->integer('is_roll')->default(0);
  128. $t->decimal('amount', 22, 2); $t->decimal('cny_rate', 22, 10)->nullable(); $t->decimal('win_amount', 22, 2)->default(0);
  129. $t->decimal('return_amount', 22, 2)->default(0); $t->decimal('profit_and_loss', 22, 2)->default(0); $t->string('game_result')->nullable();
  130. $t->integer('is_faker')->default(0); $t->integer('is_win')->default(0); $t->integer('status')->default(0); $t->integer('settlement_status')->default(0);
  131. $t->integer('pay_status')->default(0); $t->integer('pay_time')->nullable(); $t->string('odd_name')->nullable(); $t->decimal('odd', 12, 6)->nullable();
  132. $t->string('odd_value')->nullable(); $t->string('remark')->nullable(); $t->integer('create_time')->default(0); $t->integer('update_time')->default(0);
  133. $t->integer('settlement_time')->nullable();
  134. });
  135. Schema::create('third_game_orders', function (Blueprint $t) {
  136. $t->id(); $t->string('order_key', 64)->unique(); $t->unsignedBigInteger('user_id')->nullable(); $t->string('member_id')->nullable();
  137. $t->string('username')->default(''); $t->string('first_name')->default(''); $t->string('player_id'); $t->string('platform');
  138. $t->string('currency')->default(''); $t->integer('game_type'); $t->string('game_name')->default(''); $t->string('round_no')->default('');
  139. $t->string('table_no')->default(''); $t->string('seat')->default(''); $t->decimal('bet_amount', 30, 10)->default(0);
  140. $t->decimal('valid_amount', 30, 10)->default(0); $t->decimal('settled_amount', 30, 10)->default(0); $t->text('bet_content')->nullable();
  141. $t->integer('status'); $t->string('game_order_id'); $t->dateTime('bet_time')->nullable(); $t->dateTime('last_update_time')->nullable(); $t->timestamps();
  142. });
  143. Schema::create('user_login', function (Blueprint $t) {
  144. $t->id(); $t->string('user_id')->nullable(); $t->string('login_account')->default(''); $t->string('login_ip')->default('');
  145. $t->string('login_domain')->default(''); $t->string('platform')->default(''); $t->integer('status')->default(1);
  146. $t->dateTime('logout_time')->nullable(); $t->integer('online_duration')->default(0); $t->timestamps();
  147. });
  148. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_11_130000_create_simulation_tables.php')->up();
  149. DB::table('config')->insert([['field' => 'pc28_switch', 'val' => '0'], ['field' => 'recharge_rate', 'val' => '0']]);
  150. DB::table('config')->where('field', 'simulation_enabled')->update(['val' => '1']);
  151. DB::table('issues')->insert(['issue_no' => '3300001', 'start_time' => now(), 'end_time' => now()->addMinutes(5), 'status' => 1]);
  152. DB::table('gameplay_rules')->insert(['keywords' => '大', 'groups' => '大小单双', 'odds' => 2, 'mininum' => 1, 'maxinum' => 100000]);
  153. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_11_120000_create_lottery_odds_templates.php')->up();
  154. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_11_160000_add_sort_to_lottery_odds_templates.php')->up();
  155. }
  156. /** 一个真实会员和一个前台游客,各有充值、投注、订单。 */
  157. private function seedRealMember(): void
  158. {
  159. foreach ([['r1', '1', 0], ['g1', '2', 1]] as $i => [$member, $from, $type]) {
  160. DB::table('users')->insert(['member_id' => $member, 'user_id' => $member, 'account' => $member, 'first_name' => $member, 'from' => $from,
  161. 'user_type' => $type, 'created_at' => now(), 'updated_at' => now()]);
  162. DB::table('wallets')->insert(['user_id' => $i + 1, 'member_id' => $member, 'available_balance' => 90]);
  163. DB::table('balance_logs')->insert([
  164. ['member_id' => $member, 'amount' => 100, 'change_type' => '三方充值', 'created_at' => now(), 'updated_at' => now()],
  165. ['member_id' => $member, 'amount' => -10, 'change_type' => '投注', 'created_at' => now(), 'updated_at' => now()]]);
  166. DB::table('payment_orders')->insert(['type' => 1, 'member_id' => $member, 'amount' => 100, 'status' => 2, 'created_at' => now(), 'updated_at' => now()]);
  167. DB::table('lhc_order')->insert(['issue' => '3300001', 'ordernum' => 'o' . $member, 'member_id' => $member, 'amount' => 10, 'total_amount' => 10]);
  168. }
  169. }
  170. /** 正式口径下各处看到的数字。模拟数据进来前后必须完全一样。 */
  171. private function realView(): array
  172. {
  173. return [
  174. 'stats_deposit' => (float) StatsScope::excludeTestMembers(DB::table('balance_logs'))->where('change_type', '三方充值')->sum('amount'),
  175. 'stats_bet' => (float) StatsScope::excludeTestMembers(DB::table('balance_logs'))->where('change_type', 'like', '%投注')->sum('amount'),
  176. 'stats_wallet' => (float) StatsScope::excludeTestMembers(DB::table('wallets'))->sum('available_balance'),
  177. 'stats_orders' => StatsScope::excludeTestMembers(DB::table('payment_orders'))->count(),
  178. 'real_users' => StatsScope::realUsers(DB::table('users'))->pluck('member_id')->all(),
  179. 'guest_users' => StatsScope::guestUsers(DB::table('users'))->pluck('member_id')->all(),
  180. 'raw_users' => array_column(DB::select('SELECT u.member_id FROM bot_users u WHERE 1 = 1' . StatsScope::rawExcludeTestUsers('u')), 'member_id'),
  181. 'list_users' => MemberAudience::visibleUsers(DB::table('users'))->orderBy('id')->pluck('member_id')->all(),
  182. 'list_ledger' => MemberAudience::visibleUsers(DB::table('balance_logs')->join('users', 'users.member_id', '=', 'balance_logs.member_id'))->count(),
  183. 'list_orders' => MemberAudience::visibleMembers(DB::table('payment_orders'))->count(),
  184. 'list_lottery' => MemberAudience::visibleMembers(DB::table('lhc_order'), 'lhc_order.member_id')->count(),
  185. 'list_sport' => MemberAudience::visibleMembers(DB::table('sport_order'), 'user_id', 'user_id')->count(),
  186. 'list_logins' => MemberAudience::visibleMembers(DB::table('user_login'), 'user_id', 'user_id')->count(),
  187. 'list_third' => MemberAudience::visibleMembers(DB::table('third_game_orders'), 'third_game_orders.user_id', 'id')->count(),
  188. ];
  189. }
  190. private function wallet(string $memberId): array
  191. {
  192. $w = DB::table('wallets')->where('member_id', $memberId)->first();
  193. return [(float) $w->available_balance, (float) $w->frozen_balance];
  194. }
  195. public function test_lottery_options_and_simulated_bets_use_the_active_games_default_template(): void
  196. {
  197. [$member] = array_column(SimulationMemberService::create(1, ['initial_balance' => 1000]), 'member_id');
  198. $ledgerCount = DB::table('balance_logs')->count();
  199. LotteryOdds::setOdds('canada28', null, ['id' => 1, 'odds' => '0', 'mininum' => 1, 'maxinum' => 100]);
  200. $this->assertSame([], SimulationBetService::options('canada28')['lottery']['rules']);
  201. try {
  202. SimulationBetService::lottery($member, '大', 10);
  203. $this->fail('The active default template has disabled this play');
  204. } catch (\Exception $e) {
  205. $this->assertStringContainsString('禁止投注', $e->getMessage());
  206. }
  207. $this->assertSame([1000.0, 0.0], $this->wallet($member));
  208. $this->assertSame($ledgerCount, DB::table('balance_logs')->count());
  209. $this->assertSame(0, DB::table('lhc_order')->where('member_id', $member)->count());
  210. DB::table('config')->where('field', 'pc28_switch')->update(['val' => '1']);
  211. DB::table('pc_issues')->insert(['issue_no' => '2200001', 'start_time' => now(), 'end_time' => now()->addMinutes(5), 'status' => 1]);
  212. LotteryOdds::setOdds('jisu28', null, ['id' => 1, 'odds' => '3.25', 'mininum' => 10, 'maxinum' => 20]);
  213. $rules = SimulationBetService::options('jisu28')['lottery']['rules'];
  214. $this->assertSame(['id' => null, 'keywords' => '大', 'odds' => '3.25', 'mininum' => 10, 'maxinum' => 20], $rules[0]);
  215. try {
  216. SimulationBetService::lottery($member, '大', 21);
  217. $this->fail('The selected template caps the stake at 20');
  218. } catch (\Exception $e) {
  219. $this->assertStringContainsString('超过该玩法最大金额', $e->getMessage());
  220. }
  221. $order = SimulationBetService::lottery($member, '大', 20);
  222. $template = LotteryOdds::template('jisu28');
  223. $this->assertEquals(['2200001', 6, '3.25', $template->id, $template->revision],
  224. [$order->issue, $order->type, $order->odds, $order->odds_template_id, $order->odds_template_revision]);
  225. $this->assertSame([980.0, 0.0], $this->wallet($member));
  226. }
  227. public function test_simulated_money_and_bets_use_real_tables_but_stay_out_of_the_real_view(): void
  228. {
  229. $before = $this->realView();
  230. $this->assertSame(100.0, $before['stats_deposit'], 'guest deposits are not counted either');
  231. $this->assertSame(['r1'], $before['real_users']);
  232. $this->assertSame(['g1'], $before['guest_users']);
  233. $this->assertSame(['r1', 'g1'], $before['list_users'], 'lists still show guests');
  234. [$a, $b] = array_column(SimulationMemberService::create(2, ['initial_balance' => 1000]), 'member_id');
  235. $user = DB::table('users')->where('member_id', $a)->first();
  236. // 账号、昵称、IP 都是随机的真实风格,不带“模拟 / sim”字样
  237. foreach (DB::table('users')->where('user_type', 2)->get() as $created) {
  238. $this->assertMatchesRegularExpression('/^[a-z][a-z0-9]{5,15}$/', $created->account);
  239. $this->assertStringNotContainsString('sim', $created->account);
  240. $this->assertStringNotContainsString('模拟', $created->first_name);
  241. $this->assertNotSame('', $created->first_name);
  242. $this->assertNotSame('127.0.0.1', $created->register_ip);
  243. $this->assertNotFalse(filter_var($created->register_ip, FILTER_VALIDATE_IP));
  244. }
  245. $this->assertSame(2, DB::table('users')->where('user_type', 2)->distinct()->count('account'));
  246. $this->assertSame(['2', 2, null], [(string) $user->from, (int) $user->user_type, $user->agent_id]);
  247. $this->assertSame([1000.0, 0.0], $this->wallet($a));
  248. // 充值:三方成功、公司入款成功、失败的不入账
  249. $paid = SimulationFundService::recharge($a, 500, 'third');
  250. $this->assertSame([2, 1], [(int) $paid->status, $paid->state], 'state is an integer column in production');
  251. $this->assertSame(2, (int) SimulationFundService::recharge($a, 200, 'company')->status);
  252. $this->assertSame(3, (int) SimulationFundService::recharge($a, 999, 'third', 'fail')->status);
  253. $this->assertSame(1, (int) SimulationFundService::recharge($a, 50, 'third', 'pending')->status);
  254. $this->assertSame(1700.0, $this->wallet($a)[0]);
  255. // 彩票下在真实期号上;体育按给定结果结算
  256. $lottery = SimulationBetService::lottery($a, '大', 100);
  257. $this->assertSame(['3300001', 'simulation', 0], [$lottery->issue, $lottery->bet_source, (int) $lottery->fresh()->lottery_status]);
  258. $win = SimulationBetService::sport($a, 50, 2);
  259. $this->assertSame('100.00', SimulationBetService::settleSport($win->id, 'win')->win_amount);
  260. $half = SimulationBetService::sport($a, 40, 1.9);
  261. $settled = SimulationBetService::settleSport($half->id, 'half');
  262. $this->assertSame(['20.00', '-20.00'], [$settled->return_amount, $settled->profit_and_loss]);
  263. try { SimulationBetService::settleSport($half->id, 'win'); $this->fail('an order settles once'); }
  264. catch (\Exception $e) { $this->assertStringContainsString('已结算', $e->getMessage()); }
  265. $this->assertSame(1630.0, $this->wallet($a)[0], '1700 - 100 - 50 + 100 - 40 + 20');
  266. $this->assertSame($before, $this->realView());
  267. }
  268. public function test_full_flow_isolation_runner_and_reset(): void
  269. {
  270. $before = $this->realView();
  271. [$a] = array_column(SimulationMemberService::create(1, ['initial_balance' => 1000]), 'member_id');
  272. SimulationFundService::recharge($a, 500, 'third');
  273. SimulationFundService::recharge($a, 200, 'company');
  274. SimulationBetService::lottery($a, '大', 100);
  275. $win = SimulationBetService::sport($a, 50, 2);
  276. SimulationBetService::settleSport($win->id, 'win');
  277. $draw = SimulationBetService::sport($a, 30, 2);
  278. SimulationBetService::settleSport($draw->id, 'draw');
  279. $lose = SimulationBetService::sport($a, 20, 2);
  280. SimulationBetService::settleSport($lose->id, 'lose');
  281. $cancelled = SimulationBetService::sport($a, 10, 2);
  282. SimulationBetService::cancelSport($cancelled->id);
  283. $approved = SimulationFundService::withdrawApply($a, 300);
  284. SimulationFundService::withdrawAudit($approved->id, true);
  285. $rejected = SimulationFundService::withdrawApply($a, 70);
  286. SimulationFundService::withdrawAudit($rejected->id, false, '测试拒绝');
  287. SimulationFundService::adjust($a, 5, 'debit', '扣款');
  288. SimulationMemberService::login($a);
  289. // 1000 + 500 + 200 - 100 - 50 + 100 - 30 + 30 - 20 - 10 + 10 - 300 - 70 + 70 - 5
  290. $this->assertSame([1325.0, 0.0], $this->wallet($a));
  291. $this->assertSame([2, 3], [(int) $approved->fresh()->status, (int) $rejected->fresh()->status]);
  292. $this->assertSame($before, $this->realView(), 'nothing the real back office reads has changed');
  293. $sim = MemberAudience::simulated(fn () => [
  294. 'deposit' => (float) StatsScope::excludeTestMembers(DB::table('balance_logs'))->whereIn('change_type', ['三方充值', '充值'])->sum('amount'),
  295. 'bet' => (float) StatsScope::excludeTestMembers(DB::table('balance_logs'))->where('change_type', 'like', '%投注')->sum('amount'),
  296. 'users' => StatsScope::realUsers(DB::table('users'))->pluck('member_id')->all(),
  297. 'raw' => array_column(DB::select('SELECT u.member_id FROM bot_users u WHERE 1 = 1' . StatsScope::rawExcludeTestUsers('u')), 'member_id'),
  298. 'guests' => StatsScope::guestUsers(DB::table('users'))->count(),
  299. 'list' => MemberAudience::visibleUsers(DB::table('users'))->pluck('member_id')->all(),
  300. 'sport' => MemberAudience::visibleMembers(DB::table('sport_order'), 'user_id', 'user_id')->count(),
  301. ]);
  302. $this->assertSame(['deposit' => 700.0, 'bet' => -210.0, 'users' => [$a], 'raw' => [$a], 'guests' => 0, 'list' => [$a], 'sport' => 4], $sim);
  303. $this->assertFalse(MemberAudience::isSimulated(), 'audience is restored');
  304. // 概览接口与手算一致
  305. $this->request();
  306. $overview = (new SimulationController())->overview()->getData(true);
  307. $this->assertSame(0, $overview['code'], $overview['msg']);
  308. $this->assertSame(['1', '1'], [(string) $overview['data']['members']['total'], (string) $overview['data']['members']['online']]);
  309. $this->assertSame(['700.00', '1000.00', '5.00', '300.00', '1325.00'], [$overview['data']['funds']['recharge'], $overview['data']['funds']['manual_credit'],
  310. $overview['data']['funds']['manual_debit'], $overview['data']['funds']['withdraw'], $overview['data']['funds']['wallet_available']]);
  311. $this->assertSame(['210.00', '140.00', 1], [$overview['data']['bets']['bet_amount'], $overview['data']['bets']['payout'], $overview['data']['bets']['unsettled']]);
  312. // 自动任务:注册 2 人 → 登录 → 充值 → 体育下注 2 次 → 结算 → 提现 → 退出
  313. $steps = [['type' => 'register', 'interval' => 0], ['type' => 'login', 'interval' => 0], ['type' => 'recharge', 'amount' => 100, 'interval' => 0],
  314. ['type' => 'bet', 'category' => 'sport', 'amount' => 10, 'count' => 2, 'interval' => 0], ['type' => 'settle', 'settle_mode' => 'lose', 'interval' => 0],
  315. ['type' => 'withdraw', 'amount' => 30, 'interval' => 0], ['type' => 'logout', 'interval' => 0]];
  316. $runId = SimulationTaskService::start(['name' => '完整流程', 'steps' => $steps, 'member_count' => 2], 1);
  317. $paused = SimulationTaskService::setStatus($runId, 'pause');
  318. $this->assertSame('paused', $paused->status);
  319. $this->assertSame(0, SimulationRunner::tick(), 'a paused run does nothing');
  320. SimulationTaskService::setStatus($runId, 'resume');
  321. for ($i = 0; $i < 60 && DB::table('simulation_task_runs')->where('id', $runId)->value('status') === 'running'; $i++) SimulationRunner::tick();
  322. $run = DB::table('simulation_task_runs')->where('id', $runId)->first();
  323. $this->assertSame(['completed', 16, 16, 0], [$run->status, (int) $run->total, (int) $run->completed, (int) $run->failed], (string) $run->last_error);
  324. $this->assertSame(16, DB::table('simulation_task_events')->where('run_id', $runId)->where('status', 'ok')->count());
  325. $this->assertSame(0, SimulationRunner::tick(), 'a finished run is not executed again');
  326. foreach (json_decode($run->member_ids, true) as $memberId) $this->assertSame([50.0, 0.0], $this->wallet($memberId), '100 - 10 - 10 - 30');
  327. $this->assertSame(3, DB::table('users')->where('user_type', 2)->count());
  328. $this->assertSame($before, $this->realView(), 'the task run did not leak either');
  329. // 第三方游戏:只在本地生成已完成的注单,输赢净额进出钱包
  330. $balance = $this->wallet($a)[0];
  331. $lost = SimulationBetService::third($a, 40, 2, 'lose');
  332. $this->assertSame([1, 2, 'SIM', '-40.0000000000', 64], [(int) $lost->status, (int) $lost->game_type, $lost->platform, (string) $lost->settled_amount, strlen($lost->order_key)]);
  333. $won = SimulationBetService::third($a, 40, null, 'win');
  334. $this->assertGreaterThanOrEqual(20, (float) $won->settled_amount);
  335. $this->assertEqualsWithDelta($balance - 40 + (float) $won->settled_amount, $this->wallet($a)[0], 0.001);
  336. $this->assertSame(2, DB::table('balance_logs')->where('member_id', $a)->where('change_type', '资产转移')->count());
  337. try { SimulationBetService::third('r1', 10); $this->fail('real members are refused'); }
  338. catch (\Exception $e) { $this->assertSame('只能操作模拟会员', $e->getMessage()); }
  339. $this->assertSame($before, $this->realView());
  340. // 混合下注:体育、彩票、第三方游戏都会下
  341. $mixId = SimulationTaskService::start(['name' => '混合', 'member_ids' => [$a], 'steps' => [['type' => 'bet', 'category' => 'mixed', 'amount' => 1, 'count' => 30, 'interval' => 0]]], 1);
  342. for ($i = 0; $i < 80 && in_array(DB::table('simulation_task_runs')->where('id', $mixId)->value('status'), ['queued', 'running'], true); $i++) SimulationRunner::tick();
  343. $mix = DB::table('simulation_task_runs')->where('id', $mixId)->first();
  344. $this->assertSame(['completed', 30, 0], [$mix->status, (int) $mix->completed, (int) $mix->failed], (string) $mix->last_error);
  345. $kinds = DB::table('simulation_task_events')->where('run_id', $mixId)->distinct()->orderBy('ref_type')->pluck('ref_type')->all();
  346. $this->assertSame(['lhc_order', 'sport_order', 'third_game_orders'], $kinds);
  347. // 彩票没有可下注期号时,混合下注改下其他分类而不是失败
  348. DB::table('issues')->update(['status' => 2]);
  349. $closedId = SimulationTaskService::start(['name' => '封盘', 'member_ids' => [$a], 'steps' => [['type' => 'bet', 'category' => 'mixed', 'amount' => 1, 'count' => 12, 'interval' => 0]]], 1);
  350. for ($i = 0; $i < 40 && in_array(DB::table('simulation_task_runs')->where('id', $closedId)->value('status'), ['queued', 'running'], true); $i++) SimulationRunner::tick();
  351. $this->assertSame([12, 0, 0], [(int) DB::table('simulation_task_runs')->where('id', $closedId)->value('completed'), (int) DB::table('simulation_task_runs')->where('id', $closedId)->value('failed'),
  352. DB::table('simulation_task_events')->where('run_id', $closedId)->where('ref_type', 'lhc_order')->count()]);
  353. DB::table('issues')->update(['status' => 1]);
  354. $this->assertSame($before, $this->realView());
  355. // 一键启动:按模拟设置的默认任务参数跑完整流程
  356. $this->assertSame('mixed', SimulationSettings::get('simulation_task_bet_category'), 'the default task bets on every category');
  357. // 这里用体育:彩票要等真实开奖,测试里不会开奖
  358. SimulationSettings::save(['simulation_task_bet_category' => 'sport', 'simulation_task_members' => 2, 'simulation_task_recharge' => 500, 'simulation_task_bet_amount' => 50,
  359. 'simulation_task_bet_count' => 2, 'simulation_task_withdraw' => 100, 'simulation_task_interval' => 0,
  360. // 金额区间:充值 500~900,下注 50~80,提现 100~150
  361. 'simulation_task_recharge_max' => 900, 'simulation_task_bet_amount_max' => 80, 'simulation_task_withdraw_max' => 150]);
  362. $quickId = SimulationTaskService::quickStart(1);
  363. for ($i = 0; $i < 60 && in_array(DB::table('simulation_task_runs')->where('id', $quickId)->value('status'), ['queued', 'running'], true); $i++) SimulationRunner::tick();
  364. $quick = DB::table('simulation_task_runs')->where('id', $quickId)->first();
  365. $this->assertSame(['completed', 16, 16, 0], [$quick->status, (int) $quick->total, (int) $quick->completed, (int) $quick->failed], (string) $quick->last_error);
  366. $this->assertSame(['register', 'login', 'recharge', 'bet', 'settle', 'withdraw', 'logout'], array_column(json_decode($quick->steps, true), 'type'));
  367. // 每次动作的金额都落在各自的区间内,并且是整数;同一步骤不是每次都一样
  368. $amounts = fn (string $action) => DB::table('simulation_task_events')->where('run_id', $quickId)->where('action', $action)->pluck('amount')->map(fn ($a) => (float) $a)->all();
  369. foreach (['recharge' => [500, 900], 'bet' => [50, 80], 'withdraw' => [100, 150]] as $action => [$min, $max]) {
  370. foreach ($amounts($action) as $amount) {
  371. $this->assertTrue($amount >= $min && $amount <= $max && floor($amount) === $amount, "$action $amount is within $min-$max");
  372. }
  373. }
  374. $this->assertSame([2, 4, 2], [count($amounts('recharge')), count($amounts('bet')), count($amounts('withdraw'))]);
  375. $this->assertSame((float) DB::table('payment_orders')->whereIn('member_id', json_decode($quick->member_ids, true))->where('type', 1)->sum('amount'), array_sum($amounts('recharge')));
  376. try { SimulationSettings::save(['simulation_task_bet_amount' => 90]); $this->fail('max below min'); }
  377. catch (\Exception $e) { $this->assertStringContainsString('最大值不能小于最小值', $e->getMessage()); }
  378. $this->assertSame('50', SimulationSettings::get('simulation_task_bet_amount'));
  379. // 余额不够区间最小值:跳过这一次,不算失败,也不会把整轮拖成失败
  380. $poor = SimulationMemberService::create(1)[0]['member_id'];
  381. $skipId = SimulationTaskService::start(['name' => '余额不足', 'member_ids' => [$poor], 'steps' => [
  382. ['type' => 'bet', 'category' => 'sport', 'amount' => 50, 'amount_max' => 80, 'count' => 3, 'interval' => 0],
  383. ['type' => 'withdraw', 'amount' => 100, 'amount_max' => 150, 'interval' => 0]]], 1);
  384. for ($i = 0; $i < 20 && in_array(DB::table('simulation_task_runs')->where('id', $skipId)->value('status'), ['queued', 'running'], true); $i++) SimulationRunner::tick();
  385. $skip = DB::table('simulation_task_runs')->where('id', $skipId)->first();
  386. $this->assertSame(['completed', 4, 0, 0], [$skip->status, (int) $skip->completed, (int) $skip->failed, DB::table('sport_order')->where('user_id', $poor)->count()]);
  387. $this->assertStringContainsString('本次跳过', DB::table('simulation_task_events')->where('run_id', $skipId)->value('message'));
  388. DB::table('users')->where('member_id', $poor)->delete();
  389. DB::table('wallets')->where('member_id', $poor)->delete();
  390. SimulationSettings::save(['simulation_task_recharge_max' => 20000]);
  391. try { SimulationTaskService::quickStart(1); $this->fail('defaults above the amount cap are refused'); }
  392. catch (\Exception $e) { $this->assertStringContainsString('单次金额上限', $e->getMessage()); }
  393. $this->assertSame($before, $this->realView());
  394. // 持续运行:日期区间内一轮接一轮;区间外、间隔未到、上一轮没结束时都不启动
  395. SimulationSettings::save(['simulation_task_recharge_max' => 900]);
  396. try { SimulationSettings::save(['simulation_schedule_enabled' => 1, 'simulation_schedule_start' => '', 'simulation_schedule_end' => '']); $this->fail('dates are required'); }
  397. catch (\Exception $e) { $this->assertStringContainsString('开始和结束日期', $e->getMessage()); }
  398. $this->assertSame('0', SimulationSettings::get('simulation_schedule_enabled'), 'a refused save changes nothing');
  399. $today = now()->toDateString();
  400. SimulationSettings::save(['simulation_schedule_enabled' => 1, 'simulation_schedule_start' => now()->addDay()->toDateString(), 'simulation_schedule_end' => now()->addDays(30)->toDateString(), 'simulation_schedule_gap_minutes' => 10]);
  401. $this->assertNull(SimulationTaskService::scheduleNext(), 'not started before the range');
  402. SimulationSettings::save(['simulation_schedule_start' => $today]);
  403. $runsBefore = DB::table('simulation_task_runs')->count();
  404. SimulationRunner::tick();
  405. $first = DB::table('simulation_task_runs')->orderByDesc('id')->first();
  406. $this->assertSame([$runsBefore + 1, true], [DB::table('simulation_task_runs')->count(), str_starts_with($first->name, '持续任务 ')]);
  407. $this->assertNull(SimulationTaskService::scheduleNext(), 'one round at a time');
  408. for ($i = 0; $i < 60 && DB::table('simulation_task_runs')->where('id', $first->id)->value('status') === 'running'; $i++) SimulationRunner::tick();
  409. $this->assertSame('completed', DB::table('simulation_task_runs')->where('id', $first->id)->value('status'));
  410. $this->assertNull(SimulationTaskService::scheduleNext(), 'waits for the gap after a round');
  411. \Carbon\Carbon::setTestNow(now()->addMinutes(11));
  412. // 会员数到上限后不再注册,改用已有模拟会员
  413. SimulationSettings::save(['simulation_max_members_total' => DB::table('users')->where('user_type', 2)->count()]);
  414. $second = SimulationTaskService::scheduleNext();
  415. $this->assertNotNull($second);
  416. $secondRun = DB::table('simulation_task_runs')->where('id', $second)->first();
  417. $this->assertNotContains('register', array_column(json_decode($secondRun->steps, true), 'type'));
  418. $this->assertCount(2, json_decode($secondRun->member_ids, true));
  419. \Carbon\Carbon::setTestNow(now()->addDays(40));
  420. SimulationTaskService::setStatus($second, 'stop');
  421. $this->assertNull(SimulationTaskService::scheduleNext(), 'not started after the range');
  422. \Carbon\Carbon::setTestNow();
  423. SimulationSettings::save(['simulation_schedule_enabled' => 0]);
  424. $this->assertSame($before, $this->realView());
  425. $simulatedMembers = DB::table('users')->where('user_type', 2)->count();
  426. // 重置:只删模拟数据
  427. $preview = SimulationResetService::preview();
  428. $this->assertSame([7, 7], [$simulatedMembers, $preview['counts']['users']]);
  429. try { SimulationResetService::reset('wrong', SimulationResetService::CONFIRM_TEXT); $this->fail('wrong token must be refused'); }
  430. catch (\Exception $e) { $this->assertStringContainsString('失效', $e->getMessage()); }
  431. $preview = SimulationResetService::preview();
  432. $deleted = SimulationResetService::reset($preview['confirm_token'], $preview['confirm_text']);
  433. $this->assertSame(7, $deleted['users']);
  434. $this->assertSame(0, DB::table('users')->where('user_type', 2)->count());
  435. $this->assertSame([2, 2, 4, 2, 2], [DB::table('users')->count(), DB::table('wallets')->count(), DB::table('balance_logs')->count(),
  436. DB::table('payment_orders')->count(), DB::table('lhc_order')->count()], 'real and guest rows are untouched');
  437. $this->assertSame([0, 0], [DB::table('sport_order')->count(), DB::table('user_login')->count()]);
  438. $this->assertSame($before, $this->realView());
  439. }
  440. /** 非 PC28 彩种:注单格式与前台一致(玩法编号、彩种 type、期号规则、流水类型),封盘时不能下。 */
  441. public function test_other_lotteries_bet_on_real_issues_in_the_front_end_order_format(): void
  442. {
  443. foreach (['jisu_gameplay', 'lhc_number'] as $table) Schema::table($table, function (Blueprint $t) use ($table) {
  444. $t->string('gameplay')->nullable(); $t->string('number')->nullable();
  445. if ($table === 'lhc_number') $t->string('game')->nullable();
  446. });
  447. Schema::table('lhc_order', function (Blueprint $t) { $t->string('game')->nullable(); $t->string('gameplay')->nullable(); });
  448. Schema::table('lhc_order_item', fn (Blueprint $t) => $t->integer('number_id')->nullable());
  449. foreach (['jisu_lottery', 'lhc_lottery'] as $table) Schema::create($table, function (Blueprint $t) {
  450. $t->id(); $t->string('issue'); $t->integer('type'); $t->integer('status')->default(1); $t->integer('open_time')->nullable();
  451. $t->integer('next_open_time')->nullable(); $t->string('next_issue')->nullable();
  452. });
  453. $racing = DB::table('jisu_gameplay')->insertGetId(['type' => 1, 'gameplay' => '冠军', 'number' => '大', 'odds' => '1.98', 'mininum' => 1, 'maxinum' => 1000]);
  454. $combo = DB::table('jisu_gameplay')->insertGetId(['type' => 1, 'gameplay' => '连码', 'number' => '1,2', 'odds' => '5', 'mininum' => 1, 'maxinum' => 1000]);
  455. $zodiac = DB::table('lhc_number')->insertGetId(['game' => '特码', 'gameplay' => '特码A', 'number' => '08', 'odds' => '47', 'mininum' => 1, 'maxinum' => 1000]);
  456. foreach ([['jisu:1', $racing, '1.98'], ['jisu:1', $combo, '5'], ['lhc:2', $zodiac, '47'], ['lhc:1', $zodiac, '47']] as [$key, $id, $odds]) {
  457. LotteryOdds::setOdds($key, null, ['id' => $id, 'odds' => $odds, 'mininum' => 1, 'maxinum' => 1000]);
  458. }
  459. DB::table('jisu_lottery')->insert(['issue' => '20261113001', 'type' => 1, 'status' => 1, 'open_time' => time() + 120]);
  460. DB::table('lhc_lottery')->insert([['issue' => '2026130', 'type' => 2, 'status' => 1, 'next_open_time' => time() + 3600, 'next_issue' => '2026131'],
  461. ['issue' => '2026317', 'type' => 1, 'status' => 1, 'next_open_time' => null, 'next_issue' => null]]);
  462. $before = $this->realView();
  463. [$member] = array_column(SimulationMemberService::create(1, ['initial_balance' => 1000]), 'member_id');
  464. $games = array_column(SimulationBetService::options('jisu:1')['lottery']['games'], null, 'key');
  465. $this->assertSame(['canada28', 'lhc:1', 'lhc:2', 'lhc:3', 'lhc:4', 'jisu:1', 'jisu:2', 'jisu:3', 'jisu:4', 'jisu:5', 'jisu:6', 'jisu:7'], array_keys($games));
  466. $this->assertSame([true, '20261113001', 7, false], [$games['jisu:1']['open'], $games['jisu:1']['issue'], $games['jisu:1']['type'], $games['jisu:2']['open']]);
  467. $this->assertSame('2026131', $games['lhc:2']['issue'], 'Hong Kong bets on the next issue');
  468. $rules = SimulationBetService::options('jisu:1')['lottery']['rules'];
  469. $this->assertSame([[$racing, '冠军 大']], array_map(fn ($rule) => [$rule['id'], $rule['keywords']], $rules), 'plays that need several numbers are not offered');
  470. $order = SimulationBetService::lottery($member, '', 10, 'jisu:1', $racing);
  471. $this->assertSame([7, '20261113001', '冠军', 'simulation', '1.98'], [(int) $order->type, $order->issue, $order->gameplay, $order->bet_source, (string) $order->odds]);
  472. $this->assertSame([$racing, '大'], [(int) DB::table('lhc_order_item')->where('ordernum', $order->ordernum)->value('number_id'), DB::table('lhc_order_item')->where('ordernum', $order->ordernum)->value('number')]);
  473. $hk = SimulationBetService::lottery($member, '', 20, 'lhc:2', $zodiac);
  474. $this->assertSame([2, '2026131', '特码', '特码A'], [(int) $hk->type, $hk->issue, $hk->game, $hk->gameplay]);
  475. if (date('H:i') < '21:30' || date('H:i') > '21:40') {
  476. $this->assertSame('2026318', SimulationBetService::lottery($member, '', 5, 'lhc:1', $zodiac)->issue, 'New Macau bets on the issue after the latest draw');
  477. $this->assertSame(-5.0, (float) DB::table('balance_logs')->where('member_id', $member)->where('change_type', '新澳门六合彩投注')->sum('amount'));
  478. }
  479. $this->assertSame([-10.0, -20.0], [(float) DB::table('balance_logs')->where('member_id', $member)->where('change_type', '极速赛车投注')->sum('amount'),
  480. (float) DB::table('balance_logs')->where('member_id', $member)->where('change_type', '香港六合彩投注')->sum('amount')]);
  481. // 自动任务的彩票下注在所有开盘的彩种里随机选
  482. $types = [];
  483. for ($i = 0; $i < 40; $i++) $types[(int) SimulationBetService::lotteryRandom($member, 1)->type] = true;
  484. $this->assertArrayHasKey(7, $types);
  485. $this->assertArrayHasKey(2, $types);
  486. $this->assertArrayHasKey(5, $types);
  487. // 封盘:开奖前 15 秒内、状态为封盘都不能下
  488. DB::table('jisu_lottery')->update(['open_time' => time() + 10]);
  489. try { SimulationBetService::lottery($member, '', 10, 'jisu:1', $racing); $this->fail('closed before the draw'); }
  490. catch (\Exception $e) { $this->assertStringContainsString('封盘', $e->getMessage()); }
  491. try { SimulationBetService::lottery($member, '', 10, 'jisu:2', $racing); $this->fail('no issue'); }
  492. catch (\Exception $e) { $this->assertStringContainsString('封盘', $e->getMessage()); }
  493. try { SimulationBetService::lottery($member, '', 10, 'jisu:1', $combo + 99); $this->fail('unknown play'); }
  494. catch (\Exception $e) { $this->assertStringContainsString('封盘', $e->getMessage()); }
  495. $this->assertSame($before, $this->realView());
  496. }
  497. /** 早期的模拟会员(sim_ 账号、“模拟_”昵称)原地改名:名下记录里的旧名字一起换,钱和订单不动,真实会员不动,可重复执行。 */
  498. public function test_legacy_simulated_members_are_renamed_in_place(): void
  499. {
  500. [$a, $b] = array_column(SimulationMemberService::create(2, ['initial_balance' => 1000]), 'member_id');
  501. SimulationMemberService::login($a);
  502. SimulationBetService::settleSport(SimulationBetService::sport($a, 50, 2)->id, 'win');
  503. SimulationBetService::third($a, 40, 2, 'lose');
  504. $withdraw = SimulationFundService::withdrawApply($a, 100);
  505. // 还原成旧版本创建出来的样子
  506. $user = DB::table('users')->where('member_id', $a)->first();
  507. DB::table('users')->where('member_id', $a)->update(['account' => 'sim_' . $a, 'first_name' => '模拟_1234', 'register_ip' => '127.0.0.1']);
  508. DB::table('user_login')->where('user_id', $a)->update(['login_account' => 'sim_' . $a, 'login_ip' => '127.0.0.1']);
  509. DB::table('third_game_orders')->where('user_id', $user->id)->update(['first_name' => '模拟_1234', 'game_name' => '模拟老虎机', 'bet_content' => '模拟注单']);
  510. DB::table('payment_orders')->where('id', $withdraw->id)->update(['account' => '模拟_1234', 'bank_name' => '模拟银行', 'card_no' => 'SIM' . $a]);
  511. DB::table('sport_order')->where('user_id', $a)->update(['odd_name' => '模拟主队 VS 模拟客队', 'detail' => json_encode(['simulation' => true, 'title' => '模拟主队 VS 模拟客队', 'selection' => '主胜'])]);
  512. $untouched = (array) DB::table('users')->where('member_id', $b)->first();
  513. $real = (array) DB::table('users')->where('member_id', 'r1')->first();
  514. $money = [$this->wallet($a), DB::table('balance_logs')->where('member_id', $a)->sum('amount'), DB::table('payment_orders')->where('member_id', $a)->sum('amount')];
  515. $preview = SimulationMemberService::renameLegacy(true);
  516. $this->assertSame(['members' => 1, 'accounts' => 1, 'nicknames' => 1, 'ips' => 1, 'login_logs' => 1, 'third_game_orders' => 1, 'withdraw_orders' => 1, 'sport_orders' => 1], $preview);
  517. $this->assertSame('sim_' . $a, DB::table('users')->where('member_id', $a)->value('account'), 'a dry run writes nothing');
  518. $this->assertSame($preview, SimulationMemberService::renameLegacy());
  519. $renamed = DB::table('users')->where('member_id', $a)->first();
  520. $this->assertMatchesRegularExpression('/^[a-z][a-z0-9]{5,15}$/', $renamed->account);
  521. $this->assertStringNotContainsString('模拟', $renamed->first_name);
  522. $this->assertNotSame('127.0.0.1', $renamed->register_ip);
  523. $this->assertSame([$user->id, $a, 2], [$renamed->id, $renamed->member_id, (int) $renamed->user_type], 'identity is unchanged');
  524. $login = DB::table('user_login')->where('user_id', $a)->first();
  525. $this->assertSame([$renamed->account, $renamed->register_ip], [$login->login_account, $login->login_ip]);
  526. $third = DB::table('third_game_orders')->where('user_id', $user->id)->first();
  527. $this->assertSame([$renamed->first_name, '财神到', '财神到'], [$third->first_name, $third->game_name, $third->bet_content]);
  528. $order = DB::table('payment_orders')->where('id', $withdraw->id)->first();
  529. $this->assertSame($renamed->first_name, $order->account);
  530. $this->assertStringNotContainsString('模拟', $order->bank_name);
  531. $this->assertMatchesRegularExpression('/^62\d{17}$/', $order->card_no);
  532. $sport = DB::table('sport_order')->where('user_id', $a)->first();
  533. $this->assertStringNotContainsString('模拟', $sport->odd_name);
  534. $this->assertSame($sport->odd_name, json_decode($sport->detail, true)['title']);
  535. $this->assertSame($money, [$this->wallet($a), DB::table('balance_logs')->where('member_id', $a)->sum('amount'), DB::table('payment_orders')->where('member_id', $a)->sum('amount')]);
  536. $this->assertSame([$untouched, $real], [(array) DB::table('users')->where('member_id', $b)->first(), (array) DB::table('users')->where('member_id', 'r1')->first()]);
  537. $this->assertSame(0, array_sum(SimulationMemberService::renameLegacy()), 'nothing left to rename');
  538. }
  539. public function test_recharge_withdraw_and_bet_amounts_are_whole_numbers_picked_from_a_range(): void
  540. {
  541. SimulationSettings::save(['simulation_enabled' => 1]);
  542. foreach (['simulation_task_recharge' => '20.5', 'simulation_task_bet_amount_max' => '99.99', 'simulation_task_withdraw' => '0.5'] as $key => $value) {
  543. try { SimulationSettings::save([$key => $value]); $this->fail('decimals must be refused'); }
  544. catch (\Exception $e) { $this->assertStringContainsString('整数', $e->getMessage()); }
  545. }
  546. $saved = SimulationSettings::save(['simulation_task_recharge' => '20.00', 'simulation_task_recharge_max' => 3000])['after'];
  547. $this->assertSame(['20', '3000'], [$saved['simulation_task_recharge'], $saved['simulation_task_recharge_max']]);
  548. try { SimulationTaskService::normalizeSteps([['type' => 'bet', 'amount' => '10.5']]); $this->fail('decimal step'); }
  549. catch (\Exception $e) { $this->assertStringContainsString('整数', $e->getMessage()); }
  550. try { SimulationTaskService::normalizeSteps([['type' => 'withdraw', 'amount' => '10', 'amount_max' => '30.5']]); $this->fail('decimal step max'); }
  551. catch (\Exception $e) { $this->assertStringContainsString('整数', $e->getMessage()); }
  552. $picked = [];
  553. for ($i = 0; $i < 60; $i++) $picked[] = SimulationSettings::pickAmount(20, 3000);
  554. foreach ($picked as $amount) {
  555. $this->assertMatchesRegularExpression('/^\d+\.00$/', $amount);
  556. $this->assertTrue($amount >= 20 && $amount <= 3000);
  557. }
  558. $this->assertGreaterThan(5, count(array_unique($picked)));
  559. $this->assertSame('50.00', SimulationSettings::pickAmount(50));
  560. $this->assertSame('50.00', SimulationSettings::pickAmount('50', '50'));
  561. foreach ([fn () => SimulationSettings::pickAmount('10.5'), fn () => SimulationSettings::pickAmount(10, '20.5'), fn () => SimulationSettings::pickAmount(30, 20)] as $call) {
  562. try { $call(); $this->fail('bad amount'); } catch (\Exception $e) { $this->assertNotSame('', $e->getMessage()); }
  563. }
  564. // 提现、下注的区间不超过会员当时的可用余额
  565. $memberId = SimulationMemberService::create(1)[0]['member_id'];
  566. SimulationFundService::recharge($memberId, 45, 'third', 'success');
  567. for ($i = 0; $i < 30; $i++) $this->assertLessThanOrEqual(45, (float) SimulationSettings::pickAmount(20, 3000, $memberId));
  568. try { SimulationSettings::pickAmount(100, 3000, $memberId); $this->fail('balance below the minimum'); }
  569. catch (\Exception $e) { $this->assertStringContainsString('余额不足', $e->getMessage()); }
  570. }
  571. public function test_guards_refuse_real_members_and_a_closed_switch(): void
  572. {
  573. foreach ([fn () => SimulationFundService::recharge('r1', 10), fn () => SimulationFundService::adjust('g1', 10, 'credit', 'x'),
  574. fn () => SimulationBetService::sport('r1', 10, 2), fn () => SimulationFundService::withdrawApply('r1', 10)] as $call) {
  575. try { $call(); $this->fail('real members and guests must be refused'); }
  576. catch (\Exception $e) { $this->assertSame('只能操作模拟会员', $e->getMessage()); }
  577. }
  578. $this->assertSame(90.0, $this->wallet('r1')[0]);
  579. $order = \App\Models\PaymentOrder::create(['type' => 1, 'member_id' => 'r1', 'amount' => 10, 'status' => 1]);
  580. try { \App\Services\PaymentOrderService::simulatePaySuccess($order); $this->fail('a real order must not be credited'); }
  581. catch (\Exception $e) { $this->assertStringContainsString('模拟会员', $e->getMessage()); }
  582. SimulationSettings::save(['simulation_enabled' => 0]);
  583. try { SimulationMemberService::create(1); $this->fail('closed switch'); }
  584. catch (\Exception $e) { $this->assertStringContainsString('未开启', $e->getMessage()); }
  585. $this->assertSame(0, SimulationRunner::tick());
  586. $this->assertSame(0, User::where('user_type', 2)->count());
  587. $this->assertNotNull(UserService::class);
  588. }
  589. }