UnionLogic.lua 51 KB

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