Merge branch 'master_test_hw_server_arena' of http://60.1.1.230/backend/jieling_server into master_test_hw_server_arena
commit
6b4d5296db
|
|
@ -141,7 +141,8 @@ public class TimeUtils {
|
|||
|
||||
int lastHour = getOverTimeCount(1599602400000L,1600462810000L,5);
|
||||
|
||||
System.out.println(lastHour);
|
||||
long lastOrUnderHour = getLastOrUnderHour(TimeUtils.now(), 1, 0, 0, true);
|
||||
System.out.println(lastOrUnderHour);
|
||||
}
|
||||
/**
|
||||
* 判断是否合法的时间格式(HH:mm:ss)
|
||||
|
|
@ -1356,12 +1357,25 @@ public class TimeUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*获取传入时间的上个几时几分或下个几时几分
|
||||
* @param hour
|
||||
* @param lastOrUnder true为上个,false为下个
|
||||
* @return
|
||||
*/
|
||||
|
||||
|
||||
public static long getLastOrUnderHour(long time,int week,int hour,int minute,boolean lastOrUnder) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(time);
|
||||
cal.set(Calendar.DAY_OF_WEEK,week==7?0:week+1);
|
||||
time = cal.getTimeInMillis();
|
||||
long lastOrUnderHour = getLastOrUnderHour(time, hour, minute, true);
|
||||
if(!lastOrUnder){
|
||||
lastOrUnderHour+=TimeUtils.WEEK;
|
||||
}
|
||||
return lastOrUnderHour;
|
||||
}
|
||||
/**
|
||||
*获取传入时间的上个几时几分或下个几时几分
|
||||
* @param hour
|
||||
* @param lastOrUnder true为上个,false为下个
|
||||
* @return
|
||||
*/
|
||||
public static long getLastOrUnderHour(long time,int hour,int minute,boolean lastOrUnder){
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(time);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.global;
|
|||
|
||||
import com.global.config.ConfigurableApplicationContextManager;
|
||||
import com.global.config.CoreSettings;
|
||||
import com.global.logic.GlobalLogic;
|
||||
import com.global.redis.RedisUtil;
|
||||
import com.global.thread.ThreadManager;
|
||||
import com.global.thrift.pool.GlobalRpcService;
|
||||
|
|
@ -53,6 +54,8 @@ public class GlobalServerApplication {
|
|||
RedisUtil.getInstence().init();
|
||||
bean = ConfigurableApplicationContextManager.getBean(CoreSettings.class);
|
||||
LOGGER.info("ServerProperties ->{}", bean.toString());
|
||||
//启动时候分组
|
||||
GlobalLogic.getInstance().doSplit();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
package com.global.handler;
|
||||
|
||||
import com.global.GlobalServerApplication;
|
||||
import com.global.logic.GlobalLogic;
|
||||
import com.google.protobuf.GeneratedMessage;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import rpc.global.GlobalProto;
|
||||
import rpc.net.RpcMessage;
|
||||
import rpc.net.gtGBaseHandler;
|
||||
|
|
@ -13,6 +15,7 @@ import rpc.net.gtGBaseHandler;
|
|||
*/
|
||||
public class GetWorldServerRequestHandler extends gtGBaseHandler<GlobalProto.GetWorldServerRequest> {
|
||||
|
||||
|
||||
@Override
|
||||
public GeneratedMessage processWithProto(RpcMessage rpcMessage, GlobalProto.GetWorldServerRequest proto) throws Exception {
|
||||
|
||||
|
|
@ -25,7 +28,7 @@ public class GetWorldServerRequestHandler extends gtGBaseHandler<GlobalProto.Get
|
|||
//第一版固定一个世界服
|
||||
String ip = GlobalServerApplication.bean.getWorldip();
|
||||
int port = GlobalServerApplication.bean.getPort();
|
||||
return GlobalProto.GetWorldServerResponse.newBuilder().setIp(ip).setPort(String.valueOf(port)).setGroup(1).build();
|
||||
return GlobalProto.GetWorldServerResponse.newBuilder().setIp(ip).setPort(String.valueOf(port)).setGroup(GlobalLogic.getInstance().getSplitInfo(proto.getServerID())).build();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
package com.global.logic;
|
||||
|
||||
|
||||
import com.global.redis.RedisKey;
|
||||
import com.global.redis.RedisUtil;
|
||||
import config.SMServerArenaSetting;
|
||||
import manager.STableManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.redis.core.ZSetOperations;
|
||||
import util.TimeUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author lvxinran
|
||||
* @date 2020/12/1
|
||||
* @discribe
|
||||
*/
|
||||
|
||||
public class GlobalLogic {
|
||||
|
||||
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(GlobalLogic.class);
|
||||
|
||||
/**
|
||||
* 获取单例
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static GlobalLogic getInstance() {
|
||||
return Instance.instance;
|
||||
}
|
||||
|
||||
public static class Instance {
|
||||
public final static GlobalLogic instance = new GlobalLogic();
|
||||
}
|
||||
|
||||
private static Map<String,Integer> splitInfo;
|
||||
/**
|
||||
* 分组
|
||||
*/
|
||||
public void doSplit(){
|
||||
SMServerArenaSetting setting = STableManager.getConfig(SMServerArenaSetting.class).get(1);
|
||||
RedisUtil redisUtil = RedisUtil.getInstence();
|
||||
//这里为所有服务器的世界等级
|
||||
Map<String,Integer> worldLevelMap = redisUtil.getMapValues(RedisKey.SERVER_WORLDLEVE_INFO+":",String.class,Integer.class);
|
||||
Object matched = redisUtil.get(RedisKey.MATCHED_SPLIT_KEY);
|
||||
if(matched!=null){
|
||||
return;
|
||||
}
|
||||
LOGGER.info("Global开始分组");
|
||||
long lastHour = TimeUtils.getLastOrUnderHour(TimeUtils.now(), 1, 0, 0, true);
|
||||
lastHour+=(2*TimeUtils.WEEK);
|
||||
int expireTime =(int)((lastHour-TimeUtils.now())/1000);
|
||||
redisUtil.set(RedisKey.MATCHED_SPLIT_KEY,String.valueOf(1),expireTime-60);
|
||||
worldLevelMap.forEach((k,v)->redisUtil.zsetAddOne(RedisKey.SERVER_LEVEL_RANK,k,v));
|
||||
Set<ZSetOperations.TypedTuple<String>> zsetRevRangeWithScore = redisUtil.getZsetRevRangeWithScore(RedisKey.SERVER_LEVEL_RANK, setting.getWorldLevel(), 999);
|
||||
// int remain = zsetRevRangeWithScore.size() % 8;
|
||||
int index = 0;
|
||||
int areaId = 1;
|
||||
Map<String,Integer> split = new HashMap<>(zsetRevRangeWithScore.size());
|
||||
for(ZSetOperations.TypedTuple<String> data:zsetRevRangeWithScore){
|
||||
if(index>=setting.getServerNum()){
|
||||
if((zsetRevRangeWithScore.size()-areaId*setting.getServerNum())>setting.getServerNum()/2){
|
||||
areaId++;
|
||||
}
|
||||
index=0;
|
||||
}
|
||||
split.put(data.getValue(),areaId);
|
||||
|
||||
index++;
|
||||
}
|
||||
redisUtil.putMapEntrys(RedisKey.SERVER_SPLIT_INFO,split);
|
||||
LOGGER.info("Global分组结束");
|
||||
splitInfo = RedisUtil.getInstence().getMapValues(RedisKey.SERVER_SPLIT_INFO,String.class,Integer.class);
|
||||
}
|
||||
|
||||
public int getSplitInfo(int serverId){
|
||||
|
||||
if(splitInfo==null){
|
||||
splitInfo = RedisUtil.getInstence().getMapValues(RedisKey.SERVER_SPLIT_INFO,String.class,Integer.class);
|
||||
}
|
||||
return splitInfo.getOrDefault(String.valueOf(serverId),-1);
|
||||
}
|
||||
}
|
||||
|
|
@ -13,4 +13,10 @@ public class RedisKey {
|
|||
|
||||
public static final String MACH_SERVERINFO = "MACH_SERVERINFO";
|
||||
|
||||
public static final String MATCHED_SPLIT_KEY = "MATCHED_SPLIT_KEY";
|
||||
|
||||
public static final String SERVER_LEVEL_RANK = "SERVER_LEVEL_RANK";
|
||||
|
||||
public static final String SERVER_SPLIT_INFO = "SERVER_SPLIT_INFO";
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,9 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.ZSetOperations;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import util.TimeUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
|
@ -155,6 +157,98 @@ public class RedisUtil {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通缓存放入并设置时间
|
||||
*
|
||||
* @param key
|
||||
* 键
|
||||
* @param value
|
||||
* 值
|
||||
* @param time
|
||||
* 时间(秒) time要大于0 如果time小于等于0 将设置无限期
|
||||
* @return true成功 false 失败
|
||||
*/
|
||||
public boolean set(String key, String value, long time) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
||||
} else {
|
||||
set(key, value);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//操作sortset
|
||||
public void zsetAddOne(String key, String uid, double useTime){
|
||||
for (int i = 0; i < MAX_TRY_TIMES; i++) {
|
||||
try {
|
||||
redisTemplate.opsForZSet().add(key, uid, useTime);
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
TimeUtils.sleep(FAILED_SLEEP);
|
||||
}
|
||||
}
|
||||
}
|
||||
//操作sortset
|
||||
public Set<ZSetOperations.TypedTuple<String>> getZsetRevRangeWithScore(String key, double min, double max){
|
||||
for (int i = 0; i < MAX_TRY_TIMES; i++) {
|
||||
try {
|
||||
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,min,max);
|
||||
} catch (Exception e) {
|
||||
TimeUtils.sleep(FAILED_SLEEP);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> void putMapEntrys(String type, Map<String,T> valus){
|
||||
Map<String, String> result = new HashMap<>();
|
||||
valus.forEach((k,v)->{
|
||||
result.put(k,gson.toJson(v));
|
||||
});
|
||||
for (int i = 0; i < MAX_TRY_TIMES; i++) {
|
||||
try {
|
||||
redisTemplate.opsForHash().putAll(type,result);
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
TimeUtils.sleep(FAILED_SLEEP);
|
||||
}
|
||||
}
|
||||
}
|
||||
public <T> List<T> getMapEntrys(String type,Collection<Object> mapKeys, Class<T> valueClazz){
|
||||
List<T> result = new ArrayList<>();
|
||||
for (int i = 0; i < MAX_TRY_TIMES; i++) {
|
||||
try {
|
||||
List<Object> objects = redisTemplate.opsForHash().multiGet(type, mapKeys);
|
||||
if(objects!=null &&!objects.isEmpty()){
|
||||
for(Object o : objects){
|
||||
if(o!=null){
|
||||
result.add(gson.fromJson(o.toString(),valueClazz));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
TimeUtils.sleep(FAILED_SLEEP);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public <K,T> Map<K,T> getMapValues(String type,Class<K> keyClazz,Class<T> valueClazz){
|
||||
Map<K,T> result = new HashMap<>();
|
||||
Map<Object, Object> entries = redisTemplate.opsForHash().entries(type);
|
||||
for(Map.Entry<Object,Object> item : entries.entrySet()){
|
||||
Object key1 = item.getKey();
|
||||
Object value = item.getValue();
|
||||
result.put(gson.fromJson(gson.toJson(key1),keyClazz),gson.fromJson(value.toString(),valueClazz));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// ================================Map=================================
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package com.global.thread;
|
||||
|
||||
import com.global.redis.RedisKey;
|
||||
import com.global.redis.RedisUtil;
|
||||
import com.global.logic.GlobalLogic;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
|
@ -24,7 +23,7 @@ public class MinuteTask extends Thread {
|
|||
public void run() {
|
||||
try {
|
||||
LOGGER.info("MinuteTask start...");
|
||||
|
||||
everyMondyZeroClockTask();
|
||||
|
||||
LOGGER.info("MinuteTask end...");
|
||||
} catch (Exception e) {
|
||||
|
|
@ -61,14 +60,15 @@ public class MinuteTask extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
//每周一凌晨五点执行
|
||||
public void everyMondyFiveClockTask() throws Exception {
|
||||
//每周一凌晨0点执行
|
||||
public void everyMondyZeroClockTask() throws Exception {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||
int minute = calendar.get(Calendar.MINUTE);
|
||||
int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
||||
if(w == 1 &&hour == 5 && minute ==0){
|
||||
if(w == 1 &&hour == 0 && minute ==0){
|
||||
LOGGER.info("everyMondyFiveClockTask start ...");
|
||||
GlobalLogic.getInstance().doSplit();
|
||||
LOGGER.info("everyMondyFiveClockTask end ...");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -360,7 +360,7 @@ public class RedisKey {
|
|||
familyKey.add(FAMILY_FIGHT_TOTAL_EXP);
|
||||
familyKey.add(FAMILY_FIGHT_FINISH_MATCHING);
|
||||
}
|
||||
public static String getKey(String type, String key, boolean withoutServerId)
|
||||
public static String getKey(String type, String key, boolean withoutServerId){
|
||||
if(RedisKey.TOKEN.equals(type)||RedisKey.USER_SERVER_INFO.equals(type)||RedisKey.SERVER_WORLDLEVE_INFO.equals(type)){
|
||||
return type + RedisKey.Delimiter_colon + key;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue