miduo_server/fightmanager/src/main/java/com/ljsd/fight/CheckFight.java

145 lines
4.7 KiB
Java
Raw Normal View History

2019-11-27 10:33:27 +08:00
package com.ljsd.fight;
2019-11-25 16:13:24 +08:00
import manager.STableManager;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
2020-06-13 16:59:39 +08:00
import java.util.concurrent.atomic.AtomicInteger;
2019-11-25 16:13:24 +08:00
public class CheckFight {
private static final Logger LOGGER = LoggerFactory.getLogger(CheckFight.class);
private ThreadLocal<LuaValue> localTransCoderObj = new ThreadLocal<LuaValue>();
2020-06-13 16:59:39 +08:00
private ThreadLocal<Integer> localVersion = new ThreadLocal<>();
2019-11-25 16:13:24 +08:00
// private String luaFileName = null;
private CheckFight(){}
public static class Instance {
public final static CheckFight instance = new CheckFight();
}
public static CheckFight getInstance() {
return CheckFight.Instance.instance;
}
2020-06-13 16:59:39 +08:00
private AtomicInteger hotFixVersion = new AtomicInteger();
2019-11-25 16:13:24 +08:00
static class LuaHotFixBean{
private int version;
public int getVersion() {
return version;
}
}
public void luaHotFix(){
LuaHotFixBean luaHotFixBean = STableManager.getJsonFilePathInJsonConf("lua-hotfix.json", LuaHotFixBean.class);
2020-06-13 16:59:39 +08:00
if(luaHotFixBean!=null && hotFixVersion.get() < luaHotFixBean.getVersion()){
LOGGER.info("the curhotFixVersion={},the new hotFixVersion={} will hotfix ",hotFixVersion.get(),luaHotFixBean.getVersion());
//init();
2019-11-25 16:13:24 +08:00
LOGGER.info("the luahotfix done");
2020-06-13 16:59:39 +08:00
hotFixVersion.set(luaHotFixBean.getVersion());
2019-11-25 16:13:24 +08:00
}
}
2020-06-13 16:59:39 +08:00
// public void init(){
// LuaHotFixBean luaHotFixBean = STableManager.getJsonFilePathInJsonConf("lua-hotfix.json", LuaHotFixBean.class);
// if(luaHotFixBean!=null) {
// hotFixVersion = luaHotFixBean.getVersion();
// }
// }
2019-11-25 16:13:24 +08:00
public static String getPath(String prefixDir, String... filePath) throws IOException {
StringBuilder path = new StringBuilder();
path.append(getRootPath()).append(prefixDir);
for (String p : filePath) {
path.append(File.separator).append(p);
}
return path.toString();
}
public static String getRootPath() throws IOException {
StringBuilder path = new StringBuilder();
if (isWindows()) {// Window 系统
path.append(new File(".").getCanonicalPath()).append(File.separator);
}else {
path.append("../");
}
return path.toString();
}
public static boolean isWindows() {
String osName = System.getProperty("os.name");
return osName.matches("^(?i)Windows.*$");
}
/**
* 线50ms100线
* http://www.luaj.org/luaj/3.0/examples/jse/SampleMultiThreaded.java
* @param seed
* @param fightData
* @param optionData
* @return {,1,2,3,4,5}
*/
public int[] checkFight(int seed,int maxTime, LuaValue fightData, LuaValue optionData,FightType fightType){
LuaValue transCoderObj = this.getTransCoderObj();
LuaValue func = transCoderObj.get(LuaValue.valueOf("Execute"));
LuaValue args = new LuaTable();
args.set("seed",seed);
//todo type
args.set("type",fightType.getType());
2020-05-07 16:31:28 +08:00
args.set("maxRound",maxTime);
2019-11-25 16:13:24 +08:00
LuaValue result = func.call( args,fightData,optionData);
int status = result.get("result").toint();
int duration = result.get("duration").toint();
2020-04-11 18:48:02 +08:00
int[] resultCache = new int[8];//对方阵容走配置 Todo 后面结构走配置
2019-11-25 16:13:24 +08:00
resultCache[0]=status;
resultCache[1]=duration;
2019-12-12 18:32:54 +08:00
LOGGER.info(" lua check test fight result : "+ status);
2019-11-25 16:13:24 +08:00
for (int i = 1; i <= result.length(); i++) {
resultCache[i+1] = result.rawget(i).toint();
2019-12-12 18:32:54 +08:00
LOGGER.info(i+" --> 单位剩余血量 : "+ resultCache[i+1]);
2019-11-25 16:13:24 +08:00
}
return resultCache;
}
public LuaValue getTransCoderObj(){
LuaValue transCoderObj = localTransCoderObj.get();
2020-06-13 16:59:39 +08:00
if(transCoderObj == null||localVersion.get()!=hotFixVersion.get()){
2019-11-25 16:13:24 +08:00
String luaFileName = null;
try {
luaFileName = getPath("luafight/BattleMain.lua");
} catch (IOException e) {
e.printStackTrace();
}
2020-06-13 16:59:39 +08:00
localVersion.set( hotFixVersion.get());
2019-11-25 16:13:24 +08:00
Globals globals = JsePlatform.debugGlobals();
transCoderObj = globals.loadfile(luaFileName).call();
localTransCoderObj.set(transCoderObj);
return transCoderObj;
}
return transCoderObj;
}
}