Browse Source

redis

master
6 years ago
parent
commit
50f131a20f
  1. 5
      game/src/main/java/com/ccsens/game/service/ClientService.java
  2. 8
      game/src/main/java/com/ccsens/game/service/IScreenService.java
  3. 250
      game/src/main/java/com/ccsens/game/service/ScreenService.java
  4. 2
      game/src/main/java/com/ccsens/game/util/SendMsg.java
  5. 2
      game/src/main/resources/mapper_dao/GameUserJoinDao.xml
  6. 13
      util/src/main/java/com/ccsens/util/RedisUtil.java

5
game/src/main/java/com/ccsens/game/service/ClientService.java

@ -60,6 +60,8 @@ public class ClientService implements IClientService {
private TallFeignClient tallFeignClient;
@Autowired
private SendMsg sendMsg;
@Autowired
private IScreenService screenService;
@Override
@ -69,7 +71,8 @@ public class ClientService implements IClientService {
int timeMore = (int)(join.getLocalTime() - System.currentTimeMillis());
log.info("{}时间差:{}", userId, timeMore);
//获取游戏信息
GameRecord gameRecord = gameRecordDao.selectByPrimaryKey(join.getUrlId());
GameRecord gameRecord = screenService.getGameRecord(join.getUrlId());
log.info("游戏信息:{}", gameRecord);
if (gameRecord == null) {
log.info("未找到游戏信息");

8
game/src/main/java/com/ccsens/game/service/IScreenService.java

@ -1,6 +1,7 @@
package com.ccsens.game.service;
import com.ccsens.game.bean.dto.ScreenDto;
import com.ccsens.game.bean.po.GameRecord;
import com.ccsens.game.bean.vo.ScreenVo;
import com.ccsens.util.bean.dto.QueryDto;
@ -19,4 +20,11 @@ public interface IScreenService {
* @return
*/
ScreenVo.StartGame startGame(ScreenDto.Start start);
/**
* 查询游戏
* @param gameRecordId
* @return
*/
GameRecord getGameRecord(long gameRecordId);
}

250
game/src/main/java/com/ccsens/game/service/ScreenService.java

@ -150,10 +150,8 @@ public class ScreenService implements IScreenService{
//返回
ScreenVo.GameInfoVo gameInfoVo = new ScreenVo.GameInfoVo();
GameRecord gameRecord = gameRecordDao.selectByPrimaryKey(memberRecord.getMemberRecord());
if(ObjectUtil.isNull(gameRecord)){
throw new BaseException(CodeEnum.NOT_GAME_RECORD);
}
GameRecord gameRecord = getGameRecord(memberRecord.getMemberRecord());
GameUserPay gameUserPay = gameUserPayDao.selectByPrimaryKey(gameRecord.getUserPayId());
if(ObjectUtil.isNull(gameUserPay)){
throw new BaseException(CodeEnum.NOT_GAME_TYPE);
@ -163,16 +161,17 @@ public class ScreenService implements IScreenService{
gameInfoVo.setTotalCount(gameUserPay.getTotalCount());
gameInfoVo.setUsedCount(gameUserPay.getUsedCount());
gameInfoVo.setGameStatus(gameRecord.getGameStatus());
//查询参加这个游戏的用户
GameUserJoinExample gameuserJoinExample = new GameUserJoinExample();
gameuserJoinExample.createCriteria().andRecordIdEqualTo(memberRecord.getMemberRecord());
List<GameUserJoin> userJoinList = gameUserJoinDao.selectByExample(gameuserJoinExample);
if(CollectionUtil.isNotEmpty(userJoinList)){
gameInfoVo.setTotalMembers(userJoinList.size());
}else {
gameInfoVo.setTotalMembers(0);
//查询参加这个游戏的总用户
Long totalUsers = redisUtil.zsGetSize(GameConstant.generateGameKey(memberRecord.getMemberRecord()));
if (totalUsers == null) {
GameUserJoinExample gameuserJoinExample = new GameUserJoinExample();
gameuserJoinExample.createCriteria().andRecordIdEqualTo(memberRecord.getMemberRecord());
totalUsers = gameUserJoinDao.countByExample(gameuserJoinExample);
}
gameInfoVo.setTotalMembers(totalUsers == null ? 0 : totalUsers.intValue());
switch (gameInfoVo.getGameStatus()){
case 0:
ScreenVo.PendingData pendingData = new ScreenVo.PendingData();
@ -192,28 +191,19 @@ public class ScreenService implements IScreenService{
if(ObjectUtil.isNull(params.getUserId())){
preparingData.setStartLocalTime(gameRecord.getStartTime() + gameRecord.getTimeDifference());
}else {
if(CollectionUtil.isNotEmpty(userJoinList)){
for(GameUserJoin gameUserJoin : userJoinList){
if(params.getUserId().longValue() == gameUserJoin.getId().longValue()){
preparingData.setStartLocalTime(gameRecord.getStartTime() + gameUserJoin.getTimeDifference());
}
}
if(ObjectUtil.isNull(preparingData.getStartLocalTime())){
throw new BaseException(CodeEnum.NOT_JOIN_GAME);
}
}else {
throw new BaseException(CodeEnum.NOT_JOIN_GAME);
GameUserJoin userJoin = getGameUserJoin(params.getUserId(), gameRecord.getId());
log.info("用户信息:{}", userJoin);
if (userJoin == null) {
throw new BaseException(CodeEnum.PARAM_ERROR);
}
preparingData.setStartLocalTime(gameRecord.getStartTime() + userJoin.getTimeDifference());
}
gameInfoVo.setPreparingData(preparingData);
break;
case 2:
break;
case 3:
ScreenVo.CompletedData completedData = new ScreenVo.CompletedData();
if(CollectionUtil.isNotEmpty(userJoinList)){
completedData = getCompletedData(userJoinList);
}
ScreenVo.CompletedData completedData = getCompletedData(memberRecord.getMemberRecord());
gameInfoVo.setCompletedData(completedData);
break;
default:
@ -229,20 +219,22 @@ public class ScreenService implements IScreenService{
public ScreenVo.GameStatusVo getGameStatusVo(QueryDto<ScreenDto.MemberRecord> params) {
ScreenDto.MemberRecord memberRecord = params.getParam();
ScreenVo.GameStatusVo gameStatusVo = new ScreenVo.GameStatusVo();
long gameRecordId = memberRecord.getMemberRecord();
String gameRecordStr = (String)redisUtil.get(GameConstant.generateGameStatusKey(memberRecord.getMemberRecord()));
GameRecord gameRecord = null;
if (StrUtil.isBlank(gameRecordStr)) {
gameRecord = gameRecordDao.selectByPrimaryKey(memberRecord.getMemberRecord());
if(ObjectUtil.isNull(gameRecord)){
throw new BaseException(CodeEnum.NOT_GAME_RECORD);
}
} else {
gameRecord = JSON.parseObject(gameRecordStr, GameRecord.class);
}
GameRecord gameRecord = getGameRecord(gameRecordId);
gameStatusVo.setGameStatus(gameRecord.getGameStatus());
// 查询总人数
Long total = redisUtil.zsGetSize(GameConstant.generateGameKey(gameRecord.getId()));
log.info("redis查询gameRecordID:{}总人数:{}", gameRecord.getId(), total);
if (total == null) {
GameUserJoinExample gameuserJoinExample = new GameUserJoinExample();
gameuserJoinExample.createCriteria().andRecordIdEqualTo(memberRecord.getMemberRecord());
total = gameUserJoinDao.countByExample(gameuserJoinExample);
}
gameStatusVo.setTotalMembers(total == null ? 0 : total.intValue());
switch (gameStatusVo.getGameStatus()){
case 0:
break;
@ -253,57 +245,20 @@ public class ScreenService implements IScreenService{
preparingData.setStartLocalTime(gameRecord.getStartTime() + gameRecord.getTimeDifference());
}else {
// 获取用户的开始时间
GameUserJoinExample joinExample = new GameUserJoinExample();
joinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId()).andUserIdEqualTo(params.getUserId());
List<GameUserJoin> gameUserJoins = gameUserJoinDao.selectByExample(joinExample);
if (CollectionUtil.isEmpty(gameUserJoins)) {
log.info("用户{}未参加游戏{}", params.getUserId(), gameRecord.getId());
throw new BaseException(CodeEnum.PARAM_ERROR);
}
preparingData.setStartLocalTime(gameRecord.getStartTime() + gameUserJoins.get(0).getTimeDifference());
GameUserJoin userJoin = getGameUserJoin(params.getUserId(), gameRecord.getId());
preparingData.setStartLocalTime(gameRecord.getStartTime() + userJoin.getTimeDifference());
}
gameStatusVo.setPreparingData(preparingData);
break;
case 2:
ScreenVo.ProcessingData processingData = new ScreenVo.ProcessingData();
// 查询前十名
String userKey = GameConstant.generateGameKey(gameRecord.getId());
Set<ZSetOperations.TypedTuple<Object>> typedTuples = redisUtil.zsRevGetWithScore(userKey, 0, 10);
if (CollectionUtil.isNotEmpty(typedTuples)) {
List<ScreenVo.TopUsers> tops = new ArrayList<>();
typedTuples.forEach(type -> {
ClientDto.RedisUser user = JSON.parseObject((String) type.getValue(), ClientDto.RedisUser.class);
ScreenVo.TopUsers topUser = new ScreenVo.TopUsers();
topUser.setHeadImgUrl(user.getAvatarUrl());
topUser.setNickname(user.getNickname());
topUser.setScore(type.getScore().intValue());
topUser.setTimes(type.getScore().intValue()/100);
Long sort = redisUtil.zsReverseRank(userKey, type.getValue());
topUser.setSort(sort == null ? 1 : sort.intValue());
// TODO 微晚待续
tops.add(topUser);
processingData.setTopMembers(tops);
gameStatusVo.setProcessingData(processingData);
});
} else {
List<ScreenVo.TopUsers> tops = gameUserJoinDao.selectTopTen(gameRecord.getId());
processingData.setTopMembers(tops);
gameStatusVo.setProcessingData(processingData);
}
// 查询前十名(列表顺序即前十名顺序)
List<ScreenVo.TopUsers> tops = getTopUsers(gameRecordId);
processingData.setTopMembers(tops);
gameStatusVo.setProcessingData(processingData);
break;
case 3:
// List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord);
GameUserJoinExample gameuserJoinExample = new GameUserJoinExample();
gameuserJoinExample.createCriteria().andRecordIdEqualTo(memberRecord.getMemberRecord());
List<GameUserJoin> userJoins = gameUserJoinDao.selectByExample(gameuserJoinExample);
ScreenVo.CompletedData completedData = new ScreenVo.CompletedData();
if(CollectionUtil.isNotEmpty(userJoins)){
completedData = getCompletedData(userJoins);
}
List<ScreenVo.TopUsers> top2 = gameUserJoinDao.selectTopTen(gameRecord.getId());
completedData.setMembers(top2);
ScreenVo.CompletedData completedData = getCompletedData(gameRecordId);
gameStatusVo.setCompletedData(completedData);
break;
default:
@ -312,6 +267,116 @@ public class ScreenService implements IScreenService{
return gameStatusVo;
}
/**
* 查询总分数总次数平均以及前十名
* @param gameRecordId
* @return
*/
private ScreenVo.CompletedData getCompletedData(long gameRecordId) {
ScreenVo.CompletedData completedData = new ScreenVo.CompletedData();
//查询总次数等
String key = GameConstant.generateGameKey(gameRecordId);
Set<ZSetOperations.TypedTuple<Object>> typedTuples = redisUtil.zsGetWithScore(key, 0, -1);
if (CollectionUtil.isNotEmpty(typedTuples)) {
int totalScore = typedTuples.stream().mapToInt(typedTuple -> typedTuple.getScore().intValue()).sum();
//总分数
completedData.setTotalScore(totalScore);
// 总次数
completedData.setTotalTimes(totalScore/100);
// 平均次数
int totalUsers = redisUtil.zsGetSize(key).intValue();
int averageTimes = completedData.getTotalTimes()/totalUsers;
completedData.setAverageTimes(averageTimes);
// 平均超过多少用户
int averageScore = totalScore / totalUsers;
if (averageScore > 0) {
Set<Object> overUsers = redisUtil.zsGetByScore(key, 0, averageScore - 1);
completedData.setOver(CollectionUtil.isEmpty(overUsers) ? 0 : overUsers.size() * 100 / totalUsers);
} else {
completedData.setOver(0);
}
} else {
GameUserJoinExample gameuserJoinExample = new GameUserJoinExample();
gameuserJoinExample.createCriteria().andRecordIdEqualTo(gameRecordId);
List<GameUserJoin> userJoins = gameUserJoinDao.selectByExample(gameuserJoinExample);
if(CollectionUtil.isNotEmpty(userJoins)){
completedData = getCompletedData(userJoins);
}
}
List<ScreenVo.TopUsers> top2 = getTopUsers(gameRecordId);
completedData.setMembers(top2);
return completedData;
}
/**
* 查询参加游戏的用户信息
* @param userId
* @param gameRecordId
* @return
*/
private GameUserJoin getGameUserJoin(Long userId, Long gameRecordId) {
GameUserJoinExample joinExample = new GameUserJoinExample();
joinExample.createCriteria().andRecordIdEqualTo(gameRecordId).andUserIdEqualTo(userId);
List<GameUserJoin> gameUserJoins = gameUserJoinDao.selectByExample(joinExample);
if (CollectionUtil.isEmpty(gameUserJoins)) {
log.info("用户{}未参加游戏{}", userId, gameRecordId);
throw new BaseException(CodeEnum.PARAM_ERROR);
}
return gameUserJoins.get(0);
}
/**
* 查询游戏信息
* @param gameRecordId
* @return
*/
@Override
public GameRecord getGameRecord(long gameRecordId) {
String gameRecordStr = (String)redisUtil.get(GameConstant.generateGameStatusKey(gameRecordId));
GameRecord gameRecord = null;
if (StrUtil.isBlank(gameRecordStr)) {
gameRecord = gameRecordDao.selectByPrimaryKey(gameRecordId);
if(ObjectUtil.isNull(gameRecord)){
throw new BaseException(CodeEnum.NOT_GAME_RECORD);
}
} else {
gameRecord = JSON.parseObject(gameRecordStr, GameRecord.class);
}
return gameRecord;
}
/**
* 查询前十名
* @param gameRecordId
* @return
*/
private List<ScreenVo.TopUsers> getTopUsers(Long gameRecordId) {
List<ScreenVo.TopUsers> tops;
String userKey = GameConstant.generateGameKey(gameRecordId);
Set<ZSetOperations.TypedTuple<Object>> typedTuples = redisUtil.zsRevGetWithScore(userKey, 0, 9);
if (CollectionUtil.isNotEmpty(typedTuples)) {
tops = new ArrayList<>();
List<ScreenVo.TopUsers> finalTops = tops;
typedTuples.forEach(type -> {
ClientDto.RedisUser user = JSON.parseObject((String) type.getValue(), ClientDto.RedisUser.class);
ScreenVo.TopUsers topUser = new ScreenVo.TopUsers();
topUser.setHeadImgUrl(user.getAvatarUrl());
topUser.setNickname(user.getNickname());
topUser.setScore(type.getScore().intValue());
topUser.setTimes(type.getScore().intValue()/100);
finalTops.add(topUser);
});
} else {
tops = gameUserJoinDao.selectTopTen(gameRecordId);
}
return tops;
}
/**
* 再玩一次
*/
@ -440,30 +505,7 @@ public class ScreenService implements IScreenService{
}
/**
* 已结束时的返回值
*/
private ScreenVo.CompletedData getCompletedDataByRedis(List<ClientDto.RedisUser> userJoinList){
//TODO
ScreenVo.CompletedData completedData = new ScreenVo.CompletedData();
int totalTimes = 0;
int totalScore = 0;
int averageTimes = 0;
int over = 0;
if(CollectionUtil.isNotEmpty(userJoinList)){
for (ClientDto.RedisUser redisUser: userJoinList) {
totalScore += (redisUser.getScore() == null ? 0 : redisUser.getScore());
totalTimes += (redisUser.getTimes() == null ? 0 : redisUser.getTimes());
}
}
completedData.setTotalTimes(totalTimes);
completedData.setTotalScore(totalScore);
completedData.setAverageTimes(averageTimes);
completedData.setOver(over);
return completedData;
}

2
game/src/main/java/com/ccsens/game/util/SendMsg.java

@ -115,7 +115,7 @@ public class SendMsg {
Long sort = redisUtil.zsReverseRank(userKey, type.getValue());
user.setSort(sort == null ? 1 : sort.intValue());
//超过
Set<Object> overs = redisUtil.zsGet(userKey, 0, user.getScore() - 1);
Set<Object> 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());

2
game/src/main/resources/mapper_dao/GameUserJoinDao.xml

@ -16,7 +16,7 @@
</update>
<select id="selectTopTen" resultType="com.ccsens.game.bean.vo.ScreenVo$TopUsers">
select user_id, avatar_url as headImgUrl, nickname, score, score as times
select user_id, avatar_url as headImgUrl, nickname, score, score/100 as times
from t_game_user_join
where record_id = #{recordId,jdbcType=BIGINT}
ORDER BY score DESC

13
util/src/main/java/com/ccsens/util/RedisUtil.java

@ -561,6 +561,19 @@ public class RedisUtil {
}
}
//===============================zset=================================
/**
* 获取sort set缓存的内容
* @author whj
* @date 2019/8/1
* @param key
* @param small 起始分数
* @param max 结束分数
* @return java.util.Set<java.lang.Object>
*/
public Set<Object> zsGetByScore(String key, double small, double max) {
return redisTemplate.opsForZSet().rangeByScore(key, small, max);
}
/**
* 获取sort set缓存的内容
* @author whj

Loading…
Cancel
Save