FriendDBLogic.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ----------------------------------------
  2. -- 好友系统DB
  3. -- addFriendData 添加好友
  4. -- delFriendData 删除好友
  5. -- getFriendUuids 获得好友列表
  6. ----------------------------------------
  7. local LuaMongo = _G.lua_mongo
  8. local DB = require("common.DB")
  9. local FriendDefine = require("friend.FriendDefine")
  10. local RoleDBLogic = require("role.RoleDBLogic")
  11. local DBOpAnd = {uuid1 = nil, uuid2 = nil}
  12. local DBOpOr = {["$or"] = {{uuid1=""}, {uuid2=""}}}
  13. local DBOpRemove = {_id = nil}
  14. local DBOpHeartRemove1 = {uuid1=nil, isGetRedHeart1=nil}
  15. local DBOpHeartRemove2 = {uuid2=nil, isGetRedHeart2=nil}
  16. function isFriend(uuid1, uuid2)
  17. if uuid1 > uuid2 then
  18. uuid1, uuid2 = uuid2, uuid1
  19. end
  20. DBOpAnd.uuid1 = uuid1
  21. DBOpAnd.uuid2 = uuid2
  22. local data = {}
  23. LuaMongo.find(DB.db_friend, DBOpAnd)
  24. if not LuaMongo.next(data) then
  25. return
  26. end
  27. return true, data
  28. end
  29. function addFriendData(uuid1, uuid2)
  30. if isFriend(uuid1, uuid2) then
  31. return
  32. end
  33. if uuid1 > uuid2 then
  34. uuid1, uuid2 = uuid2, uuid1
  35. end
  36. local data = {}
  37. data.uuid1 = uuid1
  38. data.uuid2 = uuid2
  39. data.isGetRedHeart1 = nil -- uuid2是否发了爱心给uuid1的状态 1已发未领取 2已发已领取
  40. data.isGetRedHeart2 = nil -- uuid1是否发了爱心给uuid2的状态 1已发未领取 2已发已领取
  41. LuaMongo.insert(DB.db_friend, data)
  42. return true, data
  43. end
  44. function delFriendData(uuid1, uuid2)
  45. local pass, data = isFriend(uuid1, uuid2)
  46. if not pass then return end
  47. DBOpRemove._id = data._id
  48. LuaMongo.remove(DB.db_friend, DBOpRemove)
  49. return true
  50. end
  51. function getFriendUuids(uuid)
  52. local friendUuidList = {}
  53. DBOpOr["$or"][1].uuid1 = uuid
  54. DBOpOr["$or"][2].uuid2 = uuid
  55. LuaMongo.find(DB.db_friend, DBOpOr)
  56. local cnt = 0
  57. while true do
  58. local data = {}
  59. if not LuaMongo.next(DB.db_friend, data) then
  60. break
  61. end
  62. cnt = cnt + 1
  63. if data.uuid1 ~= uuid then
  64. if friendUuidList[cnt] == nil then
  65. friendUuidList[cnt] = {}
  66. end
  67. friendUuidList[cnt].uuid = data.uuid1
  68. friendUuidList[cnt].getStatus = data.isGetRedHeart2 --如果玩家是1号位置 我要看1号位置给2号位置 的赠送信息 那就是2
  69. elseif data.uuid2 ~= uuid then
  70. if friendUuidList[cnt] == nil then
  71. friendUuidList[cnt] = {}
  72. end
  73. friendUuidList[cnt].uuid = data.uuid2
  74. friendUuidList[cnt].getStatus = data.isGetRedHeart1 --如果玩家是2号位置 我要看2号位置给1号位置 的赠送信息 那就是1
  75. else
  76. assert()
  77. end
  78. end
  79. return cnt, friendUuidList
  80. end
  81. -- 设置红心领取状态
  82. function setFriendHeart(uuid1, uuid2, status, isgive)
  83. local tempTb = {}
  84. local tb = {}
  85. if uuid1 > uuid2 then
  86. uuid1, uuid2 = uuid2, uuid1
  87. if isgive then
  88. tb.isGetRedHeart1 = status
  89. else
  90. tb.isGetRedHeart2 = status
  91. end
  92. tempTb["$set"] = tb
  93. else
  94. if isgive then
  95. tb.isGetRedHeart2 = status
  96. else
  97. tb.isGetRedHeart1 = status
  98. end
  99. tempTb["$set"] = tb
  100. end
  101. DBOpAnd.uuid1 = uuid1
  102. DBOpAnd.uuid2 = uuid2
  103. LuaMongo.update(DB.db_friend, DBOpAnd, tempTb)
  104. end
  105. -- 清空红心状态
  106. function delAllFriendHeart(uuid)
  107. --== uuid1的数据 清空isGetRetHeart1
  108. local tempTb1 = {}
  109. DBOpHeartRemove1.uuid1 = uuid
  110. DBOpHeartRemove1.isGetRedHeart1=2
  111. local updatedata1 = {}
  112. updatedata1.isGetRedHeart1=1
  113. tempTb1["$unset"] = updatedata1
  114. LuaMongo.update(DB.db_friend, DBOpHeartRemove1, tempTb1)
  115. --== uuid2的数据 清空isGetRetHeart2
  116. local tempTb2 = {}
  117. DBOpHeartRemove2.uuid2 = uuid
  118. DBOpHeartRemove2.isGetRedHeart2=2
  119. local updatedata2 = {}
  120. updatedata2.isGetRedHeart2=1
  121. tempTb2["$unset"]=updatedata2
  122. LuaMongo.update(DB.db_friend, DBOpHeartRemove2, tempTb2)
  123. end
  124. -------------------------部分东西只存内存--------------------------------
  125. local requestList = {}
  126. -- uuid1向uuid2申请
  127. function hasRequest(uuid1, uuid2)
  128. if not requestList[uuid2] then
  129. return
  130. end
  131. if not requestList[uuid2][uuid1] then
  132. return
  133. end
  134. return true
  135. end
  136. -- uuid1向uuid2申请
  137. function addRequest(uuid1, uuid2)
  138. if not requestList[uuid2] then
  139. requestList[uuid2] = {}
  140. end
  141. if not requestList[uuid2][uuid1] then
  142. requestList[uuid2][uuid1] = os.time()
  143. end
  144. end
  145. -- 获得uuid的申请列表
  146. function getRequests(uuid)
  147. return requestList[uuid]
  148. end
  149. -- 删除uuid1向uuid2申请
  150. function delRequest(uuid1, uuid2)
  151. if not requestList[uuid2] then
  152. return
  153. end
  154. if not requestList[uuid2][uuid1] then
  155. return
  156. end
  157. requestList[uuid2][uuid1] = nil
  158. return true
  159. end
  160. function haveRequest(uuid)
  161. if not requestList[uuid] then
  162. return 0
  163. end
  164. for k, v in pairs(requestList[uuid]) do
  165. if v ~= nil then
  166. return 1
  167. end
  168. end
  169. return 0
  170. end
  171. --uuid1是否被uuid2拉黑
  172. function isBlackFriend(uuid1,uuid2)
  173. local db = RoleDBLogic.getDb(uuid2,"friendBlack")
  174. if not db or not db.friendBlack then return end
  175. return db.friendBlack[uuid1]
  176. end