UnionLogic.lua 51 KB

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