Jelajahi Sumber

Merge branch 'package' of GameCenter/game-center into dev

zhimo 1 bulan lalu
induk
melakukan
d6973c3eb0

+ 1 - 1
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/SDKApplication.java

@@ -25,7 +25,7 @@ public class SDKApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(SDKApplication.class, args);
-        System.out.println("赞象SDK服务启动成功 <登录日志优化> ( ´・・)ノ(._.`) \n" +
+        System.out.println("赞象SDK服务启动成功 <玩家角色上报信息, 优化判断> ( ´・・)ノ(._.`) \n" +
                 " ___________ _   __\n" +
                 "/  ___|  _  \\ | / /\n" +
                 "\\ `--.| | | | |/ / \n" +

+ 33 - 23
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/GameUserRoleServiceImpl.java

@@ -115,7 +115,10 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
             return this.gameRoleUpdate(gameUserRoleParam, gameUserRole, userData);
         }
         //新建游戏角色
-        this.gameRoleCreate(gameUserRoleParam, userData);
+        boolean result = this.gameRoleCreate(gameUserRoleParam, userData);
+        if (!result) {
+            return Boolean.FALSE;
+        }
         //创建角色通知监听服务
         this.callListenIn(gameUserRoleParam, userData);
         //返回结果
@@ -134,9 +137,12 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
         //创建角色
         if (Objects.equals(dataType, DataTypeEnum.TYPE_CREATE_ROLE.getDateType())) {
             //新建游戏角色
-            this.gameRoleCreate(param, userData);
-            //插入用户登录记录
-            return userLoginLogService.createRoleLoginLog(userData, param, LoginTypeEnum.LOGIN_IN);
+            boolean result = this.gameRoleCreate(param, userData);
+            if (result) {
+                //插入用户登录记录
+                userLoginLogService.createRoleLoginLog(userData, param, LoginTypeEnum.LOGIN_IN);
+            }
+            return result;
         }
         //判断角色是否存在
         GameUserRole gameUserRole = super.getOne(new LambdaQueryWrapper<GameUserRole>()
@@ -145,8 +151,10 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
                 .eq(GameUserRole::getRoleId, param.getRoleId()));
         //逻辑兜底, 万一角色未创建成功, 补充创建
         if (gameUserRole == null) {
-            //新建游戏角色
-            this.gameRoleCreate(param, userData);
+            //未新建成功, 直接结束
+            if (!this.gameRoleCreate(param, userData)) {
+                return false;
+            }
         } else {
             //角色信息更新
             this.gameRoleUpdate(param, gameUserRole, userData);
@@ -164,8 +172,8 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
 
     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.getGameId()
+                + "_" + param.getRoleId(), 0L, 20L, TimeUnit.SECONDS)) {
             return Boolean.TRUE;
         }
         //判断是否等级变更
@@ -207,19 +215,14 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
         return result;
     }
 
-    private void gameRoleCreate(GameUserRoleUpdateParam param, UserData userData) {
-        //查询玩家角色信息
-        GameUserRole userRole = this.getOne(new LambdaQueryWrapper<GameUserRole>()
-                .eq(GameUserRole::getUserId, userData.getUserId())
+    private boolean gameRoleCreate(GameUserRoleUpdateParam param, UserData userData) {
+        //角色在游戏中已经存在, 不可创建
+        if (this.count(new LambdaQueryWrapper<GameUserRole>()
                 .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)) {
-            return;
+                .eq(GameUserRole::getRoleId, param.getRoleId())
+        ) > 0) {
+            log.error("玩家收到别人角色上报, userId : {}, param : {}", userData.getUserId(), JsonUtil.toString(param));
+            return false;
         }
         //查询玩家信息
         User user = userService.getOne(new LambdaQueryWrapper<User>()
@@ -230,11 +233,16 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
                 .eq(GameUser::getUserId, userData.getUserId()));
         //参数不匹配
         if (user == null || gameUser == null) {
-            log.error("上报参数不匹配,玩家信息为空! param : {}, userData : {}", JsonUtil.toString(param), JsonUtil.toString(userData));
-            return;
+            log.error("角色上报玩家信息为空, userData : {}", JsonUtil.toString(userData));
+            return false;
+        }
+        //上锁
+        if (!distributedLockComponent.doLock(RedisKeyConstant.ROLE_CREATE_LOCK + userData.getGameId()
+                + "_" + param.getRoleId(), 0L, 3L, TimeUnit.MINUTES)) {
+            return false;
         }
         //创建角色
-        userRole = this.transform(param, userData, gameUser, user);
+        GameUserRole userRole = this.transform(param, userData, gameUser, user);
         super.save(userRole);
         //更新玩家创角数
         gameUserService.update(new LambdaUpdateWrapper<GameUser>()
@@ -254,6 +262,8 @@ public class GameUserRoleServiceImpl extends ServiceImpl<GameUserRoleMapper, Gam
         this.callListenIn(param, userData);
         //角色操作记录添加
         roleOperateService.systemRoleOperate(userRole);
+        //返回成功
+        return true;
     }
 
     private GameUserRole transform(GameUserRoleUpdateParam param, UserData userData, GameUser gameUser, User user) {