UnionLogic.lua 48 KB

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