RelationGiftLogic.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if relationRew[i].cnt then
  56. if relationRew[i].cnt < config.cnt then
  57. relationRew[i].maxCnt = relationRew[i].maxCnt or 0
  58. relationRew[i].maxCnt = relationRew[i].maxCnt + 1
  59. if relationRew[i].maxCnt >= config.cnt then
  60. relationRew[i].maxCnt = config.cnt
  61. end
  62. end
  63. else
  64. relationRew[i].maxCnt = relationRew[i].maxCnt or 0
  65. relationRew[i].maxCnt = relationRew[i].maxCnt + 1
  66. if relationRew[i].maxCnt >= config.cnt then
  67. relationRew[i].maxCnt = config.cnt
  68. end
  69. end
  70. end
  71. end
  72. end
  73. end
  74. end
  75. -- 生成关系码
  76. function relationCreate(human)
  77. -- 是否有唯一标识
  78. -- 生成十二位数唯一标识
  79. local firstStr = Config.SVR_INDEX
  80. local nowMax = CommonDB.getIdentityMax()
  81. local relation = string.format("%06d", nowMax)..""..firstStr
  82. human.db.relationKey = relation
  83. end
  84. -- 建立绑定关系
  85. -- relationId 绑定码[目标]
  86. -- relationBind 绑定队列
  87. -- relationKey 推广码
  88. local queryById = {relationKey = nil}
  89. function relationBind(human, relationId)
  90. if human.db.relationId then
  91. return Broadcast.sendErr(human, Lang.RELATIONGIFT_ERR_3)
  92. end
  93. if relationId == "" then
  94. return
  95. end
  96. -- 查找唯一标识的玩家
  97. queryById.relationKey = relationId
  98. local data = {}
  99. LuaMongo.find(DB.db_char, queryById)
  100. if not LuaMongo.next(data) then
  101. return Broadcast.sendErr(human, Lang.RELATIONGIFT_ERR_1)
  102. end
  103. -- 检测玩家是否在线
  104. local bindHuman = ObjHuman.onlineUuid[data._id]
  105. local isOnline = true
  106. if not bindHuman then
  107. bindHuman = { db = data }
  108. isOnline = false
  109. end
  110. -- 检测自己是否绑定过
  111. if bindHuman.db.relationBind[human.db._id] then
  112. return Broadcast.sendErr(human, Lang.RELATIONGIFT_ERR_3)
  113. end
  114. -- 检测绑定数量
  115. local len = 0
  116. for k, v in pairs(bindHuman.db.relationBind) do
  117. len = len + 1
  118. if len >= RELATION_NUMMAX then
  119. return Broadcast.sendErr(human, Lang.RELATIONGIFT_ERR_2)
  120. end
  121. end
  122. -- 插入到绑定关系中
  123. human.db.relationId = relationId
  124. bindHuman.db.relationBind[human.db._id] = { vip={}, zhandouli={}}
  125. -- 离线玩家保存数据
  126. if not isOnline then
  127. ObjHuman.save(bindHuman)
  128. end
  129. return Broadcast.sendErr(human, Lang.RELATIONGIFT_SUCESS)
  130. end
  131. -- 关系奖励数据查询
  132. -- 标识码
  133. -- 奖励状态
  134. -- 推荐的玩家列表
  135. function relationQuery(human)
  136. local relationRew = human.db.relationRew
  137. local relationBind = human.db.relationBind
  138. local msgRet = Msg.gc.GC_RELATION_GIFT_QUERY
  139. msgRet.relationId = human.db.relationKey
  140. local len = 0
  141. for k, v in ipairs(RelationGiftExcel) do
  142. len = len + 1
  143. local net = msgRet.list[len]
  144. -- 奖励
  145. local rewardLen = #v.item
  146. for i = 1, rewardLen do
  147. local itemID = v.item[i][1]
  148. local itemCnt = v.item[i][2]
  149. Grid.makeItem(net.rewards[i], itemID, itemCnt)
  150. end
  151. net.id = k
  152. net.rewards[0] = rewardLen
  153. net.cnt = relationRew[k] and relationRew[k].maxCnt or 0 -- 可以领的次数
  154. net.cntMax = v.cnt -- 最大领取次数
  155. net.getCht = relationRew[k] and relationRew[k].cnt or 0 -- 已经领取的次数
  156. net.type = v.type
  157. net.counter = v.counter
  158. net.txt = v.txt
  159. end
  160. msgRet.list[0] = len
  161. len = 0
  162. for k, v in pairs(relationBind) do
  163. len = len + 1
  164. local net = msgRet.roleList[len]
  165. local bReulst, human = RoleLogic.getRoleBaseByUuid(k, net.role)
  166. if human.db ~= nil then
  167. net.role.zhandouli = CombatPosLogic.getCombatHeroZDL(human, CombatDefine.COMBAT_TYPE1)
  168. net.vipLv = VipLogic.getVipLv(human)
  169. else
  170. relationBind[k] = nil
  171. end
  172. end
  173. msgRet.roleList[0] = len
  174. Msg.send(msgRet, human.fd)
  175. end
  176. -- 奖励领取
  177. function relationRewardGet(human, id)
  178. local config = RelationGiftExcel[id]
  179. if not config then return end
  180. local relationRew = human.db.relationRew[id]
  181. if not relationRew then return end
  182. relationRew.cnt = relationRew.cnt or 0
  183. if relationRew.cnt >= config.cnt then
  184. return
  185. end
  186. if relationRew.maxCnt <= 0 then return end
  187. relationRew.cnt = relationRew.cnt + 1
  188. relationRew.maxCnt = relationRew.maxCnt - 1
  189. BagLogic.addItemList(human, config.item,"relationGift")
  190. -- 刷新页面
  191. relationQuery(human)
  192. for k, v in pairs(funcID) do
  193. YunYingLogic.updateIcon(YYInfo[k], human)
  194. YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3503)
  195. break
  196. end
  197. end
  198. function isRed(human)
  199. local relationBind = human.db.relationBind
  200. if not relationBind then return end
  201. local relationRew = human.db.relationRew
  202. if not relationRew then return end
  203. for i = 1, #RelationGiftExcel do
  204. local config = RelationGiftExcel[i]
  205. if relationRew[i] and relationRew[i].maxCnt > 0 then
  206. return true
  207. end
  208. end
  209. end