UnionLogic.lua 50 KB

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