Browse Source

1.smartbowsdk 添加额外操作接口。
2.调整了RankComp部分代码

slambb 1 năm trước cách đây
mục cha
commit
5275af6147

+ 3 - 3
SmartBowBusinessServer3/res/LocalTest.json

@@ -1,5 +1,5 @@
 {
-  "localIp": "192.168.0.112",
-  "dataBaseUsername": "root",
-  "dataBasePassword": "Kk7235110"
+  "localIp": "本地id",
+  "dataBaseUsername": "本地账户",
+  "dataBasePassword": "本地密码"
 }

+ 152 - 18
SmartBowBusinessServer3/src/main/java/com/example/demo/module/rank/RankComp.java

@@ -95,10 +95,10 @@ public class RankComp {
         });
         socketResponse.send(rankList, myRank);
     }
-
-    List<Integer> pkGameTypes = Arrays.asList(new Integer[]{9, 10, 11, 12});
+    //pk 游戏
+    public static List<Integer> pkGameTypes = Arrays.asList(new Integer[]{9, 10, 11, 12});
     //PK游戏结果,0:玩家1胜利;1:玩家2胜利;2:双方平局
-    List<Integer> pkGameResults = Arrays.asList(new Integer[]{0, 1, 2});
+    public static List<Integer> pkGameResults = Arrays.asList(new Integer[]{0, 1, 2});
     @SocketMethod(async = true)
     public void uploadPKGameRes(String roomKey, int gameType,
             int p1ID, int p2ID, int gameRes
@@ -138,8 +138,8 @@ public class RankComp {
         RankService.ins.updateGameScore(gameType, p1ID, (int)Ra);
         RankService.ins.updateGameScore(gameType, p2ID, (int)Rb);
     }
-    //FruitMaster:15,WildAttack:14,DuckHunter:13
-    List<Integer> singleGameTypes = Arrays.asList(new Integer[]{15, 14, 13});
+    //单机游戏 FruitMaster:15,WildAttack:14,DuckHunter:13
+    public static List<Integer> singleGameTypes = Arrays.asList(new Integer[]{15, 14, 13});
 
     //上传用户单机游戏分数
     @SocketMethod(async = true)
@@ -201,12 +201,64 @@ public class RankComp {
     public void uploadSeasonPKGameRes(String roomKey, int gameType,
                                 int p1ID, int p2ID, int gameRes
     ) {
-        if (roomKey == null || roomKey.length() != 32) return;
-        if (!pkGameTypes.contains(gameType)) return;
-        if (!pkGameResults.contains(gameRes)) return;
+        seasonPKGameRes(roomKey, gameType, p1ID,  p2ID,  gameRes);
+        //if (roomKey == null || roomKey.length() != 32) return;
+        //if (!pkGameTypes.contains(gameType)) return;
+        //if (!pkGameResults.contains(gameRes)) return;
+        //boolean res = RankService.ins.insertRoomGameRecord(roomKey, gameType, p1ID, p2ID, gameRes);
+        //if (!res) return;
+        //if (!RankService.ins.isRoomNeedCalculatePKScore(roomKey)) return;
+        //
+        ////获取时间
+        //String[] dates = getCurrentDates();
+        //String startDateStr = dates[0];
+        //String endDateStr = dates[1];
+        //
+        //Map map1 = RankService.ins.getSeasonGameScore(gameType, p1ID, startDateStr, endDateStr);
+        //Map map2 =  RankService.ins.getSeasonGameScore(gameType, p2ID, startDateStr, endDateStr);
+        //int id1 = (int)map1.get("id");
+        //int id2 = (int)map2.get("id");
+        //
+        //double Ra = (int)map1.get("score"); //RankService.ins.getSeasonGameScore(gameType, p1ID, startDateStr, endDateStr);
+        //double Rb = (int)map2.get("score");// RankService.ins.getSeasonGameScore(gameType, p2ID, startDateStr, endDateStr);
+        //
+        //double Ea = 1 / ( 1 + Math.pow(10, (Rb-Ra)/400d) );
+        //double Eb = 1 / ( 1 + Math.pow(10, (Ra-Rb)/400d) );
+        //double Sa;
+        //double Sb;
+        //double Ka;
+        //double Kb;
+        //if (gameRes == 0) {
+        //    Sa = 1;
+        //    Sb = 0;
+        //    Ka = 32;
+        //    Kb = Rb < 1100 ? 12 : 32;
+        //} else if (gameRes == 1) {
+        //    Sa = 0;
+        //    Sb = 1;
+        //    Ka = Ra < 1100 ? 12 : 32;
+        //    Kb = 32;
+        //} else {
+        //    Sa = 0.5;
+        //    Sb = 0.5;
+        //    Ka = 22;
+        //    Kb = 22;
+        //}
+        //Ra += Ka * (Sa - Ea);
+        //Rb += Kb * (Sb - Eb);
+        //RankService.ins.updateSeasonGameScore(gameType,id1, p1ID, (int)Ra);
+        //RankService.ins.updateSeasonGameScore(gameType,id2, p2ID, (int)Rb);
+    }
+    //处理赛季分数
+    public String seasonPKGameRes(String roomKey, int gameType,
+                                      int p1ID, int p2ID, int gameRes
+    ) {
+        if (roomKey == null || roomKey.length() != 32) return "roomKey error!";
+        if (!pkGameTypes.contains(gameType)) return "gameType error!";
+        if (!pkGameResults.contains(gameRes)) return "gameRes error!";
         boolean res = RankService.ins.insertRoomGameRecord(roomKey, gameType, p1ID, p2ID, gameRes);
-        if (!res) return;
-        if (!RankService.ins.isRoomNeedCalculatePKScore(roomKey)) return;
+        if (!res) return "room error!";
+        if (!RankService.ins.isRoomNeedCalculatePKScore(roomKey)) return "calculate pk score error";
 
         //获取时间
         String[] dates = getCurrentDates();
@@ -247,28 +299,49 @@ public class RankComp {
         Rb += Kb * (Sb - Eb);
         RankService.ins.updateSeasonGameScore(gameType,id1, p1ID, (int)Ra);
         RankService.ins.updateSeasonGameScore(gameType,id2, p2ID, (int)Rb);
+
+        return  "上传成功!";
     }
     //上传用户单机游戏分数
     @SocketMethod(async = true)
     public void uploadSeasonSinglePlayerGameRes(
             Player player, int gameType,int score
     ) {
+        seasonSinglePlayerGameRes(player.userID,gameType,score);
+        //if (!singleGameTypes.contains(gameType)) return;
+        ////只加一个分数
+        ////获取时间
+        //String[] dates = getCurrentDates();
+        //String startDateStr = dates[0];
+        //String endDateStr = dates[1];
+        //Map map1 =  RankService.ins.getSingleSeasonGameScore(gameType, player.userID, startDateStr, endDateStr);
+        //int id1 = (int)map1.get("id");
+        //int Ra = (int)map1.get("score"); //RankService.ins.getGameScore(gameType, player.userID);
+        ////Ra += score ;
+        ////记录一个最高分数
+        //if(score > Ra) Ra = score;
+        //RankService.ins.updateSeasonGameScore(gameType, id1 , player.userID, Ra);
+    }
+    //上传单机分数,目前只处理单机部分
+    public boolean seasonSinglePlayerGameRes(
+            int userId, int gameType,int score
+    ) {
 
-        if (!singleGameTypes.contains(gameType)) return;
+        if (!singleGameTypes.contains(gameType)) return false;
         //只加一个分数
         //获取时间
         String[] dates = getCurrentDates();
         String startDateStr = dates[0];
         String endDateStr = dates[1];
-        Map map1 =  RankService.ins.getSingleSeasonGameScore(gameType, player.userID, startDateStr, endDateStr);
+        Map map1 =  RankService.ins.getSingleSeasonGameScore(gameType, userId, startDateStr, endDateStr);
         int id1 = (int)map1.get("id");
         int Ra = (int)map1.get("score"); //RankService.ins.getGameScore(gameType, player.userID);
         //Ra += score ;
         //记录一个最高分数
         if(score > Ra) Ra = score;
-        RankService.ins.updateSeasonGameScore(gameType, id1 , player.userID, Ra);
+        RankService.ins.updateSeasonGameScore(gameType, id1 , userId, Ra);
+        return true;
     }
-
     List<Integer> allGameTypes = Arrays.asList(new Integer[]{9, 10, 11, 12,13,14,15});
     //获取赛季排行版
     @SocketMethod(async = true)
@@ -276,7 +349,67 @@ public class RankComp {
             Player player, SocketResponse socketResponse, int rankArea, int rankGameType,
             String countryCode, String stateCode, String cityCode
     ) {
-        if (!allGameTypes.contains(rankGameType)) return;
+        Object[] results =  seasonRankListByGameType(player.userID, rankArea, rankGameType, countryCode, stateCode, cityCode);
+        //不符合条件的直接return
+        if (results.length > 0) {
+            socketResponse.send(results);
+        } else {
+           return;
+        }
+        //if (!allGameTypes.contains(rankGameType)) return;
+        ////获取时间
+        //String[] dates = getCurrentDates();
+        //String startDateStr = dates[0];
+        //String endDateStr = dates[1];
+        //
+        //SQL sqlUtil = new SQL();
+        ////singleGameTypes.contains(rankGameType) ? 0 :
+        ////int _score =  RankService.ins.getDefaultPkScore();
+        //int _score = singleGameTypes.contains(rankGameType) ? RankService.ins.getDefaultSingleScore() : RankService.ins.getDefaultPkScore();
+        //String formatScoreSQL = "IFNULL(score," + _score + ") as score";
+        //String sqlRankList = "SELECT user_info.id, " + formatScoreSQL
+        //        + ", avatarID, avatarUrl, nickname, country FROM user_info LEFT JOIN game_season_"
+        //        + rankGameType + " ON userID = user_info.id";
+        //
+        //if (rankArea == 3) { //国排行
+        //    sqlRankList += " WHERE country = " + sqlUtil.PARAM(countryCode);
+        //    sqlRankList += " AND create_time >= '"+startDateStr+"' AND create_time <= '" + endDateStr + "'";
+        //} else if (rankArea == 1) { //省排行
+        //    sqlRankList += " WHERE country = " + sqlUtil.PARAM(countryCode);
+        //    sqlRankList += " AND state = " + sqlUtil.PARAM(stateCode);
+        //    sqlRankList += " AND create_time >= '"+startDateStr+"' AND create_time <= '" + endDateStr + "'";
+        //} else if (rankArea == 2) { //市排行
+        //    sqlRankList += " WHERE country = " + sqlUtil.PARAM(countryCode);
+        //    sqlRankList += " AND state = " + sqlUtil.PARAM(stateCode);
+        //    sqlRankList += " AND city = " + sqlUtil.PARAM(cityCode);
+        //    sqlRankList += " AND create_time >= '"+startDateStr+"' AND create_time <= '" + endDateStr + "'";
+        //} else {
+        //    //否则为全球排行
+        //    sqlRankList += " WHERE create_time >= '"+startDateStr+"' AND create_time <= '" + endDateStr + "'";
+        //}
+        //
+        //sqlRankList += " ORDER BY score DESC, update_time ASC, id ASC";
+        //List<JSONObject> rankList = GlobalRoot.db.select(sqlRankList + " Limit 20");
+        //for (int i = 0; i < rankList.size(); i++) {
+        //    rankList.get(i).put("rankNum", i + 1);
+        //}
+        //String sqlMyRank = "SELECT id, score, avatarID, avatarUrl, nickname, country, rankNum FROM\n" +
+        //        " (SELECT id, score, avatarID, avatarUrl, nickname, country, @rankNum:=@rankNum+1 AS rankNum\n" +
+        //        " FROM (" + sqlRankList + ") a, (SELECT @rankNum:=0) b) c" +
+        //        " WHERE id = " + sqlUtil.PARAM(player.userID);
+        //List<JSONObject> myRank = GlobalRoot.db.select(sqlMyRank);
+        //myRank.forEach((a) -> {
+        //    a.put("userID", a.get("id"));
+        //});
+        //String seasonName = "S";
+        //socketResponse.send(rankList, myRank, seasonName,new String[]{startDateStr,endDateStr});
+    }
+
+    public Object[] seasonRankListByGameType(
+            int userId, int rankArea, int rankGameType,
+            String countryCode, String stateCode, String cityCode
+    ) {
+        if (!allGameTypes.contains(rankGameType))  return new Object[]{};
         //获取时间
         String[] dates = getCurrentDates();
         String startDateStr = dates[0];
@@ -316,17 +449,18 @@ public class RankComp {
         String sqlMyRank = "SELECT id, score, avatarID, avatarUrl, nickname, country, rankNum FROM\n" +
                 " (SELECT id, score, avatarID, avatarUrl, nickname, country, @rankNum:=@rankNum+1 AS rankNum\n" +
                 " FROM (" + sqlRankList + ") a, (SELECT @rankNum:=0) b) c" +
-                " WHERE id = " + sqlUtil.PARAM(player.userID);
+                " WHERE id = " + sqlUtil.PARAM(userId);
         List<JSONObject> myRank = GlobalRoot.db.select(sqlMyRank);
         myRank.forEach((a) -> {
             a.put("userID", a.get("id"));
         });
         String seasonName = "S";
-        socketResponse.send(rankList, myRank, seasonName,new String[]{startDateStr,endDateStr});
+        //socketResponse.send(rankList, myRank, seasonName,new String[]{startDateStr,endDateStr});
+        //返回 Object... args
+        return new Object[]{ rankList, myRank, seasonName,new String[]{startDateStr,endDateStr}};
     }
 
 
-
     ////上传分数测试用,限制特定账号
     //@SocketMethod(async = true)
     //public void uploadSeasonPKGameResTest( int gameType,int p1ID, int p2ID, int gameRes

+ 89 - 0
SmartBowBusinessServer3/src/main/java/com/example/demo/module/smartbowsdk/SmartBowSDKController.java

@@ -3,23 +3,31 @@ package com.example.demo.module.smartbowsdk;
 import com.example.demo.common.GlobalConfig;
 import com.example.demo.common.GlobalRoot;
 import com.example.demo.common.RequestResult;
+import com.example.demo.module.rank.RankComp;
+import com.example.demo.module.rank.RankService;
 import com.example.demo.module.smartbowsdk.dto.SBS_UserInfo;
 import com.example.demo.module.smartbowsdk.config.GameChannel;
 import com.example.demo.module.smartbowsdk.table.GameChannelLogin;
 import com.example.demo.table.Mac9Axis;
 import com.example.demo.table.UserLogin;
 import com.example.demo.table.user.UserInfo;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import pers.jc.sql.SQL;
 
 import javax.servlet.http.HttpServletRequest;
 import java.sql.Timestamp;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
 
 @CrossOrigin
 @RestController
 @RequestMapping("/SmartBowSDK")
 public class SmartBowSDKController {
 
+    RankComp rankComp = new RankComp();
+
     @PostMapping("/login")
     public RequestResult login(String gameID, String channelID, String username, String password) {
         RequestResult requestResult = new RequestResult();
@@ -146,4 +154,85 @@ public class SmartBowSDKController {
         requestResult.setMsg("获取成功");
         return requestResult;
     }
+
+    /**
+     * 处理赛季排行
+     */
+
+    /**
+     * 上传赛季pk分数
+     * @param roomKey pk时候房间id
+     * @param gameType pk游戏类型  对应RankComp定义的 pkGameTypes
+     * @param p2ID 对战用户2 ID
+     * @param gameRes PK游戏结果,0:玩家1胜利;1:玩家2胜利;2:双方平局
+     */
+    @PostMapping("/UploadSeasonPKGameRes")
+    public RequestResult UploadSeasonPKGameRes(String token, String roomKey, int gameType, int p2ID, int gameRes
+    ) {
+        RequestResult requestResult = new RequestResult();
+        SBS_LoginToken loginToken = new SBS_LoginToken(token);
+        if (!loginToken.isValid())
+        {
+            requestResult.setCode(-1);
+            requestResult.setMsg("Token失效");
+            return requestResult;
+        }
+        String msg = rankComp.seasonPKGameRes(roomKey, gameType, loginToken.id, p2ID, gameRes);
+        requestResult.setMsg(msg);
+        return requestResult;
+    }
+
+    @PostMapping("/UploadSeasonSinglePlayerGameRes")
+    public RequestResult UploadSeasonSinglePlayerGameRes(
+            String token,  int gameType,int score
+    ) {
+        RequestResult requestResult = new RequestResult();
+        SBS_LoginToken loginToken = new SBS_LoginToken(token);
+        if (!loginToken.isValid())
+        {
+            requestResult.setCode(-1);
+            requestResult.setMsg("Token失效");
+            return requestResult;
+        }
+       boolean uploadState =  rankComp.seasonSinglePlayerGameRes(loginToken.id,gameType,score);
+        if(uploadState){
+            requestResult.setMsg("上传成功");
+        }
+        else {
+            requestResult.setMsg("上传失败,确认单机游戏id");
+        }
+
+        return requestResult;
+    }
+
+    /**
+     * 获取用户排行榜
+     * @param token 用于登录token
+     * @param rankArea 排行区域 3:国,1:省,2市,其他:全球
+     * @param rankGameType allGameTypes 游戏类型
+     * @param countryCode
+     * @param stateCode
+     * @param cityCode
+     * @return
+     */
+    @PostMapping("/GetSeasonRankListByGameType")
+    public RequestResult GetSeasonRankListByGameType(
+            String token, int rankArea, int rankGameType,
+            String countryCode, String stateCode, String cityCode
+    ) {
+        RequestResult requestResult = new RequestResult();
+        SBS_LoginToken loginToken = new SBS_LoginToken(token);
+        if (!loginToken.isValid())
+        {
+            requestResult.setCode(-1);
+            requestResult.setMsg("Token失效");
+            return requestResult;
+        }
+
+        Object[] results =  rankComp.seasonRankListByGameType(loginToken.id, rankArea, rankGameType, countryCode, stateCode, cityCode);
+        //不符合条件的直接return
+        requestResult.setData(results);
+        requestResult.setMsg("获取排行榜成功!");
+        return requestResult;
+    }
 }