MiddleLogic.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. --------------------------------------------------------
  2. -- 跨服逻辑
  3. -- 注意:跨服活动结束,返还操作列表给本服,不覆盖本服的DB!!!
  4. --------------------------------------------------------
  5. local Config = require("Config")
  6. local CommonDefine = require("common.CommonDefine")
  7. local Log = require("common.Log")
  8. local Lang = require("common.Lang")
  9. local Util = require("common.Util")
  10. local CommonDB = require("common.CommonDB")
  11. local Msg = require("core.Msg")
  12. local InnerMsg = require("core.InnerMsg")
  13. local Obj = require("core.Obj")
  14. local ObjHuman = require("core.ObjHuman")
  15. local MiddleDefine = require("middle.MiddleDefine")
  16. local MiddleManager = require("middle.MiddleManager")
  17. local MiddleOption = require("middle.MiddleOption")
  18. local MiddleConnect = require("middle.MiddleConnect")
  19. local Broadcast = require("broadcast.Broadcast")
  20. local MailLogic = require("mail.MailLogic")
  21. local MailManager = require("mail.MailManager")
  22. local BuyLogic = require("topup.BuyLogic")
  23. local RoleDBLogic = require("role.RoleDBLogic")
  24. local RoleAttr = require("role.RoleAttr")
  25. TRY_LOGIN_HUMAN = TRY_LOGIN_HUMAN or {} -- 尝试登陆跨服的human数据(存在这个表里的是还没有发CG_MIDDLE_LOGIN的)
  26. -- 尝试进入跨服
  27. function CG_MIDDLE_TRY_ENTER(human, msg)
  28. print("CG_MIDDLE_TRY_ENTER")
  29. if MiddleConnect.IS_MIDDLE_CONNECT ~= true then
  30. human.db.middleFlag = nil
  31. return Broadcast.sendErr(human, Lang.MIDDLE_SVR_ERR_CONNECT)
  32. end
  33. -- logic向middle传输玩家数据
  34. local msgSend = InnerMsg.lw.LW_TRY_ENTER_HUMAN
  35. msgSend.db = msgSend.db or {}
  36. msgSend.reason = msg.reason
  37. Util.cleanTable(msgSend.db)
  38. Util.copyTableSimple(human.db, msgSend.db)
  39. -- 某些数据太大了,发到跨服会超出协议大小,所以不发 -- reyes todo 这里到时要确认有没有太大的db数据 选择性不发
  40. msgSend.pfInfo = msgSend.pfInfo or {}
  41. Util.cleanTable(msgSend.pfInfo)
  42. msgSend.pfInfo.region = human.region
  43. msgSend.pfInfo.ip = human.ip
  44. msgSend.pfInfo.pf = human.pf
  45. msgSend.pfInfo.lang = human.lang
  46. msgSend.pfInfo.appid = human.appid
  47. msgSend.pfInfo.vopenid = human.vopenid
  48. msgSend.pfInfo.openkey = human.openkey
  49. msgSend.pfInfo.realSvrIndex = Config.SVR_INDEX
  50. msgSend.pfInfo.realSvrName = Config.SVR_NAME
  51. InnerMsg.sendMsg(0, msgSend)
  52. end
  53. function checkEnterReason(reason)
  54. do return end
  55. if reason == MiddleDefine.MIDDLE_ENTER_REASON_1 then --
  56. return true
  57. end
  58. end
  59. function LW_TRY_ENTER_HUMAN(fd, msg)
  60. local db = msg.db
  61. local pfInfo = msg.pfInfo
  62. local account = db.account
  63. local reason = msg.reason
  64. local retCode = MiddleDefine.MIDDLE_TRY_LOGIN_OK
  65. local msg = InnerMsg.wl.WL_TRY_ENTER_HUMAN
  66. msg.account = account
  67. local human = ObjHuman.onlineAccount[account]
  68. if human == nil then
  69. local onlineCnt = 0
  70. for k, v in pairs(ObjHuman.onlineAccount) do
  71. onlineCnt = onlineCnt + 1
  72. end
  73. if onlineCnt >= CommonDefine.MAX_ONLINE_COUNT then
  74. retCode = MiddleDefine.MIDDLE_TRY_LOGIN_ERR_FULL
  75. msg.retCode = retCode
  76. InnerMsg.sendMsg(fd, msg)
  77. return
  78. end
  79. if not checkEnterReason(reason) then
  80. msg.retCode = MiddleDefine.MIDDLE_TRY_LOGIN_ACT_END
  81. InnerMsg.sendMsg(fd, msg)
  82. return
  83. end
  84. if TRY_LOGIN_HUMAN[account] then
  85. -- 已经在尝试登陆列表中
  86. local tempData = TRY_LOGIN_HUMAN[account]
  87. Util.cleanTable(tempData.db)
  88. Util.copyTableSimple(db, tempData.db)
  89. Util.cleanTable(tempData.pfInfo)
  90. Util.copyTableSimple(pfInfo, tempData.pfInfo)
  91. tempData.tsForMiddle = os.time()
  92. tempData.reasonForMiddle = reason
  93. tempData.svrIndexForMiddle = MiddleManager.FD_2_SVRINDEX[fd]
  94. else
  95. local tempData = {}
  96. local newDB = {}
  97. Util.copyTableSimple(db, newDB)
  98. tempData.db = newDB
  99. local newPfInfo = {}
  100. Util.copyTableSimple(pfInfo, newPfInfo)
  101. tempData.pfInfo = newPfInfo
  102. tempData.tsForMiddle = os.time()
  103. tempData.reasonForMiddle = reason
  104. tempData.svrIndexForMiddle = MiddleManager.FD_2_SVRINDEX[fd]
  105. TRY_LOGIN_HUMAN[account] = tempData
  106. end
  107. else
  108. human.reasonForMiddle = reason
  109. human.svrIndexForMiddle = MiddleManager.FD_2_SVRINDEX[fd]
  110. end
  111. msg.retCode = retCode
  112. InnerMsg.sendMsg(fd, msg)
  113. end
  114. function WL_TRY_ENTER_HUMAN(fd, msg)
  115. local account = msg.account
  116. local retCode = msg.retCode
  117. local human = ObjHuman.onlineAccount[account]
  118. print("WL_TRY_ENTER_HUMAN",account, human, retCode)
  119. if human == nil then
  120. return
  121. end
  122. if retCode == MiddleDefine.MIDDLE_TRY_LOGIN_ERR_FULL then
  123. human.db.middleFlag = nil
  124. return Broadcast.sendErr(human, Lang.MIDDLE_SVR_ERR_FULL)
  125. elseif retCode == MiddleDefine.MIDDLE_TRY_LOGIN_ACT_END then
  126. human.db.middleFlag = nil
  127. return Broadcast.sendErr(human, Lang.MIDDLE_SVR_ERR_ACTEND)
  128. end
  129. if MiddleConnect.IS_MIDDLE_CONNECT ~= true then
  130. human.db.middleFlag = nil
  131. return Broadcast.sendErr(human, Lang.MIDDLE_SVR_ERR_CONNECT)
  132. end
  133. local ip, port, host = MiddleConnect.getMiddleInfo()
  134. local msgRet = Msg.gc.GC_MIDDLE_TRY_ENTER
  135. msgRet.host = host
  136. Msg.send(msgRet, human.fd)
  137. end
  138. function CG_MIDDLE_LOGIN(fd, msg)
  139. if _G.is_middle ~= true then return end
  140. local account = msg.account
  141. print("CG_MIDDLE_LOGIN", account, param)
  142. local human = ObjHuman.onlineAccount[account]
  143. if human == nil then
  144. local tempData = TRY_LOGIN_HUMAN[account]
  145. if tempData == nil then
  146. return
  147. end
  148. TRY_LOGIN_HUMAN[account] = nil
  149. human = ObjHuman.create(fd, account, tempData.db)
  150. human.reasonForMiddle = tempData.reasonForMiddle
  151. human.svrIndexForMiddle = tempData.svrIndexForMiddle
  152. human.realSvrIndex = tempData.pfInfo.realSvrIndex
  153. human.realSvrName = tempData.pfInfo.realSvrName
  154. ObjHuman.doCalc(human)
  155. else
  156. -- 销毁老的 建新的
  157. if human.fd then
  158. ObjHuman.fds[human.fd] = nil
  159. end
  160. ObjHuman.fds[fd] = human.id
  161. human.fd = fd
  162. end
  163. local reason = human.reasonForMiddle or 0
  164. local msgRet = Msg.gc.GC_MIDDLE_LOGIN
  165. msgRet.param = reason
  166. Msg.send(msgRet, human.fd)
  167. print("middle has real human ", account, reason)
  168. --登录日志
  169. Log.write(Log.LOGID_OSS_LOGIN, human.db._id, human.db.account, human.db.name, human.db.lv, human.db.ip)
  170. if not checkEnterReason(reason) then
  171. CG_MIDDLE_LOGOUT(human) -- 断开
  172. return
  173. end
  174. if reason == MiddleDefine.MIDDLE_ENTER_REASON_1 then
  175. end
  176. end
  177. function CG_MIDDLE_LOGOUT(human)
  178. if _G.is_middle ~= true then return end
  179. local fd = MiddleManager.getFDBySvrIndex(human.svrIndexForMiddle)
  180. if not fd then
  181. Log.write(Log.LOGID_MIDDLE_BT, "CG_MIDDLE_LOGOUT:" .. (human.svrIndexForMiddle or 0))
  182. return
  183. end
  184. Msg.send(Msg.gc.GC_MIDDLE_LOGOUT, human.fd)
  185. ObjHuman.doDisconnect(human, CommonDefine.DISCONNECT_NORMAL_AFTER, CommonDefine.DISCONNECT_MSG[CommonDefine.DISCONNECT_NORMAL_AFTER])
  186. end
  187. function sendWL_DB_BACK(human)
  188. local msgSend = InnerMsg.wl.WL_DB_BACK
  189. local fd = MiddleManager.getFDBySvrIndex(human.svrIndexForMiddle)
  190. if not fd then
  191. Log.write(Log.LOGID_MIDDLE_BT, "sendWL_DB_BACK:" .. (human.svrIndexForMiddle or 0))
  192. return
  193. end
  194. msgSend.account = human.db.account
  195. msgSend.options = msgSend.options or {}
  196. Util.cleanTable(msgSend.options)
  197. if human.db.middleOptions then
  198. Util.copyTableSimple(human.db.middleOptions, msgSend.options)
  199. human.db.middleOptions = nil
  200. end
  201. InnerMsg.sendMsg(fd, msgSend)
  202. end
  203. local fakeHuman = {}
  204. function WL_DB_BACK(fd, msg)
  205. local account = msg.account
  206. local options = msg.options
  207. local human = ObjHuman.onlineAccount[account]
  208. if human then
  209. human.db.middleFlag = nil
  210. BuyLogic.onLogin(human)
  211. MiddleOption.handleOptions(human, options)
  212. ObjHuman.doCalc(human)
  213. RoleDBLogic.saveRole(human.db)
  214. else
  215. local db = RoleDBLogic.getDbByAccount(account)
  216. if db then
  217. Util.cleanTable(fakeHuman)
  218. fakeHuman.db = db
  219. fakeHuman.db.middleFlag = nil
  220. fakeHuman.obj_type = Obj.TYPE_HUMAN
  221. RoleAttr.doCalc(fakeHuman)
  222. MiddleOption.handleOptions(fakeHuman, options)
  223. RoleDBLogic.saveRole(fakeHuman.db)
  224. else
  225. assert(nil, "ML_DB_BACK no this account " .. account)
  226. end
  227. end
  228. print("normal receive middle db and update")
  229. end
  230. -- 客户端告诉正常服 现在这个玩家在跨服玩ing
  231. function CG_MIDDLE_PLAYING_STATUS(human, middleFlag)
  232. if _G.is_middle == true then return end
  233. human.db.middleFlag = middleFlag
  234. ObjHuman.save(human)
  235. end
  236. -- 发送mlmail通知
  237. function sendWLMail(type,receiverUuid,title,content,items,senderName, svrIndex)
  238. local fd = MiddleManager.getFDBySvrIndex(svrIndex)
  239. if not fd then
  240. Log.write(Log.LOGID_DEBUG, "sendWLMail:" .. (svrIndex or 0))
  241. return
  242. end
  243. local msgSend = InnerMsg.wl.WL_MAIL
  244. msgSend.type = type
  245. msgSend.receiverUuid = receiverUuid
  246. msgSend.title = title
  247. msgSend.content = content
  248. msgSend.items = items
  249. msgSend.senderName = senderName
  250. InnerMsg.sendMsg(fd, msgSend)
  251. return fd
  252. end
  253. -- middle通知logic发邮件/道具
  254. function WL_MAIL(fd, msg)
  255. MailManager.add(msg.type, msg.receiverUuid,msg.title,msg.content,msg.items,msg.senderName)
  256. end
  257. function sendWLBroadcast(fd, uuid, lang)
  258. local msgRet = InnerMsg.wl.WL_BROADCAST
  259. msgRet.uuid = uuid
  260. msgRet.lang = lang
  261. InnerMsg.sendMsg(fd, msgRet)
  262. return true
  263. end
  264. -- 中心服务器结果
  265. function WL_BROADCAST(fd, msg)
  266. local human = ObjHuman.onlineUuid[msg.uuid]
  267. if human == nil then
  268. return
  269. end
  270. Broadcast.sendErr(human, msg.lang)
  271. end
  272. function LW_GM(fd, msg)
  273. local str = msg.gm
  274. local Gm = require("chat.Gm")
  275. local pos = string.find(str, " ", 1, true)
  276. local cmd = string.sub(str, 1, pos and pos - 1)
  277. if not Gm.d3[cmd] then
  278. return
  279. end
  280. local arg = string.sub(str, #cmd + 2)
  281. Gm.d3[cmd](nil, tonumber(arg) or arg)
  282. end
  283. function onLogin(human)
  284. if human.db.middleFlag == 0 then
  285. human.db.middleFlag = nil
  286. end
  287. if human.db.middleFlag == nil then
  288. return
  289. end
  290. local msg = {reason = human.db.middleFlag}
  291. CG_MIDDLE_TRY_ENTER(human, msg)
  292. end