|
@@ -4,26 +4,28 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.zanxiang.game.module.mybatis.entity.GameUser;
|
|
|
-import com.zanxiang.game.module.mybatis.entity.GameUserRole;
|
|
|
-import com.zanxiang.game.module.mybatis.entity.User;
|
|
|
+import com.zanxiang.game.module.mybatis.entity.*;
|
|
|
import com.zanxiang.game.module.mybatis.mapper.GameUserRoleMapper;
|
|
|
import com.zanxiang.game.module.sdk.constant.RedisKeyConstant;
|
|
|
import com.zanxiang.game.module.sdk.enums.DataTypeEnum;
|
|
|
import com.zanxiang.game.module.sdk.enums.KafkaEventTrackEnum;
|
|
|
import com.zanxiang.game.module.sdk.enums.LoginTypeEnum;
|
|
|
import com.zanxiang.game.module.sdk.pojo.param.GameRoleActiveCallParam;
|
|
|
+import com.zanxiang.game.module.sdk.pojo.param.GameUserRoleSubmitParam;
|
|
|
import com.zanxiang.game.module.sdk.pojo.param.GameUserRoleUpdateParam;
|
|
|
import com.zanxiang.game.module.sdk.pojo.param.UserData;
|
|
|
import com.zanxiang.game.module.sdk.service.*;
|
|
|
import com.zanxiang.module.redis.service.IDistributedLockComponent;
|
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
+import com.zanxiang.module.util.exception.BaseException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.logging.log4j.util.Strings;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
+import reactor.util.function.Tuple2;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
@@ -64,6 +66,56 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
|
|
|
@Autowired
|
|
|
private IDistributedLockComponent distributedLockComponent;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IGameExtService gameExtService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateUserGameRole(GameUserRoleSubmitParam param) {
|
|
|
+ //查询游戏信息
|
|
|
+ GameExt gameExt = gameExtService.getByGameAppId(param.getGameId());
|
|
|
+ if (gameExt == null || Strings.isBlank(gameExt.getLoginKey())) {
|
|
|
+ log.error("参数错误, 游戏信息不存在, param : {}", JsonUtil.toString(param));
|
|
|
+ throw new BaseException("参数错误, 游戏信息不存在");
|
|
|
+ }
|
|
|
+ //根据token检验
|
|
|
+ UserToken userToken = userTokenService.getCheckUserToken(param.getUserId(), param.getToken());
|
|
|
+ if (userToken == null) {
|
|
|
+ log.error("参数错误, token令牌无效, param : {}", JsonUtil.toString(param));
|
|
|
+ throw new BaseException("参数错误, token令牌无效");
|
|
|
+ }
|
|
|
+ //验证签名
|
|
|
+ Tuple2<String, String> tuple2 = userTokenService.getMySign(gameExt, param.getUserId(), param.getToken());
|
|
|
+ if (!Objects.equals(tuple2.getT2(), param.getSign())) {
|
|
|
+ log.error("参数错误 , str : {}, mySign : {}, sign : {}", tuple2.getT1(), tuple2.getT2(), param.getSign());
|
|
|
+ throw new BaseException("参数错误, sign验证失败, str : " + tuple2.getT1() + ", mySign : " + tuple2.getT2());
|
|
|
+ }
|
|
|
+ //提交的角色信息
|
|
|
+ GameUserRoleUpdateParam gameUserRoleParam = param.getGameUserRoleParam();
|
|
|
+ //查询玩家信息
|
|
|
+ User user = userService.getById(userToken.getUserId());
|
|
|
+ //构建 userData
|
|
|
+ UserData userData = new UserData();
|
|
|
+ userData.setUserId(user.getId());
|
|
|
+ userData.setGameId(user.getGameId());
|
|
|
+ userData.setDeviceSystem(param.getOs());
|
|
|
+ //判断角色是否存在
|
|
|
+ GameUserRole gameUserRole = super.getOne(new LambdaQueryWrapper<GameUserRole>()
|
|
|
+ .eq(GameUserRole::getUserId, user.getId())
|
|
|
+ .eq(GameUserRole::getGameId, user.getGameId())
|
|
|
+ .eq(GameUserRole::getRoleId, gameUserRoleParam.getRoleId()));
|
|
|
+ //更新游戏角色
|
|
|
+ if (gameUserRole != null) {
|
|
|
+ this.gameRoleUpdate(gameUserRoleParam, gameUserRole, userData);
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+ //新建游戏角色
|
|
|
+ this.gameRoleCreate(gameUserRoleParam, userData);
|
|
|
+ //创建角色通知监听服务
|
|
|
+ this.callListenIn(gameUserRoleParam, userData);
|
|
|
+ //返回结果
|
|
|
+ return Boolean.TRUE;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Boolean updateUserGameRole(GameUserRoleUpdateParam param, UserData userData) {
|
|
@@ -78,7 +130,8 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
|
|
|
//新建游戏角色
|
|
|
this.gameRoleCreate(param, userData);
|
|
|
//插入用户登录记录
|
|
|
- return userLoginLogService.createRoleLoginLog(userData, param.getRoleId(), param.getRoleName(), LoginTypeEnum.LOGIN_IN.getLoginType());
|
|
|
+ return userLoginLogService.createRoleLoginLog(userData, param.getRoleId(), param.getRoleName(),
|
|
|
+ LoginTypeEnum.LOGIN_IN.getLoginType());
|
|
|
}
|
|
|
//判断角色是否存在
|
|
|
GameUserRole gameUserRole = super.getOne(new LambdaQueryWrapper<GameUserRole>()
|
|
@@ -97,7 +150,8 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
|
|
|
//进入游戏
|
|
|
if (Objects.equals(dataType, DataTypeEnum.TYPE_ENTER_GAME.getDateType())) {
|
|
|
//插入用户登录记录
|
|
|
- return userLoginLogService.createRoleLoginLog(userData, param.getRoleId(), param.getRoleName(), LoginTypeEnum.LOGIN_IN.getLoginType());
|
|
|
+ return userLoginLogService.createRoleLoginLog(userData, param.getRoleId(), param.getRoleName(),
|
|
|
+ LoginTypeEnum.LOGIN_IN.getLoginType());
|
|
|
}
|
|
|
//等级提升更新
|
|
|
if (Objects.equals(dataType, DataTypeEnum.TYPE_LEVEL_UP.getDateType())) {
|
|
@@ -106,14 +160,16 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
|
|
|
//退出游戏
|
|
|
if (Objects.equals(dataType, DataTypeEnum.TYPE_EXIT_GAME.getDateType())) {
|
|
|
//插入用户退出记录
|
|
|
- return userLoginLogService.createRoleLoginLog(userData, param.getRoleId(), param.getRoleName(), LoginTypeEnum.LOGIN_OUT.getLoginType());
|
|
|
+ return userLoginLogService.createRoleLoginLog(userData, param.getRoleId(), param.getRoleName(),
|
|
|
+ LoginTypeEnum.LOGIN_OUT.getLoginType());
|
|
|
}
|
|
|
return Boolean.FALSE;
|
|
|
}
|
|
|
|
|
|
private boolean gameRoleUpdate(GameUserRoleUpdateParam param, GameUserRole gameUserRole, UserData userData) {
|
|
|
//更新频率限制, 20秒更新一次, 避免游戏实时战力高频上报
|
|
|
- if (!distributedLockComponent.doLock(RedisKeyConstant.ROLE_LEVEL_UP + userData.getUserId(), 0L, 20L, TimeUnit.SECONDS)) {
|
|
|
+ if (!distributedLockComponent.doLock(RedisKeyConstant.ROLE_LEVEL_UP + userData.getUserId(),
|
|
|
+ 0L, 20L, TimeUnit.SECONDS)) {
|
|
|
return Boolean.TRUE;
|
|
|
}
|
|
|
//玩家角色信息更新
|
|
@@ -142,13 +198,15 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
|
|
|
private void gameRoleCreate(GameUserRoleUpdateParam param, UserData userData) {
|
|
|
//查询玩家角色信息
|
|
|
GameUserRole userRole = this.getOne(new LambdaQueryWrapper<GameUserRole>()
|
|
|
+ .eq(GameUserRole::getUserId, userData.getUserId())
|
|
|
.eq(GameUserRole::getGameId, userData.getGameId())
|
|
|
.eq(GameUserRole::getRoleId, param.getRoleId()));
|
|
|
if (userRole != null) {
|
|
|
return;
|
|
|
}
|
|
|
//上锁
|
|
|
- if (!distributedLockComponent.doLock(RedisKeyConstant.ROLE_CREATE_LOCK + param.getRoleId(), 0L, 3L, TimeUnit.MINUTES)) {
|
|
|
+ if (!distributedLockComponent.doLock(RedisKeyConstant.ROLE_CREATE_LOCK + param.getRoleId(),
|
|
|
+ 0L, 3L, TimeUnit.MINUTES)) {
|
|
|
return;
|
|
|
}
|
|
|
//查询玩家信息
|