UnionLogic.lua 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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. -- 创建工会
  327. function createUnion(human, unionName, bannerID, notice, needLv, needAgree)
  328. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1001, true) then
  329. return
  330. end
  331. -- 需要VIP等级
  332. --[[
  333. local vipLv = VipLogic.getVipLv(human)
  334. if vipLv < CREATEUNION_NEED_VIPLV and human.db.lv < CREATEUNION_NEED_LV then
  335. return Broadcast.sendErr(human, Util.format(Lang.ROLE_VIP_ERROR, CREATEUNION_NEED_VIPLV, CREATEUNION_NEED_LV))
  336. end
  337. ]]
  338. if UnionDBLogic.getUnion(human.db.unionUuid) then
  339. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_ONE)
  340. end
  341. if not checkUnionName(human, unionName) then return end
  342. if not ObjHuman.checkRMB(human, CREATEUNION_NEED_ZUANSHI) then
  343. return
  344. end
  345. local now = os.time()
  346. if human.db.leaveUnionLimit ~= nil and now < human.db.leaveUnionLimit then
  347. return Broadcast.sendErr(human, Lang.UNION_LEAVE_TIME_NO_ENOUGH)
  348. end
  349. ObjHuman.decZuanshi(human, -CREATEUNION_NEED_ZUANSHI, "createUnion")
  350. bannerID = bannerID or 1
  351. notice = notice or ""
  352. local union = UnionDBLogic.addUnion(human, unionName, bannerID, notice, needLv, needAgree)
  353. if not union then return end
  354. human.db.unionUuid = union._id
  355. ObjHuman.save(human)
  356. addUnionLog(union._id, 1, human.db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  357. sendUnionQuery(human)
  358. Broadcast.sendErr(human, Lang.UNION_CREATE_SUCCESS)
  359. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1001)
  360. unionChangeOp(UNION_CHANGE_OP_2, human.db._id, union._id)
  361. end
  362. -- 离开公会回调
  363. function onLeaveUnion(unionUuid, uuid, logType, mailId)
  364. local db = RoleDBLogic.getDb(uuid)
  365. if db then
  366. FakeHuman.db = db
  367. db.unionUuid = nil
  368. RoleDBLogic.saveRole(db)
  369. addUnionLog(unionUuid, 4, db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  370. db.dailyBanggong = 0
  371. db.totalBanggong = 0
  372. if mailId then
  373. local title = MailExcel.mail[mailId].title
  374. local content = MailExcel.mail[mailId].content
  375. local senderName = MailExcel.mail[mailId].senderName
  376. MailManager.add(MailManager.SYSTEM, uuid, title, content, nil, senderName)
  377. end
  378. end
  379. local human = ObjHuman.onlineUuid[uuid]
  380. if human and human.fd then
  381. Msg.send(Msg.gc.GC_UNION_LEAVE, human.fd)
  382. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1001)
  383. end
  384. if mailId and mailId == MailDefine.MAIL_ID_UNION_DISMISS then
  385. unionChangeOp(UNION_CHANGE_OP_3, uuid, unionUuid)
  386. else
  387. unionChangeOp(UNION_CHANGE_OP_1, uuid, unionUuid)
  388. end
  389. end
  390. -- 解散公会
  391. function dismissUnion(human)
  392. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  393. local unionUuid = human.db.unionUuid
  394. if not union then
  395. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  396. end
  397. if union.presidentUuid ~= human.db._id then
  398. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  399. end
  400. if union.curCnt > 1 then
  401. return Broadcast.sendErr(human, Lang.UNION_HAS_MEMBER)
  402. end
  403. -- 当前公会战功能未开放, 直接屏蔽,后续开启公会战时再处理
  404. -- local warState = UnionWarLogic.getState()
  405. -- if warState > UnionWarLogic.STATE1 then -- 公会战期间
  406. -- return Broadcast.sendErr(human, Lang.UNION_IS_WARRING)
  407. -- end
  408. local res = isABEnd(union)
  409. if not res then
  410. return Broadcast.sendErr(human, Lang.AB_CANNOT_OPERATION)
  411. end
  412. UnionDBLogic.delUnion(union._id)
  413. onLeaveUnion(union._id, human.db._id, nil, MailDefine.MAIL_ID_UNION_DISMISS)
  414. if not human.db.leaveUnionLimit then
  415. human.db.leaveUnionLimit = 0
  416. else
  417. human.db.leaveUnionLimit = os.time() + 8*60*60
  418. end
  419. Broadcast.sendErr(human, Lang.UNION_DISMISS_SUCCESS)
  420. BillboardDB.dismissUnion(unionUuid)
  421. UnionWarDBLogic.dismissUnion(unionUuid)
  422. CHAT_RECORD_UNION[unionUuid] = nil
  423. end
  424. -- 申请加入公会
  425. function applyJoinUnion(human, unionUuid)
  426. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1001, true) then
  427. return
  428. end
  429. local now = os.time()
  430. if human.db.leaveUnionLimit ~= nil and now < human.db.leaveUnionLimit then
  431. return Broadcast.sendErr(human, Lang.UNION_LEAVE_TIME_NO_ENOUGH)
  432. end
  433. if UnionDBLogic.getUnion(human.db.unionUuid) then
  434. return Broadcast.sendErr(human, Lang.UNION_PLAYER_JOIN_OTHER)
  435. end
  436. local union = UnionDBLogic.getUnion(unionUuid)
  437. if not union then
  438. return Broadcast.sendErr(human, Lang.UNION_IS_UNREAL)
  439. end
  440. if (union.needLv or 0) > human.db.lv then
  441. return Broadcast.sendErr(human, Lang.UNION_IS_NEED_LV)
  442. end
  443. local config = getUnionConfig(union.lv)
  444. -- if union.curCnt >= config.maxCnt then -- 人数满了
  445. -- return Broadcast.sendErr(human, Lang.UNION_IS_FULL)
  446. -- end
  447. local isJoin = false
  448. if union.needAgree == 1 or union.curCnt >= config.maxCnt then
  449. UnionDBLogic.addApply(union, human.db._id)
  450. local msgRet = Msg.gc.GC_UNION_APPLY_JOIN
  451. msgRet.unionUuid = unionUuid
  452. Msg.send(msgRet, human.fd)
  453. for _,tpHuman in pairs(ObjHuman.onlineUuid) do
  454. if isPost(tpHuman) and tpHuman.db.unionUuid == unionUuid then
  455. RoleSystemLogic.onDot(tpHuman, RoleSystemDefine.ROLE_SYS_ID_1005)
  456. end
  457. end
  458. else
  459. local res = isABEnd(union)
  460. if not res then
  461. return Broadcast.sendErr(human, Lang.AB_CANNOT_OPERATION)
  462. end
  463. -- 自动同意
  464. human.db.unionUuid = union._id
  465. isJoin = true
  466. ObjHuman.save(human)
  467. UnionDBLogic.addUnionMember(union, human.db._id)
  468. addUnionLog(union._id, 3, human.db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  469. sendUnionMsg(human.db._id, union)
  470. -- 邮件
  471. local mailID = MailDefine.MAIL_ID_UNION_JOIN
  472. local title = MailExcel.mail[mailID].title
  473. local content = Util.format(MailExcel.mail[mailID].content, union.name)
  474. local senderName = MailExcel.mail[mailID].senderName
  475. MailManager.add(MailManager.SYSTEM, human.db._id, title, content, nil, senderName)
  476. --unionQuery(human)
  477. sendMemberUpdate(union)
  478. end
  479. Broadcast.sendErr(human, Lang.UNION_APPLY_OK)
  480. if isJoin then
  481. unionChangeOp(UNION_CHANGE_OP_2, human.db._id, union._id)
  482. end
  483. end
  484. -- 会长同意/拒绝加入公会
  485. function agreeJoinUnion(human, uuid, isArgee)
  486. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  487. if not union then
  488. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  489. end
  490. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  491. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  492. member.post ~= UnionDefine.POST_OFFICIAL) then
  493. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  494. end
  495. -- 申请不存在
  496. if not UnionDBLogic.isApply(union, uuid) then
  497. return Broadcast.sendErr(human, Lang.UNION_APPLY_AGREE_ERR_NONE)
  498. end
  499. local isJoin = false
  500. if isArgee == 1 then
  501. local res = isABEnd(union)
  502. if not res then
  503. return Broadcast.sendErr(human, Lang.AB_CANNOT_OPERATION)
  504. end
  505. local config = getUnionConfig(union.lv)
  506. if union.curCnt >= config.maxCnt then -- 人数满了
  507. return Broadcast.sendErr(human, Lang.UNION_CNT_IS_OVER)
  508. end
  509. UnionDBLogic.delApply(union, uuid)
  510. local db = RoleDBLogic.getDb(uuid)
  511. if db == nil then -- 不存在
  512. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_ID)
  513. end
  514. if UnionDBLogic.getUnion(db.unionUuid) then -- 有公会
  515. return Broadcast.sendErr(human, Lang.UNION_PLAYER_JOIN_OTHER)
  516. end
  517. db.unionUuid = union._id
  518. RoleDBLogic.saveRole(db)
  519. UnionDBLogic.addUnionMember(union, uuid)
  520. addUnionLog(union._id, 3, db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  521. sendUnionMsg(uuid, union)
  522. -- 邮件
  523. local mailID = MailDefine.MAIL_ID_UNION_JOIN
  524. local title = MailExcel.mail[mailID].title
  525. local content = Util.format(MailExcel.mail[mailID].content, union.name)
  526. local senderName = MailExcel.mail[mailID].senderName
  527. MailManager.add(MailManager.SYSTEM, db._id, title, content, nil, senderName)
  528. sendMemberUpdate(union,uuid)
  529. sendMemberUpdate(union,human.db._id)
  530. isJoin = true
  531. else
  532. UnionDBLogic.delApply(union, uuid)
  533. end
  534. sendApplyMsg(human, union)
  535. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1005)
  536. if isJoin then
  537. unionChangeOp(UNION_CHANGE_OP_2, uuid, union._id)
  538. end
  539. end
  540. -- 一键拒绝
  541. function oneKeyDisagree(human)
  542. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  543. if not union then
  544. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  545. end
  546. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  547. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  548. member.post ~= UnionDefine.POST_OFFICIAL) then
  549. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  550. end
  551. UnionDBLogic.resetApply(union)
  552. sendApplyMsg(human, union)
  553. RoleSystemLogic.onDot(tpHuman, RoleSystemDefine.ROLE_SYS_ID_1005)
  554. end
  555. -- 会长踢玩家出公会
  556. function expelPlayer(human, uuid)
  557. if human.db._id == uuid then --不能踢自己
  558. return Broadcast.sendErr(human, Lang.UNION_OPERATE_FAIL)
  559. end
  560. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  561. if not union then
  562. return Broadcast.sendErr(human, Lang.UNION_IS_UNREAL)
  563. end
  564. local member = UnionDBLogic.getUnionMember(union, uuid)
  565. if not member or member.post == UnionDefine.POST_PRESIDENT then
  566. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  567. end
  568. member = UnionDBLogic.getUnionMember(union, human.db._id)
  569. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  570. member.post ~= UnionDefine.POST_OFFICIAL) then
  571. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  572. end
  573. -- if UnionDBLogic.getKickCntToday(union) >= EXPEL_CNT_MAX then
  574. -- return Broadcast.sendErr(human, Lang.UNION_EXPEL_NUM_MAX_ERROR)
  575. -- end
  576. local tmember = UnionDBLogic.getUnionMember(union, uuid)
  577. if not tmember then --该玩家不在本公会
  578. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_OTHER)
  579. end
  580. local res = isABEnd(union)
  581. if not res then
  582. return Broadcast.sendErr(human, Lang.AB_CANNOT_OPERATION)
  583. end
  584. UnionDBLogic.delUnionMember(union, uuid, true)
  585. onLeaveUnion(union._id, uuid, 2, MailDefine.MAIL_ID_UNION_LEAVE)
  586. sendMemberUpdate(union)
  587. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  588. local fakeHuman = ObjHuman.onlineUuid[uuid]
  589. if fakeHuman and fakeHuman.db then
  590. RoleAttr.cleanHeroAttrCache(fakeHuman)
  591. end
  592. end
  593. -- 玩家自己离开公会
  594. function leaveUnion(human)
  595. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  596. if not union then
  597. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  598. end
  599. if union.presidentUuid == human.db._id then
  600. return Broadcast.sendErr(human, Lang.UNION_LEVEL_ERR_LEADER)
  601. end
  602. local res = isABEnd(union)
  603. if not res then
  604. return Broadcast.sendErr(human, Lang.AB_CANNOT_OPERATION)
  605. end
  606. UnionDBLogic.delUnionMember(union, human.db._id)
  607. onLeaveUnion(union._id, human.db._id)
  608. if not human.db.leaveUnionLimit then
  609. human.db.leaveUnionLimit = 0
  610. else
  611. human.db.leaveUnionLimit = os.time() + 8*60*60
  612. end
  613. sendMemberUpdate(union)
  614. Broadcast.sendErr(human, Lang.UNION_LEVEL_SUCCESS)
  615. if human.db.roleDot and human.db.roleDot.unionLog then
  616. human.db.roleDot.unionLog = nil
  617. end
  618. RoleAttr.cleanHeroAttrCache(human)
  619. end
  620. -- 玩家公会变化
  621. function unionChangeOp(op, uuid, unionUuid)
  622. local msgInner = {} --InnerMsg.lw.LW_ROLE_UNION_OP
  623. msgInner.op = op
  624. msgInner.uuid = uuid
  625. msgInner.unionUuid = unionUuid
  626. --InnerMsg.sendMsg(0, msgInner)
  627. return MozhuMiddleLogic.LW_ROLE_UNION_OP(nil,msgInner)
  628. end
  629. -- 设置工会官员
  630. function setPost(human, uuid)
  631. if human.db._id == uuid then
  632. return Broadcast.sendErr(human, Lang.UNION_OPERATE_FAIL)
  633. end
  634. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  635. if not union then
  636. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  637. end
  638. if union.presidentUuid ~= human.db._id then
  639. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  640. end
  641. local member = UnionDBLogic.getUnionMember(union, uuid)
  642. if not member then
  643. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_OTHER)
  644. end
  645. local postCnt = UnionExcel.union[union.lv].postCnt
  646. local db = RoleDBLogic.getDb(uuid, "name")
  647. if member.post == UnionDefine.POST_OFFICIAL then
  648. -- 取消官员
  649. UnionDBLogic.setPost(union, uuid, UnionDefine.POST_MEMBER)
  650. addUnionLog(union._id, 7, db and db.name or "",UnionDefine.UNION_LOG_CLASSIFY_BASE)
  651. local mailID = MailDefine.MAIL_ID_UNION_BE_MEMBER
  652. local title = MailExcel.mail[mailID].title
  653. local content = MailExcel.mail[mailID].content
  654. local senderName = MailExcel.mail[mailID].senderName
  655. MailManager.add(MailManager.SYSTEM, uuid, title, content, nil, senderName)
  656. else
  657. if UnionDBLogic.getPostCnt(union, UnionDefine.POST_OFFICIAL) >= postCnt then
  658. return Broadcast.sendErr(human, Lang.UNION_POST_IS_MAX)
  659. end
  660. -- 设置为官员
  661. UnionDBLogic.setPost(union, uuid, UnionDefine.POST_OFFICIAL)
  662. addUnionLog(union._id, 5, db and db.name or "",UnionDefine.UNION_LOG_CLASSIFY_BASE)
  663. local mailID = MailDefine.MAIL_ID_UNION_BE_POST
  664. local title = MailExcel.mail[mailID].title
  665. local content = MailExcel.mail[mailID].content
  666. local senderName = MailExcel.mail[mailID].senderName
  667. MailManager.add(MailManager.SYSTEM, uuid, title, content, nil, senderName)
  668. end
  669. sendMemberUpdate(union)
  670. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  671. end
  672. -- 更换会长
  673. function changePresident(human, uuid)
  674. if human.db._id == uuid then
  675. return Broadcast.sendErr(human, Lang.UNION_OPERATE_FAIL)
  676. end
  677. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  678. if not union then
  679. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  680. end
  681. if union.presidentUuid ~= human.db._id then
  682. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  683. end
  684. local member = UnionDBLogic.getUnionMember(union, uuid)
  685. if not member then
  686. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_OTHER)
  687. end
  688. UnionDBLogic.setPresident(union, uuid)
  689. local db = RoleDBLogic.getDb(uuid, "name")
  690. addUnionLog(union._id, 6, db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  691. sendMemberUpdate(union)
  692. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  693. -- 发送邮件
  694. for uuid in pairs(union.member) do
  695. local mailID = MailDefine.MAIL_ID_UNION_CHANGE_PRE1
  696. local title = MailExcel.mail[mailID].title
  697. local content = Util.format(MailExcel.mail[mailID].content, db.name)
  698. local senderName = MailExcel.mail[mailID].senderName
  699. MailManager.add(MailManager.SYSTEM, uuid, title, content, nil, senderName)
  700. end
  701. end
  702. -- 成员管理
  703. function memberMam(human, uuid, mamType)
  704. if mamType == UNION_MEM_EXPEL then
  705. return expelPlayer(human, uuid)
  706. elseif mamType == UNION_MEM_SET_POST then
  707. return setPost(human, uuid)
  708. elseif mamType == UNION_MEM_CHANGE_PRESIDENT then
  709. return changePresident(human, uuid)
  710. else
  711. return Broadcast.sendErr(human, Lang.UNION_OPERATE_FAIL)
  712. end
  713. end
  714. -- 设置公告
  715. function setNotice(human, notice)
  716. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  717. if not union then
  718. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  719. end
  720. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  721. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  722. member.post ~= UnionDefine.POST_OFFICIAL) then
  723. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  724. end
  725. if notice == union.notice then
  726. return Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  727. end
  728. if union.noticeTime and (os.time() - union.noticeTime < UNION_CHANGE_CD) then
  729. local hour = math.floor((UNION_CHANGE_CD - (os.time() - union.noticeTime)) / 60/60)
  730. local min = math.floor((UNION_CHANGE_CD - (os.time() - union.noticeTime)) / 60) - (hour*60)
  731. return Broadcast.sendErr(human, Util.format(Lang.UNION_CHANGE_CD_ERROR, hour,min))
  732. end
  733. if notice ~= FilterUtil.filter(notice) then
  734. return Broadcast.sendErr(human, Lang.UNION_NOTICE_IS_FAIL)
  735. end
  736. UnionDBLogic.setNotice(union, notice)
  737. sendUnionChange(union)
  738. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  739. end
  740. -- 查询入会设置
  741. function sendJoinLimitQuery(human)
  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. local msgRet = Msg.gc.GC_UNION_CHANGE_LIMIT_QUERY
  752. msgRet.needLv = union.needLv or 0
  753. msgRet.needAgree = union.needAgree or 0
  754. Msg.send(msgRet, human.fd)
  755. end
  756. -- 修改入会设置
  757. function setJoinLimit(human, needLv, needAgree)
  758. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  759. if not union then
  760. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  761. end
  762. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  763. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  764. member.post ~= UnionDefine.POST_OFFICIAL) then
  765. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  766. end
  767. UnionDBLogic.setJoinLimit(union, needLv, needAgree)
  768. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  769. end
  770. function addUnionExpByItem(human, add)
  771. if not human.db.unionUuid or human.db.unionUuid == "" then
  772. return
  773. end
  774. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  775. if not union then return end
  776. addUnionExp(union, add)
  777. UnionDBLogic.updateUnionData(union)
  778. end
  779. -- 添加公会经验
  780. function addUnionExp(union, exp)
  781. local oldLv = union.lv
  782. union.exp = union.exp + exp
  783. while true do
  784. local config = UnionExcel.union[union.lv]
  785. if not config then break end
  786. if config.exp < 1 then -- 满级
  787. union.exp = 0
  788. break
  789. end
  790. if union.exp < config.exp then
  791. break
  792. end
  793. union.exp = union.exp - config.exp
  794. union.lv = union.lv + 1
  795. end
  796. end
  797. -- 公会申请查询
  798. function applyQuery(human)
  799. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  800. if not union then
  801. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  802. end
  803. sendApplyMsg(human, union)
  804. end
  805. -- 获取改名价格
  806. local function getChangeNameCost(union)
  807. if not union.nameTime then
  808. return 0 -- 首次改名免费
  809. end
  810. return CHANGENAME_NEED_ZUANSHI
  811. end
  812. -- 改名相关查询 例如改名价格
  813. function sendChangeNameQuery(human)
  814. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  815. if not union then
  816. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  817. end
  818. if union.presidentUuid ~= human.db._id then
  819. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  820. end
  821. local msgRet = Msg.gc.GC_UNION_CHANGE_NAME_QUERY
  822. msgRet.needCost = getChangeNameCost(union)
  823. Msg.send(msgRet, human.fd)
  824. end
  825. -- 公会改名
  826. function changeUnionName(human, name, bannerID)
  827. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  828. if not union then
  829. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  830. end
  831. if union.presidentUuid ~= human.db._id then
  832. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  833. end
  834. if union.nameTime and (os.time() - union.nameTime < UNION_CHANGE_CD) then
  835. local leftTime = math.ceil((UNION_CHANGE_CD - (os.time() - union.nameTime)) / 60)
  836. return Broadcast.sendErr(human, Util.format(Lang.UNION_CHANGE_CD_ERROR, leftTime))
  837. end
  838. if not checkUnionName(human, name) and name ~= union.name then
  839. return Broadcast.sendErr(human, Lang.CREATE_UNION_FAIL_NAME)
  840. end
  841. local needCost = getChangeNameCost(union)
  842. if not ObjHuman.checkRMB(human, needCost) then
  843. return
  844. end
  845. ObjHuman.decZuanshi(human, -needCost, "changeUnionName")
  846. UnionDBLogic.setUnionName(union, name)
  847. sendUnionChange(union)
  848. Broadcast.sendErr(human, Lang.UNION_CHANGE_NAME_SUCCESS)
  849. if union.joinAnotherWorldBattleTi then
  850. union = UnionDBLogic.getUnion(human.db.unionUuid)
  851. AnotherWorldBattleNS = AnotherWorldBattleNS or require("anotherWorldBattle.AnotherWorldBattleNS")
  852. AnotherWorldBattleNS.UnionChangeName(human.db.unionUuid, union.name)
  853. end
  854. end
  855. -- 公会改旗帜
  856. function changeUnionBannerID(human,bannerID)
  857. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  858. if not union then
  859. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  860. end
  861. if union.presidentUuid ~= human.db._id then
  862. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  863. end
  864. if not UnionExcel.banner[bannerID] then
  865. return Broadcast.sendErr(human, Lang.UNION_BANNER_ID_ILL)
  866. end
  867. UnionDBLogic.setBannerID(union, bannerID)
  868. sendUnionChange(union)
  869. Broadcast.sendErr(human, Lang.UNION_OPERATE_SUCCESS)
  870. end
  871. -- 发送邮件
  872. function sendUnionMail(human, content)
  873. local msgRet = Msg.gc.GC_UNION_MAIL
  874. msgRet.flag = 0
  875. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  876. if not union then
  877. Msg.send(msgRet, human.fd)
  878. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  879. end
  880. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  881. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  882. member.post ~= UnionDefine.POST_OFFICIAL) then
  883. Msg.send(msgRet, human.fd)
  884. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  885. end
  886. local mailCnt = UnionDBLogic.getMailCnt(union)
  887. if mailCnt >= UNION_ALL_MAIL_MAX_CNT then
  888. Msg.send(msgRet, human.fd)
  889. return Broadcast.sendErr(human, Lang.UNION_MAIL_CNT_MAX)
  890. end
  891. if content == "" or content ~= FilterUtil.filter(content) then
  892. return Broadcast.sendErr(human, Lang.MAIL_CONTENT_ERROR)
  893. end
  894. union.mailCnt = mailCnt + 1
  895. union.mailTs = union.mailTs or os.time()
  896. UnionDBLogic.updateUnionData(union)
  897. for uuid in pairs(union.member) do
  898. MailManager.add(MailManager.SYSTEM, uuid, Lang.UNION_MAIL, content, nil, human.db.name, human)
  899. end
  900. Broadcast.sendErr(human, Lang.UNION_SEND_SUCCESS)
  901. msgRet.flag = 1
  902. Msg.send(msgRet, human.fd)
  903. end
  904. function hasLog(human, union)
  905. -- local time = 0
  906. -- if human.db.roleDot and human.db.roleDot.unionLog then
  907. -- time = human.db.roleDot.unionLog
  908. -- end
  909. -- for i = 1, #union.logs do
  910. -- local log = union.logs[i]
  911. -- if log.time > time then
  912. -- return true
  913. -- end
  914. -- end
  915. return false
  916. end
  917. -- 公会日志
  918. function getLog(human,classify)
  919. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  920. if not union then
  921. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  922. end
  923. local msgRet = Msg.gc.GC_UNION_GET_LOG
  924. local cnt = 0
  925. for i = 1, #union.logs do
  926. local log = union.logs[i]
  927. if log.classify == classify or classify == 0 then
  928. cnt = cnt + 1
  929. msgRet.logMsg[cnt].logMsg = log.content
  930. end
  931. if cnt >= #msgRet.logMsg then
  932. break
  933. end
  934. end
  935. msgRet.logMsg[0] = cnt
  936. --Msg.trace(msgRet)
  937. Msg.send(msgRet, human.fd)
  938. human.db.roleDot = human.db.roleDot or {}
  939. human.db.roleDot.unionLog = os.time()
  940. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1005)
  941. end
  942. local function getRecruitCost(union)
  943. if not union.recruit then return 0 end
  944. return RECRUIT_COST_VALUE
  945. end
  946. -- 招募查询
  947. function sendRecruitQuery(human)
  948. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  949. if not union then
  950. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  951. end
  952. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  953. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  954. member.post ~= UnionDefine.POST_OFFICIAL) then
  955. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  956. end
  957. local msgRet = Msg.gc.GC_UNION_RECRUIT_QUERY
  958. msgRet.costValue = getRecruitCost(union)
  959. Msg.send(msgRet, human.fd)
  960. end
  961. -- 招募宣言
  962. function unionRecruit(human,str)
  963. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  964. if not union then
  965. return Broadcast.sendErr(human, Lang.UNION_PLAYER_IN_NO)
  966. end
  967. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  968. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  969. member.post ~= UnionDefine.POST_OFFICIAL) then
  970. return Broadcast.sendErr(human, Lang.UNION_HAVE_NO_POWER)
  971. end
  972. -- 检查花费
  973. local costValue = getRecruitCost(union)
  974. if not ObjHuman.checkRMB(human, costValue) then
  975. Msg.send(msgRet, human.fd)
  976. return
  977. end
  978. human.chatTime = human.chatTime or {}
  979. local chatTime = human.chatTime[ChatHandler.CHAT_TYPE_WORLD] or 0
  980. if chatTime + ChatHandler.CHAT_CD >= Timer.now then
  981. local cdLeftSec = math.ceil((ChatHandler.CHAT_CD - (Timer.now - chatTime))/1000)
  982. Broadcast.sendDown(human, Util.format(Lang.CHAT_TIME_SHORT, cdLeftSec))
  983. return
  984. end
  985. human.worldChatLast = human.worldChatLast or {}
  986. if human.worldChatLast[ChatHandler.CHAT_TYPE_WORLD] == str then
  987. Broadcast.sendDown(human, Lang.CHAT_TIME_DUPLICATE)
  988. return
  989. end
  990. ObjHuman.decZuanshi(human, -costValue, "union_recruit")
  991. if not union.recruit then
  992. union.recruit = 1
  993. UnionDBLogic.updateUnionData(union)
  994. end
  995. local dec = Util.format(Lang.UNION_RECRUIT_BC, union.lv, union.name)
  996. ChatUnion.chatUnion(human, ChatHandler.CHAT_TYPE_UNION, str)
  997. Broadcast.sendErr(human, Lang.UNION_SEND_SUCCESS)
  998. end
  999. -- 是否有红点 申请
  1000. function dot(human)
  1001. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1002. if union == nil then
  1003. return
  1004. end
  1005. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  1006. if not member then return end
  1007. if member.post ~= UnionDefine.POST_PRESIDENT and
  1008. member.post ~= UnionDefine.POST_OFFICIAL then
  1009. return
  1010. end
  1011. if union.apply == nil then
  1012. return
  1013. end
  1014. return next(union.apply) ~= nil
  1015. end
  1016. -- 用于显示红点
  1017. function isDot(human)
  1018. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1019. if union == nil then
  1020. -- 没加入公会 不显示红点
  1021. return
  1022. end
  1023. if dot(human) then
  1024. return true
  1025. end
  1026. -- if hasLog(human, union) then
  1027. -- return true
  1028. -- end
  1029. return nil
  1030. end
  1031. -- 0点后自动让位
  1032. function onHour()
  1033. PRESIDENT_CHECK_FLAG = true
  1034. end
  1035. -- 自动让位
  1036. PRESIDENT_AUTO_LIST = PRESIDENT_AUTO_LIST or nil -- 自动让位列表
  1037. PRESIDENT_CHECK_FLAG = PRESIDENT_CHECK_FLAG or nil -- 自动让位标记
  1038. function checkPresidentOffline()
  1039. if PRESIDENT_AUTO_LIST then return end
  1040. PRESIDENT_CHECK_FLAG = nil
  1041. PRESIDENT_AUTO_LIST = {}
  1042. local lastSevenDays = os.time() - UNION_AUTO_PRESIDENT_TIME
  1043. LuaMongo.find(DB.db_union, nil, FieldsUnionOffline)
  1044. local union2president = {}
  1045. while true do
  1046. local union = {}
  1047. if not LuaMongo.next(union) then
  1048. break
  1049. end
  1050. union2president[union._id] = union.presidentUuid
  1051. end
  1052. for unionUuid, presidentUuid in pairs(union2president) do
  1053. local db, online = RoleDBLogic.getDb(presidentUuid, FieldsRoleOffline)
  1054. if not db or (not online and (db.lastLogoutTime or 0) < lastSevenDays) then
  1055. PRESIDENT_AUTO_LIST[#PRESIDENT_AUTO_LIST + 1] = unionUuid
  1056. end
  1057. end
  1058. end
  1059. -- 是否可继承会长
  1060. local function canBeNewPresident(db, lastSevenDays, online)
  1061. if not db then return end
  1062. if not online and db.lastLogoutTime < lastSevenDays then
  1063. return
  1064. end
  1065. return true
  1066. end
  1067. -- 比较
  1068. local function cmpBeNewMember(db1, db2)
  1069. if not db1 then return end
  1070. if not db2 then return true end
  1071. if db1.lastLogoutTime ~= db2.lastLogoutTime then
  1072. return db1.lastLogoutTime > db2.lastLogoutTime
  1073. end
  1074. return db1.lv > db2.lv
  1075. end
  1076. -- 自动转让会长
  1077. function autoPresidentChange()
  1078. local unionUuid = PRESIDENT_AUTO_LIST[#PRESIDENT_AUTO_LIST]
  1079. if not unionUuid then
  1080. PRESIDENT_AUTO_LIST = nil
  1081. return
  1082. end
  1083. PRESIDENT_AUTO_LIST[#PRESIDENT_AUTO_LIST] = nil
  1084. local union = UnionDBLogic.getUnion(unionUuid)
  1085. if not union then return end
  1086. local lastSevenDays = os.time() - UNION_AUTO_PRESIDENT_TIME
  1087. local newdb = nil
  1088. for uuid, member in pairs(union.member) do -- 优先挑选管理
  1089. if member.post == UnionDefine.POST_OFFICIAL then
  1090. local db, online = RoleDBLogic.getDb(uuid, FieldsRoleOffline)
  1091. if canBeNewPresident(db, lastSevenDays, online) then
  1092. newdb = db
  1093. break
  1094. end
  1095. end
  1096. end
  1097. if not newdb then
  1098. for uuid, member in pairs(union.member) do -- 再挑普通成员
  1099. if member.post == UnionDefine.POST_MEMBER then
  1100. local db, online = RoleDBLogic.getDb(uuid, FieldsRoleOffline)
  1101. if canBeNewPresident(db, lastSevenDays, online) and
  1102. not cmpBeNewMember(newdb, db) then
  1103. newdb = db
  1104. break
  1105. end
  1106. end
  1107. end
  1108. end
  1109. if not newdb then return end
  1110. -- 设置新会长
  1111. UnionDBLogic.setPresident(union, newdb._id)
  1112. addUnionLog(union._id, 6, newdb.name,UnionDefine.UNION_LOG_CLASSIFY_BASE)
  1113. end
  1114. -- 定时检测
  1115. function onTimer()
  1116. if PRESIDENT_CHECK_FLAG then
  1117. checkPresidentOffline()
  1118. end
  1119. if PRESIDENT_AUTO_LIST then
  1120. autoPresidentChange()
  1121. end
  1122. end
  1123. -- 弹劾会长
  1124. function impeachPresident(human)
  1125. -- 判断玩家是否有工会
  1126. -- 判断玩家公会是否存在
  1127. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1128. if union == nil then
  1129. return
  1130. end
  1131. local db,online = RoleDBLogic.getDb(union.presidentUuid,FieldsRoleOffline)
  1132. local lastSevenDays = os.time() - UNION_IMPEACH_TIME
  1133. if online ~= nil or db.lastLogoutTime >= lastSevenDays then
  1134. return -- 弹劾失败
  1135. end
  1136. -- 扣除花费
  1137. local cost = UNION_OP_IMPEACH_PRESIDENT
  1138. if not ObjHuman.checkRMB(human, cost) then
  1139. return
  1140. end
  1141. ObjHuman.decZuanshi(human, -cost, "abs_answer")
  1142. -- 设置新会长
  1143. UnionDBLogic.setPresident(union, human.db._id)
  1144. addUnionLog(union._id, 14, human.db.name,UnionDefine.UNION_LOG_CLASSIFY_BASE,db.name)
  1145. sendMemberUpdate(union)
  1146. -- 发送系统消息
  1147. -- todo
  1148. -- 发送邮件
  1149. local mailID = MailDefine.MAIL_ID_UNION_CHANGE_PRE2
  1150. local title = MailExcel.mail[mailID].title
  1151. local content = Util.format(MailExcel.mail[mailID].content, human.db.name)
  1152. local senderName = MailExcel.mail[mailID].senderName
  1153. MailManager.add(MailManager.SYSTEM, db._id, title, content, nil, senderName)
  1154. end
  1155. function getUnionLogMemberDetail(human,type)
  1156. local msgRet = Msg.gc.GC_UNION_LOG_DETAIL_QUERY
  1157. if type == 2 then
  1158. return -- 临时
  1159. --getCanUnionWarMember(human.db.unionUuid,msgRet)
  1160. elseif type == 3 then
  1161. getCanBossMember(human.db.unionUuid,msgRet)
  1162. elseif type == 4 then
  1163. getCanDonateMember(human.db.unionUuid,msgRet)
  1164. end
  1165. Msg.send(msgRet,human.fd)
  1166. end
  1167. -- 获得还有挑战工会战次数的玩家
  1168. -- todo 等全谈写完工会战,再完成
  1169. function getCanUnionWarMember(unionUuid)
  1170. local union = UnionDBLogic.getUnion(unionUuid)
  1171. if not union then return end
  1172. for uuid, member in pairs(union.member) do
  1173. if uuid ~= union.presidentUuid then
  1174. if msgRet.memberDetail[0] >= #msgRet.memberDetail then
  1175. break
  1176. end
  1177. msgRet.memberDetail[0] = msgRet.memberDetail[0] + 1
  1178. fontUnionMember(msgRet.memberDetail[msgRet.memberDetail[0]], uuid, member)
  1179. end
  1180. end
  1181. end
  1182. -- 获得还有捐献次数的玩家
  1183. function getCanDonateMember(unionUuid,net)
  1184. local union = UnionDBLogic.getUnion(unionUuid)
  1185. if not union then return end
  1186. local maxCnt = 0
  1187. local leftCnt = 0
  1188. local len = 0
  1189. local maxLen = #net.memberDetail
  1190. for uuid, member in pairs(union.member) do
  1191. maxCnt= maxCnt + 1
  1192. local db = RoleDBLogic.getDb(uuid)
  1193. if db.todayDonate ~= 0 then
  1194. if len >= maxLen then
  1195. break
  1196. end
  1197. leftCnt = leftCnt + 1
  1198. len = len + 1
  1199. fontUnionMember(net.memberDetail[len], uuid, member)
  1200. end
  1201. end
  1202. net.memberDetail[0] = len
  1203. net.maxCnt = maxCnt
  1204. net.leftCnt = leftCnt
  1205. end
  1206. -- 获得还有挑战工会boss次数的玩家
  1207. -- todo 等全谈写完工会boss,再完成
  1208. function getCanBossMember(unionUuid,net)
  1209. local union = UnionDBLogic.getUnion(unionUuid)
  1210. if not union then return end
  1211. local maxCnt = 0
  1212. local leftCnt = 0
  1213. local len = 0
  1214. local maxLen = #net.memberDetail
  1215. for uuid, member in pairs(union.member) do
  1216. maxCnt= maxCnt + 1
  1217. local db = RoleDBLogic.getDb(uuid)
  1218. if db.ectypeCnt == nil or db.ectypeCnt < UnionEctypeLogic.FIGHT_MAX_CNT then
  1219. if len >= maxLen then
  1220. break
  1221. end
  1222. leftCnt = leftCnt + 1
  1223. len = len + 1
  1224. fontUnionMember(net.memberDetail[len], uuid, member)
  1225. end
  1226. end
  1227. net.memberDetail[0] = len
  1228. net.maxCnt = maxCnt
  1229. net.leftCnt = leftCnt
  1230. end
  1231. function isPost(human)
  1232. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1233. if union == nil then
  1234. return
  1235. end
  1236. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  1237. if not member then return end
  1238. if member.post == UnionDefine.POST_PRESIDENT or
  1239. member.post == UnionDefine.POST_OFFICIAL then
  1240. return true
  1241. end
  1242. return nil
  1243. end
  1244. function cancelApply(human,unionUuid)
  1245. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1001, true) then
  1246. return
  1247. end
  1248. if UnionDBLogic.getUnion(human.db.unionUuid) then
  1249. return
  1250. end
  1251. local union = UnionDBLogic.getUnion(unionUuid)
  1252. if not union then
  1253. return Broadcast.sendErr(human, Lang.UNION_IS_UNREAL)
  1254. end
  1255. UnionDBLogic.delApply(union, human.db._id)
  1256. for _,tpHuman in pairs(ObjHuman.onlineUuid) do
  1257. if isPost(tpHuman) and tpHuman.db.unionUuid == unionUuid then
  1258. RoleSystemLogic.onDot(tpHuman, RoleSystemDefine.ROLE_SYS_ID_1005)
  1259. end
  1260. end
  1261. end
  1262. -- 是否是会长/副会长
  1263. function IsTopTwoManager(human,unionId)
  1264. local union = UnionDBLogic.getUnion(unionId)
  1265. local member = UnionDBLogic.getUnionMember(union, human.db._id)
  1266. if not member or (member.post ~= UnionDefine.POST_PRESIDENT and
  1267. member.post ~= UnionDefine.POST_OFFICIAL) then
  1268. return false
  1269. end
  1270. return true
  1271. end
  1272. -- 获取公会数据
  1273. function GetUnionData(unionId, fields)
  1274. local union = UnionDBLogic.getUnion(unionId, fields)
  1275. return union
  1276. end
  1277. -- 成员战力有更新
  1278. function UpdateMemberPower(human)
  1279. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1280. UnionDBLogic.MemberPowerChange(union)
  1281. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  1282. if union.joinAnotherWorldBattleTi then
  1283. AnotherWorldBattleNS = AnotherWorldBattleNS or require("anotherWorldBattle.AnotherWorldBattleNS")
  1284. AnotherWorldBattleNS.UnionPowerChange(human.db.unionUuid, union.zhandouli)
  1285. end
  1286. end
  1287. -- 成功报名异界之战
  1288. function UpdateJoinAbTime(unionId, newTime)
  1289. local union = UnionDBLogic.getUnion(unionId)
  1290. if not union then
  1291. return
  1292. end
  1293. union.joinAnotherWorldBattleTi = newTime
  1294. UnionDBLogic.updateUnionData(union)
  1295. end