|
|
@ -3,11 +3,15 @@ package com.ccsens.game.service; |
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
import cn.hutool.core.lang.Snowflake; |
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.ccsens.cloudutil.feign.TallFeignClient; |
|
|
|
import com.ccsens.game.bean.dto.ClientDto; |
|
|
|
import com.ccsens.game.bean.dto.ScreenDto; |
|
|
|
import com.ccsens.game.bean.dto.message.BaseMessageDto; |
|
|
|
import com.ccsens.game.bean.dto.message.ChromeMessageDto; |
|
|
|
import com.ccsens.game.bean.po.*; |
|
|
|
import com.ccsens.game.bean.vo.ClientVo; |
|
|
|
import com.ccsens.game.bean.vo.ScreenVo; |
|
|
|
import com.ccsens.game.persist.dao.*; |
|
|
|
import com.ccsens.game.util.GameConstant; |
|
|
@ -21,7 +25,9 @@ import com.ccsens.util.exception.BaseException; |
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate; |
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.redis.core.ZSetOperations; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
@ -224,56 +230,80 @@ public class ScreenService implements IScreenService{ |
|
|
|
ScreenDto.MemberRecord memberRecord = params.getParam(); |
|
|
|
ScreenVo.GameStatusVo gameStatusVo = new ScreenVo.GameStatusVo(); |
|
|
|
|
|
|
|
GameRecord gameRecord = gameRecordDao.selectByPrimaryKey(memberRecord.getMemberRecord()); |
|
|
|
if(ObjectUtil.isNull(gameRecord)){ |
|
|
|
throw new BaseException(CodeEnum.NOT_GAME_RECORD); |
|
|
|
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); |
|
|
|
} |
|
|
|
GameUserJoinExample gameuserJoinExample = new GameUserJoinExample(); |
|
|
|
gameuserJoinExample.createCriteria().andRecordIdEqualTo(memberRecord.getMemberRecord()); |
|
|
|
List<GameUserJoin> userJoinList = gameUserJoinDao.selectByExample(gameuserJoinExample); |
|
|
|
|
|
|
|
gameStatusVo.setGameStatus(gameRecord.getGameStatus()); |
|
|
|
|
|
|
|
switch (gameStatusVo.getGameStatus()){ |
|
|
|
case 0: |
|
|
|
break; |
|
|
|
case 1: |
|
|
|
ScreenVo.PreparingData preparingData = new ScreenVo.PreparingData(); |
|
|
|
if(ObjectUtil.isNull(params.getUserId())){ |
|
|
|
// 无userId,默认大屏,获取大屏的游戏开始时间
|
|
|
|
preparingData.setStartLocalTime(gameRecord.getStartTime() + gameRecord.getTimeDifference()); |
|
|
|
}else { |
|
|
|
if(CollectionUtil.isNotEmpty(userJoinList)){ |
|
|
|
for(GameUserJoin gameUserJoin : userJoinList){ |
|
|
|
if(gameUserJoin.getId().longValue() == params.getUserId().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); |
|
|
|
// 获取用户的开始时间
|
|
|
|
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()); |
|
|
|
} |
|
|
|
gameStatusVo.setPreparingData(preparingData); |
|
|
|
break; |
|
|
|
case 2: |
|
|
|
// TODO
|
|
|
|
List<ScreenVo.TopUsers> tops = gameUserJoinDao.selectTopTen(gameRecord.getId()); |
|
|
|
ScreenVo.ProcessingData processingData = new ScreenVo.ProcessingData(); |
|
|
|
processingData.setTopMembers(tops); |
|
|
|
gameStatusVo.setProcessingData(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); |
|
|
|
} |
|
|
|
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(userJoinList)){ |
|
|
|
completedData = getCompletedData(userJoinList); |
|
|
|
if(CollectionUtil.isNotEmpty(userJoins)){ |
|
|
|
completedData = getCompletedData(userJoins); |
|
|
|
} |
|
|
|
List<ScreenVo.TopUsers> top2 = gameUserJoinDao.selectTopTen(gameRecord.getId()); |
|
|
|
completedData.setMembers(top2); |
|
|
|
gameStatusVo.setCompletedData(completedData); |
|
|
|
|
|
|
|
break; |
|
|
|
default: |
|
|
|
break; |
|
|
@ -376,6 +406,7 @@ public class ScreenService implements IScreenService{ |
|
|
|
} |
|
|
|
return instructionsList; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 已结束时的返回值 |
|
|
|
*/ |
|
|
@ -407,6 +438,32 @@ 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; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public ScreenVo.StartGame startGame(ScreenDto.Start start) { |
|
|
@ -431,61 +488,35 @@ public class ScreenService implements IScreenService{ |
|
|
|
gameRecord.setEndTime(gameRecord.getStartTime() + GameConstant.GAME_TIME); |
|
|
|
gameRecord.setTimeDifference((int)moreTime); |
|
|
|
gameRecordDao.updateByPrimaryKeySelective(gameRecord); |
|
|
|
//设置redis 游戏状态 准备中
|
|
|
|
redisUtil.set(GameConstant.generateGameStatusKey(gameRecord.getId()), JSON.toJSONString(gameRecord), GameConstant.REDIS_TIME); |
|
|
|
|
|
|
|
//延时通知
|
|
|
|
long startSend = gameRecord.getStartTime() - System.currentTimeMillis(); |
|
|
|
long endSend = gameRecord.getEndTime() - System.currentTimeMillis(); |
|
|
|
System.out.println("开始延时:" + startSend + ",结束延时:" + endSend); |
|
|
|
System.out.println("发送开始时间" + new Date()); |
|
|
|
log.info("发送开始时间" + new Date()); |
|
|
|
ScheduledExecutorService executor = Executors.newScheduledThreadPool(10); |
|
|
|
//更新开始状态
|
|
|
|
sendMsg.sendMsg(executor, startSend, () -> { |
|
|
|
System.out.println(GameConstant.GAME_PREPARATION + "start: "+new Date()); |
|
|
|
gameRecord.setGameStatus(GameConstant.GAME_PROCESSING); |
|
|
|
gameRecordDao.updateByPrimaryKeySelective(gameRecord); |
|
|
|
//设置 游戏状态 进行中
|
|
|
|
gameRecord.setGameStatus(GameConstant.GAME_PROCESSING); |
|
|
|
gameRecordDao.updateByPrimaryKeySelective(gameRecord); |
|
|
|
redisUtil.set(GameConstant.generateGameStatusKey(gameRecord.getId()), JSON.toJSONString(gameRecord), GameConstant.REDIS_TIME); |
|
|
|
}); |
|
|
|
//更新结束状态
|
|
|
|
sendMsg.sendMsg(executor, endSend, () -> { |
|
|
|
System.out.println(GameConstant.GAME_COMPLETED + "end: "+new Date()); |
|
|
|
//设置 游戏状态 已结束
|
|
|
|
gameRecord.setGameStatus(GameConstant.GAME_COMPLETED); |
|
|
|
gameRecordDao.updateByPrimaryKeySelective(gameRecord); |
|
|
|
redisUtil.set(GameConstant.generateGameStatusKey(gameRecord.getId()), JSON.toJSONString(gameRecord), GameConstant.REDIS_TIME); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
//更新用户游戏开始时间和结束时间
|
|
|
|
gameUserJoinDao.updateTimeBatch(gameRecord.getId(), gameRecord.getStartTime(), gameRecord.getEndTime()); |
|
|
|
//查询游戏用户,通知游戏开始和结束
|
|
|
|
GameUserJoinExample joinExample = new GameUserJoinExample(); |
|
|
|
joinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId()); |
|
|
|
List<GameUserJoin> userJoins = gameUserJoinDao.selectByExample(joinExample); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (CollectionUtil.isNotEmpty(userJoins)) { |
|
|
|
|
|
|
|
new Thread(()->{ |
|
|
|
|
|
|
|
|
|
|
|
long startTime = gameRecord.getStartTime() - System.currentTimeMillis(); |
|
|
|
sendMsg.sendMsg(executor, startTime > 0 ? startTime : 0, ()->{ |
|
|
|
try { |
|
|
|
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PROCESSING); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
long endTime = gameRecord.getEndTime() - System.currentTimeMillis(); |
|
|
|
sendMsg.sendMsg(executor, endTime > 0 ? endTime : 0, ()->{ |
|
|
|
try { |
|
|
|
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_COMPLETED); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
}).start(); |
|
|
|
|
|
|
|
} |
|
|
|
List<ClientDto.RedisUser> userJoins = sendMsg.getAllScore(gameRecord); |
|
|
|
|
|
|
|
//推送客户端
|
|
|
|
pushClient(gameRecord, executor, userJoins); |
|
|
|
//返回大屏开始时间
|
|
|
|
ScreenVo.StartGame startGame = new ScreenVo.StartGame(); |
|
|
|
startGame.setStartLocalTime(gameRecord.getStartTime() + moreTime); |
|
|
@ -493,4 +524,42 @@ public class ScreenService implements IScreenService{ |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 推送客户端状态 |
|
|
|
* @param gameRecord |
|
|
|
* @param executor |
|
|
|
* @param userJoins |
|
|
|
*/ |
|
|
|
private void pushClient(GameRecord gameRecord, ScheduledExecutorService executor, List<ClientDto.RedisUser> userJoins) { |
|
|
|
new Thread(()->{ |
|
|
|
sendMsg.sendMsg(executor, 0, ()->{ |
|
|
|
try { |
|
|
|
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PREPARATION); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}); |
|
|
|
long startTime = gameRecord.getStartTime() - System.currentTimeMillis(); |
|
|
|
sendMsg.sendMsg(executor, startTime > 0 ? startTime : 0, ()->{ |
|
|
|
try { |
|
|
|
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_PROCESSING); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
long endTime = gameRecord.getEndTime() - System.currentTimeMillis(); |
|
|
|
sendMsg.sendMsg(executor, endTime > 0 ? endTime : 0, ()->{ |
|
|
|
try { |
|
|
|
sendMsg.sendStatus(gameRecord, userJoins, GameConstant.GAME_COMPLETED); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
}).start(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|