RelationGiftLogic.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. local LuaMongo = _G.lua_mongo
  2. local Grid = require("bag.Grid")
  3. local Msg = require("core.Msg")
  4. local Config = require("Config")
  5. local Lang = require("common.Lang")
  6. local ObjHuman = require("core.ObjHuman")
  7. local CommonDB = require("common.CommonDB")
  8. local MailManager = require("mail.MailManager")
  9. local Broadcast = require("broadcast.Broadcast")
  10. local RelationGiftExcel = require("excel.present").relationGift
  11. local RoleLogic = require("role.RoleLogic")
  12. local RoleDBLogic = require("role.RoleDBLogic")
  13. local VipLogic = require("vip.VipLogic")
  14. local DB = require("common.DB")
  15. local BagLogic = require("bag.BagLogic")
  16. local YunYingLogic = require("yunying.YunYingLogic")
  17. local PanelDefine = require("broadcast.PanelDefine")
  18. local CombatPosLogic = require("combat.CombatPosLogic")
  19. local CombatDefine = require("combat.CombatDefine")
  20. local RELATION_NUMMAX = 20
  21. local RELATION_ZDL = 1 -- 战斗力判定
  22. local RELATION_VIP = 2 -- VIP等级判定
  23. -- 登陆检测关系玩家战力
  24. local loginFileds = { combatHero = 1,heroBag = 1, vipLv = 1 }
  25. function onLogin(human)
  26. local relationBind = human.db.relationBind
  27. if not relationBind then return end
  28. local relationRew = human.db.relationRew
  29. if not relationRew then return end
  30. for k, v in pairs(relationBind) do
  31. local bindHuman = ObjHuman.onlineUuid[k]
  32. if not bindHuman then
  33. local db = RoleDBLogic.getDb(k, loginFileds)
  34. bindHuman = { db = db }
  35. end
  36. if bindHuman.db then
  37. local zhandouli = CombatPosLogic.getCombatHeroZDL(bindHuman, CombatDefine.COMBAT_TYPE1)
  38. local vipLv = VipLogic.getVipLv(bindHuman)
  39. for i = 1, #RelationGiftExcel do
  40. local config = RelationGiftExcel[i]
  41. local add = false
  42. if config.type == RELATION_ZDL and v.zhandouli[i] == nil then
  43. if zhandouli >= config.counter then
  44. add = true
  45. v.zhandouli[i] = 1
  46. end
  47. elseif config.type == RELATION_VIP and v.vip[i] == nil then
  48. if vipLv >= config.counter then
  49. add = true
  50. v.vip[i] = 1
  51. end
  52. end
  53. if add then
  54. relationRew[i] = relationRew[i] or {}
  55. relationRew[i].maxCnt = relationRew[i].maxCnt or 0
  56. relationRew[i].maxCnt = relationRew[i].maxCnt + 1
  57. if relationRew[i].maxCnt >= config.cnt then
  58. relationRew[i].maxCnt = config.cnt
  59. end
  60. end
  61. end
  62. end
  63. end
  64. end
  65. -- 生成关系码
  66. function relationCreate(human)
  67. -- 是否有唯一标识
  68. -- 生成十二位数唯一标识
  69. local firstStr = Config.SVR_INDEX
  70. local nowMax = CommonDB.getIdentityMax()
  71. local relation = string.format("%06d", nowMax)..""..firstStr
  72. human.db.relationKey = relation
  73. end
  74. -- 建立绑定关系
  75. -- relationId 绑定码[目标]
  76. -- relationBind 绑定队列
  77. -- relationKey 推广码
  78. local queryById = {relationKey = nil}
  79. function relationBind(human, relationId)
  80. if human.db.relationId then
  81. return Broadcast.sendErr(human, Lang.RELATIONGIFT_ERR_3)
  82. end
  83. if relationId == "" then
  84. return
  85. end
  86. -- 查找唯一标识的玩家
  87. queryById.relationKey = relationId
  88. local data = {}
  89. LuaMongo.find(DB.db_char, queryById)
  90. if not LuaMongo.next(data) then
  91. return Broadcast.sendErr(human, Lang.RELATIONGIFT_ERR_1)
  92. end
  93. -- 检测玩家是否在线
  94. local bindHuman = ObjHuman.onlineUuid[data._id]
  95. local isOnline = true
  96. if not bindHuman then
  97. bindHuman = { db = data }
  98. isOnline = false
  99. end
  100. -- 检测自己是否绑定过
  101. if bindHuman.db.relationBind[human.db._id] then
  102. return Broadcast.sendErr(human, Lang.RELATIONGIFT_ERR_3)
  103. end
  104. -- 检测绑定数量
  105. local len = 0
  106. for k, v in pairs(bindHuman.db.relationBind) do
  107. len = len + 1
  108. if len >= RELATION_NUMMAX then
  109. return Broadcast.sendErr(human, Lang.RELATIONGIFT_ERR_2)
  110. end
  111. end
  112. -- 插入到绑定关系中
  113. human.db.relationId = relationId
  114. bindHuman.db.relationBind[human.db._id] = { vip={}, zhandouli={}}
  115. -- 离线玩家保存数据
  116. if not isOnline then
  117. ObjHuman.save(bindHuman)
  118. end
  119. return Broadcast.sendErr(human, Lang.RELATIONGIFT_SUCESS)
  120. end
  121. -- 关系奖励数据查询
  122. -- 标识码
  123. -- 奖励状态
  124. -- 推荐的玩家列表
  125. function relationQuery(human)
  126. local relationRew = human.db.relationRew
  127. local relationBind = human.db.relationBind
  128. local msgRet = Msg.gc.GC_RELATION_GIFT_QUERY
  129. msgRet.relationId = human.db.relationKey
  130. local len = 0
  131. for k, v in ipairs(RelationGiftExcel) do
  132. len = len + 1
  133. local net = msgRet.list[len]
  134. -- 奖励
  135. local rewardLen = #v.item
  136. for i = 1, rewardLen do
  137. local itemID = v.item[i][1]
  138. local itemCnt = v.item[i][2]
  139. Grid.makeItem(net.rewards[i], itemID, itemCnt)
  140. end
  141. net.id = k
  142. net.rewards[0] = rewardLen
  143. net.cnt = relationRew[k] and relationRew[k].maxCnt or 0 -- 可以领的次数
  144. net.cntMax = v.cnt -- 最大领取次数
  145. net.getCht = relationRew[k] and relationRew[k].cnt or 0 -- 已经领取的次数
  146. net.type = v.type
  147. net.counter = v.counter
  148. net.txt = v.txt
  149. end
  150. msgRet.list[0] = len
  151. len = 0
  152. for k, v in pairs(relationBind) do
  153. len = len + 1
  154. local net = msgRet.roleList[len]
  155. local bReulst, human = RoleLogic.getRoleBaseByUuid(k, net.role)
  156. if human.db ~= nil then
  157. net.role.zhandouli = CombatPosLogic.getCombatHeroZDL(human, CombatDefine.COMBAT_TYPE1)
  158. net.vipLv = VipLogic.getVipLv(human)
  159. else
  160. relationBind[k] = nil
  161. end
  162. end
  163. msgRet.roleList[0] = len
  164. Msg.send(msgRet, human.fd)
  165. end
  166. -- 奖励领取
  167. function relationRewardGet(human, id)
  168. local config = RelationGiftExcel[id]
  169. if not config then return end
  170. local relationRew = human.db.relationRew[id]
  171. if not relationRew then return end
  172. relationRew.cnt = relationRew.cnt or 0
  173. if relationRew.cnt >= config.cnt then
  174. return
  175. end
  176. if relationRew.maxCnt <= 0 then return end
  177. relationRew.cnt = relationRew.cnt + 1
  178. relationRew.maxCnt = relationRew.maxCnt - 1
  179. BagLogic.addItemList(human, config.item,"relationGift")
  180. -- 刷新页面
  181. relationQuery(human)
  182. for k, v in pairs(funcID) do
  183. YunYingLogic.updateIcon(YYInfo[k], human)
  184. YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3503)
  185. break
  186. end
  187. end
  188. function isRed(human)
  189. local relationBind = human.db.relationBind
  190. if not relationBind then return end
  191. local relationRew = human.db.relationRew
  192. if not relationRew then return end
  193. for i = 1, #RelationGiftExcel do
  194. local config = RelationGiftExcel[i]
  195. if relationRew[i] and relationRew[i].maxCnt > 0 then
  196. return true
  197. end
  198. end
  199. end