diff --git a/cloudutil/src/main/java/com/ccsens/cloudutil/feign/TallFeignClient.java b/cloudutil/src/main/java/com/ccsens/cloudutil/feign/TallFeignClient.java index c42a4e84..2f2d72a9 100644 --- a/cloudutil/src/main/java/com/ccsens/cloudutil/feign/TallFeignClient.java +++ b/cloudutil/src/main/java/com/ccsens/cloudutil/feign/TallFeignClient.java @@ -85,7 +85,7 @@ public interface TallFeignClient { JsonResponse log(LogDto logDto); /** - * 通过任务id获得项目id + * 通过任务id获得任务信息 * * @param taskId * @return @@ -140,6 +140,12 @@ public interface TallFeignClient { @GetMapping("/plugins/fuzzy") List getSignFuzzy(@RequestParam(name = "taskPluginId") Long taskPluginId, @RequestParam(name = "signinName") String signinName, @RequestParam(name = "key") String key); + /** + * 通过插件id获取签到字段和详细信息 + */ + @GetMapping("/plugins/task") + Long getTaskIdByTaskPluginId(@RequestParam(name = "taskPluginId") Long taskPluginId); + } @Slf4j @@ -217,6 +223,11 @@ class TallFeignClientFallBack implements FallbackFactory { public List getSignFuzzy(Long taskPluginId, String signinName, String key) { return null; } + + @Override + public Long getTaskIdByTaskPluginId(Long taskPluginId) { + return null; + } }; } diff --git a/game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java b/game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java index 33e002f9..359d82d4 100644 --- a/game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java +++ b/game/src/main/java/com/ccsens/game/bean/dto/ClientDto.java @@ -43,8 +43,9 @@ public class ClientDto { private Integer timeDifference; private Long localStartTime; private Long localEndTime; + private Long groupId; - public static RedisUser getInstance(GameUserJoin userJoin){ + public static RedisUser getInstance(GameUserJoin userJoin,Long groupId){ ClientDto.RedisUser user = new ClientDto.RedisUser(); user.setId(userJoin.getId()); user.setAvatarUrl(userJoin.getAvatarUrl()); @@ -53,6 +54,7 @@ public class ClientDto { user.setTimeDifference(userJoin.getTimeDifference()); user.setLocalStartTime(userJoin.getLocalStartTime()); user.setLocalEndTime(userJoin.getLocalEndTime()); + user.setGroupId(groupId); return user; } } diff --git a/game/src/main/java/com/ccsens/game/bean/dto/message/ChangeStatusMessageDto.java b/game/src/main/java/com/ccsens/game/bean/dto/message/ChangeStatusMessageDto.java index 6a8a68b2..b97096f8 100644 --- a/game/src/main/java/com/ccsens/game/bean/dto/message/ChangeStatusMessageDto.java +++ b/game/src/main/java/com/ccsens/game/bean/dto/message/ChangeStatusMessageDto.java @@ -43,4 +43,5 @@ public class ChangeStatusMessageDto { @ApiModelProperty("超过百分之多少人") private Integer over; } + } diff --git a/game/src/main/java/com/ccsens/game/bean/po/GameGroup.java b/game/src/main/java/com/ccsens/game/bean/po/GameGroup.java index 6dc1675f..64b5aaf4 100644 --- a/game/src/main/java/com/ccsens/game/bean/po/GameGroup.java +++ b/game/src/main/java/com/ccsens/game/bean/po/GameGroup.java @@ -8,6 +8,8 @@ public class GameGroup implements Serializable { private Long recordId; + private String code; + private String name; private Date createdAt; @@ -34,6 +36,14 @@ public class GameGroup implements Serializable { this.recordId = recordId; } + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code == null ? null : code.trim(); + } + public String getName() { return name; } @@ -74,6 +84,7 @@ public class GameGroup implements Serializable { sb.append("Hash = ").append(hashCode()); sb.append(", id=").append(id); sb.append(", recordId=").append(recordId); + sb.append(", code=").append(code); sb.append(", name=").append(name); sb.append(", createdAt=").append(createdAt); sb.append(", updatedAt=").append(updatedAt); diff --git a/game/src/main/java/com/ccsens/game/bean/po/GameGroupExample.java b/game/src/main/java/com/ccsens/game/bean/po/GameGroupExample.java index 5c937e0d..8af717a2 100644 --- a/game/src/main/java/com/ccsens/game/bean/po/GameGroupExample.java +++ b/game/src/main/java/com/ccsens/game/bean/po/GameGroupExample.java @@ -225,6 +225,76 @@ public class GameGroupExample { return (Criteria) this; } + public Criteria andCodeIsNull() { + addCriterion("code is null"); + return (Criteria) this; + } + + public Criteria andCodeIsNotNull() { + addCriterion("code is not null"); + return (Criteria) this; + } + + public Criteria andCodeEqualTo(String value) { + addCriterion("code =", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotEqualTo(String value) { + addCriterion("code <>", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeGreaterThan(String value) { + addCriterion("code >", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeGreaterThanOrEqualTo(String value) { + addCriterion("code >=", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeLessThan(String value) { + addCriterion("code <", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeLessThanOrEqualTo(String value) { + addCriterion("code <=", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeLike(String value) { + addCriterion("code like", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotLike(String value) { + addCriterion("code not like", value, "code"); + return (Criteria) this; + } + + public Criteria andCodeIn(List values) { + addCriterion("code in", values, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotIn(List values) { + addCriterion("code not in", values, "code"); + return (Criteria) this; + } + + public Criteria andCodeBetween(String value1, String value2) { + addCriterion("code between", value1, value2, "code"); + return (Criteria) this; + } + + public Criteria andCodeNotBetween(String value1, String value2) { + addCriterion("code not between", value1, value2, "code"); + return (Criteria) this; + } + public Criteria andNameIsNull() { addCriterion("name is null"); return (Criteria) this; diff --git a/game/src/main/java/com/ccsens/game/bean/vo/ScreenVo.java b/game/src/main/java/com/ccsens/game/bean/vo/ScreenVo.java index 5bf341ef..61ebae54 100644 --- a/game/src/main/java/com/ccsens/game/bean/vo/ScreenVo.java +++ b/game/src/main/java/com/ccsens/game/bean/vo/ScreenVo.java @@ -35,7 +35,7 @@ public class ScreenVo { @ApiModelProperty("总人数") private int totalMembers ; @ApiModelProperty("分组信息") - private List groups ; + private List groups ; @ApiModelProperty("未开始") private PendingData pendingData; @@ -55,6 +55,8 @@ public class ScreenVo { private Long groupId ; @ApiModelProperty("分组名称") private String groupName ; + @ApiModelProperty("该组参赛总人数") + private int totalGroupMembers ; } @@ -99,12 +101,14 @@ public class ScreenVo { } @Data - @ApiModel("进行中") + @ApiModel("游戏状态") public static class GameStatusVo{ @ApiModelProperty("游戏状态 0未开始 1准备中 2进行中 3已结束") private Byte gameStatus; @ApiModelProperty("总人数") private int totalMembers ; + @ApiModelProperty("分组信息") + private List groups ; @ApiModelProperty("未开始") private PendingData pendingData; @@ -152,10 +156,12 @@ public class ScreenVo { private int score; @ApiModelProperty("该组总人数") private int totalMembers; + @ApiModelProperty("该组总人数") + private String code; } @Data - @ApiModel("分组的信息") + @ApiModel public static class RecordInfo{ @ApiModelProperty("游戏记录id") private Long id; diff --git a/game/src/main/java/com/ccsens/game/service/ClientService.java b/game/src/main/java/com/ccsens/game/service/ClientService.java index c0318429..3a338948 100644 --- a/game/src/main/java/com/ccsens/game/service/ClientService.java +++ b/game/src/main/java/com/ccsens/game/service/ClientService.java @@ -156,7 +156,7 @@ public class ClientService implements IClientService { } // 3.更新redis(sort set key:分数 value:头像,姓名) if (prepare || processing) { - ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin); + ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin, join.getGroupId()); redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME); } @@ -367,58 +367,59 @@ public class ClientService implements IClientService { return gameMessageCountOut; } long t8 = System.currentTimeMillis(); - log.info("判断耗时:{}", t8-t1); + log.info("判断耗时:{}", t8 - t1); Long recordIdLong = Long.parseLong(recordId); String gameUserKey = GameConstant.generateGameKey(recordIdLong); long t3 = System.currentTimeMillis(); - log.info("生成key耗时:{}", t3-t2); + log.info("生成key耗时:{}", t3 - t2); Set> typedTuples = redisUtil.zsRevGetWithScore(gameUserKey, 0, -1); long t4 = System.currentTimeMillis(); - log.info("查询分数耗时:{}", t4-t3); + log.info("查询分数耗时:{}", t4 - t3); if (CollectionUtil.isEmpty(typedTuples)) { return gameMessageCountOut; } long t5 = System.currentTimeMillis(); - log.info("判断分数是否为空耗时:{}", t5-t4); + log.info("判断分数是否为空耗时:{}", t5 - t4); log.info("查询耗时:{}", t5 - t8); - for(ZSetOperations.TypedTuple type : typedTuples) { + for (ZSetOperations.TypedTuple type : typedTuples) { long a1 = System.currentTimeMillis(); - ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class); + ClientDto.RedisUser user = JSON.parseObject((String) type.getValue(), ClientDto.RedisUser.class); long a2 = System.currentTimeMillis(); - log.info("json转换耗时:{}", a2-a1); + log.info("json转换耗时:{}", a2 - a1); if (user.getUserId().longValue() == Long.parseLong(userId)) { int score = type.getScore().intValue(); long a3 = System.currentTimeMillis(); - log.info("获取分数耗时:{}", a3-a2); + log.info("获取分数耗时:{}", a3 - a2); String userStatus = GameConstant.generateGameStatusKey(recordIdLong); long a4 = System.currentTimeMillis(); - log.info("生成游戏状态key耗时:{}", a4-a3); - String gameStausObj = (String)redisUtil.get(userStatus); + log.info("生成游戏状态key耗时:{}", a4 - a3); + String gameStausObj = (String) redisUtil.get(userStatus); long a5 = System.currentTimeMillis(); - log.info("获取状态耗时:{}", a5-a4); - log.info("gameStatus:{},equals:{}",gameStausObj, gameStausObj.equals(String.valueOf(GameConstant.GAME_COMPLETED))); - if (StrUtil.isBlank(gameStausObj) || gameStausObj.equals(String.valueOf(GameConstant.GAME_COMPLETED))){ - gameMessageCountOut = new GameMessageCountOut(score/100, score); + log.info("获取状态耗时:{}", a5 - a4); + log.info("gameStatus:{},equals:{}", gameStausObj, gameStausObj.equals(String.valueOf(GameConstant.GAME_COMPLETED))); + if (StrUtil.isBlank(gameStausObj) || gameStausObj.equals(String.valueOf(GameConstant.GAME_COMPLETED))) { + gameMessageCountOut = new GameMessageCountOut(score / 100, score); return gameMessageCountOut; } long a6 = System.currentTimeMillis(); - log.info("判断游戏结束耗时:{}", a6-a5); + log.info("判断游戏结束耗时:{}", a6 - a5); score += 100; redisUtil.zsSet(gameUserKey, JSON.toJSONString(user), score); long a7 = System.currentTimeMillis(); - log.info("更新redis耗时:{}", a7-a6); + log.info("更新redis耗时:{}", a7 - a6); gameMessageCountOut.getData().setTotalScore(score); - gameMessageCountOut.getData().setTotalTimes(score/100); + gameMessageCountOut.getData().setTotalTimes(score / 100); long t = System.currentTimeMillis(); log.info("循环耗时:{}", t - t5); - log.info("返回耗时:{},循环耗时:{}", t-a7, t-t5); + log.info("返回耗时:{},循环耗时:{}", t - a7, t - t5); return gameMessageCountOut; } - }; + } + ; long t = System.currentTimeMillis(); - log.info("总耗时:{}", t- t1); + log.info("总耗时:{}", t - t1); // if (ObjectUtil.isNotNull(userId)) { // GameUserJoinExample gameUserJoinExample = new GameUserJoinExample(); @@ -440,4 +441,14 @@ public class ClientService implements IClientService { // } return gameMessageCountOut; } + + /** + * 查询游戏是否是分组游戏 + */ + public List isGroupGame(Long recordId) { + GameGroupExample gameGroupExample = new GameGroupExample(); + gameGroupExample.createCriteria().andRecordIdEqualTo(recordId); + List gameGroupList = gameGroupDao.selectByExample(gameGroupExample); + return gameGroupList; + } } diff --git a/game/src/main/java/com/ccsens/game/service/ScreenService.java b/game/src/main/java/com/ccsens/game/service/ScreenService.java index 4aa50af9..cefa8c36 100644 --- a/game/src/main/java/com/ccsens/game/service/ScreenService.java +++ b/game/src/main/java/com/ccsens/game/service/ScreenService.java @@ -41,7 +41,9 @@ import java.util.concurrent.*; @Slf4j @Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) -public class ScreenService implements IScreenService{ +public class ScreenService implements IScreenService { + @Autowired + private ClientService clientService; @Autowired private GameUserPayDao gameUserPayDao; @Autowired @@ -74,6 +76,7 @@ public class ScreenService implements IScreenService{ /** * 传入用户信息,返回游戏大屏路径 + * * @return */ @Override @@ -84,10 +87,10 @@ public class ScreenService implements IScreenService{ GameTypeExample gameTypeExample = new GameTypeExample(); gameTypeExample.createCriteria().andCodeEqualTo(memberGame.getGameType()); List gameTypeList = gameTypeDao.selectByExample(gameTypeExample); - if(CollectionUtil.isNotEmpty(gameTypeList)){ + if (CollectionUtil.isNotEmpty(gameTypeList)) { gameType = gameTypeList.get(0); } - if(ObjectUtil.isNull(gameType)){ + if (ObjectUtil.isNull(gameType)) { throw new BaseException(CodeEnum.NOT_GAME_TYPE); } //2、查找此用户购买的此游戏的信息,若没有则添加一条记录,默认已付款,结束时间为添加后的一个月,默认次数为10次 @@ -95,9 +98,9 @@ public class ScreenService implements IScreenService{ GameUserPayExample gameUserPayExample = new GameUserPayExample(); gameUserPayExample.createCriteria().andUserIdEqualTo(params.getUserId()).andGameTypeIdEqualTo(gameType.getId()); List gameUserPayList = gameUserPayDao.selectByExample(gameUserPayExample); - if(CollectionUtil.isNotEmpty(gameUserPayList)){ + if (CollectionUtil.isNotEmpty(gameUserPayList)) { gameUserPay = gameUserPayList.get(0); - }else { + } else { gameUserPay = new GameUserPay(); gameUserPay.setId(snowflake.nextId()); gameUserPay.setUserId(params.getUserId()); @@ -105,11 +108,11 @@ public class ScreenService implements IScreenService{ gameUserPay.setTotalCount(10); gameUserPay.setUsedCount(0); gameUserPay.setCreatedTime(System.currentTimeMillis()); - gameUserPay.setDueTime(gameUserPay.getCreatedTime() + (3600*24*30)); + gameUserPay.setDueTime(gameUserPay.getCreatedTime() + (3600 * 24 * 30)); gameUserPayDao.insertSelective(gameUserPay); } //查询该任务下的游戏记录信息 - if(ObjectUtil.isNotNull(memberGame.getTaskId())) { + if (ObjectUtil.isNotNull(memberGame.getTaskId())) { GameRecordExample recordExample = new GameRecordExample(); recordExample.createCriteria().andTaskIdEqualTo(memberGame.getTaskId()); recordExample.setOrderByClause("created_at DESC"); @@ -127,20 +130,30 @@ public class ScreenService implements IScreenService{ gameRecord.setId(snowflake.nextId()); gameRecord.setUserPayId(gameUserPay.getId()); gameRecord.setTaskId(memberGame.getTaskId()); - gameRecord.setUrl(WebConstant.TEST_URL_GAME + gameType.getScreenUrl() + "?id="+gameRecord.getId()); - gameRecord.setQrCodeUrl(WebConstant.TEST_URL_GAME + gameRecord.getId() + File.separator + gameType.getClientUrl()); + //添加路径 + String gameUrl = WebConstant.TEST_URL_GAME_SQ; + switch (gameType.getCode()){ + case GameConstant.GAME_TYPE_SQ: break; + case GameConstant.GAME_TYPE_SP: gameUrl = WebConstant.TEST_URL_GAME_SP; break; + case GameConstant.GAME_TYPE_BH: gameUrl = WebConstant.TEST_URL_GAME_BH; break; + default:break; + } + gameRecord.setUrl(gameUrl + gameType.getScreenUrl() + "?id=" + gameRecord.getId()); + gameRecord.setQrCodeUrl(gameUrl + gameRecord.getId() + File.separator + gameType.getClientUrl()); gameRecordDao.insertSelective(gameRecord); //4、 判断是否有分组,如果是分组游戏,添加两条分组信息 - if(gameType.getIsGroup() == 1){ + if (gameType.getIsGroup() == 1) { GameGroup gameGroupRed = new GameGroup(); gameGroupRed.setId(snowflake.nextId()); gameGroupRed.setRecordId(gameRecord.getId()); gameGroupRed.setName(memberGame.getFirstTeam()); + gameGroupRed.setCode(GameConstant.FIRST_GROUP); gameGroupDao.insertSelective(gameGroupRed); GameGroup gameGroupBlue = new GameGroup(); gameGroupBlue.setId(snowflake.nextId()); gameGroupBlue.setRecordId(gameRecord.getId()); gameGroupBlue.setName(memberGame.getSecondTeam()); + gameGroupBlue.setCode(GameConstant.SECOND_GROUP); gameGroupDao.insertSelective(gameGroupBlue); } @@ -153,15 +166,15 @@ public class ScreenService implements IScreenService{ urlVo.setRuleList(ruleList); //路径(添加项目id) - String url = gameRecord.getUrl() + "&projectId="+ memberGame.getProjectId(); + String url = gameRecord.getUrl() + "&projectId=" + memberGame.getProjectId(); //给所有人发送消息发送消息 - ChromeMessageDto chromeMessageDto = new ChromeMessageDto(url,gameRecord.getId(),memberGame.getProjectId()); + ChromeMessageDto chromeMessageDto = new ChromeMessageDto(url, gameRecord.getId(), memberGame.getProjectId()); BaseMessageDto.MessageUser messageUser = null; List messageUserList = new ArrayList<>(); //获取项目下所有成员 List memberIdList = tallFeignClient.getMemberIdListByProject(memberGame.getProjectId()); - if(CollectionUtil.isNotEmpty(memberIdList)){ - for(Long memberId:memberIdList){ + if (CollectionUtil.isNotEmpty(memberIdList)) { + for (Long memberId : memberIdList) { messageUser = new BaseMessageDto.MessageUser(); messageUser.setUserId(memberId); messageUserList.add(messageUser); @@ -175,6 +188,7 @@ public class ScreenService implements IScreenService{ /** * 获取游戏基本信息 + * * @return */ @Override @@ -187,7 +201,7 @@ public class ScreenService implements IScreenService{ GameRecord gameRecord = getGameRecord(memberRecord.getMemberRecord()); GameUserPay gameUserPay = gameUserPayDao.selectByPrimaryKey(gameRecord.getUserPayId()); - if(ObjectUtil.isNull(gameUserPay)){ + if (ObjectUtil.isNull(gameUserPay)) { throw new BaseException(CodeEnum.NOT_GAME_TYPE); } @@ -204,23 +218,10 @@ public class ScreenService implements IScreenService{ } //总人数 gameInfoVo.setTotalMembers(totalUsers == null ? 0 : totalUsers.intValue()); - - //获取分组信息 - GameGroupExample gameGroupExample = new GameGroupExample(); - gameGroupExample.createCriteria().andRecordIdEqualTo(gameRecord.getId()); - List gameGroupList = gameGroupDao.selectByExample(gameGroupExample); - if(CollectionUtil.isNotEmpty(gameGroupList)){ - List groups = new ArrayList<>(); - for(GameGroup gameGroup:gameGroupList){ - ScreenVo.Group group = new ScreenVo.Group(); - group.setGroupId(gameGroup.getId()); - group.setGroupName(gameGroup.getName()); - groups.add(group); - } - gameInfoVo.setGroups(groups); - } - - switch (gameInfoVo.getGameStatus()){ + //获取分组的信息 + List groupVo = getGroupScore(gameRecord.getId()); + gameInfoVo.setGroups(groupVo); + switch (gameInfoVo.getGameStatus()) { case 0: ScreenVo.PendingData pendingData = new ScreenVo.PendingData(); //查询活动规则 @@ -236,9 +237,9 @@ public class ScreenService implements IScreenService{ break; case 1: ScreenVo.PreparingData preparingData = new ScreenVo.PreparingData(); - if(ObjectUtil.isNull(params.getUserId())){ + if (ObjectUtil.isNull(params.getUserId())) { preparingData.setStartLocalTime(gameRecord.getStartTime() + gameRecord.getTimeDifference()); - }else { + } else { GameUserJoin userJoin = getGameUserJoin(params.getUserId(), gameRecord.getId()); log.info("用户信息:{}", userJoin); if (userJoin == null) { @@ -254,9 +255,9 @@ public class ScreenService implements IScreenService{ //查询游戏是否有分组 GameType gameType = gameTypeDao.selectByPrimaryKey(gameUserPay.getGameTypeId()); ScreenVo.CompletedData completedData = null; - if(gameType.getIsGroup() == 0) { + if (gameType.getIsGroup() == 0) { completedData = getCompletedData(memberRecord.getMemberRecord()); - }else { + } else { completedData = getCompletedDataByWin(memberRecord.getMemberRecord()); } gameInfoVo.setCompletedData(completedData); @@ -268,7 +269,6 @@ public class ScreenService implements IScreenService{ } - /** * 获取游戏状态 */ @@ -284,8 +284,9 @@ public class ScreenService implements IScreenService{ GameType gameType = gameTypeDao.selectByPrimaryKey(gameUserPay.getGameTypeId()); gameStatusVo.setGameStatus(gameRecord.getGameStatus()); - - + //获取分组的信息 + List groupVo = getGroupScore(gameRecord.getId()); + gameStatusVo.setGroups(groupVo); // 查询总人数 Long total = redisUtil.zsGetSize(GameConstant.generateGameKey(gameRecord.getId())); log.info("redis查询gameRecordID:{}总人数:{}", gameRecord.getId(), total); @@ -296,41 +297,42 @@ public class ScreenService implements IScreenService{ } gameStatusVo.setTotalMembers(total == null ? 0 : total.intValue()); - switch (gameStatusVo.getGameStatus()){ - case 0: + + switch (gameStatusVo.getGameStatus()) { + case GameConstant.GAME_PENDING: break; - case 1: + case GameConstant.GAME_PREPARATION: ScreenVo.PreparingData preparingData = new ScreenVo.PreparingData(); - if(ObjectUtil.isNull(params.getUserId())){ + if (ObjectUtil.isNull(params.getUserId())) { // 无userId,默认大屏,获取大屏的游戏开始时间 preparingData.setStartLocalTime(gameRecord.getStartTime() + gameRecord.getTimeDifference()); - }else { + } else { // 获取用户的开始时间 GameUserJoin userJoin = getGameUserJoin(params.getUserId(), gameRecord.getId()); preparingData.setStartLocalTime(gameRecord.getStartTime() + userJoin.getTimeDifference()); } gameStatusVo.setPreparingData(preparingData); break; - case 2: + case GameConstant.GAME_PROCESSING: //查询游戏是否有分组 - if(gameType.getIsGroup() == 0) { + if (gameType.getIsGroup() == 0) { //普通游戏返回前十名的信息 ScreenVo.ProcessingData processingData = new ScreenVo.ProcessingData(); // 查询前十名(列表顺序即前十名顺序) List tops = getTopUsers(gameRecordId); processingData.setTopMembers(tops); gameStatusVo.setProcessingData(processingData); - }else { + } else { //分组游戏则返回每个分组的信息 List groupVoList = getGroupScore(gameRecordId); gameStatusVo.setProcessingData(groupVoList); } break; - case 3: + case GameConstant.GAME_COMPLETED: ScreenVo.CompletedData completedData = null; - if(gameType.getIsGroup() == 0) { + if (gameType.getIsGroup() == 0) { completedData = getCompletedData(memberRecord.getMemberRecord()); - }else { + } else { completedData = getCompletedDataByWin(memberRecord.getMemberRecord()); } gameStatusVo.setCompletedData(completedData); @@ -344,20 +346,21 @@ public class ScreenService implements IScreenService{ /** * 进行中查询每个组的信息 */ - private List getGroupScore(Long gameRecordId){ + private List getGroupScore(Long gameRecordId) { List groupVoList = new ArrayList<>(); //查询分组信息 GameGroupExample gameGroupExample = new GameGroupExample(); gameGroupExample.createCriteria().andRecordIdEqualTo(gameRecordId); List gameGroupList = gameGroupDao.selectByExample(gameGroupExample); - if(CollectionUtil.isNotEmpty(gameGroupList)){ - for(GameGroup gameGroup : gameGroupList){ + if (CollectionUtil.isNotEmpty(gameGroupList)) { + for (GameGroup gameGroup : gameGroupList) { ScreenVo.GroupVo groupVo = new ScreenVo.GroupVo(); groupVo.setGroupId(gameGroup.getId()); groupVo.setGroupName(gameGroup.getName()); + groupVo.setCode(gameGroup.getCode()); - Map group = getGroupTotalScore(gameGroup.getId()); - if(CollectionUtil.isNotEmpty(group)){ + Map group = getGroupTotalScore(gameGroup.getId()); + if (CollectionUtil.isNotEmpty(group)) { List userJoinList = (List) group.get("userJoinList"); groupVo.setScore((Integer) group.get("totalScore")); groupVo.setTotalMembers(userJoinList.size()); @@ -381,17 +384,17 @@ public class ScreenService implements IScreenService{ GameGroupExample gameGroupExample = new GameGroupExample(); gameGroupExample.createCriteria().andRecordIdEqualTo(gameRecordId); List gameGroupList = gameGroupDao.selectByExample(gameGroupExample); - if(CollectionUtil.isNotEmpty(gameGroupList)){ + if (CollectionUtil.isNotEmpty(gameGroupList)) { List userJoinList = null; //分别查找两个队伍的总分 - Map group1 = getGroupTotalScore(gameGroupList.get(0).getId()); - Map group2 = getGroupTotalScore(gameGroupList.get(1).getId()); - if(CollectionUtil.isNotEmpty(group1) && CollectionUtil.isNotEmpty(group2)){ + Map group1 = getGroupTotalScore(gameGroupList.get(0).getId()); + Map group2 = getGroupTotalScore(gameGroupList.get(1).getId()); + if (CollectionUtil.isNotEmpty(group1) && CollectionUtil.isNotEmpty(group2)) { int score1 = (int) group1.get("totalScore"); int score2 = (int) group1.get("totalScore"); - if(score1 > score2){ + if (score1 > score2) { userJoinList = (List) group1.get("userJoinList"); - }else { + } else { userJoinList = (List) group2.get("userJoinList"); } } @@ -400,27 +403,28 @@ public class ScreenService implements IScreenService{ } return completedData; } + /** * 获取队伍的总分 */ - private Map getGroupTotalScore(Long groupId){ - Map groupMap = null; + private Map getGroupTotalScore(Long groupId) { + Map groupMap = null; //查找队伍的参赛人 GameUserJoinGroupExample joinGroupExample = new GameUserJoinGroupExample(); joinGroupExample.createCriteria().andGameGroupIdEqualTo(groupId); List userJoinGroupList = gameUserJoinGroupDao.selectByExample(joinGroupExample); - if(CollectionUtil.isNotEmpty(userJoinGroupList)){ + if (CollectionUtil.isNotEmpty(userJoinGroupList)) { groupMap = new HashMap<>(); int totalScore = 0; List userJoinList = new ArrayList<>(); - for (GameUserJoinGroup userJoinGroup : userJoinGroupList){ + for (GameUserJoinGroup userJoinGroup : userJoinGroupList) { GameUserJoin userJoin = gameUserJoinDao.selectByPrimaryKey(userJoinGroup.getUserJoinId()); totalScore += userJoin.getScore(); userJoinList.add(userJoin); } - groupMap.put("totalScore",totalScore); - groupMap.put("userJoinList",userJoinList); + groupMap.put("totalScore", totalScore); + groupMap.put("userJoinList", userJoinList); } return groupMap; } @@ -428,6 +432,7 @@ public class ScreenService implements IScreenService{ /** * 查询总分数,总次数,平均以及前十名 + * * @param gameRecordId * @return */ @@ -441,10 +446,10 @@ public class ScreenService implements IScreenService{ //总分数 completedData.setTotalScore(totalScore); // 总次数 - completedData.setTotalTimes(totalScore/100); + completedData.setTotalTimes(totalScore / 100); // 平均次数 int totalUsers = redisUtil.zsGetSize(key).intValue(); - int averageTimes = completedData.getTotalTimes()/totalUsers; + int averageTimes = completedData.getTotalTimes() / totalUsers; completedData.setAverageTimes(averageTimes); // 平均超过多少用户 int averageScore = totalScore / totalUsers; @@ -458,7 +463,7 @@ public class ScreenService implements IScreenService{ GameUserJoinExample gameuserJoinExample = new GameUserJoinExample(); gameuserJoinExample.createCriteria().andRecordIdEqualTo(gameRecordId); List userJoins = gameUserJoinDao.selectByExample(gameuserJoinExample); - if(CollectionUtil.isNotEmpty(userJoins)){ + if (CollectionUtil.isNotEmpty(userJoins)) { completedData = getCompletedData(userJoins); } } @@ -471,6 +476,7 @@ public class ScreenService implements IScreenService{ /** * 查询参加游戏的用户信息 + * * @param userId * @param gameRecordId * @return @@ -488,16 +494,17 @@ public class ScreenService implements IScreenService{ /** * 查询游戏信息 + * * @param gameRecordId * @return */ @Override public GameRecord getGameRecord(long gameRecordId) { - String gameRecordStr = (String)redisUtil.get(GameConstant.generateGameStatusKey(gameRecordId)); + String gameRecordStr = (String) redisUtil.get(GameConstant.generateGameStatusKey(gameRecordId)); GameRecord gameRecord = null; if (StrUtil.isBlank(gameRecordStr)) { gameRecord = gameRecordDao.selectByPrimaryKey(gameRecordId); - if(ObjectUtil.isNull(gameRecord)){ + if (ObjectUtil.isNull(gameRecord)) { throw new BaseException(CodeEnum.NOT_GAME_RECORD); } } else { @@ -508,6 +515,7 @@ public class ScreenService implements IScreenService{ /** * 查询前十名 + * * @param gameRecordId * @return */ @@ -525,7 +533,7 @@ public class ScreenService implements IScreenService{ topUser.setHeadImgUrl(user.getAvatarUrl()); topUser.setNickname(user.getNickname()); topUser.setScore(type.getScore().intValue()); - topUser.setTimes(type.getScore().intValue()/100); + topUser.setTimes(type.getScore().intValue() / 100); finalTops.add(topUser); }); @@ -542,44 +550,52 @@ public class ScreenService implements IScreenService{ public String startAgain(QueryDto params) throws Exception { ScreenDto.MemberRecord memberRecord = params.getParam(); GameRecord gameRecord = gameRecordDao.selectByPrimaryKey(memberRecord.getMemberRecord()); - if(ObjectUtil.isNull(gameRecord)){ + if (ObjectUtil.isNull(gameRecord)) { throw new BaseException(CodeEnum.NOT_GAME_RECORD); } GameUserPay gameUserPay = gameUserPayDao.selectByPrimaryKey(gameRecord.getUserPayId()); - if(ObjectUtil.isNull(gameUserPay)){ + if (ObjectUtil.isNull(gameUserPay)) { throw new BaseException(CodeEnum.NOT_GAME_TYPE); } GameType gameType = gameTypeDao.selectByPrimaryKey(gameUserPay.getGameTypeId()); - if(ObjectUtil.isNull(gameType)){ + if (ObjectUtil.isNull(gameType)) { throw new BaseException(CodeEnum.NOT_GAME_TYPE); } - if(gameUserPay.getUsedCount() >= gameUserPay.getTotalCount()){ - throw new BaseException(CodeEnum. GAME_NOT_TIMES); + if (gameUserPay.getUsedCount() >= gameUserPay.getTotalCount()) { + throw new BaseException(CodeEnum.GAME_NOT_TIMES); } GameRecord gameRecordNew = null; - if(gameRecord.getGameStatus() == 3){ + if (gameRecord.getGameStatus() == 3) { //添加一场新的游戏记录 gameRecordNew = new GameRecord(); gameRecordNew.setId(snowflake.nextId()); gameRecordNew.setUserPayId(gameUserPay.getId()); - gameRecordNew.setUrl(WebConstant.TEST_URL_GAME + gameType.getScreenUrl() + "?id="+gameRecordNew.getId()); - gameRecordNew.setQrCodeUrl(WebConstant.TEST_URL_GAME + gameRecordNew.getId() + File.separator + gameType.getClientUrl()); + //添加路径 + String gameUrl = WebConstant.TEST_URL_GAME_SQ; + switch (gameType.getCode()){ + case GameConstant.GAME_TYPE_SQ: break; + case GameConstant.GAME_TYPE_SP: gameUrl = WebConstant.TEST_URL_GAME_SP ; break; + case GameConstant.GAME_TYPE_BH: gameUrl = WebConstant.TEST_URL_GAME_BH; break; + default:break; + } + gameRecordNew.setUrl(gameUrl + gameType.getScreenUrl() + "?id=" + gameRecordNew.getId()); + gameRecordNew.setQrCodeUrl(gameUrl+ gameRecordNew.getId() + File.separator + gameType.getClientUrl()); gameRecordDao.insertSelective(gameRecordNew); //修改购买的游戏的使用次数 gameUserPay.setUsedCount(gameUserPay.getUsedCount() + 1); gameUserPayDao.updateByPrimaryKeySelective(gameUserPay); //路径(添加项目id) - String url = gameRecord.getUrl() + "&projectId="+ memberRecord.getProjectId(); + String url = gameRecord.getUrl() + "&projectId=" + memberRecord.getProjectId(); //给所有人发送消息发送消息 - ChromeMessageDto chromeMessageDto = new ChromeMessageDto(url,gameRecord.getId(),memberRecord.getProjectId()); + ChromeMessageDto chromeMessageDto = new ChromeMessageDto(url, gameRecord.getId(), memberRecord.getProjectId()); BaseMessageDto.MessageUser messageUser = null; List messageUserList = new ArrayList<>(); //获取项目下所有成员 List memberIdList = tallFeignClient.getMemberIdListByProject(memberRecord.getProjectId()); - if(CollectionUtil.isNotEmpty(memberIdList)){ - for(Long memberId:memberIdList){ + if (CollectionUtil.isNotEmpty(memberIdList)) { + for (Long memberId : memberIdList) { messageUser = new BaseMessageDto.MessageUser(); messageUser.setUserId(memberId); messageUserList.add(messageUser); @@ -588,7 +604,7 @@ public class ScreenService implements IScreenService{ chromeMessageDto.setReceivers(messageUserList); messageService.sendGameMessageWithGetUrl(chromeMessageDto); - }else { + } else { throw new BaseException(CodeEnum.GAME_NO_END); } return gameRecordNew.getUrl(); @@ -598,10 +614,10 @@ public class ScreenService implements IScreenService{ * 查找活动规则 */ @Override - public List getGameActivityRule(Long gameTypeId){ + public List getGameActivityRule(Long gameTypeId) { List ruleList = new ArrayList<>(); - if(ObjectUtil.isNotNull(gameTypeId)) { + if (ObjectUtil.isNotNull(gameTypeId)) { GameActivityRuleExample activityRuleExample = new GameActivityRuleExample(); activityRuleExample.createCriteria().andGameIdEqualTo(gameTypeId); List gameActivityRuleList = activityRuleDao.selectByExample(activityRuleExample); @@ -613,14 +629,15 @@ public class ScreenService implements IScreenService{ } return ruleList; } + /** * 查询活动奖品 */ @Override - public List getGameActivityPrize(Long gameTypeId){ + public List getGameActivityPrize(Long gameTypeId) { List prizeList = new ArrayList<>(); - if(ObjectUtil.isNotNull(gameTypeId)) { + if (ObjectUtil.isNotNull(gameTypeId)) { GameActivityPrizeExample activityPrizeExample = new GameActivityPrizeExample(); activityPrizeExample.createCriteria().andGameIdEqualTo(gameTypeId); List gameActivityPrizeList = activityPrizeDao.selectByExample(activityPrizeExample); @@ -632,14 +649,15 @@ public class ScreenService implements IScreenService{ } return prizeList; } + /** * 奖券使用说明表 */ @Override - public List getGamePrizeInstructions(Long gameTypeId){ + public List getGamePrizeInstructions(Long gameTypeId) { List instructionsList = new ArrayList<>(); - if(ObjectUtil.isNotNull(gameTypeId)) { + if (ObjectUtil.isNotNull(gameTypeId)) { GamePrizeInstructionsExample prizeInstructionsExample = new GamePrizeInstructionsExample(); prizeInstructionsExample.createCriteria().andGameIdEqualTo(gameTypeId); List gamePrizeInstructionsList = prizeInstructionsDao.selectByExample(prizeInstructionsExample); @@ -655,21 +673,21 @@ public class ScreenService implements IScreenService{ /** * 已结束时的返回值 */ - private ScreenVo.CompletedData getCompletedData(List userJoinList){ + private ScreenVo.CompletedData getCompletedData(List userJoinList) { ScreenVo.CompletedData completedData = new ScreenVo.CompletedData(); int totalTimes = 0; int totalScore = 0; int averageTimes = 0; int over = 0; - if(CollectionUtil.isNotEmpty(userJoinList)){ - for(GameUserJoin userJoin : userJoinList){ + if (CollectionUtil.isNotEmpty(userJoinList)) { + for (GameUserJoin userJoin : userJoinList) { totalTimes += userJoin.getTimes(); totalScore += userJoin.getScore(); } averageTimes = totalTimes / userJoinList.size(); int a = 0; - for(GameUserJoin userJoin : userJoinList){ - if(userJoin.getTimes() < averageTimes){ + for (GameUserJoin userJoin : userJoinList) { + if (userJoin.getTimes() < averageTimes) { a++; } } @@ -683,11 +701,9 @@ public class ScreenService implements IScreenService{ } - - - /** * 开始游戏 + * * @param start * @return */ @@ -704,8 +720,10 @@ public class ScreenService implements IScreenService{ } //判断游戏状态,仅未开始允许操作开始 switch (gameRecord.getGameStatus().byteValue()) { - case GameConstant.GAME_PREPARATION: throw new BaseException(CodeEnum.GAME_PREPARATION); - case GameConstant.GAME_PROCESSING: throw new BaseException(CodeEnum.GAME_PROCESSING); + case GameConstant.GAME_PREPARATION: + throw new BaseException(CodeEnum.GAME_PREPARATION); + case GameConstant.GAME_PROCESSING: + throw new BaseException(CodeEnum.GAME_PROCESSING); case GameConstant.GAME_COMPLETED: if (start.getStartStatus() == null || start.getStartStatus().byteValue() != GameConstant.GAME_RESTART_STATUS) { throw new BaseException(CodeEnum.GAME_COMPLETED); @@ -716,7 +734,7 @@ public class ScreenService implements IScreenService{ gameRecord.setGameStatus(GameConstant.GAME_PREPARATION); gameRecord.setStartTime(current + GameConstant.COUNT_DOWN_TIME); gameRecord.setEndTime(gameRecord.getStartTime() + GameConstant.GAME_TIME); - gameRecord.setTimeDifference((int)moreTime); + gameRecord.setTimeDifference((int) moreTime); gameRecordDao.updateByPrimaryKeySelective(gameRecord); //设置redis 游戏状态 准备中 redisUtil.set(GameConstant.generateGameStatusKey(gameRecord.getId()), JSON.toJSONString(gameRecord), GameConstant.REDIS_TIME); @@ -727,7 +745,7 @@ public class ScreenService implements IScreenService{ delUserJoin.setRecStatus(WebConstant.REC_STATUS.Deleted.value); GameUserJoinExample example = new GameUserJoinExample(); example.createCriteria().andRecordIdEqualTo(start.getMemberRecord()); - gameUserJoinDao.updateByExampleSelective(delUserJoin,example); + gameUserJoinDao.updateByExampleSelective(delUserJoin, example); redisUtil.del(GameConstant.generateGameKey(start.getMemberRecord())); } //延时通知 @@ -758,7 +776,7 @@ public class ScreenService implements IScreenService{ GameUserJoin join = new GameUserJoin(); join.setId(user.getId()); join.setScore(score); - join.setTimes(score/100); + join.setTimes(score / 100); gameUserJoinDao.updateByPrimaryKeySelective(join); }); } @@ -778,8 +796,15 @@ public class ScreenService implements IScreenService{ List userJoins = gameUserJoinDao.selectByExample(joinExample); if (CollectionUtil.isNotEmpty(userJoins)) { - userJoins.forEach( userJoin -> { - ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin); + userJoins.forEach(userJoin -> { + Long groupId = null; + GameUserJoinGroupExample joinGroupExample = new GameUserJoinGroupExample(); + joinGroupExample.createCriteria().andUserJoinIdEqualTo(userJoin.getId()); + List gameUserJoinGroups = gameUserJoinGroupDao.selectByExample(joinGroupExample); + if (CollectionUtil.isNotEmpty(gameUserJoinGroups)) { + groupId = gameUserJoinGroups.get(0).getGameGroupId(); + } + ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin, groupId); redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME); }); } @@ -791,16 +816,16 @@ public class ScreenService implements IScreenService{ } - /** * 推送客户端状态 + * * @param gameRecord * @param executor */ private void pushClient(GameRecord gameRecord, ScheduledExecutorService executor) { - new Thread(()->{ - sendMsg.sendMsg(executor, 0, ()->{ + new Thread(() -> { + sendMsg.sendMsg(executor, 0, () -> { try { //查询游戏用户,通知游戏倒计时 List userJoins = sendMsg.getAllScore(gameRecord); @@ -811,7 +836,7 @@ public class ScreenService implements IScreenService{ } }); long startTime = gameRecord.getStartTime() - System.currentTimeMillis(); - sendMsg.sendMsg(executor, startTime > 0 ? startTime : 0, ()->{ + sendMsg.sendMsg(executor, startTime > 0 ? startTime : 0, () -> { //查询游戏用户,通知游戏开始 List userJoins = sendMsg.getAllScore(gameRecord); try { @@ -824,7 +849,7 @@ public class ScreenService implements IScreenService{ long endTime = gameRecord.getEndTime() - System.currentTimeMillis(); System.out.println("游戏结束"); - sendMsg.sendMsg(executor, endTime > 0 ? endTime : 0, ()->{ + sendMsg.sendMsg(executor, endTime > 0 ? endTime : 0, () -> { try { //查询游戏用户,通知游戏结束 List userJoins = sendMsg.getAllScore(gameRecord); @@ -844,15 +869,15 @@ public class ScreenService implements IScreenService{ * 查询游戏类型id */ @Override - public Long getGameTypeId(Long recordId){ + public Long getGameTypeId(Long recordId) { Long gameTypeId = null; GameRecord gameRecord = gameRecordDao.selectByPrimaryKey(recordId); - if(ObjectUtil.isNotNull(gameRecord)){ + if (ObjectUtil.isNotNull(gameRecord)) { //获取游戏的购买记录和类型 GameUserPay gameUserPay = gameUserPayDao.selectByPrimaryKey(gameRecord.getUserPayId()); - if(ObjectUtil.isNotNull(gameUserPay)) { + if (ObjectUtil.isNotNull(gameUserPay)) { GameType gameType = gameTypeDao.selectByPrimaryKey(gameUserPay.getGameTypeId()); - if(ObjectUtil.isNotNull(gameType)){ + if (ObjectUtil.isNotNull(gameType)) { gameTypeId = gameType.getId(); } } @@ -867,9 +892,9 @@ public class ScreenService implements IScreenService{ GameGroupExample gameGroupExample = new GameGroupExample(); gameGroupExample.createCriteria().andRecordIdEqualTo(recordId); List gameGroupList = gameGroupDao.selectByExample(gameGroupExample); - if(CollectionUtil.isNotEmpty(gameGroupList)){ + if (CollectionUtil.isNotEmpty(gameGroupList)) { groupList = new ArrayList<>(); - for(GameGroup gameGroup:gameGroupList){ + for (GameGroup gameGroup : gameGroupList) { ScreenVo.Group group = new ScreenVo.Group(); group.setGroupId(gameGroup.getId()); group.setGroupName(gameGroup.getName()); @@ -880,15 +905,15 @@ public class ScreenService implements IScreenService{ } @Override - public ScreenVo.RecordInfo getRecordByTaskId(Long taskId,String gameType) { + public ScreenVo.RecordInfo getRecordByTaskId(Long taskId, String gameType) { ScreenVo.RecordInfo recordInfo = new ScreenVo.RecordInfo(); GameRecordExample recordExample = new GameRecordExample(); recordExample.createCriteria().andTaskIdEqualTo(taskId); recordExample.setOrderByClause("created_at DESC"); List gameRecordList = gameRecordDao.selectByExample(recordExample); - if(CollectionUtil.isNotEmpty(gameRecordList)){ - if(StrUtil.isNotEmpty(gameType)) { + if (CollectionUtil.isNotEmpty(gameRecordList)) { + if (StrUtil.isNotEmpty(gameType)) { for (GameRecord record : gameRecordList) { GameUserPay gameUserPay = gameUserPayDao.selectByPrimaryKey(record.getUserPayId()); if (ObjectUtil.isNotNull(gameUserPay)) { @@ -900,7 +925,7 @@ public class ScreenService implements IScreenService{ } } } - }else { + } else { recordInfo.setId(gameRecordList.get(0).getId()); recordInfo.setGameStatus(gameRecordList.get(0).getGameStatus()); } diff --git a/game/src/main/java/com/ccsens/game/util/GameConstant.java b/game/src/main/java/com/ccsens/game/util/GameConstant.java index cc4a12c5..a82ac891 100644 --- a/game/src/main/java/com/ccsens/game/util/GameConstant.java +++ b/game/src/main/java/com/ccsens/game/util/GameConstant.java @@ -6,6 +6,17 @@ package com.ccsens.game.util; * @create: 2019/12/26 17:32 */ public class GameConstant { + /**游戏类型:数钱*/ + public static final String GAME_TYPE_SQ = "SQ"; + /**游戏类型:赛跑*/ + public static final String GAME_TYPE_SP = "SP"; + /**游戏类型:拔河*/ + public static final String GAME_TYPE_BH = "BH"; + + /**游戏类型:第一个队伍*/ + public static final String FIRST_GROUP = "firstGroup"; + /**游戏类型:第一个队伍*/ + public static final String SECOND_GROUP = "secondGroup"; /**游戏状态:未开始*/ public static final byte GAME_PENDING = 0; diff --git a/game/src/main/java/com/ccsens/game/util/SendMsg.java b/game/src/main/java/com/ccsens/game/util/SendMsg.java index 35ee2d4d..a406705a 100644 --- a/game/src/main/java/com/ccsens/game/util/SendMsg.java +++ b/game/src/main/java/com/ccsens/game/util/SendMsg.java @@ -1,6 +1,7 @@ package com.ccsens.game.util; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.ccsens.game.bean.dto.ClientDto; import com.ccsens.game.bean.dto.message.ChangeStatusMessageDto; @@ -10,10 +11,12 @@ import com.ccsens.game.bean.po.GameGroupExample; import com.ccsens.game.bean.po.GameRecord; import com.ccsens.game.persist.dao.GameGroupDao; import com.ccsens.game.persist.dao.GameUserJoinDao; +import com.ccsens.game.service.ClientService; import com.ccsens.util.JacksonUtil; import com.ccsens.util.RedisUtil; import com.ccsens.util.config.RabbitMQConfig; import com.fasterxml.jackson.core.JsonProcessingException; +import io.swagger.models.auth.In; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -35,6 +38,8 @@ import java.util.concurrent.TimeUnit; @Service @Slf4j public class SendMsg { + @Autowired + private ClientService clientService; @Autowired private GameUserJoinDao gameUserJoinDao; @Autowired @@ -52,16 +57,16 @@ public class SendMsg { return; } userJoins.forEach(join -> { - outs.add(getMsg(gameRecord, join, status)); + outs.add(getMsg(userJoins, gameRecord, join, status)); }); if (CollectionUtil.isNotEmpty(outs)) { rabbitTemplate.convertAndSend(RabbitMQConfig.GAME_STATUS, JacksonUtil.beanToJson(outs)); - log.info("发送成功:{}",JacksonUtil.beanToJson(outs)); + log.info("发送成功:{}", JacksonUtil.beanToJson(outs)); } } - public GameMessageWithChangeStatusOut getMsg(GameRecord gameRecord, ClientDto.RedisUser join, byte gameStatus) { + public GameMessageWithChangeStatusOut getMsg(List userJoins, GameRecord gameRecord, ClientDto.RedisUser join, byte gameStatus) { GameMessageWithChangeStatusOut out = new GameMessageWithChangeStatusOut(); GameMessageWithChangeStatusOut.Data data = new GameMessageWithChangeStatusOut.Data(); @@ -71,12 +76,32 @@ public class SendMsg { ChangeStatusMessageDto dtos = new ChangeStatusMessageDto(); switch (gameStatus) { case GameConstant.GAME_COMPLETED: - ChangeStatusMessageDto.CompletedData completedData = new ChangeStatusMessageDto.CompletedData(); - completedData.setTimes(join.getTimes()); - completedData.setScore(join.getScore()); - completedData.setSort(join.getSort()); - completedData.setOver(join.getOver()); - dtos.setCompletedData(completedData); + if (ObjectUtil.isNotNull(join.getGroupId())) { + Integer times = 0; + Integer score = 0; + Integer sort = 1; + for (ClientDto.RedisUser user : userJoins) { + if (user.getGroupId().longValue() == join.getGroupId().longValue()) { + times += user.getTimes(); + score += user.getScore(); + if(user.getScore() > join.getScore()){ + sort++; + } + } + } + ChangeStatusMessageDto.CompletedData completedData = new ChangeStatusMessageDto.CompletedData(); + completedData.setTimes(times); + completedData.setScore(score); + completedData.setSort(sort); + dtos.setCompletedData(completedData); + } else { + ChangeStatusMessageDto.CompletedData completedData = new ChangeStatusMessageDto.CompletedData(); + completedData.setTimes(join.getTimes()); + completedData.setScore(join.getScore()); + completedData.setSort(join.getSort()); + completedData.setOver(join.getOver()); + dtos.setCompletedData(completedData); + } break; case GameConstant.GAME_PREPARATION: //preparingData @@ -115,9 +140,9 @@ public class SendMsg { if (CollectionUtil.isNotEmpty(typedTuples)) { List userJoins = new ArrayList<>(); - typedTuples.forEach( type -> { - ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class); - user.setTimes(type.getScore().intValue()/100); + typedTuples.forEach(type -> { + ClientDto.RedisUser user = JSON.parseObject((String) type.getValue(), ClientDto.RedisUser.class); + user.setTimes(type.getScore().intValue() / 100); user.setScore(type.getScore().intValue()); log.info("{} score: {}", type.getValue(), type.getScore()); Long sort = redisUtil.zsReverseRank(userKey, type.getValue()); @@ -126,7 +151,7 @@ public class SendMsg { Set overs = redisUtil.zsGetByScore(userKey, 0, user.getScore() - 1); Long total = redisUtil.zsGetSize(userKey); log.info("超过用户数:{},总用户数:{}", overs == null ? 0 : overs.size(), total); - user.setOver(overs == null || total == null || total.intValue() == 0 ? 0 : overs.size()*100/total.intValue()); + user.setOver(overs == null || total == null || total.intValue() == 0 ? 0 : overs.size() * 100 / total.intValue()); userJoins.add(user); }); return userJoins; @@ -134,4 +159,6 @@ public class SendMsg { } return null; } + + } diff --git a/game/src/main/resources/mapper_raw/GameGroupMapper.xml b/game/src/main/resources/mapper_raw/GameGroupMapper.xml index 6cf9a973..38e6baa4 100644 --- a/game/src/main/resources/mapper_raw/GameGroupMapper.xml +++ b/game/src/main/resources/mapper_raw/GameGroupMapper.xml @@ -4,6 +4,7 @@ + @@ -68,7 +69,7 @@ - id, record_id, name, created_at, updated_at, rec_status + id, record_id, code, name, created_at, updated_at, rec_status