Union.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. Union = {}
  2. local this = {}
  3. ---@class UnionData
  4. ---@field createtime number 创建时间
  5. ---@field memberinfos table<number, UnionMemberInfos> 成员列表
  6. ---@field leaderid number 盟主id
  7. ---@field unionid number 战盟id
  8. ---@field leaderontime number 盟主上位时间
  9. ---@field unionname string 战盟名称
  10. ---@field unionlevel number 战盟等级
  11. ---@field totaldevote number 战盟总贡献
  12. ---@field unionarmband number 战盟臂章ID
  13. ---@field ischangename number 是否改名 0 未改名 1 已改名
  14. ---@field unionexp number 战盟经验
  15. ---@field num number 当前人数
  16. ---@field maxnum number 最大人数
  17. ---@field enemyunionids table<number> 敌对战盟
  18. ---@field announcement string 战盟公告
  19. ---@field unioncapital number 战盟资金
  20. ---@field unioncachet number 战盟声望
  21. ---@class UnionMemberInfos
  22. ---@field rid number 角色id
  23. ---@field name string 角色名字
  24. ---@field level number 角色等级
  25. ---@field career number 职业
  26. ---@field position number 职位
  27. ---@field devote number 贡献值
  28. ---@field isonline number 是否在线 0 离线 1 在线
  29. ---@field jointime number 加入时间
  30. ---@field curweekactivetimes number 当前周活跃度
  31. ---@field quitlinetime number 下线时间
  32. function Union.sendLeaderOnlineStateMsg(actor)
  33. this.sendLeaderOnlineStateMsg(actor)
  34. end
  35. function this.sendLeaderOnlineStateMsg(actor)
  36. local union_id = tostring(getbaseinfo(actor, "unionid"))
  37. if string.tonumber(union_id) < 1 then
  38. return
  39. end
  40. local union_data = getunioninfo(actor)
  41. if table.isEmpty(union_data) then
  42. this.jprint(" ---- 发送盟主状态时 获取战盟数据失败!----")
  43. return
  44. end
  45. local rid = tostring(getbaseinfo(actor, "id"))
  46. local leader_id = tostring(union_data.leaderid)
  47. if tonumber(rid) ~= tonumber(leader_id) then
  48. return
  49. end
  50. local online_state = getbaseinfo(actor, "onlinestate")
  51. if online_state == "" or tonumber(online_state) == 0 then
  52. online_state = 0
  53. else
  54. online_state = 1
  55. end
  56. for _, member in pairs(union_data.memberinfos) do
  57. local member_actor = getactor(member.rid)
  58. sendluamsg(member_actor, LuaMessageIdToClient.RES_UNION_LEADER_ON_LINE_STATE, union_data)
  59. end
  60. this.jprint("------- 发送盟主在线状态 ------")
  61. end
  62. ------------------------------------ ↓战盟活动↓ ----------------------------------
  63. Union.UnionActivity = {}
  64. ---@class Union.UnionActivity
  65. ---@field activityid number 活动id
  66. ---@field open string 是否开启
  67. -- 获取战盟活动列表
  68. function Union.UnionActivity.getUnionActivity(actor)
  69. local data = this.getUnionActivity(actor)
  70. sendluamsg(actor, LuaMessageIdToClient.RES_UNION_ACTIVITY_DATA, data)
  71. end
  72. function this.getUnionActivity(actor)
  73. local data = getallactivities(actor)
  74. if table.isEmpty(data) then
  75. this.jprint("获取活动列表失败")
  76. return
  77. end
  78. local union_active = {}
  79. for _, value in pairs(data) do
  80. local type = ConfigDataManager.getTableValue("cfg_activity_rule", "initType", "id", value.activityid)
  81. if tonumber(type) == UnionConst.UNION_ACTIVITY_INITTYPE then
  82. ---@type Union.UnionActivity
  83. local activity = {
  84. activityid = value.activityid,
  85. open = value.open
  86. }
  87. table.insert(union_active, activity)
  88. end
  89. end
  90. return union_active
  91. end
  92. ------------------------------------- ↑战盟活动↑ -----------------------------------
  93. Union.DefaultUnion = {}
  94. --- 服务器启动后创建默认战盟 全局数据
  95. this.GLOBAL_DEFAULT_UNION_CREATE_DATA_KEY = PlayerDefKey.UNION.GLOBAL_DEFAULT_UNION_CREATE_DATA_KEY
  96. this.DEFAULT_UNION_GLOBAL_ID = 1601
  97. function Union.DefaultUnion.initDefaultUnion(is_gm)
  98. local serverType = getbaseinfo("servertype")
  99. if serverType == 2 then
  100. -- 跨服服务器不执行
  101. return
  102. end
  103. local summary_list = getallunionsummary()
  104. local is_create = getsysvar(this.GLOBAL_DEFAULT_UNION_CREATE_DATA_KEY)
  105. this.debug("is_create", is_create, table.notEmpty(summary_list), is_create or table.notEmpty(summary_list))
  106. if (is_create or table.notEmpty(summary_list)) and not is_gm then
  107. return
  108. end
  109. local value_str = ConfigDataManager.getTableValue("cfg_global", "value", "id", this.DEFAULT_UNION_GLOBAL_ID)
  110. if string.isNullOrEmpty(value_str) then
  111. return
  112. end
  113. local list = ConfigDataManager.getList("cfg_unionname")
  114. if table.isEmpty(list) then
  115. return
  116. end
  117. local create_info = string.split(value_str, "#")
  118. local union_count = tonumber(create_info[1])
  119. local curr_count = 0
  120. local usedIndexes = {}
  121. for idx, level in pairs(create_info) do
  122. if idx ~= 1 and curr_count < union_count then
  123. -- 随机一个战盟名称和战盟臂章
  124. local log = math.random(1, 5)
  125. local randomIndex
  126. repeat
  127. randomIndex = math.random(1, #list)
  128. until not usedIndexes[randomIndex]
  129. --创建战盟
  130. local config = list[randomIndex]
  131. local union_id = systemcreateunion(config.name, log, level)
  132. local union = getunioninfo(union_id)
  133. -- 标记已使用的索引
  134. usedIndexes[randomIndex] = true
  135. this.debug("创建默认战盟:", union, union_id, log)
  136. end
  137. end
  138. setsysvar(this.GLOBAL_DEFAULT_UNION_CREATE_DATA_KEY, true)
  139. end
  140. function Union.combineglobalvar(varName)
  141. setsysvar(varName, true)
  142. end
  143. ---------------------------- 日志打印 -----------------------------
  144. this.log_open = false
  145. function this.jprint(param)
  146. if not this.log_open then
  147. return
  148. end
  149. if param == nil then
  150. param = "error! 输出内容为空. nil"
  151. end
  152. jprint(param)
  153. end
  154. function this.loginfo(actor, param)
  155. if not this.log_open then
  156. return
  157. end
  158. if param == nil then
  159. param = "error! 日志输出内容为空. nil"
  160. end
  161. jprint(param)
  162. info(actor, param)
  163. end
  164. function this.debug(...)
  165. if not this.log_open then
  166. return
  167. end
  168. gameDebug.print(...)
  169. end
  170. --- 修改战盟名称
  171. ---@param actor table 玩家对象
  172. ---@param msgData table 战盟名称
  173. function Union.changeUnionName(actor, msgData)
  174. local unionName = msgData["unionName"]
  175. local unionInfo = getunioninfo(actor)
  176. local leaderId = unionInfo.leaderid
  177. if tostring(leaderId) ~= actor:toString() then
  178. noticeTip.noticeinfo(actor, StringIdConst.TEXT454)
  179. return
  180. end
  181. -- 如果首次更改战盟名称不消耗道具
  182. if unionInfo.ischangename and unionInfo.ischangename == 1 then
  183. -- 如果背包中有战盟改名卡,则先消耗战盟改名卡
  184. local cfgCost = ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.CHANGE_NAME_CARD_COST)
  185. local split = string.split(cfgCost, "|")
  186. local tmps = string.split(split[1], "#")
  187. local costItemId = tmps[1]
  188. local costCount = tmps[2]
  189. if not Bag.checkItem(actor, costItemId, costCount) then
  190. local value = ConfigDataManager.getTableValue("cfg_global", "value", "id", GlobalConfigId.CHANGE_NAME_COST_WITHOUT_CARD)
  191. local split2 = string.split(value, "|")
  192. local tmp2 = string.split(split2[1], "#")
  193. costItemId = tmp2[1]
  194. costCount = tmp2[2]
  195. if not Bag.checkItem(actor, costItemId, costCount) then
  196. noticeTip.noticeinfo(actor, StringIdConst.TEXT346)
  197. return
  198. end
  199. end
  200. if not checkunionname(actor, unionName) then
  201. jprint("战盟名称不合法或重复 actor:" .. actor:toString() .. "unionName:" .. unionName)
  202. return
  203. end
  204. -- 从背包中移除指定数量的道具
  205. removeitemfrombag(actor, costItemId, costCount,0,9999,'战盟改名')
  206. else
  207. if not checkunionname(actor, unionName) then
  208. jprint("战盟名称不合法或重复 actor:" .. actor:toString() .. "unionName:" .. unionName)
  209. return
  210. end
  211. end
  212. -- 修改战盟名称
  213. changeunionname(actor, unionName)
  214. end
  215. this.gm_open = true
  216. function l_default_create_system_union(_, is_gm)
  217. if not this.gm_open then
  218. return
  219. end
  220. Union.DefaultUnion.initDefaultUnion(is_gm)
  221. end