From 1cb2576515df8e9a73012a3c02d675c35c15a19e Mon Sep 17 00:00:00 2001 From: zhizhi wu <2377881365@qq.com> Date: Sat, 22 Feb 2020 15:58:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=B8=E6=88=8F=E8=AE=A1=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bean/dto/message/GameMessageCountOut.java | 2 +- .../game/netty/wsserver/WebSocketHandler.java | 55 ++++++++++- .../ccsens/game/service/ClientService.java | 94 +++++++++++++++---- .../ccsens/game/service/ScreenService.java | 15 +++ game/src/main/resources/application.yml | 4 +- 5 files changed, 147 insertions(+), 23 deletions(-) diff --git a/game/src/main/java/com/ccsens/game/bean/dto/message/GameMessageCountOut.java b/game/src/main/java/com/ccsens/game/bean/dto/message/GameMessageCountOut.java index a3deb69a..e093c701 100644 --- a/game/src/main/java/com/ccsens/game/bean/dto/message/GameMessageCountOut.java +++ b/game/src/main/java/com/ccsens/game/bean/dto/message/GameMessageCountOut.java @@ -13,7 +13,7 @@ public class GameMessageCountOut extends BaseMessageDto { private int totalScore; } - private Data data; + private Data data = new Data(); public GameMessageCountOut(){ setType(WebConstant.Message_Type.Count.phase); diff --git a/game/src/main/java/com/ccsens/game/netty/wsserver/WebSocketHandler.java b/game/src/main/java/com/ccsens/game/netty/wsserver/WebSocketHandler.java index 9e976f8b..f11c04ec 100644 --- a/game/src/main/java/com/ccsens/game/netty/wsserver/WebSocketHandler.java +++ b/game/src/main/java/com/ccsens/game/netty/wsserver/WebSocketHandler.java @@ -1,10 +1,16 @@ package com.ccsens.game.netty.wsserver; +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSON; +import com.ccsens.game.bean.dto.ClientDto; import com.ccsens.game.bean.dto.message.*; import com.ccsens.game.netty.ChannelManager; import com.ccsens.game.service.IClientService; import com.ccsens.game.service.IMessageService; +import com.ccsens.game.util.GameConstant; +import com.ccsens.util.RedisUtil; import com.ccsens.util.WebConstant; import com.ccsens.util.bean.message.common.MessageConstant; import io.netty.channel.ChannelHandler; @@ -16,8 +22,12 @@ import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.ZSetOperations; import org.springframework.stereotype.Component; +import java.util.Date; +import java.util.Set; + /** * @author wei */ @@ -33,6 +43,8 @@ public class WebSocketHandler extends SimpleChannelInboundHandler> typedTuples = redisUtil.zsRevGetWithScore(gameUserKey, 0, -1); + + if (CollectionUtil.isEmpty(typedTuples)) { + return gameMessageCountOut; + } + for(ZSetOperations.TypedTuple type : typedTuples) { + ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class); + if (user.getUserId().longValue() == Long.parseLong(userId)) { + int score = type.getScore().intValue(); + String userStatus = GameConstant.generateGameStatusKey(recordIdLong); + String gameStausObj = (String)redisUtil.get(userStatus); + if (StrUtil.isBlank(gameStausObj) || gameStausObj.equals(String.valueOf(GameConstant.GAME_COMPLETED))){ + gameMessageCountOut = new GameMessageCountOut(score/100, score); + return gameMessageCountOut; + } + score += 100; + redisUtil.zsSet(gameUserKey, JSON.toJSONString(user), score); + gameMessageCountOut.getData().setTotalScore(score); + gameMessageCountOut.getData().setTotalTimes(score/100); + return gameMessageCountOut; + } + }; + + return gameMessageCountOut; + } + private void doAuthMessage(ChannelHandlerContext ctx, AuthMessageDto message) throws Exception { WebConstant.Message_Auth_Event event = WebConstant.Message_Auth_Event.phaseOf(message.getEvent()); switch (event) { 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 ab9ee19a..c0318429 100644 --- a/game/src/main/java/com/ccsens/game/service/ClientService.java +++ b/game/src/main/java/com/ccsens/game/service/ClientService.java @@ -358,26 +358,86 @@ public class ClientService implements IClientService { */ @Override public GameMessageCountOut clientAddTimes(String userId, String recordId) { + long t1 = System.currentTimeMillis(); GameMessageCountOut gameMessageCountOut = new GameMessageCountOut(); + long t2 = System.currentTimeMillis(); + log.info("创建out耗时:{}", t2 - t1); log.info("userId:{}", userId); - if (ObjectUtil.isNotNull(userId)) { - GameUserJoinExample gameUserJoinExample = new GameUserJoinExample(); - gameUserJoinExample.createCriteria().andUserIdEqualTo(Long.valueOf(userId)).andRecordIdEqualTo(Long.valueOf(recordId)); - List userJoinList = gameUserJoinDao.selectByExample(gameUserJoinExample); - log.info("游戏对象:{}", userJoinList); - if (CollectionUtil.isNotEmpty(userJoinList)) { - GameUserJoin userJoin = userJoinList.get(0); - userJoin.setTimes(userJoin.getTimes() + 1); - userJoin.setScore(userJoin.getScore() + 100); - log.info("更新分数:{}", userJoin); - gameUserJoinDao.updateByPrimaryKeySelective(userJoin); - //更新redis - ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin); - log.info("游戏id:{}, 对象:{}, 分数:{}", recordId, user, userJoin.getScore()); - redisUtil.zsSet(GameConstant.generateGameKey(Long.parseLong(recordId)), JSON.toJSONString(user), userJoin.getScore(), GameConstant.REDIS_TIME); - gameMessageCountOut = new GameMessageCountOut(userJoin.getTimes(), userJoin.getScore()); - } + if (StrUtil.isBlank(userId) || StrUtil.isBlank(userId)) { + return gameMessageCountOut; + } + long t8 = System.currentTimeMillis(); + log.info("判断耗时:{}", t8-t1); + Long recordIdLong = Long.parseLong(recordId); + String gameUserKey = GameConstant.generateGameKey(recordIdLong); + long t3 = System.currentTimeMillis(); + log.info("生成key耗时:{}", t3-t2); + Set> typedTuples = redisUtil.zsRevGetWithScore(gameUserKey, 0, -1); + long t4 = System.currentTimeMillis(); + log.info("查询分数耗时:{}", t4-t3); + + if (CollectionUtil.isEmpty(typedTuples)) { + return gameMessageCountOut; } + long t5 = System.currentTimeMillis(); + log.info("判断分数是否为空耗时:{}", t5-t4); + log.info("查询耗时:{}", t5 - t8); + for(ZSetOperations.TypedTuple type : typedTuples) { + long a1 = System.currentTimeMillis(); + ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class); + long a2 = System.currentTimeMillis(); + 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); + String userStatus = GameConstant.generateGameStatusKey(recordIdLong); + long a4 = System.currentTimeMillis(); + 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); + return gameMessageCountOut; + } + long a6 = System.currentTimeMillis(); + log.info("判断游戏结束耗时:{}", a6-a5); + score += 100; + redisUtil.zsSet(gameUserKey, JSON.toJSONString(user), score); + long a7 = System.currentTimeMillis(); + log.info("更新redis耗时:{}", a7-a6); + gameMessageCountOut.getData().setTotalScore(score); + gameMessageCountOut.getData().setTotalTimes(score/100); + long t = System.currentTimeMillis(); + log.info("循环耗时:{}", t - t5); + log.info("返回耗时:{},循环耗时:{}", t-a7, t-t5); + return gameMessageCountOut; + } + }; + + long t = System.currentTimeMillis(); + log.info("总耗时:{}", t- t1); + +// if (ObjectUtil.isNotNull(userId)) { +// GameUserJoinExample gameUserJoinExample = new GameUserJoinExample(); +// gameUserJoinExample.createCriteria().andUserIdEqualTo(Long.valueOf(userId)).andRecordIdEqualTo(Long.valueOf(recordId)); +// List userJoinList = gameUserJoinDao.selectByExample(gameUserJoinExample); +// log.info("游戏对象:{}", userJoinList); +// if (CollectionUtil.isNotEmpty(userJoinList)) { +// GameUserJoin userJoin = userJoinList.get(0); +// userJoin.setTimes(userJoin.getTimes() + 1); +// userJoin.setScore(userJoin.getScore() + 100); +// log.info("更新分数:{}", userJoin); +// gameUserJoinDao.updateByPrimaryKeySelective(userJoin); +// //更新redis +// ClientDto.RedisUser user = ClientDto.RedisUser.getInstance(userJoin); +// log.info("游戏id:{}, 对象:{}, 分数:{}", recordId, user, userJoin.getScore()); +// redisUtil.zsSet(GameConstant.generateGameKey(Long.parseLong(recordId)), JSON.toJSONString(user), userJoin.getScore(), GameConstant.REDIS_TIME); +// gameMessageCountOut = new GameMessageCountOut(userJoin.getTimes(), userJoin.getScore()); +// } +// } return gameMessageCountOut; } } 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 9c10f63a..4aa50af9 100644 --- a/game/src/main/java/com/ccsens/game/service/ScreenService.java +++ b/game/src/main/java/com/ccsens/game/service/ScreenService.java @@ -748,6 +748,21 @@ public class ScreenService implements IScreenService{ gameRecord.setGameStatus(GameConstant.GAME_COMPLETED); gameRecordDao.updateByPrimaryKeySelective(gameRecord); redisUtil.set(GameConstant.generateGameStatusKey(gameRecord.getId()), JSON.toJSONString(gameRecord), GameConstant.REDIS_TIME); + //TODO 将redis里的分数同步到数据库 + String key = GameConstant.generateGameKey(start.getMemberRecord()); + Set> typedTuples = redisUtil.zsGetWithScore(key, 0, -1); + if (CollectionUtil.isNotEmpty(typedTuples)) { + typedTuples.forEach(type -> { + int score = type.getScore().intValue(); + ClientDto.RedisUser user = JSON.parseObject((String) type.getValue(), ClientDto.RedisUser.class); + GameUserJoin join = new GameUserJoin(); + join.setId(user.getId()); + join.setScore(score); + join.setTimes(score/100); + gameUserJoinDao.updateByPrimaryKeySelective(join); + }); + } + }); //更新用户游戏开始时间和结束时间 gameUserJoinDao.updateTimeBatch(gameRecord.getId(), gameRecord.getStartTime(), gameRecord.getEndTime()); diff --git a/game/src/main/resources/application.yml b/game/src/main/resources/application.yml index 5c2cd5c4..5889ff7f 100644 --- a/game/src/main/resources/application.yml +++ b/game/src/main/resources/application.yml @@ -1,4 +1,4 @@ spring: profiles: - active: dev - include: common, util-dev \ No newline at end of file + active: test + include: common, util-test \ No newline at end of file