|
@ -1,15 +1,17 @@ |
|
|
package com.ccsens.game.service; |
|
|
package com.ccsens.game.service; |
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
|
|
import cn.hutool.core.lang.Snowflake; |
|
|
import com.ccsens.game.bean.dto.ClientDto; |
|
|
import com.ccsens.game.bean.dto.ClientDto; |
|
|
import com.ccsens.game.bean.po.GameMemberJoin; |
|
|
|
|
|
import com.ccsens.game.bean.po.GameMemberJoinExample; |
|
|
|
|
|
import com.ccsens.game.bean.po.GameRecord; |
|
|
import com.ccsens.game.bean.po.GameRecord; |
|
|
|
|
|
import com.ccsens.game.bean.po.GameUserJoin; |
|
|
|
|
|
import com.ccsens.game.bean.po.GameUserJoinExample; |
|
|
import com.ccsens.game.bean.vo.ClientVo; |
|
|
import com.ccsens.game.bean.vo.ClientVo; |
|
|
import com.ccsens.game.persist.dao.GameMemberJoinDao; |
|
|
|
|
|
import com.ccsens.game.persist.dao.GameRecordDao; |
|
|
import com.ccsens.game.persist.dao.GameRecordDao; |
|
|
|
|
|
import com.ccsens.game.persist.dao.GameUserJoinDao; |
|
|
import com.ccsens.game.util.GameConstant; |
|
|
import com.ccsens.game.util.GameConstant; |
|
|
import com.ccsens.util.CodeEnum; |
|
|
import com.ccsens.util.CodeEnum; |
|
|
|
|
|
import com.ccsens.util.RedisUtil; |
|
|
import com.ccsens.util.exception.BaseException; |
|
|
import com.ccsens.util.exception.BaseException; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
@ -19,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @description: |
|
|
* @description: |
|
|
* @author: wuHuiJuan |
|
|
* @author: wuHuiJuan |
|
@ -30,24 +33,20 @@ import java.util.List; |
|
|
public class ClientService implements IClientService { |
|
|
public class ClientService implements IClientService { |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private GameMemberJoinDao gameMemberJoinDao; |
|
|
private GameUserJoinDao gameUserJoinDao; |
|
|
@Autowired |
|
|
@Autowired |
|
|
private GameRecordDao gameRecordDao; |
|
|
private GameRecordDao gameRecordDao; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private Snowflake snowflake; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private RedisUtil redisUtil; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public ClientVo.Join join(ClientDto.Join join, Long userId) { |
|
|
public ClientVo.Join join(ClientDto.Join join, Long userId) { |
|
|
log.info("加入游戏:{}, userId:{}", join, userId); |
|
|
log.info("加入游戏:{}, userId:{}", join, userId); |
|
|
ClientVo.Join joinVo = new ClientVo.Join(); |
|
|
|
|
|
//根据游戏ID和用户ID查询用户是否加入
|
|
|
int timeMore = (int)(join.getLocalTime() - System.currentTimeMillis()); |
|
|
GameMemberJoinExample example = new GameMemberJoinExample(); |
|
|
log.info("{}时间差:{}", userId, timeMore); |
|
|
example.createCriteria().andRecordIdEqualTo(join.getUrlId()).andMemberIdEqualTo(userId); |
|
|
|
|
|
List<GameMemberJoin> gameMemberJoins = gameMemberJoinDao.selectByExample(example); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(gameMemberJoins)) { |
|
|
|
|
|
Long localStartTime = gameMemberJoins.get(0).getLocalStartTime(); |
|
|
|
|
|
log.info("用户已加入该游戏,直接返回开始时间{}", localStartTime); |
|
|
|
|
|
joinVo.setStartLocalTime(localStartTime); |
|
|
|
|
|
return joinVo; |
|
|
|
|
|
} |
|
|
|
|
|
//获取游戏信息
|
|
|
//获取游戏信息
|
|
|
GameRecord gameRecord = gameRecordDao.selectByPrimaryKey(join.getUrlId()); |
|
|
GameRecord gameRecord = gameRecordDao.selectByPrimaryKey(join.getUrlId()); |
|
|
log.info("游戏信息:{}", gameRecord); |
|
|
log.info("游戏信息:{}", gameRecord); |
|
@ -56,10 +55,98 @@ public class ClientService implements IClientService { |
|
|
throw new BaseException(CodeEnum.PARAM_ERROR); |
|
|
throw new BaseException(CodeEnum.PARAM_ERROR); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//判断游戏状态
|
|
|
//根据游戏ID和用户ID查询用户是否加入
|
|
|
gameRecord.getGameStatus().byteValue() == GameConstant.GAME_END |
|
|
GameUserJoinExample example = new GameUserJoinExample(); |
|
|
|
|
|
example.createCriteria().andRecordIdEqualTo(join.getUrlId()).andUserIdEqualTo(userId); |
|
|
|
|
|
List<GameUserJoin> gameUserJoins = gameUserJoinDao.selectByExample(example); |
|
|
|
|
|
// 已加入
|
|
|
|
|
|
if (CollectionUtil.isNotEmpty(gameUserJoins)) { |
|
|
|
|
|
//查询结果,返回对应的信息
|
|
|
|
|
|
return joinResult(gameUserJoins.get(0), gameRecord); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//游戏已结束
|
|
|
|
|
|
if (gameRecord.getGameStatus().byteValue() == GameConstant.GAME_COMPLETED) { |
|
|
|
|
|
ClientVo.Join joinVo = initStatusAndCount(gameRecord); |
|
|
|
|
|
ClientVo.CompletedData completedData = new ClientVo.CompletedData(); |
|
|
|
|
|
completedData.setTimes(0); |
|
|
|
|
|
completedData.setScore(0); |
|
|
|
|
|
completedData.setSort(joinVo.getCountMembers().intValue() + 1); |
|
|
|
|
|
completedData.setOver(0); |
|
|
|
|
|
joinVo.setCompletedData(completedData); |
|
|
|
|
|
return joinVo; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 2.保存游戏路径用户表(设置游戏路径ID+用户ID组合为唯一索引 replace)
|
|
|
|
|
|
GameUserJoin userJoin = new GameUserJoin(); |
|
|
|
|
|
userJoin.setId(snowflake.nextId()); |
|
|
|
|
|
userJoin.setUserId(userId); |
|
|
|
|
|
userJoin.setRecordId(join.getUrlId()); |
|
|
|
|
|
userJoin.setTimeDifference(timeMore); |
|
|
|
|
|
boolean prepare = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PREPARATION; |
|
|
|
|
|
boolean processing = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PROCESSING; |
|
|
|
|
|
userJoin.setLocalStartTime(prepare || processing ? gameRecord.getStartTime() + timeMore : 0); |
|
|
|
|
|
userJoin.setLocalStartTime(prepare || processing ? gameRecord.getEndTime() + timeMore : 0); |
|
|
|
|
|
gameUserJoinDao.insertSelective(userJoin); |
|
|
|
|
|
|
|
|
|
|
|
// 3.更新redis(sort set key:分数 value:头像,姓名)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//4.根据状态延时发送消息
|
|
|
|
|
|
|
|
|
|
|
|
//5.返回状态
|
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ClientVo.Join joinResult(GameUserJoin join, GameRecord gameRecord){ |
|
|
|
|
|
ClientVo.Join joinVo = initStatusAndCount(gameRecord); |
|
|
|
|
|
switch (gameRecord.getGameStatus()) { |
|
|
|
|
|
case GameConstant.GAME_PENDING : |
|
|
|
|
|
// 未开始
|
|
|
|
|
|
joinVo.setPendingData(new ClientVo.PendingData()); |
|
|
|
|
|
break; |
|
|
|
|
|
case GameConstant.GAME_PREPARATION : |
|
|
|
|
|
// 准备中
|
|
|
|
|
|
ClientVo.PreparingData preparingData = new ClientVo.PreparingData(); |
|
|
|
|
|
preparingData.setStartLocalTime(join.getLocalStartTime()); |
|
|
|
|
|
preparingData.setEndLocalTime(join.getLocalEndTime()); |
|
|
|
|
|
joinVo.setPreparingData(preparingData); |
|
|
|
|
|
break; |
|
|
|
|
|
case GameConstant.GAME_PROCESSING : |
|
|
|
|
|
// 进行中
|
|
|
|
|
|
ClientVo.ProcessingData processingData = new ClientVo.ProcessingData(); |
|
|
|
|
|
processingData.setStartLocalTime(join.getLocalStartTime()); |
|
|
|
|
|
processingData.setEndLocalTime(join.getLocalEndTime()); |
|
|
|
|
|
joinVo.setProcessingData(processingData); |
|
|
|
|
|
break; |
|
|
|
|
|
case GameConstant.GAME_COMPLETED : |
|
|
|
|
|
//已结束
|
|
|
|
|
|
ClientVo.CompletedData completedData = new ClientVo.CompletedData(); |
|
|
|
|
|
completedData.setTimes(Integer.parseInt(join.getTimes())); |
|
|
|
|
|
completedData.setScore(Integer.parseInt(join.getScore())); |
|
|
|
|
|
completedData.setSort(gameUserJoinDao.getRanking(join.getUserId(), join.getUserId())); |
|
|
|
|
|
// TODO 超过百分之几的用户
|
|
|
|
|
|
joinVo.setCompletedData(completedData); |
|
|
|
|
|
break; |
|
|
|
|
|
default: break; |
|
|
|
|
|
} |
|
|
|
|
|
log.info("参加游戏:{}", joinVo); |
|
|
|
|
|
return joinVo; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 设置状态和总人数 |
|
|
|
|
|
* @param gameRecord |
|
|
|
|
|
*/ |
|
|
|
|
|
private ClientVo.Join initStatusAndCount(GameRecord gameRecord) { |
|
|
|
|
|
ClientVo.Join joinVo = new ClientVo.Join(); |
|
|
|
|
|
// 游戏状态
|
|
|
|
|
|
joinVo.setGameStatus(gameRecord.getGameStatus()); |
|
|
|
|
|
// 总人数
|
|
|
|
|
|
// GameUserJoinExample userJoinExample = new GameUserJoinExample();
|
|
|
|
|
|
// userJoinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId());
|
|
|
|
|
|
// long count = gameUserJoinDao.countByExample(userJoinExample);
|
|
|
|
|
|
joinVo.setCountMembers(redisUtil.zsGetSize(gameRecord.getId() + GameConstant.GAME_KEY)); |
|
|
|
|
|
return joinVo; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|