UnionLogic.lua 47 KB

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