|
|
@ -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; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|