|
|
@ -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<GameUserJoin> 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<ZSetOperations.TypedTuple<Object>> 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<Object> 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<GameUserJoin> 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; |
|
|
|
} |
|
|
|
} |
|
|
|