util_battle.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. --[[
  2. Descripttion:战斗
  3. version:
  4. Author: Neo,Huang
  5. Date: 2023-11-18 12:21:05
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2023-11-18 12:26:57
  8. --]]
  9. local timeUtil = require("utils.timeUtil")
  10. local mysqlUtil = require("utils.mysqlUtil")
  11. local util_player = require("utils.util_player")
  12. local boxAdapt = require("adapt.boxAdapt")
  13. local root = {}
  14. -- 新增记录
  15. function root:add_battle_record(settle, totalPrice)
  16. if is_empty(settle) then
  17. return false
  18. end
  19. if
  20. is_empty(settle.roomId) or is_empty(settle.battleBoxList) or is_empty(settle.createTime) or
  21. is_empty(settle.battleTime) or
  22. is_empty(settle.battleId) or
  23. is_empty(settle.winUid) or
  24. is_empty(settle.battlePlayerList)
  25. then
  26. return false
  27. end
  28. local expireTime = timeUtil.now() + timeUtil.SEC_PER_DAY * 365
  29. local columns =
  30. "`roomId`,`battleBoxList`,`createTime`,`battleTime`,`battleId`,`winUid`,`totalPrice`,`battlePlayerList`,`expireTime`"
  31. local values =
  32. string.format(
  33. "%s,'%s',%s,%s,'%s',%s,%s,'%s',%s",
  34. tostring(settle.roomId),
  35. cjson_encode(settle.battleBoxList),
  36. tostring(settle.createTime),
  37. tostring(settle.battleTime),
  38. tostring(settle.battleId),
  39. tostring(settle.winUid),
  40. tostring(totalPrice),
  41. cjson_encode(settle.battlePlayerList),
  42. tostring(expireTime)
  43. )
  44. for k, v in ipairs(settle.battlePlayerList) do
  45. local key = string.format("`battleUid%s`", tostring(k))
  46. columns = columns .. "," .. key
  47. values = values .. string.format(",%s", tostring(v.uid))
  48. end
  49. local sql = string.format("INSERT INTO `mdl_battlerecord` (%s) VALUES (%s); ", columns, values)
  50. local ok = mysqlUtil:insert(sql)
  51. log.info("add_battle_record sql[%s] ok[%s]", tostring(sql), tostring(ok))
  52. return ok
  53. end
  54. -- 打包历史战绩
  55. function root:pack_battle_record_list(data)
  56. local list = {}
  57. if is_empty(data) then
  58. return list
  59. end
  60. for _, v in ipairs(data) do
  61. -- 房间信息
  62. local roomInfo = {
  63. roomId = tonumber(v.roomId),
  64. battleBoxList = cjson_decode(v.battleBoxList),
  65. status = 2,
  66. createTime = tonumber(v.createTime),
  67. battleTime = tonumber(v.battleTime),
  68. playerList = {}
  69. }
  70. -- 战斗玩家列表
  71. local battlePlayerList = cjson_decode(v.battlePlayerList)
  72. for _, _v in ipairs(battlePlayerList) do
  73. local info = {
  74. playerInfo = util_player:get_base_info(v.uid),
  75. seatId = _v.seatId
  76. }
  77. table.insert(roomInfo.playerList, info)
  78. end
  79. local info = {
  80. roomInfo = roomInfo,
  81. battleId = v.battleId,
  82. winUid = tonumber(v.winUid),
  83. battlePlayerList = battlePlayerList
  84. }
  85. table.insert(list, info)
  86. end
  87. return list
  88. end
  89. -- 获取精彩对战记录
  90. function root:get_brilliant_record_list()
  91. local minTotalPrice = boxAdapt:battle_const("battle_box_open_total_price_is_exciting_battle") or 10000
  92. local count = boxAdapt:battle_const("exciting_battle_record_num") or 20
  93. local sql =
  94. string.format(
  95. "SELECT * FROM `mdl_battlerecord` WHERE `totalPrice`>=%s ORDER BY battleTime DESC limit %s",
  96. tostring(minTotalPrice),
  97. tostring(count)
  98. )
  99. local ok, ret = mysqlUtil:select(sql)
  100. if not ok or is_empty(ret) then
  101. return
  102. end
  103. return self:pack_battle_record_list(ret)
  104. end
  105. -- 获取对战记录
  106. function root:get_record_list(lastTime)
  107. lastTime = lastTime or timeUtil.now()
  108. local count = 20
  109. local sql =
  110. string.format(
  111. "SELECT * FROM `mdl_battlerecord` WHERE `battleTime`<%s ORDER BY battleTime DESC limit %s",
  112. tostring(lastTime),
  113. tostring(count)
  114. )
  115. local ok, ret = mysqlUtil:select(sql)
  116. if not ok or is_empty(ret) then
  117. return
  118. end
  119. return self:pack_battle_record_list(ret)
  120. end
  121. -- 获取玩家个人对战记录
  122. function root:get_player_record_list(uid, lastTime)
  123. if is_empty(uid) then
  124. return
  125. end
  126. lastTime = lastTime or timeUtil.now()
  127. local count = 20
  128. local sql =
  129. string.format(
  130. "SELECT * FROM `mdl_battlerecord` WHERE `battleTime`<%s and (`battleUid1`=%s or `battleUid2`=%s or `battleUid3`=%s or `battleUid4`=%s or `battleUid5`=%s) ORDER BY battleTime DESC limit %s",
  131. tostring(lastTime),
  132. tostring(uid),
  133. tostring(uid),
  134. tostring(uid),
  135. tostring(uid),
  136. tostring(uid),
  137. tostring(count)
  138. )
  139. local ok, ret = mysqlUtil:select(sql)
  140. if not ok or is_empty(ret) then
  141. return
  142. end
  143. return self:pack_battle_record_list(ret)
  144. end
  145. return root