服务器推荐

master
zhangshanxue 2019-11-05 10:57:31 +08:00
parent ab008e8da1
commit cd3409f779
1 changed files with 43 additions and 8 deletions

View File

@ -13,9 +13,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class GetServerListController extends HttpServlet {
@ -23,6 +21,7 @@ public class GetServerListController extends HttpServlet {
private final static String _WHITE_LIST = "white_list";
private static ConcurrentHashMap<String, DBObject> serverInfosCache = new ConcurrentHashMap<>();
private static volatile DBObject whiteListCache = new BasicDBObject();
private static volatile String recommend = "";//推荐列表
private static volatile long lastRefreshTime = 0;
public GetServerListController() {
@ -55,36 +54,55 @@ public class GetServerListController extends HttpServlet {
req.put("plat", plat);
List<DBObject> serverList = new ArrayList<>();
List<String> serverIDList = new ArrayList<>();
long local = System.currentTimeMillis();
if ((local - lastRefreshTime) > 60 * 1000 || serverInfosCache.isEmpty()) {
serverInfosCache.clear();
lastRefreshTime = local;
recommend = "";
whiteListCache = BaseGlobal.getInstance().mongoDBPool.findOne(_WHITE_LIST, openId);
List<DBObject> serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req);
SortedSet<String> newServerList = new TreeSet<>();
String maxNumServerId= "";
int maxNum = 0;
for (DBObject serverInfo : serverInfoList) {
int state = Integer.parseInt(serverInfo.get("state").toString());
if (state == 0 || 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 (whiteListCache != null && state == 1) {
state = 2;
} else {
if (state == 5) {
state = getState(serverInfo.get("server_id").toString());
state = getState(server_id,num);
}
}
if(state==2&&maxNum<num){
maxNum = num;
maxNumServerId = server_id;
}
//new
int isnew = 0;
if (serverInfo.containsField("is_new"))
isnew = Integer.parseInt(serverInfo.get("is_new").toString());
if(isnew==1){
newServerList.add(server_id);
}
DBObject res = new BasicDBObject();
res.put("name", serverInfo.get("name"));
res.put("server_id", serverInfo.get("server_id"));
res.put("server_id",server_id);
res.put("state", state);
res.put("open_time", serverInfo.get("open_time"));
res.put("isnew", isnew);
@ -97,8 +115,18 @@ public class GetServerListController extends HttpServlet {
res.put("ip", ip);
res.put("port", port);
serverList.add(res);
serverIDList.add(server_id);
serverInfosCache.put(serverInfo.get("server_id").toString(), res);
}
//推荐服务器 新服中选择id小的 否者流畅中选择人数最多的
if (newServerList.size() != 0) {
recommend = newServerList.first();
} else {
recommend = maxNumServerId;
}
} else {
serverList.addAll(serverInfosCache.values());
}
@ -109,17 +137,25 @@ public class GetServerListController extends HttpServlet {
List<String> myServerList = new ArrayList<>();
int time = 0;
for (Map.Entry<String, UserRecentLoginInfo> entry : userServerInfoMap.entrySet()) {
if(!serverIDList.contains(entry.getKey())){
//服务器已清除
continue;
}
myServerList.add(entry.getKey());
if (entry.getValue().getLoginTime() > time) {
myLastServer = getDBObject(entry.getKey(), entry.getValue());
}
}
DBObject resp = new BasicDBObject();
resp.put("serverList", serverList);
resp.put("myServerList", myServerList);
if (null != myLastServer)
resp.put("lastServer", myLastServer);
if(recommend!=null&&!recommend.equals("")){
resp.put("recommend", recommend);
}
out.print(resp.toString());
} catch (Exception e) {
e.printStackTrace();
@ -143,8 +179,7 @@ public class GetServerListController extends HttpServlet {
this.doGet(request, response);
}
private int getState(String server_id) throws Exception {
int num = getOnlineNum(server_id);
private int getState(String server_id,int num) throws Exception {
int state = 2;
Map<String, String> globalCfgMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.GLOBAL_SYS_PRO, "", String.class, -1, false);
int crowdnum = Integer.valueOf(globalCfgMap.getOrDefault("crowdnum", "0"));