|
|
@ -2,6 +2,10 @@ package com.ccsens.game.service; |
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
import cn.hutool.core.lang.Snowflake; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.ccsens.cloudutil.bean.tall.vo.MemberVo; |
|
|
|
import com.ccsens.cloudutil.feign.TallFeignClient; |
|
|
|
import com.ccsens.game.bean.dto.ClientDto; |
|
|
|
import com.ccsens.game.bean.po.GameRecord; |
|
|
|
import com.ccsens.game.bean.po.GameUserJoin; |
|
|
@ -11,6 +15,7 @@ import com.ccsens.game.persist.dao.GameRecordDao; |
|
|
|
import com.ccsens.game.persist.dao.GameUserJoinDao; |
|
|
|
import com.ccsens.game.util.GameConstant; |
|
|
|
import com.ccsens.util.CodeEnum; |
|
|
|
import com.ccsens.util.JsonResponse; |
|
|
|
import com.ccsens.util.RedisUtil; |
|
|
|
import com.ccsens.util.exception.BaseException; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
@ -40,6 +45,8 @@ public class ClientService implements IClientService { |
|
|
|
private Snowflake snowflake; |
|
|
|
@Autowired |
|
|
|
private RedisUtil redisUtil; |
|
|
|
@Autowired |
|
|
|
private TallFeignClient tallFeignClient; |
|
|
|
|
|
|
|
@Override |
|
|
|
public ClientVo.Join join(ClientDto.Join join, Long userId) { |
|
|
@ -76,6 +83,8 @@ public class ClientService implements IClientService { |
|
|
|
joinVo.setCompletedData(completedData); |
|
|
|
return joinVo; |
|
|
|
} |
|
|
|
boolean prepare = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PREPARATION; |
|
|
|
boolean processing = gameRecord.getGameStatus().byteValue() == GameConstant.GAME_PROCESSING; |
|
|
|
|
|
|
|
// 2.保存游戏路径用户表(设置游戏路径ID+用户ID组合为唯一索引 replace)
|
|
|
|
GameUserJoin userJoin = new GameUserJoin(); |
|
|
@ -83,19 +92,26 @@ public class ClientService implements IClientService { |
|
|
|
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); |
|
|
|
// 获取头像和用户名
|
|
|
|
JsonResponse<MemberVo.MemberInfo> memberInfo = tallFeignClient.getMemberByUserId(userId, join.getProjectId()); |
|
|
|
if (memberInfo.getCode().intValue() == CodeEnum.SUCCESS.getCode() && memberInfo.getData() != null) { |
|
|
|
userJoin.setAvatarurl(memberInfo.getData().getAvatarUrl()); |
|
|
|
userJoin.setNickname(memberInfo.getData().getNickname()); |
|
|
|
} |
|
|
|
gameUserJoinDao.insertSelective(userJoin); |
|
|
|
|
|
|
|
// 3.更新redis(sort set key:分数 value:头像,姓名)
|
|
|
|
|
|
|
|
|
|
|
|
ClientDto.RedisUser user = new ClientDto.RedisUser(); |
|
|
|
user.setId(userJoin.getId()); |
|
|
|
user.setAvatarurl(userJoin.getAvatarurl()); |
|
|
|
user.setNickname(userJoin.getNickname()); |
|
|
|
redisUtil.zsSet(GameConstant.generateGameKey(gameRecord.getId()), JSON.toJSONString(user), 0, GameConstant.REDIS_TIME); |
|
|
|
//4.根据状态延时发送消息
|
|
|
|
|
|
|
|
//5.返回状态
|
|
|
|
return null; |
|
|
|
return joinResult(userJoin, gameRecord); |
|
|
|
} |
|
|
|
|
|
|
|
private ClientVo.Join joinResult(GameUserJoin join, GameRecord gameRecord){ |
|
|
@ -122,8 +138,8 @@ public class ClientService implements IClientService { |
|
|
|
case GameConstant.GAME_COMPLETED : |
|
|
|
//已结束
|
|
|
|
ClientVo.CompletedData completedData = new ClientVo.CompletedData(); |
|
|
|
completedData.setTimes(Integer.parseInt(join.getTimes())); |
|
|
|
completedData.setScore(Integer.parseInt(join.getScore())); |
|
|
|
completedData.setTimes(join.getTimes()); |
|
|
|
completedData.setScore(join.getScore()); |
|
|
|
completedData.setSort(gameUserJoinDao.getRanking(join.getUserId(), join.getUserId())); |
|
|
|
// TODO 超过百分之几的用户
|
|
|
|
joinVo.setCompletedData(completedData); |
|
|
@ -143,10 +159,13 @@ public class ClientService implements IClientService { |
|
|
|
// 游戏状态
|
|
|
|
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)); |
|
|
|
long count = redisUtil.zsGetSize(GameConstant.generateGameKey(gameRecord.getId())); |
|
|
|
if (count <= 0) { |
|
|
|
GameUserJoinExample userJoinExample = new GameUserJoinExample(); |
|
|
|
userJoinExample.createCriteria().andRecordIdEqualTo(gameRecord.getId()); |
|
|
|
count = gameUserJoinDao.countByExample(userJoinExample); |
|
|
|
} |
|
|
|
joinVo.setCountMembers(count); |
|
|
|
return joinVo; |
|
|
|
} |
|
|
|
} |
|
|
|