1.加入依赖包
<dependency>
<groupId>org.luaj</groupId>
<artifactId>luaj-jse</artifactId>
<version>3.0.1</version>
</dependency>
2.
@Value("${lua.luaPath}")
private String luaPath;//lua脚本路径
@PostMapping("rootLua")
public Object rootLua(String a,String b,String c){
String luaFileName = null;
JSONArray qbl=null;
JSONObject qb=null;
JSONArray bll=null;
try {
if(!StringUtil.isEmpty(a)) {
qbl=JSONArray.parseArray(a);
}
if(!StringUtil.isEmpty(b)) {
qb=JSON.parseObject(b);
}
if(!StringUtil.isEmpty(c)) {
bll=JSONArray.parseArray(c);
}
luaFileName =luaPath;
System.out.println("luaPath===>"+luaPath);
Globals globals = JsePlatform.standardGlobals();
LuaValue transcoderObj = globals.loadfile(luaFileName).call();
LuaValue func = transcoderObj.get(LuaValue.valueOf("readInfo"));
//CoerceJavaToLua.coerce(javaObject) 经测试,可以直接调用该方法
//将一个java对象转化为luaValue,但是嵌套model情况下的java对象转
//换有问题,因此这里直接使用LuaValue手动去包装
//a+b
LuaValue luaValue = parseQBR(qbl,qb);
//c
LuaValue luaValue1 = parseBL(bll);
String result = func.invoke(luaValue,luaValue1).toString();
System.out.println("result---"+result);
return Result.success(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("result---" + e.getMessage());
return Result.errorData(e.getMessage());
}
}
private LuaValue parseQBR(JSONArray qbl,JSONObject qb) {
LuaValue luaValue = new LuaTable();
//发包人
LuaValue l = new LuaTable();
l.set("id", qb.getIntValue("id"));
l.set("ff", qb.getString("ff"));
l.set("je", qb.getDoubleValue("je"));
l.set("s", qb.getIntValue("s"));
l.set("fy", qb.getString("fy"));
luaValue.set(1, l);
//抢包人
for (int i = 0; i < qbl.size(); i++) {
LuaValue ll = new LuaTable();
ll.set("id", qbl.getJSONObject(i).getIntValue(id));
ll.set("bRen", qbl.getJSONObject(i).getString("bRen"));
ll.set("je", qbl.getJSONObject(i).getDoubleValue("je"));
luaValue.set(i+2, ll);
}
return luaValue;
}
private LuaValue parseBL(JSONArray bll) {
LuaValue luaValue = new LuaTable();
//包类
for (int i = 0; i < bll.size(); i++) {
LuaValue ll = new LuaTable();
ll.set("bb1", bll.getJSONObject(i).getDoubleValue("bb1"));
ll.set("bb2", bll.getJSONObject(i).getDoubleValue("bb2"));
ll.set("bb3", bll.getJSONObject(i).getDoubleValue("bb3"));
ll.set("bb4", bll.getJSONObject(i).getDoubleValue("bb4"));
ll.set("bb5", bll.getJSONObject(i).getDoubleValue("bb5"));
ll.set("bb6", bll.getJSONObject(i).getDoubleValue("bb6"));
ll.set("bb7", bll.getJSONObject(i).getDoubleValue("bb7"));
ll.set("bb8", bll.getJSONObject(i).getDoubleValue("bb8"));
ll.set("bb9", bll.getJSONObject(i).getDoubleValue("bb9"));
luaValue.set(i+1, ll);
}
return luaValue;
}
3.lua脚本
local tt = {}
-- 传入一个lua对象
function tt.readInfo(a,b)
print(a[1]["je"])
print(a[2]["je"])
print(b1[1]["bb2"])
return a[1]["je"]
end
return tt