diff --git a/src/main/java/com/ljsd/controller/GamotaGetServerListController.java b/src/main/java/com/ljsd/controller/GamotaGetServerListController.java new file mode 100644 index 0000000..22d22b5 --- /dev/null +++ b/src/main/java/com/ljsd/controller/GamotaGetServerListController.java @@ -0,0 +1,207 @@ +package com.ljsd.controller; + +import com.ljsd.pojo.UserRecentLoginInfo; +import com.ljsd.redis.RedisKey; +import com.ljsd.util.BaseGlobal; +import com.ljsd.util.GamotaConstats; +import com.ljsd.util.MD5Util; +import com.ljsd.util.StringUtils; +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * gamota网页支付获取服务器列表接口 + */ +public class GamotaGetServerListController extends HttpServlet { + private final static String _COLLECTION_NAME = "server_info"; + private final static String _WHITE_LIST = "white_list"; + private static ConcurrentHashMap serverInfosCache = new ConcurrentHashMap<>(); + private static volatile DBObject whiteListCache = new BasicDBObject(); + private static volatile String recommend = "";//推荐列表 + private static volatile long lastRefreshTime = 0; + private static final Logger LOGGER = LoggerFactory.getLogger(GetServerListController.class); + + public GamotaGetServerListController() { + super(); + } + + public void destroy() { + super.destroy(); + } + + // 服务器状态 0:不可见 -1 准备状态 1:维护 2:流畅 3拥挤 4爆满 5状态根据在线人数改变 + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json; charset=utf-8"); + PrintWriter out = response.getWriter(); + String regEx="[^0-9]"; + try { + String appota_user_id = request.getParameter("appota_user_id"); + String channel = request.getParameter("channel"); + String sub_channel = request.getParameter("sub_channel"); + String api_key = request.getParameter("api_key"); + String signature = request.getParameter("signature"); + LOGGER.info("channel0={}",channel); + DBObject resp = new BasicDBObject(); + List serverList = new ArrayList<>(); + if (StringUtils.checkIsEmpty(appota_user_id)) { + resp.put("message", "appota_user_id null"); + resp.put("error_code", 1); + resp.put("data", serverList); + out.print(resp.toString()); + return; + } + if (StringUtils.checkIsEmpty(channel)) { + resp.put("message", "channel null"); + resp.put("error_code", 1); + resp.put("data", serverList); + out.print(resp.toString()); + return; + } + if (StringUtils.checkIsEmpty(sub_channel)) { + resp.put("message", "sub_channel null"); + resp.put("error_code", 1); + resp.put("data", serverList); + out.print(resp.toString()); + return; + } + if (StringUtils.checkIsEmpty(api_key)) { + resp.put("message", "api_key null"); + resp.put("error_code", 1); + resp.put("data", serverList); + out.print(resp.toString()); + return; + } + if (StringUtils.checkIsEmpty(signature)) { + resp.put("message", "signature null"); + resp.put("error_code", 1); + resp.put("data", serverList); + out.print(resp.toString()); + return; + } + if(!api_key.equals(GamotaConstats.apikey)){ + resp.put("message", "api_key error"); + resp.put("error_code", 1); + resp.put("data", serverList); + out.print(resp.toString()); + return; + } + String signStr = api_key + appota_user_id; + String sign = MD5Util.encrypByMd5(signStr); + if(!sign.equals(signature)){ + LOGGER.info("sign=" + sign + " --- signature="+signature); + resp.put("message", "signature error"); + resp.put("error_code", 1); + resp.put("data", serverList); + out.print(resp.toString()); + return; + } + DBObject req = new BasicDBObject(); + serverInfosCache.clear(); + List serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req); + String maxNumServerId= ""; + int maxNum = 0; + for (DBObject serverInfo : serverInfoList) { + int state = Integer.parseInt(serverInfo.get("state").toString()); + if ( state == -1) { + continue; + } + //opentime + long time = Long.parseLong(serverInfo.get("open_time").toString()); + if (System.currentTimeMillis() < time) { + continue; + } + + //state + String server_id=serverInfo.get("server_id").toString(); + int num = getOnlineNum(server_id); + if (state == 5) { + state = getState(server_id,num); + } + if(state==0){ + continue; + } + if(state==2&&maxNum (int) dbObject.get("sort"))); + resp.put("message", "success"); + resp.put("error_code", 0); + resp.put("data", serverList); + out.print(resp.toString()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + out.flush(); + out.close(); + } + + } + + private DBObject getDBObject(String serverId, UserRecentLoginInfo userRecentLoginInfo) { + DBObject res = new BasicDBObject(); + res.put("serverid", serverId); + res.put("name", userRecentLoginInfo.getName()); + res.put("level", userRecentLoginInfo.getLevel()); + return res; + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + this.doGet(request, response); + } + + private int getState(String server_id,int num) throws Exception { + int state = 2; + Map globalCfgMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.GLOBAL_SYS_PRO, "", String.class, -1, false); + int crowdnum = Integer.valueOf(globalCfgMap.getOrDefault("crowdnum", "0")); + int highnum = Integer.valueOf(globalCfgMap.getOrDefault("highnum", "0")); + if (crowdnum != 0 && highnum != 0) { + if (num >= crowdnum) { + state = 3; + if (num >= highnum) + state = 4; + } + } + return state; + } + + private int getOnlineNum(String server_id) throws Exception { + Integer num = BaseGlobal.getInstance().redisApp.get("ONLINE_NUM", server_id, Integer.class, -1); + if (num == null) { + num = 0; + } + return num; + } +} diff --git a/src/main/java/com/ljsd/util/GamotaConstats.java b/src/main/java/com/ljsd/util/GamotaConstats.java new file mode 100644 index 0000000..bc4168c --- /dev/null +++ b/src/main/java/com/ljsd/util/GamotaConstats.java @@ -0,0 +1,11 @@ +package com.ljsd.util; + +/** + * Gamota渠道参数常量 + */ +public class GamotaConstats { + + public final static String apikey = "GMA202401-4B0C8B6C-C3B7290DEC0B"; + + public final static String secretKey = "XAhRvf9ycmv6BJGTUJci"; +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index c645c20..bfdfc06 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -162,18 +162,6 @@ /getServerVersion - - - getHaoGameUserInfo - com.ljsd.controller.HaoGameGetUserController - - - - getHaoGameUserInfo - /getHaoGameUserInfo - - - getKTGameUserInfo com.ljsd.controller.KTGetUserOpenIdController @@ -184,6 +172,16 @@ /getKTGameUserInfo + + gamotaGetServerList + com.ljsd.controller.GamotaGetServerListController + + + + gamotaGetServerList + /gamotaGetServerList + + com.ljsd.listener.WebContextListener