UnionLogic.lua 47 KB

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