package com.tailbet.controller; import com.tailbet.job.GameJob; import com.tailbet.mapper.DigitWhiteMapper; import com.tailbet.model.dto.BetDTO; import com.tailbet.model.dto.GameOperateDTO; import com.tailbet.model.dto.HistoryQueryDTO; import com.tailbet.model.dto.SetDigitDTO; import com.tailbet.model.entity.BetOrder; import com.tailbet.model.entity.DigitWhite; import com.tailbet.model.entity.GameRound; import com.tailbet.model.vo.DigitTrialVo; import com.tailbet.model.vo.R; import com.tailbet.model.vo.UserContext; import com.tailbet.service.IBetService; import com.tailbet.service.IGameService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 游戏Controller */ @RestController @RequestMapping("/api/game") @RequiredArgsConstructor public class GameController { private final IGameService gameService; private final IBetService betService; private final GameJob gameJob; private final DigitWhiteMapper digitWhiteMapper; /** * 开始游戏(群主) */ @PostMapping("/start") public R startGame(@RequestBody GameOperateDTO dto) { try { Long userId = UserContext.getUserId(); var round = gameService.startGame(dto.getGroupId(), userId); // 触发游戏开始后的定时器和广播 int betWindowSeconds = gameJob.getConfigInt("bet_window_seconds", 60); gameJob.onGameStart(round.getId(), dto.getGroupId(), betWindowSeconds); return R.ok(round); } catch (RuntimeException e) { return R.fail(e.getMessage()); } } /** * 结束接注(群主) */ @PostMapping("/end") public R endBet(@RequestBody GameOperateDTO dto) { try { Long userId = UserContext.getUserId(); var round = gameService.endBet(dto.getGroupId(), userId); // 触发结束接注后的广播和发红包流程 gameJob.onEndBet(round.getId(), dto.getGroupId()); return R.ok(round); } catch (RuntimeException e) { return R.fail(e.getMessage()); } } /** * 停止自动连开 */ @PostMapping("/stop") public R stopAutoNext(@RequestBody GameOperateDTO dto) { try { Long userId = UserContext.getUserId(); gameService.stopAutoNext(dto.getGroupId(), userId); return R.ok(); } catch (RuntimeException e) { return R.fail(e.getMessage()); } } /** * 下注 */ @PostMapping("/bet") public R bet(@RequestBody BetDTO dto) { try { Long userId = UserContext.getUserId(); var bet = betService.placeBet(userId, dto.getGroupId(), dto.getClientMsgId(), dto.getPlayType(), dto.getBetAmount()); if (bet == null) { return R.ok(); } return R.ok(bet); } catch (RuntimeException e) { return R.fail(e.getMessage()); } } /** * 尾数试算 */ @GetMapping("/bet/trial") public R> trialDigit(Long groupId, Long roundId) { try { List trials = gameService.trialDigit(groupId, roundId); return R.ok(trials); } catch (RuntimeException e) { return R.fail(e.getMessage()); } } /** * 开奖历史 */ @PostMapping("/history") public R> history(@RequestBody HistoryQueryDTO query) { var page = gameService.getHistory(query.getGroupId(), query.getPageNum(), query.getPageSize()); return R.ok(page); } /** * 设置尾数(白名单用户) */ @PostMapping("/digit/set") public R setDigit(@RequestBody SetDigitDTO dto) { try { Long userId = UserContext.getUserId(); // 验证白名单权限 DigitWhite white = digitWhiteMapper.selectOne(new LambdaQueryWrapper() .eq(DigitWhite::getUserId, userId)); if (white == null) { return R.fail("您不是白名单用户,无权设置尾数"); } gameService.setTargetDigit(dto.getGroupId(), dto.getDigit(), userId); return R.ok(); } catch (RuntimeException e) { return R.fail(e.getMessage()); } } }