BattleLogManager.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. BattleLogManager = {}
  2. local this = BattleLogManager
  3. local isLog = nil
  4. if AppConst and AppConst.isOpenBLog then
  5. isLog = AppConst.isOpenBLog
  6. else
  7. isLog = true --< 服务端跑战斗开启
  8. end
  9. local function pairsByKeys(t)
  10. local a = {}
  11. for n in pairs(t) do
  12. if n then
  13. a[#a+1] = n
  14. end
  15. end
  16. table.sort(a, function( op1, op2 )
  17. local type1, type2 = type(op1), type(op2)
  18. local num1, num2 = tonumber(op1), tonumber(op2)
  19. if ( num1 ~= nil) and (num2 ~= nil) then
  20. return num1 < num2
  21. elseif type1 ~= type2 then
  22. return type1 < type2
  23. elseif type1 == "string" then
  24. return op1 < op2
  25. elseif type1 == "boolean" then
  26. return op1
  27. -- 以上处理: number, string, boolean
  28. else -- 处理剩下的: function, table, thread, userdata
  29. return tostring(op1) < tostring(op2) -- tostring后比较字符串
  30. end
  31. end)
  32. local i = 0
  33. return function()
  34. i = i + 1
  35. return a[i], t[a[i]]
  36. end
  37. end
  38. function this.PrintBattleTable(tb)
  39. local indent_str = "{"
  40. local count = 0
  41. for k,v in pairs(tb) do
  42. count = count + 1
  43. end
  44. for k=1, #tb do
  45. local v = tb[k]
  46. if type(v) == "table" then
  47. indent_str = indent_str .. this.PrintBattleTable(v)
  48. elseif type(v) == "string" then
  49. indent_str = indent_str .. "\""..tostring(v) .. "\""
  50. else
  51. indent_str = indent_str .. tostring(v)
  52. end
  53. if k < count then
  54. indent_str = indent_str..","
  55. end
  56. end
  57. local index = 0
  58. for k,v in pairsByKeys(tb) do
  59. index = index + 1
  60. if type(k) ~= "number" then
  61. if type(v) == "table" then
  62. indent_str = string.format("%s%s=%s", indent_str, tostring(k), this.PrintBattleTable(v))
  63. elseif type(v) == "string" then
  64. indent_str = string.format("%s%s=\"%s\"", indent_str, tostring(k), tostring(v))
  65. else
  66. indent_str = string.format("%s%s=%s", indent_str, tostring(k), tostring(v))
  67. end
  68. if index < count then
  69. indent_str = indent_str .. ","
  70. end
  71. end
  72. end
  73. indent_str = indent_str .. "}"
  74. return indent_str
  75. end
  76. --
  77. function this.Init(fightdata)
  78. this.timestamp = Random.GetSeed()
  79. this.fightdata = fightdata
  80. -- LogYellow("### BattleLogManager fightData")
  81. -- WYLog(this.fightdata)
  82. this.logList = {}
  83. end
  84. function this.Log(...)
  85. if not isLog then return end
  86. local args = {...}
  87. local log = args[1] .. ":\n"
  88. log = log .. string.format("%s = %s, ", "frame", BattleLogic.CurFrame())
  89. for i = 2, #args, 2 do
  90. local key = args[i]
  91. local value = args[i + 1]
  92. log = log .. string.format("%s = %s, ", key, value)
  93. end
  94. table.insert(this.logList, log)
  95. end
  96. function this.WriteFile()
  97. if not isLog then return end
  98. local time = string.format("%d-%d-%d-%d-%d-%d",
  99. os.date("%Y"),
  100. os.date("%m"),
  101. os.date("%d"),
  102. os.date("%H"),
  103. os.date("%M"),
  104. os.date("%S"))
  105. local file
  106. local platform
  107. -- linux
  108. if not file then
  109. file = io.open("../luafight/BattleRecord/log-"..time..".txt", "a")
  110. platform = "Linux"
  111. end
  112. -- local window server
  113. if not file then
  114. file = io.open("luafight/BattleRecord/log-"..time..".txt", "a")
  115. platform = "Local Windows Server"
  116. end
  117. -- local
  118. if not file then
  119. file = io.open("BattleRecord/log-"..time..".txt", "a")
  120. platform = "Local"
  121. end
  122. file:write(platform..":\n\n\n")
  123. for i = 1, #this.logList do
  124. file:write(this.logList[i].."\n")
  125. end
  126. file:write("\n\n\n\n\n")
  127. file:write("fightData:\n")
  128. file:write(this.PrintBattleTable(this.fightdata))
  129. file:write("\n\n")
  130. file:write("timeStamp: " .. this.timestamp)
  131. LogYellow("timeStamp: " .. this.timestamp)
  132. io.close(file)
  133. end
  134. return BattleLogManager