186 lines
6.7 KiB
Java
186 lines
6.7 KiB
Java
package com.ljsd.fight;
|
||
|
||
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;
|
||
import java.util.concurrent.atomic.AtomicInteger;
|
||
|
||
public class CheckFight {
|
||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CheckFight.class);
|
||
|
||
private ThreadLocal<LuaValue> localTransCoderObj = new ThreadLocal<LuaValue>();
|
||
private ThreadLocal<Integer> localVersion = new ThreadLocal<>();
|
||
// 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;
|
||
}
|
||
|
||
private AtomicInteger hotFixVersion = new AtomicInteger();
|
||
|
||
static class LuaHotFixBean{
|
||
private int version;
|
||
|
||
public int getVersion() {
|
||
return version;
|
||
}
|
||
}
|
||
|
||
|
||
public void luaHotFix(){
|
||
LuaHotFixBean luaHotFixBean = STableManager.getJsonFilePathInJsonConf("lua-hotfix.json", LuaHotFixBean.class);
|
||
if(luaHotFixBean!=null && hotFixVersion.get() < luaHotFixBean.getVersion()){
|
||
LOGGER.info("the curhotFixVersion={},the new hotFixVersion={} will hotfix ",hotFixVersion.get(),luaHotFixBean.getVersion());
|
||
//init();
|
||
LOGGER.info("the luahotfix done");
|
||
hotFixVersion.set(luaHotFixBean.getVersion());
|
||
}
|
||
}
|
||
|
||
// public void init(){
|
||
// LuaHotFixBean luaHotFixBean = STableManager.getJsonFilePathInJsonConf("lua-hotfix.json", LuaHotFixBean.class);
|
||
// if(luaHotFixBean!=null) {
|
||
// hotFixVersion = luaHotFixBean.getVersion();
|
||
// }
|
||
// }
|
||
|
||
|
||
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.*$");
|
||
}
|
||
|
||
|
||
/**
|
||
* 单线程执行每次用时50ms,目前支持并发100以内,再大用线程池异步处理
|
||
* http://www.luaj.org/luaj/3.0/examples/jse/SampleMultiThreaded.java
|
||
* @param seed
|
||
* @param fightData
|
||
* @param optionData
|
||
* @return {战斗结果, 战斗伤害/战斗时间,单位1剩余血量,单位2剩余血量,单位3剩余血量,单位4剩余血量,单位5剩余血量}
|
||
*/
|
||
public long[] 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());
|
||
args.set("maxRound",maxTime);
|
||
LuaValue result = func.call( args,fightData,optionData);
|
||
long[] resultCache = new long[9];//对方阵容走配置 Todo 后面结构走配置
|
||
resultCache[0]= result.get("result").tolong();
|
||
resultCache[1]= result.get("duration").tolong();
|
||
LOGGER.info(" lua check test fight result : {} {}", resultCache[0] , result.length());
|
||
for (int i = 1; i <= result.length(); i++) {
|
||
long talon = result.rawget(i).tolong();
|
||
if (talon > 0) {
|
||
resultCache[i + 1] = Math.min(Long.MAX_VALUE, talon);
|
||
} else {
|
||
resultCache[i + 1] = Math.max(Long.MIN_VALUE, talon);
|
||
}
|
||
resultCache[i + 1] = result.rawget(i).tolong();
|
||
LOGGER.info(i + " --> 单位剩余血量 : " + resultCache[i + 1]);
|
||
}
|
||
resultCache[8]= result.get("curRound").tolong();
|
||
return resultCache;
|
||
}
|
||
|
||
public long[][] checkHardStageFight(int seed,int maxTime, LuaValue fightData, LuaValue optionData,FightType fightType){
|
||
LuaValue transCoderObj = this.getTransCoderObj();
|
||
long[][] resultX = new long[2][];
|
||
LuaValue func = transCoderObj.get(LuaValue.valueOf("Execute"));
|
||
LuaValue args = new LuaTable();
|
||
args.set("seed",seed);
|
||
//todo type
|
||
args.set("type",fightType.getType());
|
||
args.set("maxRound",maxTime);
|
||
LuaValue result = func.call( args,fightData,optionData);
|
||
int status = result.get("result").toint();
|
||
int duration = result.get("duration").toint();
|
||
long[] starRecord = new long[6];
|
||
for(int x = 1; x <= result.get("starRecord").length() ; x++){
|
||
long s = result.get("starRecord").rawget(x).tolong();
|
||
starRecord[x-1] = s;
|
||
}
|
||
|
||
long[] resultCache = new long[8];//对方阵容走配置 Todo 后面结构走配置
|
||
resultCache[0]= (long) status;
|
||
resultCache[1]= (long) duration;
|
||
LOGGER.info(" lua check test fight result : {} {}", resultCache[0] , result.length());
|
||
for (int i = 1; i <= result.length(); i++) {
|
||
long tolong = result.rawget(i).tolong();
|
||
if(tolong>0){
|
||
resultCache[i+1]= Math.min((long) Integer.MAX_VALUE,tolong);
|
||
}else {
|
||
resultCache[i+1]= Math.max((long) Integer.MIN_VALUE,tolong);
|
||
}
|
||
resultCache[i+1] = (long) result.rawget(i).toint();
|
||
LOGGER.info(i+" --> 单位剩余血量 : "+ resultCache[i+1]);
|
||
}
|
||
resultX[0] = resultCache;
|
||
resultX[1] = starRecord;
|
||
return resultX;
|
||
}
|
||
|
||
public LuaValue getTransCoderObj(){
|
||
LuaValue transCoderObj = localTransCoderObj.get();
|
||
if(transCoderObj == null||localVersion.get()!=hotFixVersion.get()){
|
||
String luaFileName = null;
|
||
try {
|
||
luaFileName = getPath("luafight/BattleMain.lua");
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
localVersion.set( hotFixVersion.get());
|
||
Globals globals = JsePlatform.debugGlobals();
|
||
transCoderObj = globals.loadfile(luaFileName).call();
|
||
localTransCoderObj.set(transCoderObj);
|
||
return transCoderObj;
|
||
}
|
||
return transCoderObj;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|