miduo_server/luafight/BattleMain_test.lua

167 lines
4.9 KiB
Lua

require("Modules.Battle.Logic.Misc.BattleDefine")
require("Modules.Battle.Logic.Misc.BattleUtil")
require("Modules.Battle.Logic.Misc.BattleQueue")
require("Modules.Battle.Logic.Misc.BattleDictionary")
require("Modules.Battle.Logic.Misc.BattleList")
require("Modules.Battle.Logic.Misc.BattleObjectPool")
require("Modules.Battle.Logic.Base.BattleEvent")
require("Modules.Battle.Logic.Base.Random")
require("Modules.Battle.Logic.Base.RoleData")
require("Modules.Battle.Logic.Base.Buff")
require("Modules.Battle.Logic.Base.Skill")
require("Modules.Battle.Logic.Base.Passivity")
require("Modules.Battle.Logic.BattleLogic")
require("Modules.Battle.Logic.RoleLogic")
local BattleMain = {}
local BattleLogic = BattleLogic
local Random = Random
local _fightData
local _optionData
local function newPairs(tbl, func)
if func == nil then
return pairs(tbl)
end
local ary = {}
local lastUsed = 0
for key --[[, val--]] in pairs(tbl) do
if (lastUsed == 0) then
ary[1] = key
else
local done = false
for j=1,lastUsed do -- 进行插入排序
if (func(key, ary[j]) == true) then
arrayInsert( ary, key, j )
done = true
break
end
end
if (done == false) then
ary[lastUsed + 1] = key
end
end
lastUsed = lastUsed + 1
end
-- 定义并返回迭代器
local i = 0
local iter = function ()
i = i + 1
if ary[i] == nil then
return nil
else
return ary[i], tbl[ary[i]]
end
end
return iter
end
local function PrintTable(tb)
local indent_str = "{"
for k=1, #tb do
local v = tb[k]
if type(v) == "table" then
indent_str = indent_str .. PrintTable(v)
else
indent_str = indent_str .. tostring(v)..","
end
end
for k,v in newPairs(tb) do
if type(k) ~= "number" then
if type(v) == "table" then
indent_str = string.format("%s%s=%s", indent_str, tostring(k), PrintTable(v))
else
indent_str = string.format("%s%s=%s", indent_str, tostring(k), tostring(v)..",")
end
end
end
indent_str = indent_str .. "},"
return indent_str
end
--捕获异常,并输出错误日志
local function error( err )
local time = string.format("%d-%d-%d-%d-%d-%d",
os.date("%Y"),
os.date("%m"),
os.date("%d"),
os.date("%H"),
os.date("%M"),
os.date("%S"))
local file = io.open("LogError/"..time..".txt", "a")
--file:write("fightData:\n"..PrintTable(_fightData).."\n")
--file:write("optionData:\n"..PrintTable(_optionData).."\n")
file:write("error:\n"..debug.traceback(err).."\n")
io.close(file)
print(debug.traceback(err))
end
local function generateRecordFile()
local time = string.format("%d-%d-%d-%d-%d-%d",
os.date("%Y"),
os.date("%m"),
os.date("%d"),
os.date("%H"),
os.date("%M"),
os.date("%S"))
local file = io.open("BattleRecord/"..time..".txt", "a")
local record = BattleLogic.GetRecord()
for i=1, #record do
file:write(record[i].."\n")
end
io.close(file)
end
function BattleMain.Execute(seed, fightData, optionData)
_fightData = fightData
_optionData = optionData
print(PrintTable(fightData))
--该开关用于输出战斗过程中的日志,用于验证前后端是否出现战斗不同步
BattleLogic.IsOpenBattleRecord = false
if xpcall(function ()
Random.SetSeed(seed)
BattleLogic.Init(fightData, optionData)
BattleLogic.StartOrder()
while not BattleLogic.IsEnd do
BattleLogic.Update()
end
end, error) then
local resultList = {}
if BattleLogic.Result == 1 then --胜利记录我方剩余血量
local arr = RoleManager.QueryIncludeExile(function (r) return r.camp == 0 end, true)
for i=1, #arr do
resultList[i] = arr[i]:GetRoleData(RoleDataName.Hp)
end
elseif BattleLogic.Result == 0 then --失败记录敌方剩余血量
local arr = BattleLogic.QueryIncludeExile(function (r) return r.camp == 1 end, true)
for i=1, #arr do
resultList[i] = arr[i]:GetRoleData(RoleDataName.Hp)
end
end
resultList.result = BattleLogic.Result
if BattleLogic.IsOpenBattleRecord then
generateRecordFile()
end
-- print -----------------------------------------
for i=1, #resultList do
print("hp:"..resultList[i])
end
print("最终运行帧数:"..BattleLogic.GetCurRound())
print("result:"..BattleLogic.Result)
return resultList
end
return { result = -1 }
end
return BattleMain