MonthCard.lua 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. --月卡相关
  2. MonthCard = {}
  3. MonthCard.monthcardtype = {
  4. --永久限购
  5. NEVER = 1,
  6. --每月
  7. MONTH = 2,
  8. --每周
  9. WEEK = 3,
  10. --每日
  11. DAY = 4
  12. }
  13. local function _rechargeType()
  14. return "2"
  15. end
  16. local function _playerDbKey()
  17. return "T$monthcard"
  18. end
  19. -- 关闭月卡功能
  20. local card_is_close = true
  21. --function MonthCard.checkitemismonthcard(itemcfgid)
  22. -- local config = ConfigDataManager.getTable("cfg_monthlyPass", "id", itemcfgid)
  23. -- if config == nil or table.count(config) == 0 then
  24. -- return false
  25. -- end
  26. -- return true
  27. --end
  28. --月卡限购
  29. function MonthCard.limitCount(actor, type)
  30. local monthcard = getplaydef(actor, _playerDbKey())
  31. if monthcard == nil then
  32. return
  33. end
  34. local change_ids = {}
  35. for goodsId, hascard in pairs(monthcard) do
  36. if type(info) == "table" then
  37. local limit_type = ConfigDataManager.getTableValue("cfg_monthlypass", "limittype", "id", goodsId)
  38. if tonumber(type) == tonumber(limit_type) then
  39. hascard.buycount = 0
  40. table.insert(change_ids, goodsId)
  41. end
  42. end
  43. end
  44. setplaydef(actor, _playerDbKey(), monthcard)
  45. for _, id in pairs(change_ids) do
  46. local result = table.valueConvertToString(monthcard[id])
  47. --给客户端发月卡信息
  48. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_CHANGE_INFO, result)
  49. end
  50. end
  51. --充值月卡
  52. function MonthCard.limitbuycount(actor, cfg, count, amount, ext, outRewards)
  53. if card_is_close then
  54. return
  55. end
  56. local goodsId = cfg.parameter
  57. local monthcard = getplaydef(actor, _playerDbKey())
  58. if monthcard == nil then
  59. monthcard = {}
  60. monthcard.todayreceivethreetime = false
  61. monthcard.receivetime = 0
  62. end
  63. local hascard = monthcard[tostring(goodsId)]
  64. if hascard == nil then
  65. hascard = {}
  66. hascard.cfgId = goodsId
  67. hascard.buycount = 0
  68. hascard.vaildTime = 0
  69. hascard.isopen = false
  70. hascard.isfirstbuy = true
  71. end
  72. hascard.buycount = hascard.buycount + tonumber(count)
  73. monthcard[tostring(goodsId)] = hascard
  74. hascard.isfirstbuy = false
  75. setplaydef(actor, _playerDbKey(), monthcard)
  76. local result = table.valueConvertToString(hascard)
  77. --给客户端发月卡信息
  78. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_CHANGE_INFO, result)
  79. end
  80. -- 月卡时间增加
  81. function MonthCard.addTime(actor, day)
  82. if card_is_close then
  83. return
  84. end
  85. local monthcard = getplaydef(actor, _playerDbKey())
  86. local hascard = nil
  87. --更新玩家月卡信息
  88. local mcfgId = getmonthcardcfgid(actor)
  89. if mcfgId == 0 then
  90. mcfgId = getdaycardcfgid(actor)
  91. end
  92. if mcfgId ~= 0 then
  93. hascard = monthcard[tostring(mcfgId)]
  94. end
  95. if not hascard then
  96. for _, info in pairs(monthcard) do
  97. if type(info) == "table" then
  98. hascard = info
  99. local now = getbaseinfo("now")
  100. hascard.vaildTime = now
  101. hascard.isopen = true
  102. end
  103. end
  104. end
  105. hascard.vaildTime = hascard.vaildTime + tonumber(day) * 24 * 60 * 60 * 1000
  106. monthcard[tostring(hascard.cfgId)] = hascard
  107. setplaydef(actor, _playerDbKey(), monthcard)
  108. local result = table.valueConvertToString(hascard)
  109. --给客户端发月卡信息
  110. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_CHANGE_INFO, result)
  111. end
  112. --使用月卡
  113. function MonthCard.usemonthcard(actor, cfgId, count)
  114. if card_is_close then
  115. return
  116. end
  117. local item = ConfigDataManager.getTable("cfg_item", "id", cfgId)
  118. if item == nil or next(item) == nil then
  119. return
  120. end
  121. if tonumber(item[1].type) ~= 7 then
  122. return
  123. end
  124. local subtype = item[1].subtype
  125. local config = ConfigDataManager.getTable("cfg_monthlyPass", "order", subtype)
  126. if config == nil or next(config) == nil then
  127. --不是月卡
  128. return
  129. end
  130. cfgId = config[1].id
  131. local now = getbaseinfo("now")
  132. local receThreeTime = tonumber(item[1].useparam) == 1 and true or false
  133. local monthcard = getplaydef(actor, _playerDbKey())
  134. if monthcard == nil then
  135. monthcard = {}
  136. monthcard.todayreceivethreetime = false
  137. monthcard.receivetime = 0
  138. end
  139. local hascard = monthcard[tostring(cfgId)]
  140. if hascard == nil then
  141. hascard = {}
  142. hascard.isfirstbuy = true
  143. hascard.isopen = false
  144. hascard.vaildTime = 0
  145. hascard.buycount = 0
  146. hascard.cfgId = cfgId
  147. end
  148. --先判断是月卡还是周卡
  149. local order = tonumber(subtype)
  150. local time = tonumber(item[1].useparam2) * 1000 * tonumber(count)
  151. --月卡,检查玩家当前是否有月卡、日卡
  152. local bool = checkactorhasmonthcard(actor)
  153. local bools = checkactorhasdaycard(actor)
  154. if order == 1 then
  155. if bool then
  156. --直接加时间就行,其他不变
  157. hascard.vaildTime = hascard.vaildTime + time
  158. --校验三倍时间是否发放
  159. if receThreeTime then
  160. if hascard.isfirstbuy then
  161. TripleIncome.AddRemainTime(actor, tonumber(config[1].firstincome) * 60 * 60)
  162. monthcard.receivetime = now
  163. else
  164. if not TimeUtil.isSameDay(math.floor(monthcard.receivetime / 1000), math.floor(now / 1000)) then
  165. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  166. monthcard.receivetime = now
  167. end
  168. end
  169. monthcard.todayreceivethreetime = true
  170. hascard.isfirstbuy = false
  171. end
  172. else
  173. if bools then
  174. --如果有日卡,算日卡的剩余时间,转化成月卡
  175. local daytime = getdaycardtime(actor)
  176. --先关闭日卡,再加到月卡上
  177. for _, info in pairs(monthcard) do
  178. if type(info) == "table" then
  179. if info.isopen then
  180. info.isopen = false
  181. end
  182. end
  183. end
  184. hascard.vaildTime = daytime + time
  185. hascard.isopen = true
  186. if receThreeTime then
  187. if hascard.isfirstbuy then
  188. --发放额外三倍时间
  189. TripleIncome.AddRemainTime(actor, tonumber(config[1].firstincome) * 60 * 60)
  190. monthcard.todayreceivethreetime = true
  191. monthcard.receivetime = now
  192. else
  193. --校验三倍时间是否发放
  194. if not TimeUtil.isSameDay(math.floor(monthcard.receivetime / 1000), math.floor(now / 1000)) then
  195. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  196. monthcard.todayreceivethreetime = true
  197. monthcard.receivetime = now
  198. end
  199. end
  200. hascard.isfirstbuy = false
  201. end
  202. else
  203. monthcard = setmonthcardinfo(actor, hascard, monthcard, now, time, config, cfgId, receThreeTime)
  204. end
  205. end
  206. end
  207. if order == 2 then
  208. --日卡
  209. if bool then
  210. --时间直接加到月卡上
  211. for _, info in pairs(monthcard) do
  212. if type(info) == "table" then
  213. if info.isopen then
  214. info.vaildTime = info.vaildTime + time
  215. end
  216. end
  217. end
  218. if receThreeTime then
  219. if hascard.isfirstbuy then
  220. --发放额外三倍时间
  221. TripleIncome.AddRemainTime(actor, tonumber(config[1].firstincome) * 60 * 60)
  222. monthcard.receivetime = now
  223. else
  224. --校验三倍时间是否发放
  225. if not TimeUtil.isSameDay(math.floor(monthcard.receivetime / 1000), math.floor(now / 1000)) then
  226. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  227. monthcard.receivetime = now
  228. end
  229. end
  230. monthcard.todayreceivethreetime = true
  231. hascard.isfirstbuy = false
  232. end
  233. else
  234. if bools then
  235. --直接加时间就行
  236. hascard.vaildTime = hascard.vaildTime + time
  237. if receThreeTime then
  238. if hascard.isfirstbuy then
  239. --发放额外三倍时间
  240. TripleIncome.AddRemainTime(actor, tonumber(config[1].firstincome) * 60 * 60)
  241. monthcard.receivetime = now
  242. else
  243. if not TimeUtil.isSameDay(math.floor(monthcard.receivetime / 1000), math.floor(now / 1000)) then
  244. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  245. monthcard.receivetime = now
  246. end
  247. end
  248. monthcard.todayreceivethreetime = true
  249. hascard.isfirstbuy = false
  250. end
  251. else
  252. monthcard = setmonthcardinfo(actor, hascard, monthcard, now, time, config, cfgId, receThreeTime)
  253. end
  254. end
  255. end
  256. -- monthcard.todayreceivethreetime = true --今日是否领取三倍时间
  257. --更新玩家月卡信息
  258. setplaydef(actor, _playerDbKey(), monthcard)
  259. local mcfgId = getmonthcardcfgid(actor)
  260. if mcfgId == 0 then
  261. mcfgId = getdaycardcfgid(actor)
  262. if mcfgId == 0 then
  263. hascard = {}
  264. end
  265. else
  266. hascard = monthcard[tostring(mcfgId)]
  267. end
  268. local result = table.valueConvertToString(hascard)
  269. --给客户端发月卡信息
  270. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_CHANGE_INFO, result)
  271. end
  272. function setmonthcardinfo(actor, hascard, monthcard, now, time, config, cfgId, receThreeTime)
  273. if hascard == nil then
  274. hascard = {}
  275. end
  276. if hascard.vaildTime == 0 then
  277. hascard.cfgId = cfgId
  278. hascard.vaildTime = now + time
  279. --首次获得赠送三倍时间
  280. if receThreeTime then
  281. TripleIncome.AddRemainTime(actor, tonumber(config[1].firstincome) * 60 * 60)
  282. monthcard.receivetime = now
  283. monthcard.todayreceivethreetime = true
  284. hascard.isfirstbuy = false -- 是否是首次购买
  285. end
  286. hascard.isopen = true -- 是否开始生效,true 生效,false 不生效 --用于校验过期时间
  287. else
  288. if hascard.vaildTime < now then
  289. hascard.vaildTime = now + time
  290. if receThreeTime then
  291. if hascard.isfirstbuy then
  292. TripleIncome.AddRemainTime(actor, tonumber(config[1].firstincome) * 60 * 60)
  293. monthcard.receivetime = now
  294. else
  295. --过期了再获得,同时发放每日三倍时间
  296. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  297. monthcard.receivetime = now
  298. end
  299. monthcard.todayreceivethreetime = true
  300. hascard.isfirstbuy = false
  301. end
  302. hascard.isopen = true
  303. else
  304. --没过期,时间叠加
  305. hascard.vaildTime = hascard.vaildTime + time
  306. hascard.isopen = true
  307. if receThreeTime then
  308. --校验三倍时间是否发放
  309. if hascard.isfirstbuy then
  310. TripleIncome.AddRemainTime(actor, tonumber(config[1].firstincome) * 60 * 60)
  311. monthcard.receivetime = now
  312. else
  313. if not TimeUtil.isSameDay(math.floor(monthcard.receivetime / 1000), math.floor(now / 1000)) then
  314. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  315. monthcard.receivetime = now
  316. end
  317. end
  318. hascard.isfirstbuy = false
  319. monthcard.todayreceivethreetime = true
  320. end
  321. end
  322. end
  323. monthcard[tostring(cfgId)] = hascard
  324. return monthcard
  325. end
  326. --检查玩家是否有月卡
  327. function checkactorhasmonthcard(actor)
  328. if card_is_close then
  329. return false
  330. end
  331. local monthcard = getplaydef(actor, _playerDbKey())
  332. if monthcard == nil then
  333. return false
  334. end
  335. for _, info in pairs(monthcard) do
  336. if type(info) == "table" then
  337. if info.isopen then
  338. local config = ConfigDataManager.getTable("cfg_monthlyPass", "id", info.cfgId)
  339. if config == nil then
  340. return false
  341. end
  342. if tonumber(config[1].order) == 1 then
  343. return true
  344. end
  345. end
  346. end
  347. end
  348. return false
  349. end
  350. --检查玩家是否有日卡
  351. function checkactorhasdaycard(actor)
  352. if card_is_close then
  353. return false
  354. end
  355. local monthcard = getplaydef(actor, _playerDbKey())
  356. if monthcard == nil then
  357. return false
  358. end
  359. for _, info in pairs(monthcard) do
  360. if type(info) == "table" then
  361. if info.isopen then
  362. local config = ConfigDataManager.getTable("cfg_monthlyPass", "id", info.cfgId)
  363. if config == nil then
  364. return false
  365. end
  366. if tonumber(config[1].order) == 2 then
  367. return true
  368. end
  369. end
  370. end
  371. end
  372. return false
  373. end
  374. --算日卡的剩余时间
  375. function getdaycardtime(actor)
  376. if card_is_close then
  377. return 0
  378. end
  379. local monthcard = getplaydef(actor, _playerDbKey())
  380. if monthcard == nil then
  381. return 0
  382. end
  383. for _, info in pairs(monthcard) do
  384. if type(info) == "table" then
  385. if info.isopen then
  386. local config = ConfigDataManager.getTable("cfg_monthlyPass", "id", info.cfgId)
  387. if config == nil then
  388. return 0
  389. end
  390. if tonumber(config[1].order) == 2 then
  391. return info.vaildTime
  392. end
  393. end
  394. end
  395. end
  396. return 0
  397. end
  398. --获取日卡cfgId
  399. function getdaycardcfgid(actor)
  400. local monthcard = getplaydef(actor, _playerDbKey())
  401. if monthcard == nil then
  402. return 0
  403. end
  404. for _, info in pairs(monthcard) do
  405. if type(info) == "table" then
  406. if info.isopen then
  407. local config = ConfigDataManager.getTable("cfg_monthlyPass", "id", info.cfgId)
  408. if config == nil then
  409. return 0
  410. end
  411. if tonumber(config[1].order) == 2 then
  412. return info.cfgId
  413. end
  414. end
  415. end
  416. end
  417. return 0
  418. end
  419. --获取月卡cfgId
  420. function getmonthcardcfgid(actor)
  421. local monthcard = getplaydef(actor, _playerDbKey())
  422. if monthcard == nil then
  423. return 0
  424. end
  425. for _, info in pairs(monthcard) do
  426. if type(info) == "table" then
  427. if info.isopen then
  428. local config = ConfigDataManager.getTable("cfg_monthlyPass", "id", info.cfgId)
  429. if config == nil then
  430. return 0
  431. end
  432. if tonumber(config[1].order) == 1 then
  433. return info.cfgId
  434. end
  435. end
  436. end
  437. end
  438. return 0
  439. end
  440. --算月卡的剩余时间
  441. function getmonthcardtime(actor)
  442. if card_is_close then
  443. return 0
  444. end
  445. local monthcard = getplaydef(actor, _playerDbKey())
  446. if monthcard == nil then
  447. return 0
  448. end
  449. for _, info in pairs(monthcard) do
  450. if type(info) == "table" then
  451. if info.isopen then
  452. local config = ConfigDataManager.getTable("cfg_monthlyPass", "id", info.cfgId)
  453. if config == nil then
  454. return 0
  455. end
  456. if tonumber(config[1].order) == 1 then
  457. return info.vaildTime
  458. end
  459. end
  460. end
  461. end
  462. return 0
  463. end
  464. --获取月卡信息
  465. function MonthCard.getmonthcardinfo(actor)
  466. local monthcard = getplaydef(actor, _playerDbKey())
  467. if monthcard == nil then
  468. monthcard = {}
  469. end
  470. --给客户端发月卡信息
  471. local result = table.valueConvertToString(monthcard)
  472. sendluamsg(actor, LuaMessageIdToClient.RES_GET_MONTHCARD_INFO, result)
  473. -- Trade.sendRecharge(actor)
  474. end
  475. --检查月卡是否过期,过期时给客户端发送变化消息
  476. function MonthCard.checkmonthcardtime(actor)
  477. if card_is_close then
  478. return
  479. end
  480. local monthcard = getplaydef(actor, _playerDbKey())
  481. if monthcard == nil then
  482. return
  483. end
  484. local result = {}
  485. local now = getbaseinfo("now")
  486. for key, info in pairs(monthcard) do
  487. if type(info) == "table" then
  488. if info.isopen then
  489. if info.vaildTime < now then
  490. info.isopen = false
  491. result[tostring(table.count(result)) + 1] = key
  492. end
  493. end
  494. end
  495. end
  496. if table.count(result) == 0 then
  497. return
  498. end
  499. --更新月卡信息
  500. setplaydef(actor, _playerDbKey(), monthcard)
  501. --月卡过期,给客户端发送过期协议
  502. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_OVERTIME, result)
  503. end
  504. function MonthCard.login(actor)
  505. if card_is_close then
  506. return
  507. end
  508. MonthCard.getmonthcardinfo(actor)
  509. jprint("--------- 登录发送月卡信息 -------")
  510. --获取上次离线的时间戳
  511. local offtime = getplaydef(actor, "T$offlinetime")
  512. if offtime == nil then
  513. return
  514. end
  515. local offlinetime = math.ceil(offtime / 1000)
  516. if offlinetime == 0 then
  517. return
  518. end
  519. local now = math.ceil(getbaseinfo("now") / 1000)
  520. --获取上次最长时间月卡距离离线时间有多久
  521. local result = summonthcardtoofflinetime(actor)
  522. if result.time < offlinetime then
  523. return
  524. end
  525. if TimeUtil.isSameDay(offlinetime, result.time) then
  526. return
  527. end
  528. --标志是否小于24小时,小于24小时要少发一天
  529. local daytime = 0
  530. if result.time > now then
  531. result.time = now
  532. --只有一种情况不用少发一天,就是当前月卡时间大于登录时间,而且超过24小时,其余情况都要少发一次
  533. if TimeUtil.within24hours(result.time, now) then
  534. daytime = timestamp_diff_days(offlinetime, result.time) - 1
  535. else
  536. daytime = timestamp_diff_days(offlinetime, result.time)
  537. end
  538. else
  539. daytime = timestamp_diff_days(offlinetime, result.time) - 1
  540. end
  541. --计算要发的时间,传递给三倍时间那边
  542. -- jprint("计算要发的时间",daytime)
  543. if daytime <= 0 then
  544. return
  545. end
  546. local config = ConfigDataManager.getTableValue("cfg_monthlyPass", "dayInCome", "id", result.cfgId)
  547. if config == nil then
  548. return
  549. end
  550. --传递
  551. TripleIncome.AddRemainTime(actor, tonumber(config) * daytime * 60 * 60)
  552. end
  553. function summonthcardtoofflinetime(actor)
  554. local monthcard = getplaydef(actor, _playerDbKey())
  555. local result = {}
  556. result.cfgId = 0
  557. result.time = 0
  558. if monthcard == nil then
  559. return result
  560. end
  561. for _, info in pairs(monthcard) do
  562. if type(info) == "table" then
  563. if info.vaildTime > result.time then
  564. result.time = math.ceil(info.vaildTime / 1000)
  565. result.cfgId = info.cfgId
  566. end
  567. end
  568. end
  569. return result
  570. end
  571. --计算两个时间戳相差了多少天
  572. function timestamp_diff_days(t1, t2)
  573. local date1 = TimeUtil.timeToDate(t1)
  574. local date2 = TimeUtil.timeToDate(t2)
  575. local function is_leap_year(year)
  576. return ((year % 4 == 0 and year % 100 ~= 0) or year % 400 == 0)
  577. end
  578. local function days_in_month(year, month)
  579. local days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  580. if month == 2 and is_leap_year(year) then
  581. return 29
  582. else
  583. return days[month]
  584. end
  585. end
  586. local function days_between(year1, month1, day1, year2, month2, day2)
  587. local years = year2 - year1
  588. local months = month2 - month1
  589. local days = day2 - day1
  590. for y = 1, math.abs(years) do
  591. local year = year1 + (y - 1) * (years > 0 and 1 or -1)
  592. days = days + (is_leap_year(year) and 366 or 365)
  593. end
  594. for m = 1, math.abs(months) do
  595. local month = month1 + (m - 1) * (months > 0 and 1 or -1)
  596. local year = year1 + (m - 1 + (years > 0 and 1 or 0)) * (years > 0 and 1 or -1)
  597. days = days + days_in_month(year, month)
  598. end
  599. return days
  600. end
  601. local days = days_between(date1.year, date1.month, date1.day, date2.year, date2.month, date2.day)
  602. return days
  603. end
  604. --发放三倍时间
  605. function MonthCard.receivemonthcardthreetime(actor)
  606. if card_is_close then
  607. return
  608. end
  609. local monthcard = getplaydef(actor, _playerDbKey())
  610. local result = {}
  611. result.success = false
  612. if monthcard == nil then
  613. -- result.reason = "请先开通月卡后领取!"
  614. -- sendluamsg(actor,LuaMessageIdToClient.RES_MONTHCARD_THREETIME,result)
  615. return
  616. end
  617. if monthcard.todayreceivethreetime == nil then
  618. -- result.reason = "请先开通月卡后领取!"
  619. -- sendluamsg(actor,LuaMessageIdToClient.RES_MONTHCARD_THREETIME,result)
  620. return
  621. end
  622. local now = getbaseinfo("now")
  623. for key, info in pairs(monthcard) do
  624. if type(info) == "table" then
  625. if info.isopen then
  626. --校验时间是否大于24小时
  627. local vaildTime = info.vaildTime
  628. local bool = TimeUtil.within24hours(now, vaildTime)
  629. if bool then
  630. -- result.reason = "月卡时间少于24小时,请续费后领取!"
  631. -- sendluamsg(actor,LuaMessageIdToClient.RES_MONTHCARD_THREETIME,result)
  632. return
  633. else
  634. --领取
  635. local dayincome = ConfigDataManager.getTableValue("cfg_monthlyPass", "dayInCome", "id", key)
  636. if dayincome == nil then
  637. -- result.reason = "领取失败!"
  638. -- sendluamsg(actor,LuaMessageIdToClient.RES_MONTHCARD_THREETIME,result)
  639. return
  640. end
  641. local time = tonumber(dayincome) * 60 * 60
  642. TripleIncome.AddRemainTime(actor, time)
  643. monthcard.todayreceivethreetime = true
  644. monthcard.receivetime = now
  645. --给客户端发消息
  646. -- result.success = true
  647. -- result.reason = "领取成功!"
  648. -- sendluamsg(actor,LuaMessageIdToClient.RES_MONTHCARD_THREETIME,result)
  649. return
  650. end
  651. end
  652. end
  653. end
  654. end
  655. --扣除指定的月卡时间,单位day
  656. function MonthCard.deletemonthcardtime(actor, day)
  657. if card_is_close then
  658. return false
  659. end
  660. local monthcard = getplaydef(actor, _playerDbKey())
  661. if monthcard == nil then
  662. return false
  663. end
  664. local bools = checkactorhasmonthcard(actor)
  665. local now = getbaseinfo("now")
  666. local kTime = tonumber(day) * 24 * 60 * 60 * 1000
  667. local hascard = {}
  668. if bools then
  669. local vaildTime = getmonthcardtime(actor)
  670. if vaildTime - now < kTime then
  671. return false
  672. end
  673. local cfgId = getmonthcardcfgid(actor)
  674. hascard = monthcard[tostring(cfgId)]
  675. hascard.vaildTime = hascard.vaildTime - kTime
  676. monthcard[tostring(cfgId)] = hascard
  677. end
  678. local bool = checkactorhasdaycard(actor)
  679. if bool then
  680. local vaildTime = getdaycardtime(actor)
  681. if vaildTime - now < kTime then
  682. return false
  683. end
  684. local cfgId = getdaycardcfgid(actor)
  685. hascard = monthcard[tostring(cfgId)]
  686. hascard.vaildTime = hascard.vaildTime - kTime
  687. monthcard[tostring(cfgId)] = hascard
  688. end
  689. if not bool and not bools then
  690. return false
  691. end
  692. setplaydef(actor, _playerDbKey(), monthcard)
  693. --发送月卡变化包
  694. local result = table.valueConvertToString(hascard)
  695. --给客户端发月卡信息
  696. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_CHANGE_INFO, result)
  697. return true
  698. end
  699. --上架月卡
  700. function MonthCard.upshelfmonthcard(actor, msgData)
  701. local cfgId = msgData.itemcfgid
  702. if tonumber(cfgId) ~= 30030305 then
  703. sendluamsg(actor, LuaMessageIdToClient.TIPS, "该道具不能上架交易行!")
  704. return
  705. end
  706. --获取要上架的天数,要先从自己的月卡中扣掉
  707. local count = msgData.count
  708. local monthcard = getplaydef(actor, _playerDbKey())
  709. if monthcard == nil then
  710. sendluamsg(actor, LuaMessageIdToClient.TIPS, "背包内没有足够的道具!")
  711. return
  712. end
  713. local now = getbaseinfo("now")
  714. local bool = checkactorhasdaycard(actor)
  715. local cardid = 0
  716. if bool then
  717. --扣时间
  718. local daycardcfgId = getdaycardcfgid(actor)
  719. local hascard = monthcard[tostring(daycardcfgId)]
  720. local number = ConfigDataManager.getTableValue("cfg_stall", "number", "id", cfgId)
  721. if number == nil then
  722. return
  723. end
  724. local vaildTime = hascard.vaildTime - now
  725. local kTime = tonumber(number) * 24 * 60 * 60 * 1000 * tonumber(count)
  726. if vaildTime < kTime then
  727. sendluamsg(actor, LuaMessageIdToClient.TIPS, "月卡剩余时间不足!")
  728. return
  729. end
  730. hascard.vaildTime = hascard.vaildTime - kTime
  731. monthcard[tostring(daycardcfgId)] = hascard
  732. cardid = daycardcfgId
  733. end
  734. local bools = checkactorhasmonthcard(actor)
  735. if bools then
  736. --扣时间
  737. local monthcardcfgId = getmonthcardcfgid(actor)
  738. local hascard = monthcard[tostring(monthcardcfgId)]
  739. local number = ConfigDataManager.getTableValue("cfg_stall", "number", "id", cfgId)
  740. if number == nil then
  741. return
  742. end
  743. local vaildTime = hascard.vaildTime - now
  744. local kTime = tonumber(number) * 24 * 60 * 60 * 1000 * tonumber(count)
  745. if vaildTime < kTime then
  746. sendluamsg(actor, LuaMessageIdToClient.TIPS, "月卡剩余时间不足!")
  747. return
  748. end
  749. hascard.vaildTime = hascard.vaildTime - kTime
  750. monthcard[tostring(monthcardcfgId)] = hascard
  751. cardid = monthcardcfgId
  752. end
  753. setplaydef(actor, _playerDbKey(), monthcard)
  754. --发送月卡变化包
  755. local hascard = monthcard[tostring(cardid)]
  756. local result = table.valueConvertToString(hascard)
  757. --给客户端发月卡信息
  758. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_CHANGE_INFO, result)
  759. msgData.bagindex = "-1"
  760. --走通用的上架方法
  761. Trade.worldTradeListing(actor, msgData)
  762. end
  763. -- 减少月卡时间
  764. function MonthCard.deleteTime(actor, day)
  765. local monthcard = getplaydef(actor, _playerDbKey())
  766. if monthcard == nil then
  767. sendluamsg(actor, LuaMessageIdToClient.TIPS, "背包内没有足够的道具!")
  768. return false
  769. end
  770. local now = getbaseinfo("now")
  771. local bool = checkactorhasdaycard(actor)
  772. local cardid = 0
  773. if bool then
  774. --扣时间
  775. local daycardcfgId = getdaycardcfgid(actor)
  776. local hascard = monthcard[tostring(daycardcfgId)]
  777. local vaildTime = hascard.vaildTime - now
  778. local kTime = 24 * 60 * 60 * 1000 * tonumber(day)
  779. if vaildTime < kTime then
  780. sendluamsg(actor, LuaMessageIdToClient.TIPS, "月卡剩余时间不足!")
  781. return false
  782. end
  783. hascard.vaildTime = hascard.vaildTime - kTime
  784. monthcard[tostring(daycardcfgId)] = hascard
  785. cardid = daycardcfgId
  786. end
  787. local bools = checkactorhasmonthcard(actor)
  788. if bools then
  789. --扣时间
  790. local monthcardcfgId = getmonthcardcfgid(actor)
  791. local hascard = monthcard[tostring(monthcardcfgId)]
  792. local vaildTime = hascard.vaildTime - now
  793. local kTime = 24 * 60 * 60 * 1000 * tonumber(day)
  794. if vaildTime < kTime then
  795. sendluamsg(actor, LuaMessageIdToClient.TIPS, "月卡剩余时间不足!")
  796. return false
  797. end
  798. hascard.vaildTime = hascard.vaildTime - kTime
  799. monthcard[tostring(monthcardcfgId)] = hascard
  800. cardid = monthcardcfgId
  801. end
  802. setplaydef(actor, _playerDbKey(), monthcard)
  803. --发送月卡变化包
  804. local hascard = monthcard[tostring(cardid)]
  805. local result = table.valueConvertToString(hascard)
  806. --给客户端发月卡信息
  807. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_CHANGE_INFO, result)
  808. return true
  809. end
  810. function MonthCard.buymonthcardgoods(actor, itemcfgid, count)
  811. if card_is_close then
  812. return
  813. end
  814. local monthcard = getplaydef(actor, _playerDbKey())
  815. if monthcard == nil then
  816. monthcard = {}
  817. monthcard.todayreceivethreetime = false
  818. monthcard.receivetime = 0
  819. end
  820. --购买的月卡时间默认是月卡,不是日卡
  821. local order = 1
  822. local config = ConfigDataManager.getTable("cfg_monthlyPass", "order", order)
  823. if config == nil then
  824. return
  825. end
  826. local cardid = config[1].id
  827. local number = ConfigDataManager.getTableValue("cfg_stall", "number", "id", itemcfgid)
  828. if number == nil then
  829. return
  830. end
  831. local time = tonumber(number) * 24 * 60 * 60 * 1000 * tonumber(count)
  832. local now = getbaseinfo("now")
  833. local hascard = monthcard[tostring(cardid)]
  834. if hascard == nil then
  835. hascard = {}
  836. hascard.vaildTime = 0
  837. hascard.buycount = 0
  838. hascard.isfirstbuy = true
  839. hascard.isopen = false
  840. hascard.cfgId = cardid
  841. end
  842. --月卡,检查玩家当前是否有月卡、日卡
  843. local bool = checkactorhasmonthcard(actor)
  844. local bools = checkactorhasdaycard(actor)
  845. if order == 1 then
  846. if bool then
  847. --直接加时间就行,其他不变
  848. hascard.vaildTime = hascard.vaildTime + time
  849. if receThreeTime then
  850. if hascard.isfirstbuy then
  851. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  852. monthcard.receivetime = now
  853. else
  854. if TimeUtil.isSameDay(math.floor(monthcard.receivetime / 1000), math.floor(now / 1000)) then
  855. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  856. monthcard.receivetime = now
  857. end
  858. end
  859. hascard.isfirstbuy = false
  860. monthcard.todayreceivethreetime = true
  861. end
  862. else
  863. if bools then
  864. --如果有日卡,算日卡的剩余时间,转化成月卡
  865. local daytime = getdaycardtime(actor)
  866. --先关闭日卡,再加到月卡上
  867. for _, info in pairs(monthcard) do
  868. if type(info) == "table" then
  869. if info.isopen then
  870. info.isopen = false
  871. end
  872. end
  873. end
  874. hascard.vaildTime = daytime + time
  875. hascard.isopen = true
  876. if receThreeTime then
  877. if hascard.isfirstbuy then
  878. --发放额外三倍时间
  879. TripleIncome.AddRemainTime(actor, tonumber(config[1].firstincome) * 60 * 60)
  880. monthcard.receivetime = now
  881. else
  882. --校验三倍时间是否发放
  883. if TimeUtil.isSameDay(math.floor(monthcard.receivetime / 1000), math.floor(now / 1000)) then
  884. TripleIncome.AddRemainTime(actor, tonumber(config[1].dayincome) * 60 * 60)
  885. monthcard.receivetime = now
  886. end
  887. end
  888. monthcard.todayreceivethreetime = true
  889. hascard.isfirstbuy = false
  890. end
  891. else
  892. monthcard = setmonthcardinfo(actor, hascard, monthcard, now, time, config, cardid, true)
  893. end
  894. end
  895. end
  896. -- if order == 2 then
  897. -- --日卡
  898. -- if bool then
  899. -- --时间直接加到月卡上
  900. -- for _, info in pairs(monthcard) do
  901. -- if type(info) =="table" then
  902. -- if info.isopen then
  903. -- info.vaildTime = info.vaildTime + time
  904. -- end
  905. -- end
  906. -- end
  907. -- if flag then
  908. -- --发放额外三倍时间
  909. -- TripleIncome.AddRemainTime(actor,tonumber(config[1].firstincome) * 60 * 60)
  910. -- monthcard.todayreceivethreetime = true
  911. -- end
  912. -- else if bools then
  913. -- --直接加时间就行
  914. -- hascard.vaildTime = hascard.vaildTime + time
  915. -- hascard.isfirstbuy = false
  916. -- else monthcard = setmonthcardinfo(actor,hascard,monthcard,now,time,config,cardid)
  917. -- end
  918. -- end
  919. -- end
  920. monthcard[tostring(cardid)] = hascard
  921. -- monthcard.todayreceivethreetime = true --今日是否领取三倍时间
  922. --更新玩家月卡信息
  923. setplaydef(actor, _playerDbKey(), monthcard)
  924. local result = table.valueConvertToString(hascard)
  925. --给客户端发月卡信息
  926. sendluamsg(actor, LuaMessageIdToClient.RES_MONTHCARD_CHANGE_INFO, result)
  927. end
  928. --聊天功能是否开启 true | false
  929. function MonthCard.checkchatvx(actor)
  930. local monthcard = getplaydef(actor, _playerDbKey())
  931. if monthcard == nil then
  932. return false
  933. end
  934. for key, info in pairs(monthcard) do
  935. if type(info) == "table" then
  936. if info.isopen then
  937. local chat = ConfigDataManager.getTableValue("cfg_monthlyPass", "chat", "id", key)
  938. if tonumber(chat) == 1 then
  939. return true
  940. end
  941. end
  942. end
  943. end
  944. return false
  945. end
  946. --自动拾取功能是否开启
  947. function MonthCard.checkpickupopen(actor)
  948. local monthcard = getplaydef(actor, _playerDbKey())
  949. if monthcard == nil then
  950. return false
  951. end
  952. for key, info in pairs(monthcard) do
  953. if type(info) == "table" then
  954. if info.isopen then
  955. local pinkUp = ConfigDataManager.getTableValue("cfg_monthlyPass", "pickUp", "id", key)
  956. if tonumber(pinkUp) == 1 then
  957. return true
  958. end
  959. end
  960. end
  961. end
  962. return false
  963. end
  964. --自动回收功能是否开启
  965. function MonthCard.checkrecoveryopen(actor)
  966. local monthcard = getplaydef(actor, _playerDbKey())
  967. if monthcard == nil then
  968. return false
  969. end
  970. for key, info in pairs(monthcard) do
  971. if type(info) == "table" then
  972. if info.isopen then
  973. local recovery = ConfigDataManager.getTableValue("cfg_monthlyPass", "recovery", "id", key)
  974. if tonumber(recovery) == 1 then
  975. return true
  976. end
  977. end
  978. end
  979. end
  980. return false
  981. end
  982. function l_month_card_limit_count_gm(actor, type)
  983. MonthCard.limitCount(actor, type)
  984. end
  985. function l_month_card_send_msg_mg(actor)
  986. gameDebug.print(getplaydef(actor, _playerDbKey()))
  987. end
  988. --TODO 一定要放到文件最后
  989. EventListerTable.registerType("月卡", _rechargeType(), _playerDbKey())
  990. --注册充值事件
  991. RechargeEventListerTable:eventLister(_rechargeType(), "月卡充值", MonthCard.limitbuycount)