PrivilegeCard.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. PrivilegeCardScript = {}
  2. -- 构造方法
  3. PrivilegeCardScript.__index = PrivilegeCardScript
  4. function PrivilegeCardScript:new()
  5. local instance = setmetatable({}, PrivilegeCardScript)
  6. -- 可以在这里添加初始化逻辑
  7. return instance
  8. end
  9. local privilegeItems = {
  10. "autopick", "autorecovery", "autopotion", "withstore", "withwarehouse", "tradenumber", "trade"
  11. }
  12. -- local privilegeCount = 0
  13. -- 用来维护每个人的提示过期信息,有没有被提示过
  14. -- local hadTipPrivileges = {}
  15. local privilegeCardScript = PrivilegeCardScript:new()
  16. function PrivilegeCardScript.toUsePrivilegeCard(actor, msgData)
  17. privilegeCardScript:usePrivilegeCard(actor, msgData)
  18. end
  19. -- 时间字符串转换为毫秒数
  20. function PrivilegeCardScript:timeStrToMilliseconds(timeStr)
  21. -- 如果字符串为空,则表示永久生效
  22. if timeStr == "" then
  23. return -1
  24. end
  25. local parts = {}
  26. for part in string.gmatch(timeStr, "%d+") do
  27. table.insert(parts, tonumber(part))
  28. end
  29. local length = #parts
  30. local days, hours, minutes, seconds = 0, 0, 0, 0
  31. if length >= 1 then days = parts[1] end
  32. if length >= 2 then hours = parts[2] end
  33. if length >= 3 then minutes = parts[3] end
  34. if length >= 4 then seconds = parts[4] end
  35. local milliseconds = (days * 24 + hours) * 60 * 60 * 1000 + minutes * 60 * 1000 + seconds * 1000
  36. return milliseconds
  37. end
  38. ---@使用特权卡
  39. function PrivilegeCardScript:usePrivilegeCard(actor, itemCfgId)
  40. -- local itemCfgId = itemCfgId or 30030122
  41. -- lg("使用特权卡", itemCfgId)
  42. -- local hasCount = getbagitemcountbyid(actor, tonumber(itemCfgId))
  43. -- lg("拥有特权卡数量", hasCount)
  44. -- if not (hasCount >= 1) then
  45. -- lg("背包里没有特权卡")
  46. -- return
  47. -- end
  48. local buff = self:getOnePrivilegeBuff(itemCfgId)
  49. -- lg("getOnePrivilegeBuff", buff)
  50. if buff ~= nil then
  51. self:handlePrivilegeData(actor, itemCfgId, buff)
  52. end
  53. end
  54. ---@获得特权卡数据
  55. function PrivilegeCardScript:getOnePrivilegeBuff(itemCfgId)
  56. local allBuffs = ConfigDataManager.getList("cfg_free_buff")
  57. if allBuffs == nil or next(allBuffs) == nil then
  58. -- lg("没有特权卡数据")
  59. return nil
  60. end
  61. for index, buff in ipairs(allBuffs) do
  62. local condition = buff["conditions"]
  63. local result = strcontains(condition, "checkUseItemTime")
  64. if tonumber(result) == 1 then
  65. local itemId = string.match(condition, "%d+")
  66. itemId = itemId and tonumber(itemId) or 0
  67. if itemId == 0 then
  68. goto next
  69. end
  70. if itemId == itemCfgId then
  71. return buff
  72. end
  73. end
  74. ::next::
  75. end
  76. return nil
  77. end
  78. ---@更新特权卡数据
  79. -- 这个方法中使用多个特权卡时间会累加,但是客户端的倒计时没有累加,后续有问题要修正
  80. function PrivilegeCardScript:handlePrivilegeData(actor, itemCfgId, buff)
  81. -- lg("更新特权卡数据", itemCfgId)
  82. local item = ConfigDataManager.getTable("cfg_item", "id", itemCfgId)
  83. if item == nil or next(item) == nil then
  84. return
  85. end
  86. local buffTime = buff["time"]
  87. buffTime = self:timeStrToMilliseconds(buffTime)
  88. -- lg("特权卡过期时间", buffTime)
  89. local buffId = item[1]["useparam"]
  90. local realBuff = ConfigDataManager.getTable("cfg_buff", "id", buffId)
  91. if realBuff ~= nil and next(realBuff) ~= nil then
  92. local realBuffTime = realBuff[1]["bufftotaltime"]
  93. -- lg("特权卡真实过期时间", realBuffTime)
  94. if realBuffTime ~= "" then
  95. buffTime = tonumber(realBuffTime)
  96. end
  97. end
  98. local currTime = getbaseinfo(actor, "now")
  99. local buffExpireTime = buffTime
  100. -- 测试用
  101. -- local buffExpireTime = 1000 * 10
  102. local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {}
  103. local cardExpireTime = myPrivilegeCardInfos[tonumber(itemCfgId)] or 0
  104. if cardExpireTime == 0 then
  105. -- 新增
  106. if buffExpireTime == -1 then
  107. myPrivilegeCardInfos[tonumber(itemCfgId)] = -1
  108. else
  109. myPrivilegeCardInfos[tonumber(itemCfgId)] = currTime + buffExpireTime
  110. end
  111. elseif cardExpireTime > 0 then
  112. -- 更新
  113. local overFlowTime = cardExpireTime - currTime
  114. if overFlowTime > 0 then
  115. -- 原来的没过期
  116. myPrivilegeCardInfos[tonumber(itemCfgId)] = currTime + buffExpireTime + overFlowTime
  117. else
  118. myPrivilegeCardInfos[tonumber(itemCfgId)] = currTime + buffExpireTime
  119. end
  120. end
  121. -- lg("更新特权卡数据", myPrivilegeCardInfos)
  122. setplaydef(actor, "T$PrivilegeCardInfos", myPrivilegeCardInfos)
  123. -- 这个buff如果有自动买药,就开启自动买药
  124. -- local autoBuyPotionPrivilege = buff["autopotion"]
  125. -- if autoBuyPotionPrivilege ~= nil and autoBuyPotionPrivilege ~= "" then
  126. -- local autoBuyPotion = getplaydef(actor, "T$autoBuyPotion") or {}
  127. -- autoBuyPotion["on"] = 1
  128. -- setplaydef(actor, "T$autoBuyPotion", autoBuyPotion)
  129. -- lg("开启自动买药",autoBuyPotion)
  130. -- end
  131. -- 复原提示过期信息
  132. local currRoleTips = getplaydef(actor,"T$hadTipPrivileges") or {}
  133. for _, privilegeItem in pairs(privilegeItems) do
  134. local hadPrivilege = buff[privilegeItem]
  135. if hadPrivilege ~= nil and hadPrivilege ~= "" then
  136. -- 变为这个特权过期信息没提示过
  137. currRoleTips[privilegeItem] = false
  138. end
  139. end
  140. setplaydef(actor,"T$hadTipPrivileges", currRoleTips)
  141. PrivilegeCardScript.getHasPrivileges(actor)
  142. end
  143. -- 检验表中用于特定功能的特权条件是否开启
  144. function PrivilegeCardScript.checkIsOpenCondition(actor, cfgTable, thing, switchFlag)
  145. -- lg("检验特定功能是否开启")
  146. cfgTable = cfgTable ~= "" and cfgTable or "cfg_free_buff"
  147. local configs = ConfigDataManager.getTable(cfgTable, thing, switchFlag)
  148. if configs == nil or next(configs) == nil then
  149. -- lg(configs, "没有开启特权项")
  150. return false
  151. end
  152. local tobeCheckedCondition = configs[1]["conditions"]
  153. local name = configs[1]["name"]
  154. if tobeCheckedCondition ~= nil and tobeCheckedCondition ~= "" then
  155. -- 改走脚本验证
  156. -- local resullt = checkcondition(actor, tobeCheckedCondition)
  157. -- lg("检验结果:", resullt)
  158. -- if resullt ~= 1 then
  159. -- lg(resullt, name, "没有开启特权")
  160. -- return false
  161. -- end
  162. local itemId = string.match(tobeCheckedCondition, "%d+")
  163. itemId = itemId and tonumber(itemId) or 0
  164. if itemId == 0 then
  165. -- lg("没有可以检验的特权卡数据")
  166. return false
  167. end
  168. local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {}
  169. -- lg("检验特权卡数据", myPrivilegeCardInfos)
  170. local cardExpireTime = myPrivilegeCardInfos[tonumber(itemId)] or 0
  171. -- lg("检验特权卡数据", cardExpireTime)
  172. if cardExpireTime == 0 then
  173. -- lg(name, "没有开启特权")
  174. return false
  175. elseif cardExpireTime == -1 then
  176. return true
  177. else
  178. local currTime = getbaseinfo(actor, "now")
  179. -- lg("检验特权卡数据", cardExpireTime, currTime)
  180. if cardExpireTime < currTime then
  181. -- lg(name, "特权已过期")
  182. return false
  183. end
  184. end
  185. end
  186. return true
  187. end
  188. -- 使用物品后判断是不是特权卡
  189. function PrivilegeCardScript.setPrivilegeCardInfos(actor, itemCfgId, count)
  190. if itemCfgId == nil or tonumber(itemCfgId) <= 0 then
  191. return
  192. end
  193. privilegeCardScript:usePrivilegeCard(actor, itemCfgId)
  194. end
  195. function PrivilegeCardScript.getHasPrivileges(actor)
  196. local privileges = {}
  197. local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {}
  198. if next(myPrivilegeCardInfos) == nil then
  199. sendluamsg(actor, LuaMessageIdToClient.RES_PRIVILEGE_RESULT, privileges)
  200. return
  201. end
  202. for itemId, buffExpireTime in pairs(myPrivilegeCardInfos) do
  203. local now = getbaseinfo(actor, "now")
  204. --过期跳过
  205. if now > buffExpireTime then
  206. goto next
  207. end
  208. local buff = privilegeCardScript:getOnePrivilegeBuff(itemId)
  209. if buff == nil then
  210. goto next
  211. end
  212. for i, v in ipairs(privilegeItems) do
  213. local state = buff[v]
  214. if state ~= "" and state ~= nil then
  215. privileges[v] = state
  216. end
  217. end
  218. ::next::
  219. end
  220. -- lg("privileges", privileges)
  221. sendluamsg(actor, LuaMessageIdToClient.RES_PRIVILEGE_RESULT, privileges)
  222. end
  223. -- 检查卡片id判断是否有使用了该特权卡
  224. function PrivilegeCardScript.checkPrivilegeCardById(actor, cardId)
  225. local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {}
  226. if next(myPrivilegeCardInfos) == nil then
  227. return false
  228. end
  229. local condition = ConfigDataManager.getTableValue("cfg_free_buff", "conditions", "id", cardId)
  230. if condition == "" then
  231. return false
  232. end
  233. local itemId = string.match(condition, "%d+")
  234. itemId = itemId and tonumber(itemId) or 0
  235. if itemId == 0 then
  236. return false
  237. end
  238. return myPrivilegeCardInfos[itemId] ~= nil
  239. end
  240. -- 可以改为登录时候和使用特权卡时检查,不足一天的时候开启定时器每秒检测
  241. function PrivilegeCardScript.checkPrivilegeExpire(actor)
  242. local myAllCradStates = {}
  243. local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {}
  244. if next(myPrivilegeCardInfos) ~= nil then
  245. -- 找出所有卡的特权状态,包括有效的和过期的,因为不能凭借某个特权卡过期了就认为它有的特权过期了
  246. -- 有可能存在没过期的卡也有这个权限
  247. for itemId, buffExpireTime in pairs(myPrivilegeCardInfos) do
  248. local now = getbaseinfo(actor, "now")
  249. local buff = privilegeCardScript:getOnePrivilegeBuff(itemId)
  250. if buff == nil then
  251. goto nextBuff
  252. end
  253. local currCradStates = {}
  254. for i, privilegeItem in ipairs(privilegeItems) do
  255. local state = buff[privilegeItem]
  256. if state ~= "" and state ~= nil then
  257. --这个卡有这个特权
  258. if buffExpireTime ~= -1 and now > buffExpireTime then
  259. currCradStates[privilegeItem] = 0
  260. else
  261. currCradStates[privilegeItem] = 1
  262. end
  263. end
  264. end
  265. myAllCradStates[itemId] = currCradStates
  266. ::nextBuff::
  267. end
  268. end
  269. -- 所有使用过卡的特权卡的状态都有了,包括有效的和过期的
  270. -- lg("myAllCradStates", myAllCradStates)
  271. -- 测试用
  272. -- myAllCradStates = {
  273. -- [30030122] = {
  274. -- autopick = 0,
  275. -- withstore = 1,
  276. -- autopotion = 0
  277. -- },
  278. -- [30030121] = {
  279. -- withstore = 0,
  280. -- autorecovery = 0,
  281. -- withwarehouse = 0,
  282. -- }
  283. -- }
  284. local myExpirePrivileges = {}
  285. for key, cardState in pairs(myAllCradStates) do
  286. -- 遍历 cardState 中的所有属性
  287. for privilegeKey, value in pairs(cardState) do
  288. if value == 0 then
  289. -- 为0说明可能过期了,看看有没有其他卡里也有这个特权而且没过期 [[检查其他所有键对应的属性值中是否有这个属性或属性值为 1]]
  290. local foundOtherNoExpired = false
  291. for otherKey, otherCardState in pairs(myAllCradStates) do
  292. if otherKey ~= key and otherCardState[privilegeKey] == 1 then
  293. foundOtherNoExpired = true
  294. break
  295. end
  296. end
  297. -- 如果没有找到其他键对应的属性值中有这个属性或属性值为1,说明这个特权没有其他卡有,或者也过期了,则添加到结果表
  298. if not foundOtherNoExpired then
  299. myExpirePrivileges[privilegeKey] = 0
  300. end
  301. end
  302. end
  303. end
  304. local currRoleTips = getplaydef(actor,"T$hadTipPrivileges") or {}
  305. -- lg("myExpirePrivileges", myExpirePrivileges)
  306. -- lg("currRoleTips", currRoleTips)
  307. if next(myExpirePrivileges) ~= nil then
  308. for privilegeKey, value in pairs(myExpirePrivileges) do
  309. -- 这个特权失效之前没有发送过,或者发送过但是使用特权卡之后复原了,就发送
  310. if not currRoleTips[privilegeKey] then
  311. -- lg("发送过期特权提示", actor, privilegeKey)
  312. sendluamsg(actor, LuaMessageIdToClient.PRIVILEGE_INFO_TIP, { [privilegeKey] = value })
  313. -- 记录发送信息
  314. currRoleTips[privilegeKey] = true
  315. end
  316. end
  317. -- lg("afterRoleTips", currRoleTips)
  318. end
  319. setplaydef(actor,"T$hadTipPrivileges", currRoleTips)
  320. end