浏览代码

最多9人加入该房间号

wuweihao 3 年之前
父节点
当前提交
278437314e

+ 2 - 0
gis_cms/src/main/java/com/gis/cms/service/GameLogService.java

@@ -27,4 +27,6 @@ public interface GameLogService extends IService<GameLogEntity> {
     Integer totalScore(Long userId, Long roomId);
 
     GameLogEntity findByRoomIdAndUserId(Long roomId, Long userId, String type);
+
+    Long countByRoomId(Long roomId);
 }

+ 7 - 0
gis_cms/src/main/java/com/gis/cms/service/impl/GameLogServiceImpl.java

@@ -206,5 +206,12 @@ public class GameLogServiceImpl extends ServiceImpl<GameLogMapper, GameLogEntity
 
     }
 
+    @Override
+    public Long countByRoomId(Long roomId) {
+        LambdaQueryWrapper<GameLogEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(GameLogEntity::getRoomId, roomId);
+        return this.count(wrapper);
+    }
+
 
 }

+ 14 - 0
gis_cms/src/main/java/com/gis/cms/service/impl/RoomServiceImpl.java

@@ -112,6 +112,10 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
         // 加入房间记录
         GameLogEntity entity = gameLogService.findByRoomIdAndUserId(roomId, userId, type);
         if (entity == null){
+
+            // 最多9人加入该房间号
+            checkSzieByRoomId(9, roomId);
+
             log.info("需要创建该房间的游戏记录");
             // 用户加入房间
             entity = new GameLogEntity();
@@ -125,6 +129,16 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
         return Result.success(entity);
     }
 
+    /**
+     * 2022-06-22
+     * 检查加入房间人数
+     * @param size
+     */
+    private void checkSzieByRoomId(int size, Long roomId){
+        Long count = gameLogService.countByRoomId(roomId);
+        BaseRuntimeException.isTrue(count >= size, 3100, "房间人数已满");
+    }
+