UnionLogic.lua 50 KB

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