MailLogic.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. local Msg = require("core.Msg")
  2. local MailManager = require("mail.MailManager")
  3. local BagLogic = require("bag.BagLogic")
  4. local Grid = require("bag.Grid")
  5. local Broadcast = require("broadcast.Broadcast")
  6. local RoleDBLogic = require("role.RoleDBLogic")
  7. local Lang = require("common.Lang")
  8. local FilterUtil = require("common.FilterUtil")
  9. local Util = require("common.Util")
  10. local Log = require("common.Log")
  11. local ObjHuman = require("core.ObjHuman")
  12. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  13. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  14. local MailExcel = require("excel.mail")
  15. local ItemDefine = require("bag.ItemDefine")
  16. local EquipLogic = require("equip.EquipLogic")
  17. local FuwenLogic = require("fuwen.FuwenLogic")
  18. local LogDefine = require("common.LogDefine")
  19. local FuwenGrid = require("fuwen.FuwenGrid")
  20. function makeMail(net, data)
  21. net.uuid = data._id
  22. net.identity = data.identity and data.identity or ""
  23. net.type = data.type
  24. net.senderName = data.senderName or Lang.MAIL_SYSTEM_NAME
  25. net.sendTime = data.time
  26. net.read = data.read or 0
  27. net.get = data.get or 0
  28. net.head = data.head or 0
  29. net.flag = data.flag or 0
  30. net.fbTime = tonumber(data.fbTime) or 0
  31. net.fbContent = data.fbContent or ""
  32. net.fbHead = data.fbHead or 0
  33. net.fbLv = data.fbLv or 0
  34. net.expireTime = data.expireTime or 7 * 86400
  35. end
  36. MAX_PACKET_CNT = 30
  37. MAX_PERSON_MAIL_CNT = 20
  38. function query(human, type)
  39. if type == MailManager.GONGGAO then
  40. queryGongGao(human)
  41. return
  42. end
  43. local mails = MailManager.getMails(human.db._id, nil)
  44. local len = 0
  45. -- local gongGaoDot = 0
  46. local systemDot = 0
  47. local msgRet = Msg.gc.GC_MAIL_QUERY
  48. msgRet.isEnd = 0
  49. msgRet.dotFlag = 0
  50. for i = 1,#mails do
  51. if mails[i].get == nil or mails[i].read == nil then
  52. systemDot = 2
  53. end
  54. if mails[i].type == type then
  55. if len >= MAX_PACKET_CNT then
  56. msgRet.list[0] = len
  57. Msg.send(msgRet, human.fd)
  58. len = 0
  59. end
  60. len = len + 1
  61. local net = msgRet.list[len]
  62. makeMail(net, mails[i])
  63. end
  64. end
  65. msgRet.isEnd = 1
  66. msgRet.dotFlag = systemDot
  67. msgRet.list[0] = len
  68. Msg.send(msgRet, human.fd)
  69. end
  70. function queryGongGao(human)
  71. local msgRet = Msg.gc.GC_MAIL_READ
  72. local config = MailExcel.gongGao[1]
  73. msgRet.uuid = ""
  74. msgRet.title = config.title
  75. msgRet.content = config.content or ""
  76. msgRet.fbTime = 0
  77. msgRet.fbContent = ""
  78. msgRet.fbHead = 0
  79. msgRet.fbLv = 0
  80. msgRet.name =config.senderName or Lang.SYSTEM_MAIL
  81. msgRet.items[0] = 0
  82. Msg.send(msgRet, human.fd)
  83. end
  84. function read(human, mailUuid)
  85. local mail = MailManager.getMail(mailUuid)
  86. if not mail then
  87. return
  88. end
  89. if mail.receiverUuid ~= human.db._id then
  90. return
  91. end
  92. if not mail.read then
  93. mail.read = 1
  94. MailManager.saveMail(mail)
  95. end
  96. local msgRet = Msg.gc.GC_MAIL_READ
  97. msgRet.uuid = mail._id
  98. msgRet.title = mail.title or ""
  99. msgRet.content = mail.content or ""
  100. msgRet.fbTime = tonumber(mail.fbTime) or 0
  101. msgRet.fbContent = mail.fbContent or ""
  102. msgRet.fbHead = mail.fbHead or 0
  103. msgRet.fbLv = mail.fbLv or 0
  104. if mail.senderName ~= false and mail.senderName ~= nil then
  105. msgRet.name = mail.senderName
  106. else
  107. msgRet.name = Lang.SYSTEM_MAIL
  108. end
  109. local len = 0
  110. if mail.items then
  111. for _, v in ipairs(mail.items) do
  112. len = len + 1
  113. if type(v[2]) == "table" then
  114. -- 给装备
  115. if ItemDefine.isEquip(v[1]) then
  116. local equipGrid = v[2]
  117. Grid.makeItem(msgRet.items[len], equipGrid.id, 1, nil, equipGrid)
  118. end
  119. -- 给符文
  120. if ItemDefine.isFuwen(v[1]) then
  121. local fuwenGrid = v[2]
  122. Grid.makeItem(msgRet.items[len], fuwenGrid.id, 1, nil, fuwenGrid)
  123. end
  124. else
  125. local cnt = v[2]
  126. if type(cnt) == "string" then
  127. cnt = tonumber(cnt)
  128. end
  129. Grid.makeItem(msgRet.items[len], v[1], cnt)
  130. end
  131. end
  132. end
  133. msgRet.items[0] = len
  134. if len <= 0 and mail.get ~= 1 then
  135. mail.get = 1
  136. MailManager.saveMail(mail)
  137. end
  138. Msg.send(msgRet, human.fd)
  139. onMailChange(human)
  140. end
  141. function get(human, mailUuid)
  142. -- if human.version == nil then
  143. -- return Broadcast.sendErr(human, Lang.MERGE_MAIL_GET_VERSION_ERR)
  144. -- end
  145. local mail = MailManager.getMail(mailUuid)
  146. if not mail then
  147. return
  148. end
  149. if mail.receiverUuid ~= human.db._id then
  150. return
  151. end
  152. if mail.get then
  153. Broadcast.sendErr(human, Lang.MAIL_GET_ERR)
  154. return
  155. end
  156. if not mail.items then
  157. Broadcast.sendErr(human, Lang.MAIL_NO_ITEM)
  158. return
  159. end
  160. mail.get = 1
  161. mail.read = 1
  162. MailManager.saveMail(mail)
  163. local itemCnt = 0
  164. local fuWenCnt = 0
  165. for _, v in ipairs(mail.items) do
  166. if type(v[2]) == "table" then
  167. -- 给装备
  168. if ItemDefine.isEquip(v[1]) then
  169. itemCnt = itemCnt + 1
  170. end
  171. -- 给符文
  172. if ItemDefine.isFuwen(v[1]) then
  173. fuWenCnt = fuWenCnt + 1
  174. end
  175. end
  176. end
  177. if not EquipLogic.checkEmptyCnt(human, itemCnt) then
  178. return
  179. end
  180. if not FuwenGrid.checkEmptyCnt(human, fuWenCnt) then
  181. return
  182. end
  183. local len = 0
  184. local msgRetItem = Msg.gc.GC_ITEM_GET_LIST
  185. msgRetItem.popupType = ItemDefine.checkIsWave(LogDefine.DEFINE["mail"]) and 1 or 0
  186. for _, v in ipairs(mail.items) do
  187. len = len + 1
  188. if type(v[2]) == "table" then
  189. -- 给装备
  190. if ItemDefine.isEquip(v[1]) then
  191. EquipLogic.addByEquipGrid(human, v[2], "mail")
  192. Grid.makeItem(msgRetItem.list[len], v[2].id, 1, nil, v[2])
  193. end
  194. -- 给符文
  195. if ItemDefine.isFuwen(v[1]) then
  196. FuwenLogic.addByGrid(human, v[2], "mail")
  197. Grid.makeItem(msgRetItem.list[len], v[2].id, 1, nil, v[2])
  198. end
  199. else
  200. local cnt = v[2]
  201. if type(v[2]) == "string" then
  202. cnt = tonumber(cnt)
  203. end
  204. BagLogic.addItem(human, v[1], cnt, "mail")
  205. Grid.makeItem(msgRetItem.list[len], v[1], cnt)
  206. end
  207. end
  208. msgRetItem.list[0] = len
  209. human.getEquip = nil
  210. Msg.send(msgRetItem, human.fd)
  211. local msgRet = Msg.gc.GC_MAIL_GET
  212. msgRet.uuid = mail._id
  213. Msg.send(msgRet, human.fd)
  214. onMailChange(human)
  215. end
  216. function allGet(human)
  217. -- if human.version == nil then
  218. -- return Broadcast.sendErr(human, Lang.MERGE_MAIL_GET_VERSION_ERR)
  219. -- end
  220. local mails = MailManager.getMails(human.db._id, MailManager.SYSTEM)
  221. local items = {}
  222. local itemEquip = {}
  223. for _, mail in ipairs(mails) do
  224. if not mail.get and mail.items then
  225. for _, item in ipairs(mail.items) do
  226. if type(item[2]) == "table" then
  227. -- 给装备 给符文
  228. if ItemDefine.isEquip(item[1]) or ItemDefine.isFuwen(item[1]) then
  229. itemEquip[#itemEquip + 1] = item
  230. end
  231. else
  232. if ItemDefine.isEquip(item[1]) or ItemDefine.isFuwen(item[1]) then
  233. itemEquip[#itemEquip + 1] = item
  234. else
  235. items[#items + 1] = item
  236. end
  237. end
  238. end
  239. end
  240. end
  241. -- 装备符文
  242. local itemCnt = 0
  243. local fuWenCnt = 0
  244. for _, v in pairs(itemEquip) do
  245. if ItemDefine.isEquip(v[1]) then
  246. itemCnt = itemCnt + 1
  247. end
  248. -- 给符文
  249. if ItemDefine.isFuwen(v[1]) then
  250. fuWenCnt = fuWenCnt + 1
  251. end
  252. end
  253. if not EquipLogic.checkEmptyCnt(human, itemCnt) then
  254. return
  255. end
  256. if not FuwenGrid.checkEmptyCnt(human, fuWenCnt) then
  257. return
  258. end
  259. -- 更新邮件
  260. for _, mail in ipairs(mails) do
  261. if not mail.get and mail.items then
  262. mail.get = 1
  263. mail.read = 1
  264. MailManager.saveMail(mail)
  265. end
  266. end
  267. -- 给道具
  268. local tlist = BagLogic.sameItemTogether(items)
  269. if tlist then
  270. for _, item in pairs(tlist) do
  271. BagLogic.addItem(human, item[1], item[2], "mail")
  272. end
  273. end
  274. -- 装备符文
  275. for _, v in pairs(itemEquip) do
  276. if ItemDefine.isEquip(v[1]) then
  277. if type(v[2]) == "table" then
  278. EquipLogic.addByEquipGrid(human, v[2], "mail")
  279. else
  280. for i = 1,v[2] do
  281. local grid = EquipLogic.makeEquip(v[1])
  282. EquipLogic.addByEquipGrid(human, grid, "mail")
  283. end
  284. end
  285. end
  286. -- 给符文
  287. if ItemDefine.isFuwen(v[1]) then
  288. if type(v[2]) == "table" then
  289. FuwenLogic.addByGrid(human, v[2], "mail")
  290. else
  291. for i = 1,v[2] do
  292. local grid = FuwenGrid.create(v[1])
  293. FuwenLogic.addByGrid(human, grid, "mail")
  294. end
  295. end
  296. end
  297. end
  298. human.getEquip = nil
  299. -- 道具
  300. local len = 0
  301. local msgRetItem = Msg.gc.GC_ITEM_GET_LIST
  302. msgRetItem.popupType = ItemDefine.checkIsWave(LogDefine.DEFINE["mail"]) and 1 or 0
  303. if tlist then
  304. for _, item in pairs(tlist) do
  305. if len >= #msgRetItem.list then
  306. break
  307. end
  308. len = len + 1
  309. Grid.makeItem(msgRetItem.list[len], item[1], item[2])
  310. end
  311. end
  312. -- 给装备
  313. for _, v in pairs(itemEquip) do
  314. if len >= #msgRetItem.list then
  315. break
  316. end
  317. if ItemDefine.isEquip(v[1]) then
  318. if type(v[2]) == "table" then
  319. len = len + 1
  320. Grid.makeItem(msgRetItem.list[len], v[1], 1, nil, v[2])
  321. else
  322. for i = 1,v[2] do
  323. local grid = EquipLogic.makeEquip(v[1])
  324. len = len + 1
  325. Grid.makeItem(msgRetItem.list[len], v[1], 1, nil, grid)
  326. end
  327. end
  328. end
  329. -- 给符文
  330. if ItemDefine.isFuwen(v[1]) then
  331. if type(v[2]) == "table" then
  332. len = len + 1
  333. Grid.makeItem(msgRetItem.list[len], v[1], 1, nil, v[2])
  334. else
  335. for i = 1,v[2] do
  336. local grid = FuwenGrid.create(v[1])
  337. len = len + 1
  338. Grid.makeItem(msgRetItem.list[len], v[1], 1, nil, grid)
  339. end
  340. end
  341. end
  342. end
  343. msgRetItem.list[0] = len
  344. Msg.send(msgRetItem, human.fd)
  345. query(human, MailManager.SYSTEM)
  346. onMailChange(human)
  347. end
  348. function del(human, mailUuid)
  349. local mail = MailManager.getMail(mailUuid)
  350. if not mail then
  351. return
  352. end
  353. MailManager.del(mailUuid)
  354. local msgRet = Msg.gc.GC_MAIL_DEL
  355. msgRet.uuid = mailUuid
  356. Msg.send(msgRet, human.fd)
  357. onMailChange(human)
  358. end
  359. CHAT_CD = 20-- 发送cd时间
  360. function sendMail(human, identity, content)
  361. do return end
  362. end
  363. function allDel(human, type)
  364. MailManager.delAll(human.db._id,type)
  365. local msgRet = Msg.gc.GC_MAIL_ALL_DEL
  366. msgRet.delStr = ""
  367. Msg.send(msgRet, human.fd)
  368. query(human, type)
  369. onMailChange(human)
  370. end
  371. function onMailChange(human)
  372. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_204)
  373. end
  374. function isDot(human)
  375. local mails = MailManager.getMails(human.db._id, nil)
  376. if not mails then
  377. return false
  378. end
  379. for i = 1,#mails do
  380. if mails[i].read == nil then
  381. return true
  382. end
  383. end
  384. return false
  385. end
  386. local CLEAN_MAIL_LIST = {}
  387. function cleanNilContent(human)
  388. local mails = MailManager.getMails(human.db._id, nil)
  389. local cleanCnt = 0
  390. for i = 1, #mails do
  391. local mail = mails[i]
  392. if mail.content == nil then
  393. cleanCnt = cleanCnt + 1
  394. CLEAN_MAIL_LIST[cleanCnt] = mail._id
  395. end
  396. end
  397. for i = 1, cleanCnt do
  398. del(human, CLEAN_MAIL_LIST[i])
  399. end
  400. end