فهرست منبع

自主游戏规则

kk 1 هفته پیش
والد
کامیت
a299e14a5d

+ 1 - 1
game-admin/src/main/resources/application.yml

@@ -138,7 +138,7 @@ xss:
   # 排除链接(多个用逗号分隔)
   excludes: /system/notice
   # 匹配链接
-  urlPatterns: /system/*,/monitor/*,/tool/*
+  urlPatterns: /system/*,/monitor/*,/tool/*,/websocket/*
 
 #websocket开关
 websocket:

+ 5 - 0
game-business/pom.xml

@@ -71,6 +71,11 @@
             </exclusions>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 21 - 0
game-business/src/main/java/com/game/business/config/GameLotteryConfig.java

@@ -0,0 +1,21 @@
+package com.game.business.config;
+
+import com.game.business.task.AppGameHanaderTask;
+import com.game.business.util.Common;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.stereotype.Component;
+
+@Component
+@EnableAsync
+public class GameLotteryConfig implements CommandLineRunner {
+
+    @Override
+    @Async
+    public void run(String... args) throws Exception {
+        AppGameHanaderTask lottery = SpringContextSetting.getBean(AppGameHanaderTask.class);
+        lottery.initGameCountdown(Common.GAME_FIVE_CODE);
+    }
+
+}

+ 2 - 0
game-business/src/main/java/com/game/business/config/WebSocketConfig.java

@@ -4,9 +4,11 @@ import org.springframework.boot.web.client.RestTemplateBuilder;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.client.RestTemplate;
+import org.springframework.web.socket.config.annotation.EnableWebSocket;
 import org.springframework.web.socket.server.standard.ServerEndpointExporter;
 
 @Configuration
+@EnableWebSocket
 public class WebSocketConfig {
 
     @Bean

+ 1 - 1
game-business/src/main/java/com/game/business/mapper/AppGameBettingMapper.java

@@ -24,7 +24,7 @@ public interface AppGameBettingMapper extends BaseMapper<AppGameBetting> {
     Map<String, BigDecimal> getBettingAmountSum(Long userId);
     Map<String, BigDecimal> getBettingAmountByDateSum(FinTranRecordDTO finTranRecordDTO);
 
-    Map<String, BigDecimal> getBettingAmountByDate(@Param(value = "gameId") Long gameId, @Param(value = "dateStr") String dateStr);
+    Map<String, Double> getBettingAmountByDate(@Param(value = "gameId") Long gameId, @Param(value = "dateStr") String dateStr);
     List<AppUserGameBettingDetailsCountVO> getBettingAmountByGameDate(@Param(value = "gameId") Long gameId, @Param(value = "gameDate") String gameDate, @Param(value = "bettingType") Integer bettingType);
 
     Integer getBettingCount(@Param(value = "userId") Long userId, @Param(value = "gameId") Long gameId, @Param(value = "gameDate") String gameDate, @Param(value = "bettingItem") String bettingItem);

+ 1 - 1
game-business/src/main/java/com/game/business/service/IAppGameBettingService.java

@@ -46,7 +46,7 @@ public interface IAppGameBettingService extends IService<AppGameBetting> {
 
     Map<String, BigDecimal> getBettingAmountSum(Long userId);
 
-    Map<String, BigDecimal> getBettingAmountByDate(Long gameId, String dateStr);
+    Map<String, Double> getBettingAmountByDate(Long gameId, String dateStr);
 
     List<AppUserGameBettingDetailsCountVO> getBettingAmountByGameDate(Long gameId, String gameDate, Integer bettingType);
 

+ 1 - 1
game-business/src/main/java/com/game/business/service/impl/AppGameBettingServiceImpl.java

@@ -162,7 +162,7 @@ public class AppGameBettingServiceImpl extends ServiceImpl<AppGameBettingMapper,
     }
 
     @Override
-    public Map<String, BigDecimal> getBettingAmountByDate(Long gameId, String dateStr) {
+    public Map<String, Double> getBettingAmountByDate(Long gameId, String dateStr) {
         return appGameBettingMapper.getBettingAmountByDate(gameId, dateStr);
     }
 

+ 19 - 63
game-business/src/main/java/com/game/business/task/AppGameHanaderTask.java

@@ -1,53 +1,61 @@
 package com.game.business.task;
 
 
+import com.alibaba.fastjson.JSONObject;
 import com.game.business.domain.AppGame;
-import com.game.business.domain.AppGameLottery;
-import com.game.business.service.IAppGameLotteryService;
 import com.game.business.service.IAppGameService;
-import com.game.common.utils.DateUtils;
+import com.game.business.websocket.server.WebSocketServer;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
 
-import java.util.Date;
-
 @Component
 public class AppGameHanaderTask {
 
     @Autowired
     private IAppGameService appGameService;
 
-    @Autowired
-    private IAppGameLotteryService appGameLotteryService;
-
     @Autowired
     private AppGameLotteryAutoTask appGameLotteryAutoTask;
 
     @Autowired
-    private AppGameLotteryTask appGameLotteryTask;
+    private WebSocketServer webSocketServer;
+
+    @Value("${websocket.switch}")
+    private Boolean taskSwitch;
 
+    @Async("asyncExecutor")
     public void initGameCountdown(String gameCode){
 
+        if(!taskSwitch){
+            return;
+        }
+
         AppGame appGame = appGameService.selectAppGameByCode(gameCode);
 
         if(appGame == null) return;
 
         int countdown = 52;
 
+        JSONObject jsonObject = new JSONObject();
+
         while (true){
 
             try {
 
                 // 推送倒计时
+                jsonObject.put("type", "1");
+                jsonObject.put("countdown", countdown);
+                webSocketServer.sendMessageAll(jsonObject.toJSONString());
 
                 // 开奖
                 if(countdown == 7){
-                    this.lotteryManager(appGame.getId());
+                    appGameLotteryAutoTask.autoTask(appGame.getId());
                 }
 
                 // 更新游戏信息
-                this.updateAppGameTime(appGame.getId(), countdown);
+                appGameLotteryAutoTask.updateAppGameTime(appGame.getClassifyId(), appGame.getId(), countdown);
 
                 // 重新开启倒计时
                 if(countdown ==  0){
@@ -62,56 +70,4 @@ public class AppGameHanaderTask {
             }
         }
     }
-
-    @Async("asyncExecutor")
-    public void updateAppGameTime(Long gameId, int countdown){
-
-        String time = "00:";
-        if(countdown < 10){
-            time = time + "0" + countdown;
-        }else{
-            time = time + countdown;
-        }
-
-        // TODO 更新时间
-        AppGame appGame = new AppGame();
-        appGame.setId(gameId);
-        appGame.setGameTime(time);
-
-        if(countdown == 0){
-            appGame.setGameDate(DateUtils.dateTimeNow());
-        }
-        appGameService.updateAppGame(appGame);
-    }
-
-    @Async("asyncExecutor")
-    public void lotteryManager(Long gameId){
-        try {
-
-            // 游戏信息
-            AppGame appGame = appGameService.selectAppGameById(gameId);
-
-            // 是否已开奖
-            AppGameLottery dbAppGameLottery = appGameLotteryService.selectLottery(appGame.getClassifyId(), appGame.getId(), appGame.getGameDate());
-            if(dbAppGameLottery != null && dbAppGameLottery.getIsLottery() == 1){
-                return;
-            }
-
-            String gameLotterySucc = appGameLotteryAutoTask.autoTask(appGame, appGame.getGameDate());
-
-            // 保存开奖记录
-            AppGameLottery appGameLottery = new AppGameLottery();
-            appGameLottery.setGameId(appGame.getId());
-            appGameLottery.setClassId(appGame.getClassifyId());
-            appGameLottery.setGameDate(appGame.getGameDate());
-            appGameLottery.setIsLottery(1);
-            appGameLottery.setGameRecordDate(new Date());
-            appGameLottery.setGameLotterySucc(gameLotterySucc);
-            if(appGameLotteryService.save(appGameLottery)){
-                appGameLotteryTask.updateLottery(appGameLottery);
-            }
-        }catch (Exception e){
-            e.printStackTrace();
-        }
-    }
 }

+ 193 - 90
game-business/src/main/java/com/game/business/task/AppGameLotteryAutoTask.java

@@ -1,15 +1,21 @@
 package com.game.business.task;
 
+import com.alibaba.fastjson.JSONObject;
 import com.game.business.domain.AppGame;
 import com.game.business.domain.AppGameItem;
+import com.game.business.domain.AppGameLottery;
 import com.game.business.service.IAppGameBettingService;
 import com.game.business.service.IAppGameItemService;
 import com.game.business.service.IAppGameLotteryService;
+import com.game.business.service.IAppGameService;
 import com.game.business.vo.AppUserGameBettingDetailsCountVO;
+import com.game.business.websocket.server.WebSocketServer;
 import com.game.common.utils.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
 
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -26,133 +32,230 @@ public class AppGameLotteryAutoTask {
     @Autowired
     private IAppGameLotteryService appGameLotteryService;
 
+    @Autowired
+    private AppGameLotteryTask appGameLotteryTask;
 
-    public String autoTask(AppGame appGame, String gameDate){
+    @Autowired
+    private IAppGameService appGameService;
 
-        Long gameId = appGame.getId();
+    @Autowired
+    private WebSocketServer webSocketServer;
 
-        Map<String, BigDecimal> dateBetting = appGameBettingService.getBettingAmountByDate(gameId, DateUtils.dateTime(new Date()));
+    @Async("asyncExecutor")
+    public void autoTask(Long gameId){
 
-        // 余额投注总输赢
-        BigDecimal diamondCoinSum = dateBetting.get("diamondCoinSum");
-        BigDecimal diamondCoinWin = dateBetting.get("diamondCoinWin");
-        BigDecimal diamondCoinLose = dateBetting.get("diamondCoinLose");
+        try {
 
-        // 金币投注总输赢
-        BigDecimal coinSum = dateBetting.get("coinSum");
-        BigDecimal coinWin = dateBetting.get("coinWin");
-        BigDecimal coinLose =dateBetting.get("coinLose");
+            AppGame appGame = appGameService.selectAppGameById(gameId);
+            if (appGame == null) return;
 
-        if(coinSum.compareTo(BigDecimal.ZERO) > 0){
-            diamondCoinSum = diamondCoinSum.add(coinSum.divide(new BigDecimal("100.00")).setScale(2, BigDecimal.ROUND_HALF_UP));
-        }
+            // 是否已开奖
+            AppGameLottery dbAppGameLottery = appGameLotteryService.selectLottery(appGame.getClassifyId(), appGame.getId(), appGame.getGameDate());
+            if(dbAppGameLottery != null && dbAppGameLottery.getIsLottery() == 1){
+                return;
+            }
 
-        if(coinWin.compareTo(BigDecimal.ZERO) > 0){
-            diamondCoinWin = diamondCoinWin.add(coinWin.divide(new BigDecimal("100.00")).setScale(2, BigDecimal.ROUND_HALF_UP));
-        }
+            String gameDate = appGame.getGameDate();
 
-        if(coinLose.compareTo(BigDecimal.ZERO) > 0){
-            diamondCoinLose = diamondCoinLose.add(coinLose.divide(new BigDecimal("100.00")).setScale(2, BigDecimal.ROUND_HALF_UP));
-        }
+            Map<String, Double> dateBetting = appGameBettingService.getBettingAmountByDate(gameId, DateUtils.dateTime(new Date()));
 
-        // 余额选项投注详情
-        List<AppUserGameBettingDetailsCountVO> diamondCoinBettingDetailsList = appGameBettingService.getBettingAmountByGameDate(gameId, gameDate, 0);
-        Map<String, List<AppUserGameBettingDetailsCountVO>> diamondCoinMap = null;
-        double diamondCoinGameDateSum = 0.0;
-        if(diamondCoinBettingDetailsList != null && !diamondCoinBettingDetailsList.isEmpty()){
-            diamondCoinMap = diamondCoinBettingDetailsList.stream().collect(Collectors.groupingBy(AppUserGameBettingDetailsCountVO::getBettingTtem));
-            diamondCoinGameDateSum = diamondCoinBettingDetailsList.stream().mapToDouble(AppUserGameBettingDetailsCountVO::getOrderAmount).sum();
-        }
+            // 余额投注总输赢
+            BigDecimal diamondCoinSum = new BigDecimal(dateBetting.get("diamondCoinSum") + "");
+            BigDecimal diamondCoinWin = new BigDecimal(dateBetting.get("diamondCoinWin") + "");
+            BigDecimal diamondCoinLose = new BigDecimal(dateBetting.get("diamondCoinLose") + "");
+
+            // 金币投注总输赢
+            BigDecimal coinSum = new BigDecimal(dateBetting.get("coinSum") + "");
+            BigDecimal coinWin = new BigDecimal(dateBetting.get("coinWin") + "");
+            BigDecimal coinLose = new BigDecimal(dateBetting.get("coinLose") + "");
 
-        // 金币选项投注详情
-        List<AppUserGameBettingDetailsCountVO> coinBettingDetailsList = appGameBettingService.getBettingAmountByGameDate(gameId, gameDate, 1);
-        Map<String, List<AppUserGameBettingDetailsCountVO>> coinMap = null;
-        if(coinBettingDetailsList != null && !coinBettingDetailsList.isEmpty()){
-            coinMap = coinBettingDetailsList.stream().collect(Collectors.groupingBy(AppUserGameBettingDetailsCountVO::getBettingTtem));
-            double coinGameDateSum = diamondCoinBettingDetailsList.stream().mapToDouble(AppUserGameBettingDetailsCountVO::getOrderAmount).sum();
-            if(coinGameDateSum > 0){
-                diamondCoinGameDateSum += coinGameDateSum / 100;
+            if(coinSum.compareTo(BigDecimal.ZERO) > 0){
+                diamondCoinSum = diamondCoinSum.add(coinSum.divide(new BigDecimal("100.00")).setScale(2, BigDecimal.ROUND_HALF_UP));
             }
-        }
 
-        AppGameItem appGameItem = new AppGameItem();
-        appGameItem.setGameId(gameId);
-        List<AppGameItem> itemList = appGameItemService.selectAppGameItemList(appGameItem);
+            if(coinWin.compareTo(BigDecimal.ZERO) > 0){
+                diamondCoinWin = diamondCoinWin.add(coinWin.divide(new BigDecimal("100.00")).setScale(2, BigDecimal.ROUND_HALF_UP));
+            }
 
-        double itemMultiple = 0;
-        for(AppGameItem item : itemList){
-            itemMultiple += item.getItemMultiple();
-        }
+            if(coinLose.compareTo(BigDecimal.ZERO) > 0){
+                diamondCoinLose = diamondCoinLose.add(coinLose.divide(new BigDecimal("100.00")).setScale(2, BigDecimal.ROUND_HALF_UP));
+            }
 
-        List<String> pools = new ArrayList<>();
+            // 余额选项投注详情
+            List<AppUserGameBettingDetailsCountVO> diamondCoinBettingDetailsList = appGameBettingService.getBettingAmountByGameDate(gameId, gameDate, 0);
+            Map<String, List<AppUserGameBettingDetailsCountVO>> diamondCoinMap = null;
+            double diamondCoinGameDateSum = 0.0;
+            if(diamondCoinBettingDetailsList != null && !diamondCoinBettingDetailsList.isEmpty()){
+                diamondCoinMap = diamondCoinBettingDetailsList.stream().collect(Collectors.groupingBy(AppUserGameBettingDetailsCountVO::getBettingTtem));
+                diamondCoinGameDateSum = diamondCoinBettingDetailsList.stream().mapToDouble(AppUserGameBettingDetailsCountVO::getOrderAmount).sum();
+            }
 
-        for(AppGameItem item : itemList){
-            Double rete = itemMultiple * 100 / item.getItemMultiple();
-            int int_rate = rete.intValue();
-            for (int i = 0; i < int_rate; i++) {
-                pools.add(item.getItemLocation());
+            // 金币选项投注详情
+            List<AppUserGameBettingDetailsCountVO> coinBettingDetailsList = appGameBettingService.getBettingAmountByGameDate(gameId, gameDate, 1);
+            Map<String, List<AppUserGameBettingDetailsCountVO>> coinMap = null;
+            if(coinBettingDetailsList != null && !coinBettingDetailsList.isEmpty()){
+                coinMap = coinBettingDetailsList.stream().collect(Collectors.groupingBy(AppUserGameBettingDetailsCountVO::getBettingTtem));
+                double coinGameDateSum = diamondCoinBettingDetailsList.stream().mapToDouble(AppUserGameBettingDetailsCountVO::getOrderAmount).sum();
+                if(coinGameDateSum > 0){
+                    diamondCoinGameDateSum += coinGameDateSum / 100;
+                }
             }
-        }
 
-        //随机开奖
-        Collections.shuffle(pools);
+            AppGameItem appGameItem = new AppGameItem();
+            appGameItem.setGameId(gameId);
+            List<AppGameItem> itemList = appGameItemService.selectAppGameItemList(appGameItem);
 
-        int index = new Random().nextInt(pools.size());
-        String lottertSucc = pools.get(index);
+            double itemMultiple = 0;
+            for(AppGameItem item : itemList){
+                itemMultiple += item.getItemMultiple();
+            }
 
-        AppGameItem gameItem = appGameItemService.selectAppGameItemByGameIdAndItemLocation(gameId, lottertSucc);
+            List<String> pools = new ArrayList<>();
 
-        double lottertAmount = 0.0;
-        if(diamondCoinMap != null && diamondCoinMap.containsKey(lottertSucc)){
-            AppUserGameBettingDetailsCountVO detailsCountVO = diamondCoinMap.get(lottertSucc).get(0);
-            lottertAmount = detailsCountVO.getOrderAmount() * gameItem.getItemMultiple();
-        }
+            for(AppGameItem item : itemList){
+                Double rete = itemMultiple * 100 / item.getItemMultiple();
+                int int_rate = rete.intValue();
+                for (int i = 0; i < int_rate; i++) {
+                    pools.add(item.getItemLocation());
+                }
+            }
 
-        if(coinMap != null && coinMap.containsKey(lottertSucc)){
-            AppUserGameBettingDetailsCountVO detailsCountVO = coinMap.get(lottertSucc).get(0);
-            lottertAmount += detailsCountVO.getOrderAmount() / 100.00 * gameItem.getItemMultiple();;
-        }
+            //随机开奖
+            Collections.shuffle(pools);
+
+            int index = new Random().nextInt(pools.size());
+            String lottertSucc = pools.get(index);
+
+            AppGameItem gameItem = appGameItemService.selectAppGameItemByGameIdAndItemLocation(gameId, lottertSucc);
+
+            double lottertAmount = 0.0;
+            if(diamondCoinMap != null && diamondCoinMap.containsKey(lottertSucc)){
+                AppUserGameBettingDetailsCountVO detailsCountVO = diamondCoinMap.get(lottertSucc).get(0);
+                lottertAmount = detailsCountVO.getOrderAmount() * gameItem.getItemMultiple();
+            }
+
+            if(coinMap != null && coinMap.containsKey(lottertSucc)){
+                AppUserGameBettingDetailsCountVO detailsCountVO = coinMap.get(lottertSucc).get(0);
+                lottertAmount += detailsCountVO.getOrderAmount() / 100.00 * gameItem.getItemMultiple();;
+            }
 
-        List<String> itemPools = new ArrayList<>();
+            List<String> itemPools = new ArrayList<>();
 
-        // 开奖次数
-        long count = appGameLotteryService.selectCountAuto(appGame.getClassifyId(), appGame.getId(), DateUtils.dateTime(new Date()));
+            // 开奖次数
+            long count = appGameLotteryService.selectCountAuto(appGame.getClassifyId(), appGame.getId(), DateUtils.dateTime(new Date()));
 
-        // 开始走最低开盘
-        if(count > 500 && diamondCoinWin.subtract(diamondCoinSum.multiply(new BigDecimal("0.8"))).compareTo(BigDecimal.ZERO) > 0){
+            // 开始走最低开盘
+            if(count > 500 && diamondCoinWin.subtract(diamondCoinSum.multiply(new BigDecimal("0.8"))).compareTo(BigDecimal.ZERO) > 0){
 
-            // 随机开奖
-            if(lottertAmount - diamondCoinGameDateSum > 0){
+                // 随机开奖
+                if(lottertAmount - diamondCoinGameDateSum > 0){
 
-                for (AppGameItem item : itemList) {
-                    double orderAmount = 0.0;
+                    for (AppGameItem item : itemList) {
+                        double orderAmount = 0.0;
 
-                    if(diamondCoinMap != null){
-                        if(diamondCoinMap.containsKey(item.getItemLocation())){
-                            AppUserGameBettingDetailsCountVO detailsCountVO = diamondCoinMap.get(item.getItemLocation()).get(0);
-                            orderAmount += detailsCountVO.getOrderAmount();
+                        if(diamondCoinMap != null){
+                            if(diamondCoinMap.containsKey(item.getItemLocation())){
+                                AppUserGameBettingDetailsCountVO detailsCountVO = diamondCoinMap.get(item.getItemLocation()).get(0);
+                                orderAmount += detailsCountVO.getOrderAmount();
+                            }
                         }
-                    }
 
-                    if(coinMap != null){
-                        if(coinMap.containsKey(item.getItemLocation())){
-                            AppUserGameBettingDetailsCountVO detailsCountVO = coinMap.get(item.getItemLocation()).get(0);
-                            orderAmount += detailsCountVO.getOrderAmount() / 100.00;
+                        if(coinMap != null){
+                            if(coinMap.containsKey(item.getItemLocation())){
+                                AppUserGameBettingDetailsCountVO detailsCountVO = coinMap.get(item.getItemLocation()).get(0);
+                                orderAmount += detailsCountVO.getOrderAmount() / 100.00;
+                            }
                         }
-                    }
 
-                    if(orderAmount == 0 || (orderAmount * itemMultiple) - diamondCoinGameDateSum <= 0){
-                        itemPools.add(item.getItemLocation());
+                        if(orderAmount == 0 || (orderAmount * itemMultiple) - diamondCoinGameDateSum <= 0){
+                            itemPools.add(item.getItemLocation());
+                        }
                     }
                 }
             }
+
+            if(!itemPools.isEmpty()){
+                index = new Random().nextInt(itemPools.size());
+                lottertSucc = pools.get(index);
+            }
+
+            JSONObject gameLottery = new JSONObject();
+            gameLottery.put("type", "2");
+            gameLottery.put("gameLottery", lottertSucc);
+            webSocketServer.sendMessageAll(gameLottery.toJSONString());
+
+
+            // 保存开奖记录
+            AppGameLottery appGameLottery = appGameLotteryService.selectLottery(appGame.getClassifyId(), appGame.getId(), gameDate);
+
+            if(appGameLottery == null){
+                // 保存开奖记录
+                appGameLottery = new AppGameLottery();
+                appGameLottery.setGameId(appGame.getId());
+                appGameLottery.setClassId(appGame.getClassifyId());
+                appGameLottery.setGameDate(gameDate);
+                appGameLottery.setIsLottery(1);
+                appGameLottery.setGameRecordDate(new Date());
+                appGameLottery.setGameLotterySucc(lottertSucc);
+                appGameLotteryService.save(appGameLottery);
+            }else{
+                if(appGameLottery.getIsLottery() == 1){
+                    return;
+                }
+
+                Date RecordDate = new Date();
+
+                appGameLottery.setIsLottery(1);
+                appGameLottery.setGameLotterySucc(lottertSucc);
+                appGameLottery.setGameRecordDate(RecordDate);
+
+                AppGameLottery updateAppGameLottery = new AppGameLottery();
+                updateAppGameLottery.setId(appGameLottery.getId());
+                updateAppGameLottery.setIsLottery(1);
+                updateAppGameLottery.setGameRecordDate(RecordDate);
+                updateAppGameLottery.setGameLotterySucc(lottertSucc);
+                appGameLotteryService.updateById(updateAppGameLottery);
+            }
+
+            appGameLotteryTask.updateLottery(appGameLottery);
+
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+
+    @Async("asyncExecutor")
+    public void updateAppGameTime(Long classId, Long gameId,int countdown) throws IOException {
+
+        String time = "00:";
+        if(countdown < 10){
+            time = time + "0" + countdown;
+        }else{
+            time = time + countdown;
         }
 
-        if(!itemPools.isEmpty()){
-            index = new Random().nextInt(itemPools.size());
-            lottertSucc = pools.get(index);
+        AppGame appGame = new AppGame();
+        appGame.setId(gameId);
+        appGame.setGameTime(time);
+
+        if(countdown == 2){
+
+            appGame.setGameDate(DateUtils.getTime());
+
+            JSONObject dateJson = new JSONObject();
+            dateJson.put("type", "3");
+            dateJson.put("gameDate", appGame.getGameDate());
+            webSocketServer.sendMessageAll(dateJson.toJSONString());
+
+            AppGameLottery appGameLottery = new AppGameLottery();
+            appGameLottery.setGameId(appGame.getId());
+            appGameLottery.setClassId(classId);
+            appGameLottery.setGameDate(appGame.getGameDate());
+            appGameLottery.setIsLottery(0);
+            appGameLottery.setGameRecordDate(new Date());
+            appGameLotteryService.save(appGameLottery);
         }
 
-        return lottertSucc;
+        appGameService.updateAppGame(appGame);
     }
 }

+ 146 - 0
game-business/src/main/java/com/game/business/websocket/server/WebSocketServer.java

@@ -0,0 +1,146 @@
+package com.game.business.websocket.server;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.*;
+import javax.websocket.server.PathParam;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+@Component
+@ServerEndpoint("/websocket/{userId}")
+public class WebSocketServer {
+
+    /**
+     * 当前在线连接数
+     */
+    private static AtomicInteger onlineCount = new AtomicInteger(0);
+
+    /**
+     * 用来存放每个客户端对应的 WebSocketServer 对象
+     */
+    private static ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
+
+    /**
+     * 与某个客户端的连接会话,需要通过它来给客户端发送数据
+     */
+    private Session session;
+
+    /**
+     * 接收 userId
+     */
+    private String userId = "";
+
+    /**
+     * 连接建立成功调用的方法
+     */
+    @OnOpen
+    public void onOpen(Session session, @PathParam("userId") String userId) {
+        this.session = session;
+        this.userId = userId;
+        if (webSocketMap.containsKey(userId)) {
+            webSocketMap.remove(userId);
+            webSocketMap.put(userId, this);
+        } else {
+            webSocketMap.put(userId, this);
+            addOnlineCount();
+        }
+        System.out.println("用户连接:" + userId + ",当前在线人数为:" + getOnlineCount());
+        try {
+            sendMessage("连接成功!");
+        } catch (IOException e) {
+            System.out.println("用户:" + userId + ",网络异常!!!!!!");
+        }
+    }
+
+    /**
+     * 发生错误时调用
+     *
+     * @param session
+     * @param error
+     */
+    @OnError
+    public void onError(Session session, Throwable error) {
+        System.out.println("用户错误:" + this.userId + ",原因:" + error.getMessage());
+        error.printStackTrace();
+    }
+
+    /**
+     * 连接关闭调用的方法
+     */
+    @OnClose
+    public void onClose() {
+        if (webSocketMap.containsKey(userId)) {
+            webSocketMap.remove(userId);
+            subOnlineCount();
+        }
+        System.out.println("用户退出:" + userId + ",当前在线人数为:" + getOnlineCount());
+    }
+
+    /**
+     * 收到客户端消息后调用的方法
+     *
+     * @param message 客户端发送过来的消息
+     */
+    @OnMessage
+    public void onMessage(String message, Session session) {
+
+        System.out.println("用户消息:" + userId + ",报文:" + message);
+
+        if (!StringUtils.isEmpty(message)) {
+            try {
+
+                if("ping".equals(message)) {
+                    sendMessage("pong");
+                    return;
+                }
+
+                JSONObject jsonObject = JSON.parseObject(message);
+                jsonObject.put("fromUserId", this.userId);
+                String toUserId = jsonObject.getString("toUserId");
+                if (!StringUtils.isEmpty(toUserId) && webSocketMap.containsKey(toUserId)) {
+                    webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString());
+                } else {
+                    System.out.println("请求的 userId:" + toUserId + "不在该服务器上");
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public void sendMessage(String message) throws IOException {
+        if(this.session.isOpen()){
+            this.session.getBasicRemote().sendText(message);
+        }
+    }
+
+    public synchronized void sendMessageAll(String message) throws IOException {
+        Set<String> set = webSocketMap.keySet();
+        for (String userId : set) {
+            WebSocketServer webSocketServer = webSocketMap.get(userId);
+            if(webSocketServer.session.isOpen()){
+                webSocketServer.session.getBasicRemote().sendText(message);
+            }
+        }
+    }
+
+
+    public static synchronized AtomicInteger getOnlineCount() {
+        return onlineCount;
+    }
+
+    public static synchronized void addOnlineCount() {
+        WebSocketServer.onlineCount.getAndIncrement();
+    }
+
+    public static synchronized void subOnlineCount() {
+        WebSocketServer.onlineCount.getAndDecrement();
+    }
+}

+ 1 - 1
game-framework/src/main/java/com/game/framework/config/SecurityConfig.java

@@ -111,7 +111,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
                 // 过滤请求
                 .authorizeRequests()
                 // 对于登录login 注册register 验证码captchaImage 允许匿名访问
-                .antMatchers("/login", "/register", "/captchaImage","/business/game_item/synItem", "/business/game_item/getSynItem").permitAll()
+                .antMatchers("/login", "/register", "/captchaImage","/business/game_item/synItem", "/business/game_item/getSynItem", "/websocket/**").permitAll()
                 // 静态资源,可匿名访问
                 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
                 .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()