UnionLogic.lua 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. ---------------------------------------------------------------------
  2. -- 公会模块
  3. -- db.unionSignIn 最近一次公会签到的0点时间戳
  4. ---------------------------------------------------------------------
  5. local Lang = require("common.Lang")
  6. local FilterUtil = require("common.FilterUtil")
  7. local UnionDBLogic = require("union.UnionDBLogic")
  8. local UnionExcel = require("excel.union")
  9. local MailManager = require("mail.MailManager")
  10. local MailExcel = require("excel.mail")
  11. local RoleDBLogic = require("role.RoleDBLogic")
  12. local Msg = require("core.Msg")
  13. local Broadcast = require("broadcast.Broadcast")
  14. local ObjHuman = require("core.ObjHuman")
  15. local Util = require("common.Util")
  16. local BagLogic = require("bag.BagLogic")
  17. local ItemDefine = require("bag.ItemDefine")
  18. local CombatLogic = require("combat.CombatLogic")
  19. local CombatDefine = require("combat.CombatDefine")
  20. local HeroGrid = require("hero.HeroGrid")
  21. local ChatUnion = require("chat.ChatUnion")
  22. local ChatHandler = require("chat.Handler")
  23. local Grid = require("bag.Grid")
  24. local UnionEctypeLogic = require("union.UnionEctypeLogic")
  25. local UnionWarLogic = require("union.UnionWarLogic")
  26. local UnionWarDBLogic = require("union.UnionWarDBLogic")
  27. local LuaMongo = _G.lua_mongo
  28. local DB = require("common.DB")
  29. local RoleLogic = require("role.RoleLogic")
  30. local UnionDefine = require("union.UnionDefine")
  31. local MonsterExcel = require("excel.monster")
  32. local VipLogic = require("vip.VipLogic")
  33. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  34. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  35. local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
  36. local UnionDonateLogic = require("union.UnionDonateLogic")
  37. local UnionLivenessLogic = require("union.UnionLivenessLogic")
  38. local MailDefine = require("mail.MailIdDefine")
  39. local RoleAttr = require("role.RoleAttr")
  40. local Timer = require("core.Timer")
  41. local BillboardDB = require("billboard.BillboardDB")
  42. local InnerMsg = require("core.InnerMsg")
  43. local MozhuMiddleLogic = require("mozhu.MoZhuMiddleLogic")
  44. local FieldsMember = {lastLoginTime = 1}
  45. local FieldsUnionOffline = {presidentUuid = 1}
  46. local FieldsRoleOffline = {lastLogoutTime = 1, lv = 1,identity = 1, name = 1}
  47. local FakeHuman = {}
  48. local CREATEUNION_NEED_VIPLV = 2 -- 创建公会需要VIP等级
  49. local CREATEUNION_NEED_LV = 35 -- 创建公会需要等级
  50. local CREATEUNION_NEED_ZUANSHI = 100 -- 创建公会花费
  51. local EXPEL_CNT_MAX = 10 -- 每日踢人上限
  52. local POST_OFFICIAL_MAXCNT = 3 -- 官员上限
  53. local UNION_CHANGE_CD = 86400 -- 公告修改CD
  54. local SIGNIN_GIVE_UNIONCOIN = 50 -- 签到给与公会币
  55. local CHANGENAME_NEED_ZUANSHI = 500 -- 改名花费
  56. local UNION_NAME_LEN_MAX = 18 -- 公会名字最大长度
  57. local UNION_NAME_LEN_MIN = 3 -- 公会名字最小长度
  58. local UNION_ALL_MAIL_MAX_CNT = 10 -- 每日邮件上限
  59. local UNION_AUTO_PRESIDENT_TIME = 3 * 86400 -- 会长离线x秒自动转让位置
  60. local RECRUIT_COST_VALUE = 18 -- 招募花费
  61. local UNION_OP_IMPEACH_PRESIDENT = 300 -- 弹劾会长操作花费
  62. -- 公会成员管理
  63. local UNION_MEM_EXPEL = 1 -- 踢出玩家
  64. local UNION_MEM_SET_POST = 2 -- 设置管理员
  65. local UNION_MEM_CHANGE_PRESIDENT = 3 -- 转让会长
  66. UNION_CHANGE_OP_1 = 1 -- 离开公会
  67. UNION_CHANGE_OP_2 = 2 -- 加入公会
  68. UNION_CHANGE_OP_3 = 3 -- 解散公会
  69. local AnotherWorldBattleNS
  70. -- 如果异界之战处于战斗阶段,并且本公会参加了, 则不能做踢人等操作
  71. local function getAbState(union)
  72. if not union.joinAnotherWorldBattleTi then
  73. return true
  74. end
  75. AnotherWorldBattleNS = AnotherWorldBattleNS or require("anotherWorldBattle.AnotherWorldBattleNS")
  76. end
  77. -------------------------------------- db -----------------------------------------------------
  78. -- 插入公会日志
  79. function addUnionLog(unionUuid, logType, name,classify,param)
  80. if not logType then return end
  81. local cf = UnionExcel.log[logType]
  82. if not cf then return end
  83. local now = os.time()
  84. local date = os.date("*t", now)
  85. local content = Util.format(cf.content, date.month, date.day, date.hour, date.min, name,param)
  86. UnionDBLogic.addLog(unionUuid, content,classify)
  87. end
  88. -- 发送到公会
  89. function send2Union(msg, union,eUuid)
  90. if not union then return end
  91. for uuid in pairs(union.member) do
  92. local human = ObjHuman.onlineUuid[uuid]
  93. if human and human.fd and uuid ~= eUuid then
  94. Msg.send(msg, human.fd)
  95. end
  96. end
  97. end
  98. ------------------------------------------ msg -----------------------------------------------
  99. local function getUnionConfig(unionLv)
  100. local unionLv = math.min(unionLv, #UnionExcel.union)
  101. return UnionExcel.union[unionLv]
  102. end
  103. -- 封装公会基础信息结构体
  104. function fontUnionMsg(net, union, uuid)
  105. local lv = math.min(union.lv, #UnionExcel.union)
  106. local config = UnionExcel.union[lv]
  107. net.unionIdentity = union.id
  108. net.unionUuid = union._id
  109. net.name = union.name or ""
  110. net.maxCnt = config.maxCnt
  111. net.curCnt = union.curCnt
  112. net.bannerID = union.bannerID
  113. net.needLv = union.needLv or 0
  114. net.notice = union.notice
  115. net.lv = lv
  116. net.exp = union.exp
  117. net.maxExp = config.exp
  118. local presidentDB = RoleDBLogic.getDb(union.presidentUuid, "name")
  119. net.presidentName = presidentDB and presidentDB.name or ""
  120. net.isApply = 0
  121. if uuid and UnionDBLogic.isApply(union, uuid) then
  122. net.isApply = 1
  123. end
  124. end
  125. -- 封装公会成员信息结构体
  126. function fontUnionMember(net, uuid, member)
  127. local FieldsMember = {unionDonate = 1,unionLiveness = 1,lastLogoutTime = 1,dailyBanggong = 1,totalBanggong = 1}
  128. RoleLogic.makeRoleBaseFields(FieldsMember)
  129. local db, online = RoleDBLogic.getDb(uuid, FieldsMember)
  130. RoleLogic.makeRoleBase(db, net.roleBase)
  131. net.post = member.post
  132. net.inUnionWar = 0 --UnionWarDBLogic.isCommonSign(uuid) and 1 or 0
  133. net.lastLogoutTime = 0
  134. -- 成员捐赠
  135. net.todayDonate = db.dailyBanggong or 0
  136. net.totalDonate = db.totalBanggong or 0
  137. -- 成员活跃度
  138. if db.unionLiveness == nil then
  139. net.liveLv = 0
  140. else
  141. net.liveLv = db.unionLiveness.lv
  142. end
  143. if not online then
  144. if db.lastLogoutTime == nil then
  145. -- 刚刚离线,未保存离线时间
  146. net.lastLogoutTime = 1
  147. else
  148. net.lastLogoutTime = os.time() - (db and db.lastLogoutTime or 0)
  149. end
  150. end
  151. end
  152. -- 发送公会信息
  153. function sendUnionQuery(human)
  154. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  155. if not union then return end
  156. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  157. if not member then return end
  158. -- 因为客户端要知道会长信息,为了客户端方便,把会长放在首位
  159. local presidentMember = union.member[union.presidentUuid]
  160. -- 由于逻辑错误导致副会长将会长踢出公会,这里做个判断修复数据
  161. -- 如果会长数据为nil,设置第一个成员为会长
  162. if not presidentMember then
  163. for k,v in pairs(union.member) do
  164. UnionDBLogic.setPresident(union, k)
  165. union = UnionDBLogic.getUnion(human.db.unionUuid)
  166. presidentMember = union.member[union.presidentUuid]
  167. break
  168. end
  169. end
  170. local msgRet = Msg.gc.GC_UNION_QUERY
  171. msgRet.post = member.post
  172. msgRet.logDot = 0 -- hasLog(human, union) and 1 or 0
  173. msgRet.leaveUnion = human.db.leaveUnionLimit and 1 or 0
  174. fontUnionMsg(msgRet.unionMsg, union)
  175. msgRet.unionMember[0] = 1
  176. fontUnionMember(msgRet.unionMember[1], union.presidentUuid, presidentMember)
  177. for uuid, member in pairs(union.member) do
  178. if uuid ~= union.presidentUuid then
  179. if msgRet.unionMember[0] >= #msgRet.unionMember then
  180. break
  181. end
  182. msgRet.unionMember[0] = msgRet.unionMember[0] + 1
  183. fontUnionMember(msgRet.unionMember[msgRet.unionMember[0]], uuid, member)
  184. end
  185. end
  186. msgRet.hasApply = dot(human) and 1 or 0
  187. Msg.send(msgRet, human.fd)
  188. return true
  189. end
  190. -- 发送公会变更
  191. function sendUnionChange(union, uuid)
  192. local msgRet = Msg.gc.GC_UNION_CHANGE_MSG
  193. fontUnionMsg(msgRet.unionMsg, union)
  194. if uuid then
  195. local human = ObjHuman.onlineUuid[uuid]
  196. if human and human.fd then
  197. Msg.send(msgRet, human.fd)
  198. end
  199. else
  200. send2Union(msgRet, union)
  201. end
  202. end
  203. -- 公推推荐列表/查询返回列表
  204. function sendRecommandList(human, rtype, list)
  205. local msgRet = Msg.gc.GC_UNION_RECOMMEND
  206. msgRet.type = rtype
  207. msgRet.unionMsg[0] = math.min(list and #list or 0, #msgRet.unionMsg)
  208. for i = 1, msgRet.unionMsg[0] do
  209. fontUnionMsg(msgRet.unionMsg[i], list[i], human.db._id)
  210. end
  211. Msg.send(msgRet, human.fd)
  212. end
  213. -- 给加入公会的成员返回公会信息(考虑删除)
  214. function sendUnionMsg(uuid, union)
  215. if not union then return end
  216. local human = ObjHuman.onlineUuid[uuid]
  217. if not human or not human.fd then return end
  218. local msgRet = Msg.gc.GC_UNION_MSG
  219. msgRet.unionIdentity = union.id
  220. msgRet.unionUuid = union._id
  221. msgRet.unionName = union.name
  222. msgRet.bannerID = union.bannerID
  223. Msg.send(msgRet, human.fd)
  224. end
  225. -- 成员信息变化
  226. function sendMemberUpdate(union, uuid)
  227. local msgRet = Msg.gc.GC_UNION_AGREE_JOIN_UPDATE_MEMBER
  228. msgRet.unionMember[0] = 0
  229. for uuid, member in pairs(union.member) do
  230. if msgRet.unionMember[0] >= #msgRet.unionMember then
  231. break
  232. end
  233. msgRet.unionMember[0] = msgRet.unionMember[0] + 1
  234. fontUnionMember(msgRet.unionMember[msgRet.unionMember[0]], uuid, member)
  235. end
  236. if uuid then
  237. local human = ObjHuman.onlineUuid[uuid]
  238. if human and human.fd then
  239. Msg.send(msgRet, human.fd)
  240. end
  241. else
  242. send2Union(msgRet, union)
  243. end
  244. end
  245. -- 申请列表
  246. function sendApplyMsg(human, union)
  247. if not union then return end
  248. local msgRet = Msg.gc.GC_UNION_APPLY_MSG
  249. msgRet.applyMsg[0] = 0
  250. if union.apply then
  251. for uuid in pairs(union.apply) do
  252. if msgRet.applyMsg[0] >= #msgRet.applyMsg then
  253. break
  254. end
  255. local applyNet = msgRet.applyMsg[msgRet.applyMsg[0] + 1]
  256. if RoleLogic.getRoleBaseByUuid(uuid, applyNet.roleBase) then
  257. applyNet.flag = 1
  258. msgRet.applyMsg[0] = msgRet.applyMsg[0] + 1
  259. end
  260. end
  261. end
  262. Msg.send(msgRet, human.fd)
  263. end
  264. -- 查询公会,有公会返回公会信息,没公会返回推荐列表
  265. function unionQuery(human)
  266. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1001, true) then
  267. return
  268. end
  269. if sendUnionQuery(human) then -- 公会基础信息
  270. return
  271. end
  272. -- 推荐列表
  273. local recommandList = UnionDBLogic.getRecommendList()
  274. sendRecommandList(human, UnionDefine.RECOMMEND_TYPE_RANDOM, recommandList)
  275. end
  276. -- 查找公会
  277. function findUnion(human, unionName)
  278. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1001, true) then
  279. return false
  280. end
  281. if UnionDBLogic.getUnion(human.db.unionUuid) then
  282. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_ONE)
  283. end
  284. local unionList = UnionDBLogic.getUnionByNameRegex(unionName)
  285. local union = UnionDBLogic.getUnion(unionName)
  286. if union then
  287. unionList = unionList or {}
  288. table.insert(unionList, 1, union)
  289. end
  290. sendRecommandList(human, UnionDefine.RECOMMEND_TYPE_FIND, unionList)
  291. if not unionList or #unionList < 1 then
  292. Broadcast.sendErr(human, Lang.UNION_IS_UNREAL)
  293. end
  294. end
  295. -- 判断公会名是否合法
  296. local function checkUnionName(human, unionName)
  297. -- 如果unionName是nil 公会名不能为空
  298. if not unionName then
  299. return Broadcast.sendErr(human, Lang.UNION_FAIL_NAME_NIL)
  300. end
  301. local nameLen = string.len(unionName)
  302. -- 如果公会名长度小于公会名最小长度 公会名过短
  303. if nameLen < UNION_NAME_LEN_MIN then
  304. return Broadcast.sendErr(human, Lang.UNION_FAIL_NAME_LEN1)
  305. end
  306. -- 如果公会名长度大于公会名最小长度 公会名过长
  307. if nameLen > UNION_NAME_LEN_MAX then
  308. return Broadcast.sendErr(human, Lang.UNION_FAIL_NAME_LEN2)
  309. end
  310. -- 如果公会名中包含非法字符 公会名中有非法字符
  311. if unionName ~= FilterUtil.filterName(unionName) then
  312. return Broadcast.sendErr(human, Lang.UNION_FAIL_NAME_FILTER)
  313. end
  314. -- 公会名已经存在
  315. if UnionDBLogic.isNameExistInDB(unionName) then
  316. return Broadcast.sendErr(human, Lang.UNION_FAIL_NAME_DUPLICATE)
  317. end
  318. return true
  319. end
  320. -- 创建工会
  321. function createUnion(human, unionName, bannerID, notice, needLv, needAgree)
  322. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1001, true) then
  323. return
  324. end
  325. -- 需要VIP等级
  326. --[[
  327. local vipLv = VipLogic.getVipLv(human)
  328. if vipLv < CREATEUNION_NEED_VIPLV and human.db.lv < CREATEUNION_NEED_LV then
  329. return Broadcast.sendErr(human, Util.format(Lang.ROLE_VIP_ERROR, CREATEUNION_NEED_VIPLV, CREATEUNION_NEED_LV))
  330. end
  331. ]]
  332. if UnionDBLogic.getUnion(human.db.unionUuid) then
  333. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_ONE)
  334. end
  335. if not checkUnionName(human, unionName) then return end
  336. if not ObjHuman.checkRMB(human, CREATEUNION_NEED_ZUANSHI) then
  337. return
  338. end
  339. local now = os.time()
  340. if human.db.leaveUnionLimit ~= nil and now < human.db.leaveUnionLimit then
  341. return Broadcast.sendErr(human, Lang.UNION_LEAVE_TIME_NO_ENOUGH)
  342. end
  343. ObjHuman.decZuanshi(human, -CREATEUNION_NEED_ZUANSHI, "createUnion")
  344. bannerID = bannerID or 1
  345. notice = notice or ""
  346. local union = UnionDBLogic.addUnion(human, unionName, bannerID, notice, needLv, needAgree)
  347. if not union then return end
  348. human.db.unionUuid = union._id
  349. ObjHuman.save(human)
  350. addUnionLog(union._id, 1, human.db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  351. sendUnionQuery(human)
  352. Broadcast.sendErr(human, Lang.UNION_CREATE_SUCCESS)
  353. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1001)
  354. unionChangeOp(UNION_CHANGE_OP_2, human.db._id, union._id)
  355. end
  356. -- 离开公会回调
  357. function onLeaveUnion(unionUuid, uuid, logType, mailId)
  358. local db = RoleDBLogic.getDb(uuid)
  359. if db then
  360. FakeHuman.db = db
  361. db.unionUuid = nil
  362. RoleDBLogic.saveRole(db)
  363. addUnionLog(unionUuid, 4, db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  364. db.dailyBanggong = 0
  365. db.totalBanggong = 0
  366. if mailId then
  367. local title = MailExcel.mail[mailId].title
  368. local content = MailExcel.mail[mailId].content
  369. local senderName = MailExcel.mail[mailId].senderName
  370. MailManager.add(MailManager.SYSTEM, uuid, title, content, nil, senderName)
  371. end
  372. end
  373. local human = ObjHuman.onlineUuid[uuid]
  374. if human and human.fd then
  375. Msg.send(Msg.gc.GC_UNION_LEAVE, human.fd)
  376. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1001)
  377. end
  378. if mailId and mailId == MailDefine.MAIL_ID_UNION_DISMISS then
  379. unionChangeOp(UNION_CHANGE_OP_3, uuid, unionUuid)
  380. else
  381. unionChangeOp(UNION_CHANGE_OP_1, uuid, unionUuid)
  382. end
  383. end
  384. -- 解散公会
  385. function dismissUnion(human)
  386. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  387. local unionUuid = human.db.unionUuid
  388. if not union then
  389. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  390. end
  391. if union.presidentUuid ~= human.db._id then
  392. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  393. end
  394. if union.curCnt > 1 then
  395. return Broadcast.sendErr(human, Lang.UNION_HAS_MEMBER)
  396. end
  397. -- 当前公会战功能未开放, 直接屏蔽,后续开启公会战时再处理
  398. -- local warState = UnionWarLogic.getState()
  399. -- if warState > UnionWarLogic.STATE1 then -- 公会战期间
  400. -- return Broadcast.sendErr(human, Lang.UNION_IS_WARRING)
  401. -- end
  402. UnionDBLogic.delUnion(union._id)
  403. onLeaveUnion(union._id, human.db._id, nil, MailDefine.MAIL_ID_UNION_DISMISS)
  404. if not human.db.leaveUnionLimit then
  405. human.db.leaveUnionLimit = 0
  406. else
  407. human.db.leaveUnionLimit = os.time() + 8*60*60
  408. end
  409. Broadcast.sendErr(human, Lang.UNION_DISMISS_SUCCESS)
  410. BillboardDB.dismissUnion(unionUuid)
  411. UnionWarDBLogic.dismissUnion(unionUuid)
  412. end
  413. -- 申请加入公会
  414. function applyJoinUnion(human, unionUuid)
  415. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1001, true) then
  416. return
  417. end
  418. local now = os.time()
  419. if human.db.leaveUnionLimit ~= nil and now < human.db.leaveUnionLimit then
  420. return Broadcast.sendErr(human, Lang.UNION_LEAVE_TIME_NO_ENOUGH)
  421. end
  422. if UnionDBLogic.getUnion(human.db.unionUuid) then
  423. return Broadcast.sendErr(human, Lang.UNION_PLAYER_JOIN_OTHER)
  424. end
  425. local union = UnionDBLogic.getUnion(unionUuid)
  426. if not union then
  427. return Broadcast.sendErr(human, Lang.UNION_IS_UNREAL)
  428. end
  429. if (union.needLv or 0) > human.db.lv then
  430. return Broadcast.sendErr(human, Lang.UNION_IS_NEED_LV)
  431. end
  432. local config = getUnionConfig(union.lv)
  433. -- if union.curCnt >= config.maxCnt then -- 人数满了
  434. -- return Broadcast.sendErr(human, Lang.UNION_IS_FULL)
  435. -- end
  436. local isJoin = false
  437. if union.needAgree == 1 or union.curCnt >= config.maxCnt then
  438. UnionDBLogic.addApply(union, human.db._id)
  439. local msgRet = Msg.gc.GC_UNION_APPLY_JOIN
  440. msgRet.unionUuid = unionUuid
  441. Msg.send(msgRet, human.fd)
  442. for _,tpHuman in pairs(ObjHuman.onlineUuid) do
  443. if isPost(tpHuman) and tpHuman.db.unionUuid == unionUuid then
  444. RoleSystemLogic.onDot(tpHuman, RoleSystemDefine.ROLE_SYS_ID_1005)
  445. end
  446. end
  447. else
  448. -- 自动同意
  449. human.db.unionUuid = union._id
  450. isJoin = true
  451. ObjHuman.save(human)
  452. UnionDBLogic.addUnionMember(union, human.db._id)
  453. addUnionLog(union._id, 3, human.db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  454. sendUnionMsg(human.db._id, union)
  455. -- 邮件
  456. local mailID = MailDefine.MAIL_ID_UNION_JOIN
  457. local title = MailExcel.mail[mailID].title
  458. local content = Util.format(MailExcel.mail[mailID].content, union.name)
  459. local senderName = MailExcel.mail[mailID].senderName
  460. MailManager.add(MailManager.SYSTEM, human.db._id, title, content, nil, senderName)
  461. --unionQuery(human)
  462. sendMemberUpdate(union)
  463. end
  464. Broadcast.sendErr(human, Lang.UNION_APPLY_OK)
  465. if isJoin then
  466. unionChangeOp(UNION_CHANGE_OP_2, human.db._id, union._id)
  467. end
  468. end
  469. -- 会长同意/拒绝加入公会
  470. function agreeJoinUnion(human, uuid, isArgee)
  471. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  472. if not union then
  473. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  474. end
  475. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  476. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  477. member.post ~= UnionDefine.POST_OFFICIAL) then
  478. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  479. end
  480. -- 申请不存在
  481. if not UnionDBLogic.isApply(union, uuid) then
  482. return Broadcast.sendErr(human, Lang.UNION_APPLY_AGREE_ERR_NONE)
  483. end
  484. local isJoin = false
  485. if isArgee == 1 then
  486. local config = getUnionConfig(union.lv)
  487. if union.curCnt >= config.maxCnt then -- 人数满了
  488. return Broadcast.sendErr(human, Lang.UNION_CNT_IS_OVER)
  489. end
  490. UnionDBLogic.delApply(union, uuid)
  491. local db = RoleDBLogic.getDb(uuid)
  492. if db == nil then -- 不存在
  493. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_ID)
  494. end
  495. if UnionDBLogic.getUnion(db.unionUuid) then -- 有公会
  496. return Broadcast.sendErr(human, Lang.UNION_PLAYER_JOIN_OTHER)
  497. end
  498. db.unionUuid = union._id
  499. RoleDBLogic.saveRole(db)
  500. UnionDBLogic.addUnionMember(union, uuid)
  501. addUnionLog(union._id, 3, db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  502. sendUnionMsg(uuid, union)
  503. -- 邮件
  504. local mailID = MailDefine.MAIL_ID_UNION_JOIN
  505. local title = MailExcel.mail[mailID].title
  506. local content = Util.format(MailExcel.mail[mailID].content, union.name)
  507. local senderName = MailExcel.mail[mailID].senderName
  508. MailManager.add(MailManager.SYSTEM, db._id, title, content, nil, senderName)
  509. sendMemberUpdate(union,uuid)
  510. sendMemberUpdate(union,human.db._id)
  511. isJoin = true
  512. else
  513. UnionDBLogic.delApply(union, uuid)
  514. end
  515. sendApplyMsg(human, union)
  516. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1005)
  517. if isJoin then
  518. unionChangeOp(UNION_CHANGE_OP_2, uuid, union._id)
  519. end
  520. end
  521. -- 一键拒绝
  522. function oneKeyDisagree(human)
  523. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  524. if not union then
  525. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  526. end
  527. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  528. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  529. member.post ~= UnionDefine.POST_OFFICIAL) then
  530. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  531. end
  532. UnionDBLogic.resetApply(union)
  533. sendApplyMsg(human, union)
  534. RoleSystemLogic.onDot(tpHuman, RoleSystemDefine.ROLE_SYS_ID_1005)
  535. end
  536. -- 会长踢玩家出公会
  537. function expelPlayer(human, uuid)
  538. if human.db._id == uuid then --不能踢自己
  539. return Broadcast.sendErr(human, Lang.UNION_OPERATE_FAIL)
  540. end
  541. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  542. if not union then
  543. return Broadcast.sendErr(human, Lang.UNION_IS_UNREAL)
  544. end
  545. local member = UnionDBLogic.getUnionMember(union, uuid)
  546. if not member or member.post == UnionDefine.POST_PRESIDENT then
  547. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  548. end
  549. member = UnionDBLogic.getUnionMember(union, human.db._id)
  550. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  551. member.post ~= UnionDefine.POST_OFFICIAL) then
  552. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  553. end
  554. if UnionDBLogic.getKickCntToday(union) >= EXPEL_CNT_MAX then
  555. return Broadcast.sendErr(human, Lang.UNION_EXPEL_NUM_MAX_ERROR)
  556. end
  557. local tmember = UnionDBLogic.getUnionMember(union, uuid)
  558. if not tmember then --该玩家不在本公会
  559. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_OTHER)
  560. end
  561. UnionDBLogic.delUnionMember(union, uuid, true)
  562. onLeaveUnion(union._id, uuid, 2, MailDefine.MAIL_ID_UNION_LEAVE)
  563. sendMemberUpdate(union)
  564. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  565. local fakeHuman = ObjHuman.onlineUuid[uuid]
  566. if fakeHuman and fakeHuman.db then
  567. RoleAttr.cleanHeroAttrCache(fakeHuman)
  568. end
  569. end
  570. -- 玩家自己离开公会
  571. function leaveUnion(human)
  572. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  573. if not union then
  574. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  575. end
  576. if union.presidentUuid == human.db._id then
  577. return Broadcast.sendErr(human, Lang.UNION_LEVEL_ERR_LEADER)
  578. end
  579. UnionDBLogic.delUnionMember(union, human.db._id)
  580. onLeaveUnion(union._id, human.db._id)
  581. if not human.db.leaveUnionLimit then
  582. human.db.leaveUnionLimit = 0
  583. else
  584. human.db.leaveUnionLimit = os.time() + 8*60*60
  585. end
  586. sendMemberUpdate(union)
  587. Broadcast.sendErr(human, Lang.UNION_LEVEL_SUCCESS)
  588. if human.db.roleDot and human.db.roleDot.unionLog then
  589. human.db.roleDot.unionLog = nil
  590. end
  591. RoleAttr.cleanHeroAttrCache(human)
  592. end
  593. -- 玩家公会变化
  594. function unionChangeOp(op, uuid, unionUuid)
  595. local msgInner = {} --InnerMsg.lw.LW_ROLE_UNION_OP
  596. msgInner.op = op
  597. msgInner.uuid = uuid
  598. msgInner.unionUuid = unionUuid
  599. --InnerMsg.sendMsg(0, msgInner)
  600. return MozhuMiddleLogic.LW_ROLE_UNION_OP(nil,msgInner)
  601. end
  602. -- 设置工会官员
  603. function setPost(human, uuid)
  604. if human.db._id == uuid then
  605. return Broadcast.sendErr(human, Lang.UNION_OPERATE_FAIL)
  606. end
  607. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  608. if not union then
  609. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  610. end
  611. if union.presidentUuid ~= human.db._id then
  612. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  613. end
  614. local member = UnionDBLogic.getUnionMember(union, uuid)
  615. if not member then
  616. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_OTHER)
  617. end
  618. local postCnt = UnionExcel.union[union.lv].postCnt
  619. local db = RoleDBLogic.getDb(uuid, "name")
  620. if member.post == UnionDefine.POST_OFFICIAL then
  621. -- 取消官员
  622. UnionDBLogic.setPost(union, uuid, UnionDefine.POST_MEMBER)
  623. addUnionLog(union._id, 7, db and db.name or "",UnionDefine.UNION_LOG_CLASSIFY_BASE)
  624. local mailID = MailDefine.MAIL_ID_UNION_BE_MEMBER
  625. local title = MailExcel.mail[mailID].title
  626. local content = MailExcel.mail[mailID].content
  627. local senderName = MailExcel.mail[mailID].senderName
  628. MailManager.add(MailManager.SYSTEM, uuid, title, content, nil, senderName)
  629. else
  630. if UnionDBLogic.getPostCnt(union, UnionDefine.POST_OFFICIAL) >= postCnt then
  631. return Broadcast.sendErr(human, Lang.UNION_POST_IS_MAX)
  632. end
  633. -- 设置为官员
  634. UnionDBLogic.setPost(union, uuid, UnionDefine.POST_OFFICIAL)
  635. addUnionLog(union._id, 5, db and db.name or "",UnionDefine.UNION_LOG_CLASSIFY_BASE)
  636. local mailID = MailDefine.MAIL_ID_UNION_BE_POST
  637. local title = MailExcel.mail[mailID].title
  638. local content = MailExcel.mail[mailID].content
  639. local senderName = MailExcel.mail[mailID].senderName
  640. MailManager.add(MailManager.SYSTEM, uuid, title, content, nil, senderName)
  641. end
  642. sendMemberUpdate(union)
  643. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  644. end
  645. -- 更换会长
  646. function changePresident(human, uuid)
  647. if human.db._id == uuid then
  648. return Broadcast.sendErr(human, Lang.UNION_OPERATE_FAIL)
  649. end
  650. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  651. if not union then
  652. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  653. end
  654. if union.presidentUuid ~= human.db._id then
  655. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  656. end
  657. local member = UnionDBLogic.getUnionMember(union, uuid)
  658. if not member then
  659. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_OTHER)
  660. end
  661. UnionDBLogic.setPresident(union, uuid)
  662. local db = RoleDBLogic.getDb(uuid, "name")
  663. addUnionLog(union._id, 6, db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  664. sendMemberUpdate(union)
  665. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  666. -- 发送邮件
  667. for uuid in pairs(union.member) do
  668. local mailID = MailDefine.MAIL_ID_UNION_CHANGE_PRE1
  669. local title = MailExcel.mail[mailID].title
  670. local content = Util.format(MailExcel.mail[mailID].content, db.name)
  671. local senderName = MailExcel.mail[mailID].senderName
  672. MailManager.add(MailManager.SYSTEM, uuid, title, content, nil, senderName)
  673. end
  674. end
  675. -- 成员管理
  676. function memberMam(human, uuid, mamType)
  677. if mamType == UNION_MEM_EXPEL then
  678. return expelPlayer(human, uuid)
  679. elseif mamType == UNION_MEM_SET_POST then
  680. return setPost(human, uuid)
  681. elseif mamType == UNION_MEM_CHANGE_PRESIDENT then
  682. return changePresident(human, uuid)
  683. else
  684. return Broadcast.sendErr(human, Lang.UNION_OPERATE_FAIL)
  685. end
  686. end
  687. -- 设置公告
  688. function setNotice(human, notice)
  689. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  690. if not union then
  691. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  692. end
  693. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  694. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  695. member.post ~= UnionDefine.POST_OFFICIAL) then
  696. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  697. end
  698. if notice == union.notice then
  699. return Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  700. end
  701. if union.noticeTime and (os.time() - union.noticeTime < UNION_CHANGE_CD) then
  702. local hour = math.floor((UNION_CHANGE_CD - (os.time() - union.noticeTime)) / 60/60)
  703. local min = math.floor((UNION_CHANGE_CD - (os.time() - union.noticeTime)) / 60) - (hour*60)
  704. return Broadcast.sendErr(human, Util.format(Lang.UNION_CHANGE_CD_ERROR, hour,min))
  705. end
  706. if notice ~= FilterUtil.filter(notice) then
  707. return Broadcast.sendErr(human, Lang.UNION_NOTICE_IS_FAIL)
  708. end
  709. UnionDBLogic.setNotice(union, notice)
  710. sendUnionChange(union)
  711. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  712. end
  713. -- 查询入会设置
  714. function sendJoinLimitQuery(human)
  715. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  716. if not union then
  717. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  718. end
  719. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  720. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  721. member.post ~= UnionDefine.POST_OFFICIAL) then
  722. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  723. end
  724. local msgRet = Msg.gc.GC_UNION_CHANGE_LIMIT_QUERY
  725. msgRet.needLv = union.needLv or 0
  726. msgRet.needAgree = union.needAgree or 0
  727. Msg.send(msgRet, human.fd)
  728. end
  729. -- 修改入会设置
  730. function setJoinLimit(human, needLv, needAgree)
  731. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  732. if not union then
  733. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  734. end
  735. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  736. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  737. member.post ~= UnionDefine.POST_OFFICIAL) then
  738. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  739. end
  740. UnionDBLogic.setJoinLimit(union, needLv, needAgree)
  741. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  742. end
  743. function addUnionExpByItem(human, add)
  744. if not human.db.unionUuid or human.db.unionUuid == "" then
  745. return
  746. end
  747. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  748. if not union then return end
  749. addUnionExp(union, add)
  750. UnionDBLogic.updateUnionData(union)
  751. end
  752. -- 添加公会经验
  753. function addUnionExp(union, exp)
  754. local oldLv = union.lv
  755. union.exp = union.exp + exp
  756. while true do
  757. local config = UnionExcel.union[union.lv]
  758. if not config then break end
  759. if config.exp < 1 then -- 满级
  760. union.exp = 0
  761. break
  762. end
  763. if union.exp < config.exp then
  764. break
  765. end
  766. union.exp = union.exp - config.exp
  767. union.lv = union.lv + 1
  768. end
  769. end
  770. -- 公会申请查询
  771. function applyQuery(human)
  772. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  773. if not union then
  774. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  775. end
  776. sendApplyMsg(human, union)
  777. end
  778. -- 获取改名价格
  779. local function getChangeNameCost(union)
  780. if not union.nameTime then
  781. return 0 -- 首次改名免费
  782. end
  783. return CHANGENAME_NEED_ZUANSHI
  784. end
  785. -- 改名相关查询 例如改名价格
  786. function sendChangeNameQuery(human)
  787. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  788. if not union then
  789. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  790. end
  791. if union.presidentUuid ~= human.db._id then
  792. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  793. end
  794. local msgRet = Msg.gc.GC_UNION_CHANGE_NAME_QUERY
  795. msgRet.needCost = getChangeNameCost(union)
  796. Msg.send(msgRet, human.fd)
  797. end
  798. -- 公会改名
  799. function changeUnionName(human, name, bannerID)
  800. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  801. if not union then
  802. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  803. end
  804. if union.presidentUuid ~= human.db._id then
  805. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  806. end
  807. if union.nameTime and (os.time() - union.nameTime < UNION_CHANGE_CD) then
  808. local leftTime = math.ceil((UNION_CHANGE_CD - (os.time() - union.nameTime)) / 60)
  809. return Broadcast.sendErr(human, Util.format(Lang.UNION_CHANGE_CD_ERROR, leftTime))
  810. end
  811. if not checkUnionName(human, name) and name ~= union.name then
  812. return Broadcast.sendErr(human, Lang.CREATE_UNION_FAIL_NAME)
  813. end
  814. local needCost = getChangeNameCost(union)
  815. if not ObjHuman.checkRMB(human, needCost) then
  816. return
  817. end
  818. ObjHuman.decZuanshi(human, -needCost, "changeUnionName")
  819. UnionDBLogic.setUnionName(union, name)
  820. sendUnionChange(union)
  821. Broadcast.sendErr(human, Lang.UNION_CHANGE_NAME_SUCCESS)
  822. end
  823. -- 公会改旗帜
  824. function changeUnionBannerID(human,bannerID)
  825. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  826. if not union then
  827. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  828. end
  829. if union.presidentUuid ~= human.db._id then
  830. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  831. end
  832. if not UnionExcel.banner[bannerID] then
  833. return Broadcast.sendErr(human, Lang.UNION_BANNER_ID_ILL)
  834. end
  835. UnionDBLogic.setBannerID(union, bannerID)
  836. sendUnionChange(union)
  837. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  838. end
  839. -- 发送邮件
  840. function sendUnionMail(human, content)
  841. local msgRet = Msg.gc.GC_UNION_MAIL
  842. msgRet.flag = 0
  843. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  844. if not union then
  845. Msg.send(msgRet, human.fd)
  846. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  847. end
  848. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  849. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  850. member.post ~= UnionDefine.POST_OFFICIAL) then
  851. Msg.send(msgRet, human.fd)
  852. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  853. end
  854. local mailCnt = UnionDBLogic.getMailCnt(union)
  855. if mailCnt >= UNION_ALL_MAIL_MAX_CNT then
  856. Msg.send(msgRet, human.fd)
  857. return Broadcast.sendErr(human, Lang.UNION_MAIL_CNT_MAX)
  858. end
  859. if content == "" or content ~= FilterUtil.filter(content) then
  860. return Broadcast.sendErr(human, Lang.MAIL_CONTENT_ERROR)
  861. end
  862. union.mailCnt = mailCnt + 1
  863. union.mailTs = union.mailTs or os.time()
  864. UnionDBLogic.updateUnionData(union)
  865. for uuid in pairs(union.member) do
  866. MailManager.add(MailManager.SYSTEM, uuid, Lang.UNION_MAIL, content, nil, human.db.name, human)
  867. end
  868. Broadcast.sendErr(human, Lang.UNION_SEND_SUCCESS)
  869. msgRet.flag = 1
  870. Msg.send(msgRet, human.fd)
  871. end
  872. function hasLog(human, union)
  873. -- local time = 0
  874. -- if human.db.roleDot and human.db.roleDot.unionLog then
  875. -- time = human.db.roleDot.unionLog
  876. -- end
  877. -- for i = 1, #union.logs do
  878. -- local log = union.logs[i]
  879. -- if log.time > time then
  880. -- return true
  881. -- end
  882. -- end
  883. return false
  884. end
  885. -- 公会日志
  886. function getLog(human,classify)
  887. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  888. if not union then
  889. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  890. end
  891. local msgRet = Msg.gc.GC_UNION_GET_LOG
  892. local cnt = 0
  893. for i = 1, #union.logs do
  894. local log = union.logs[i]
  895. if log.classify == classify or classify == 0 then
  896. cnt = cnt + 1
  897. msgRet.logMsg[cnt].logMsg = log.content
  898. end
  899. if cnt >= #msgRet.logMsg then
  900. break
  901. end
  902. end
  903. msgRet.logMsg[0] = cnt
  904. --Msg.trace(msgRet)
  905. Msg.send(msgRet, human.fd)
  906. human.db.roleDot = human.db.roleDot or {}
  907. human.db.roleDot.unionLog = os.time()
  908. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1005)
  909. end
  910. local function getRecruitCost(union)
  911. if not union.recruit then return 0 end
  912. return RECRUIT_COST_VALUE
  913. end
  914. -- 招募查询
  915. function sendRecruitQuery(human)
  916. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  917. if not union then
  918. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  919. end
  920. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  921. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  922. member.post ~= UnionDefine.POST_OFFICIAL) then
  923. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  924. end
  925. local msgRet = Msg.gc.GC_UNION_RECRUIT_QUERY
  926. msgRet.costValue = getRecruitCost(union)
  927. Msg.send(msgRet, human.fd)
  928. end
  929. -- 招募宣言
  930. function unionRecruit(human,str)
  931. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  932. if not union then
  933. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  934. end
  935. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  936. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  937. member.post ~= UnionDefine.POST_OFFICIAL) then
  938. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  939. end
  940. -- 检查花费
  941. local costValue = getRecruitCost(union)
  942. if not ObjHuman.checkRMB(human, costValue) then
  943. Msg.send(msgRet, human.fd)
  944. return
  945. end
  946. human.chatTime = human.chatTime or {}
  947. local chatTime = human.chatTime[ChatHandler.CHAT_TYPE_WORLD] or 0
  948. if chatTime + ChatHandler.CHAT_CD >= Timer.now then
  949. local cdLeftSec = math.ceil((ChatHandler.CHAT_CD - (Timer.now - chatTime))/1000)
  950. Broadcast.sendDown(human, Util.format(Lang.CHAT_TIME_SHORT, cdLeftSec))
  951. return
  952. end
  953. human.worldChatLast = human.worldChatLast or {}
  954. if human.worldChatLast[ChatHandler.CHAT_TYPE_WORLD] == str then
  955. Broadcast.sendDown(human, Lang.CHAT_TIME_DUPLICATE)
  956. return
  957. end
  958. ObjHuman.decZuanshi(human, -costValue, "union_recruit")
  959. if not union.recruit then
  960. union.recruit = 1
  961. UnionDBLogic.updateUnionData(union)
  962. end
  963. local dec = Util.format(Lang.UNION_RECRUIT_BC, union.lv, union.name)
  964. ChatUnion.chatUnion(human, ChatHandler.CHAT_TYPE_UNION, str)
  965. Broadcast.sendErr(human, Lang.UNION_SEND_SUCCESS)
  966. end
  967. -- 是否有红点 申请
  968. function dot(human)
  969. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  970. if union == nil then
  971. return
  972. end
  973. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  974. if not member then return end
  975. if member.post ~= UnionDefine.POST_PRESIDENT and
  976. member.post ~= UnionDefine.POST_OFFICIAL then
  977. return
  978. end
  979. if union.apply == nil then
  980. return
  981. end
  982. return next(union.apply) ~= nil
  983. end
  984. -- 用于显示红点
  985. function isDot(human)
  986. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  987. if union == nil then
  988. -- 没加入公会 不显示红点
  989. return
  990. end
  991. if dot(human) then
  992. return true
  993. end
  994. -- if hasLog(human, union) then
  995. -- return true
  996. -- end
  997. return nil
  998. end
  999. -- 0点后自动让位
  1000. function onHour()
  1001. PRESIDENT_CHECK_FLAG = true
  1002. end
  1003. -- 自动让位
  1004. PRESIDENT_AUTO_LIST = PRESIDENT_AUTO_LIST or nil -- 自动让位列表
  1005. PRESIDENT_CHECK_FLAG = PRESIDENT_CHECK_FLAG or nil -- 自动让位标记
  1006. function checkPresidentOffline()
  1007. if PRESIDENT_AUTO_LIST then return end
  1008. PRESIDENT_CHECK_FLAG = nil
  1009. PRESIDENT_AUTO_LIST = {}
  1010. local lastSevenDays = os.time() - UNION_AUTO_PRESIDENT_TIME
  1011. LuaMongo.find(DB.db_union, nil, FieldsUnionOffline)
  1012. local union2president = {}
  1013. while true do
  1014. local union = {}
  1015. if not LuaMongo.next(union) then
  1016. break
  1017. end
  1018. union2president[union._id] = union.presidentUuid
  1019. end
  1020. for unionUuid, presidentUuid in pairs(union2president) do
  1021. local db, online = RoleDBLogic.getDb(presidentUuid, FieldsRoleOffline)
  1022. if not db or (not online and (db.lastLogoutTime or 0) < lastSevenDays) then
  1023. PRESIDENT_AUTO_LIST[#PRESIDENT_AUTO_LIST + 1] = unionUuid
  1024. end
  1025. end
  1026. end
  1027. -- 是否可继承会长
  1028. local function canBeNewPresident(db, lastSevenDays, online)
  1029. if not db then return end
  1030. if not online and db.lastLogoutTime < lastSevenDays then
  1031. return
  1032. end
  1033. return true
  1034. end
  1035. -- 比较
  1036. local function cmpBeNewMember(db1, db2)
  1037. if not db1 then return end
  1038. if not db2 then return true end
  1039. if db1.lastLogoutTime ~= db2.lastLogoutTime then
  1040. return db1.lastLogoutTime > db2.lastLogoutTime
  1041. end
  1042. return db1.lv > db2.lv
  1043. end
  1044. -- 自动转让会长
  1045. function autoPresidentChange()
  1046. local unionUuid = PRESIDENT_AUTO_LIST[#PRESIDENT_AUTO_LIST]
  1047. if not unionUuid then
  1048. PRESIDENT_AUTO_LIST = nil
  1049. return
  1050. end
  1051. PRESIDENT_AUTO_LIST[#PRESIDENT_AUTO_LIST] = nil
  1052. local union = UnionDBLogic.getUnion(unionUuid)
  1053. if not union then return end
  1054. local lastSevenDays = os.time() - UNION_AUTO_PRESIDENT_TIME
  1055. local newdb = nil
  1056. for uuid, member in pairs(union.member) do -- 优先挑选管理
  1057. if member.post == UnionDefine.POST_OFFICIAL then
  1058. local db, online = RoleDBLogic.getDb(uuid, FieldsRoleOffline)
  1059. if canBeNewPresident(db, lastSevenDays, online) then
  1060. newdb = db
  1061. break
  1062. end
  1063. end
  1064. end
  1065. if not newdb then
  1066. for uuid, member in pairs(union.member) do -- 再挑普通成员
  1067. if member.post == UnionDefine.POST_MEMBER then
  1068. local db, online = RoleDBLogic.getDb(uuid, FieldsRoleOffline)
  1069. if canBeNewPresident(db, lastSevenDays, online) and
  1070. not cmpBeNewMember(newdb, db) then
  1071. newdb = db
  1072. break
  1073. end
  1074. end
  1075. end
  1076. end
  1077. if not newdb then return end
  1078. -- 设置新会长
  1079. UnionDBLogic.setPresident(union, newdb._id)
  1080. addUnionLog(union._id, 6, newdb.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  1081. end
  1082. -- 定时检测
  1083. function onTimer()
  1084. if PRESIDENT_CHECK_FLAG then
  1085. checkPresidentOffline()
  1086. end
  1087. if PRESIDENT_AUTO_LIST then
  1088. autoPresidentChange()
  1089. end
  1090. end
  1091. -- 弹劾会长
  1092. function impeachPresident(human)
  1093. -- 判断玩家是否有工会
  1094. -- 判断玩家公会是否存在
  1095. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1096. if union == nil then
  1097. return
  1098. end
  1099. local db,online = RoleDBLogic.getDb(union.presidentUuid,FieldsRoleOffline)
  1100. local lastSevenDays = os.time() - UNION_AUTO_PRESIDENT_TIME
  1101. if online ~= nil or db.lastLogoutTime >= lastSevenDays then
  1102. return -- 弹劾失败
  1103. end
  1104. -- 扣除花费
  1105. local cost = UNION_OP_IMPEACH_PRESIDENT
  1106. if not ObjHuman.checkRMB(human, cost) then
  1107. return
  1108. end
  1109. ObjHuman.decZuanshi(human, -cost, "abs_answer")
  1110. -- 设置新会长
  1111. UnionDBLogic.setPresident(union, human.db._id)
  1112. addUnionLog(union._id, 14, human.db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE,db.name)
  1113. sendMemberUpdate(union)
  1114. -- 发送系统消息
  1115. -- todo
  1116. -- 发送邮件
  1117. local mailID = MailDefine.MAIL_ID_UNION_CHANGE_PRE2
  1118. local title = MailExcel.mail[mailID].title
  1119. local content = Util.format(MailExcel.mail[mailID].content, human.db.name)
  1120. local senderName = MailExcel.mail[mailID].senderName
  1121. MailManager.add(MailManager.SYSTEM, db._id, title, content, nil, senderName)
  1122. end
  1123. function getUnionLogMemberDetail(human,type)
  1124. local msgRet = Msg.gc.GC_UNION_LOG_DETAIL_QUERY
  1125. if type == 2 then
  1126. return -- 临时
  1127. --getCanUnionWarMember(human.db.unionUuid,msgRet)
  1128. elseif type == 3 then
  1129. getCanBossMember(human.db.unionUuid,msgRet)
  1130. elseif type == 4 then
  1131. getCanDonateMember(human.db.unionUuid,msgRet)
  1132. end
  1133. Msg.send(msgRet,human.fd)
  1134. end
  1135. -- 获得还有挑战工会战次数的玩家
  1136. -- todo 等全谈写完工会战,再完成
  1137. function getCanUnionWarMember(unionUuid)
  1138. local union = UnionDBLogic.getUnion(unionUuid)
  1139. if not union then return end
  1140. for uuid, member in pairs(union.member) do
  1141. if uuid ~= union.presidentUuid then
  1142. if msgRet.memberDetail[0] >= #msgRet.memberDetail then
  1143. break
  1144. end
  1145. msgRet.memberDetail[0] = msgRet.memberDetail[0] + 1
  1146. fontUnionMember(msgRet.memberDetail[msgRet.memberDetail[0]], uuid, member)
  1147. end
  1148. end
  1149. end
  1150. -- 获得还有捐献次数的玩家
  1151. function getCanDonateMember(unionUuid,net)
  1152. local union = UnionDBLogic.getUnion(unionUuid)
  1153. if not union then return end
  1154. local maxCnt = 0
  1155. local leftCnt = 0
  1156. local len = 0
  1157. local maxLen = #net.memberDetail
  1158. for uuid, member in pairs(union.member) do
  1159. maxCnt= maxCnt + 1
  1160. local db = RoleDBLogic.getDb(uuid)
  1161. if db.todayDonate ~= 0 then
  1162. if len >= maxLen then
  1163. break
  1164. end
  1165. leftCnt = leftCnt + 1
  1166. len = len + 1
  1167. fontUnionMember(net.memberDetail[len], uuid, member)
  1168. end
  1169. end
  1170. net.memberDetail[0] = len
  1171. net.maxCnt = maxCnt
  1172. net.leftCnt = leftCnt
  1173. end
  1174. -- 获得还有挑战工会boss次数的玩家
  1175. -- todo 等全谈写完工会boss,再完成
  1176. function getCanBossMember(unionUuid,net)
  1177. local union = UnionDBLogic.getUnion(unionUuid)
  1178. if not union then return end
  1179. local maxCnt = 0
  1180. local leftCnt = 0
  1181. local len = 0
  1182. local maxLen = #net.memberDetail
  1183. for uuid, member in pairs(union.member) do
  1184. maxCnt= maxCnt + 1
  1185. local db = RoleDBLogic.getDb(uuid)
  1186. if db.ectypeCnt == nil or db.ectypeCnt < UnionEctypeLogic.FIGHT_MAX_CNT then
  1187. if len >= maxLen then
  1188. break
  1189. end
  1190. leftCnt = leftCnt + 1
  1191. len = len + 1
  1192. fontUnionMember(net.memberDetail[len], uuid, member)
  1193. end
  1194. end
  1195. net.memberDetail[0] = len
  1196. net.maxCnt = maxCnt
  1197. net.leftCnt = leftCnt
  1198. end
  1199. function isPost(human)
  1200. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1201. if union == nil then
  1202. return
  1203. end
  1204. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  1205. if not member then return end
  1206. if member.post == UnionDefine.POST_PRESIDENT or
  1207. member.post == UnionDefine.POST_OFFICIAL then
  1208. return true
  1209. end
  1210. return nil
  1211. end
  1212. function cancelApply(human,unionUuid)
  1213. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1001, true) then
  1214. return
  1215. end
  1216. if UnionDBLogic.getUnion(human.db.unionUuid) then
  1217. return
  1218. end
  1219. local union = UnionDBLogic.getUnion(unionUuid)
  1220. if not union then
  1221. return Broadcast.sendErr(human, Lang.UNION_IS_UNREAL)
  1222. end
  1223. UnionDBLogic.delApply(union, human.db._id)
  1224. for _,tpHuman in pairs(ObjHuman.onlineUuid) do
  1225. if isPost(tpHuman) and tpHuman.db.unionUuid == unionUuid then
  1226. RoleSystemLogic.onDot(tpHuman, RoleSystemDefine.ROLE_SYS_ID_1005)
  1227. end
  1228. end
  1229. end
  1230. -- 是否是会长/副会长
  1231. function IsTopTwoManager(human,unionId)
  1232. local union = UnionDBLogic.getUnion(unionId)
  1233. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  1234. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  1235. member.post ~= UnionDefine.POST_OFFICIAL) then
  1236. return false
  1237. end
  1238. return true
  1239. end
  1240. -- 获取公会数据
  1241. function GetUnionData(unionId, fields)
  1242. local union = UnionDBLogic.getUnion(unionId, fields)
  1243. return union
  1244. end
  1245. -- 成员战力有更新
  1246. function UpdateMemberPower(human)
  1247. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1248. UnionDBLogic.MemberPowerChange(union)
  1249. end
  1250. -- 成功报名异界之战
  1251. function Join_AB_Succ(unionId)
  1252. local union = UnionDBLogic.getUnion(unionId)
  1253. if not union then
  1254. return
  1255. end
  1256. union.joinAnotherWorldBattleTi = os.time()
  1257. UnionDBLogic.updateUnionData(union)
  1258. end