Browse Source

再玩一次发送消息

master
zhangye 6 years ago
parent
commit
9b662786e3
  1. 2
      game/src/main/java/com/ccsens/game/bean/dto/ScreenDto.java
  2. 5
      game/src/main/java/com/ccsens/game/service/ClientService.java
  3. 8
      game/src/main/java/com/ccsens/game/service/IScreenService.java
  4. 288
      game/src/main/java/com/ccsens/game/service/ScreenService.java
  5. 7
      game/src/main/java/com/ccsens/game/util/SendMsg.java
  6. 2
      game/src/main/resources/mapper_dao/GameUserJoinDao.xml
  7. 13
      util/src/main/java/com/ccsens/util/RedisUtil.java

2
game/src/main/java/com/ccsens/game/bean/dto/ScreenDto.java

@ -22,6 +22,8 @@ public class ScreenDto {
public static class MemberRecord{
@ApiModelProperty("创建的游戏记录的id")
private Long memberRecord;
@ApiModelProperty("项目id")
private Long projectId;
}
@Data

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

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

@ -120,8 +120,10 @@ public class ScreenService implements IScreenService{
urlVo.setUrl(gameRecord.getUrl());
urlVo.setRuleList(ruleList);
//路径(添加项目id)
String url = gameRecord.getUrl() + "&projectId="+ memberGame.getProjectId();
//给所有人发送消息发送消息
ChromeMessageDto chromeMessageDto = new ChromeMessageDto(gameRecord.getUrl(),gameRecord.getId(),memberGame.getProjectId());
ChromeMessageDto chromeMessageDto = new ChromeMessageDto(url,gameRecord.getId(),memberGame.getProjectId());
BaseMessageDto.MessageUser messageUser = null;
List<BaseMessageDto.MessageUser> messageUserList = new ArrayList<>();
//获取项目下所有成员
@ -150,10 +152,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 +163,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 +193,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,24 +221,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());
GameUserJoinExample gameuserJoinExample = new GameUserJoinExample();
gameuserJoinExample.createCriteria().andRecordIdEqualTo(memberRecord.getMemberRecord());
List<GameUserJoin> userJoins = gameUserJoinDao.selectByExample(gameuserJoinExample);
gameStatusVo.setTotalMembers(userJoins.size());
// 查询总人数
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:
@ -258,56 +248,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:
// 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:
@ -316,6 +270,115 @@ 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;
}
/**
* 再玩一次
*/
@ -351,21 +414,23 @@ public class ScreenService implements IScreenService{
gameUserPay.setUsedCount(gameUserPay.getUsedCount() + 1);
gameUserPayDao.updateByPrimaryKeySelective(gameUserPay);
// //给所有人发送消息发送消息
// ChromeMessageDto chromeMessageDto = new ChromeMessageDto(gameRecord.getUrl(),gameRecord.getId(),memberGame.getProjectId());
// BaseMessageDto.MessageUser messageUser = null;
// List<BaseMessageDto.MessageUser> messageUserList = new ArrayList<>();
// //获取项目下所有成员
// List<Long> memberIdList = tallFeignClient.getMemberIdListByProject(memberGame.getProjectId());
// if(CollectionUtil.isNotEmpty(memberIdList)){
// for(Long memberId:memberIdList){
// messageUser = new BaseMessageDto.MessageUser();
// messageUser.setUserId(memberId);
// messageUserList.add(messageUser);
// }
// }
// chromeMessageDto.setReceivers(messageUserList);
// messageService.sendGameMessageWithGetUrl(chromeMessageDto);
//路径(添加项目id)
String url = gameRecord.getUrl() + "&projectId="+ memberRecord.getProjectId();
//给所有人发送消息发送消息
ChromeMessageDto chromeMessageDto = new ChromeMessageDto(url,gameRecord.getId(),memberRecord.getProjectId());
BaseMessageDto.MessageUser messageUser = null;
List<BaseMessageDto.MessageUser> messageUserList = new ArrayList<>();
//获取项目下所有成员
List<Long> memberIdList = tallFeignClient.getMemberIdListByProject(memberRecord.getProjectId());
if(CollectionUtil.isNotEmpty(memberIdList)){
for(Long memberId:memberIdList){
messageUser = new BaseMessageDto.MessageUser();
messageUser.setUserId(memberId);
messageUserList.add(messageUser);
}
}
chromeMessageDto.setReceivers(messageUserList);
messageService.sendGameMessageWithGetUrl(chromeMessageDto);
}else {
throw new BaseException(CodeEnum.GAME_NO_END);
@ -450,30 +515,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;
}
/**

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

@ -39,11 +39,8 @@ public class SendMsg {
private RedisUtil redisUtil;
public void sendStatus(GameRecord gameRecord, List<ClientDto.RedisUser> userJoins, byte status) throws JsonProcessingException {
<<<<<<< HEAD
System.out.println("准备发送"+userJoins.size());
=======
log.info("userJoins:{}, status:{}", userJoins, status);
>>>>>>> 84ba41cd383cf3d26685e48262d42260316865d9
List<GameMessageWithChangeStatusOut> outs = new ArrayList<>();
if (CollectionUtil.isEmpty(userJoins)) {
return;
@ -121,7 +118,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