ZhuanpanLogic.lua 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. local Lang = require("common.Lang")
  2. local Msg = require("core.Msg")
  3. local ObjHuman = require("core.ObjHuman")
  4. local InnerMsg = require("core.InnerMsg")
  5. local Config = require("Config")
  6. local MiddleManager = require("middle.MiddleManager")
  7. local CommonDB = require("common.CommonDB")
  8. local ZhuanpanExcel = require("excel.zhuanpan")
  9. local ItemDefine = require("bag.ItemDefine")
  10. local BagLogic = require("bag.BagLogic")
  11. local Grid = require("bag.Grid")
  12. local Broadcast = require("broadcast.Broadcast")
  13. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  14. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  15. local ItemExcel = require("excel.item").item
  16. local Util = require("common.Util")
  17. local HeroExcel = require("excel.hero").hero
  18. local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
  19. local VipLogic = require("vip.VipLogic")
  20. local EquipLogic = require("equip.EquipLogic")
  21. local YunYingLogic = require("yunying.YunYingLogic")
  22. local TriggerDefine = require("trigger.TriggerDefine")
  23. local TriggerLogic = require("trigger.TriggerLogic")
  24. local WeekTaskLogic = require("dailyTask.WeekTaskLogic")
  25. local ClutterDataLogic = require("clutter.ClutterDataLogic")
  26. local ClutterDataDefine = require("clutter.ClutterDataDefine")
  27. local ZhuanpanGift = require("zhuanpan.ZhuanpanGift")
  28. local Log = require("common.Log")
  29. local DB = require("common.DB")
  30. local LuaMongo = _G.lua_mongo
  31. local MailManager = require("mail.MailManager")
  32. -- 允许的渠道
  33. local ALLOW_CHANNELS = {
  34. [11] = true,
  35. [17] = true, -- 美团2楼
  36. }
  37. DEFAULT_ZHUANPAN_TYPE_NORMAL = 1 -- 基础转盘
  38. DEFAULT_ZHUANPAN_TYPE_GAOJI = 2 -- 高级转盘
  39. DEFAULT_BUY_ZHUANPAN_CNT = 50 -- 购买许愿珠所需钻石
  40. DEFAULT_FREE_REFRESH_TIME = 10800 -- 免费刷新的时间
  41. ZHUAN_PAN_ITEM_KIND_CNT = 8 -- 转盘种类数量
  42. DEFAULT_MAX_LUCK = 1000
  43. REWARD_RECORD = { index={}, order={} }
  44. REWARD_RECORD_CNT = 15
  45. function getLuckConfig(mainType)
  46. local config = nil
  47. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  48. config = ZhuanpanExcel.luck1
  49. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  50. config = ZhuanpanExcel.luck2
  51. end
  52. return config
  53. end
  54. function red(human, type)
  55. if not human.db.zhuanpan then return end
  56. local zhuanpan = human.db.zhuanpan[type]
  57. local config = getLuckConfig(type)
  58. if zhuanpan then
  59. local luck = zhuanpan.luck or 0
  60. local luckStatus = zhuanpan.luckStatus
  61. for k, v in pairs(config) do
  62. if luck >= k then
  63. if luckStatus[k] == 1 then
  64. return true
  65. end
  66. end
  67. end
  68. end
  69. if zhuanpan.free == 1 then
  70. return true
  71. end
  72. end
  73. -- 转盘查询
  74. function query(human, mainType)
  75. local zhuanpanConfig = ZhuanpanExcel[mainType]
  76. if not zhuanpanConfig then return end
  77. -- 高级转盘判断等级
  78. local roleConfig = nil
  79. local flag, roleConfig = RoleSystemLogic.isOpen(human,RoleSystemDefine.ROLE_SYS_ID_501)
  80. if mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  81. if flag ~= true then
  82. return Broadcast.sendErr(human, Lang.ZHUANPAN_GAIJI_NEED_LV)
  83. end
  84. end
  85. local oldCnt = getCntByType(human, mainType)
  86. local oldLuck
  87. local tOldStatus = nil
  88. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  89. oldLuck = human.db.zhuanpan[mainType].luck
  90. if human.db.zhuanpan[mainType].luckStatus then
  91. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  92. end
  93. end
  94. -- 更新
  95. update(human, mainType)
  96. -- 初始化
  97. initDB(human, mainType, oldCnt,oldLuck, tOldStatus)
  98. local zuanpan = human.db.zhuanpan[mainType]
  99. local config = ZhuanpanExcel.define[mainType]
  100. local msgRet = Msg.gc.GC_ZHUANPAN_QUERY
  101. msgRet.type = mainType
  102. msgRet.isFirst = zuanpan.free == 1 and 1 or 0
  103. local now = os.time()
  104. local ts1 = now - zuanpan.ts1
  105. msgRet.refreshTime = 24 * 60 * 60 - ts1
  106. msgRet.freeRefreshTime = DEFAULT_FREE_REFRESH_TIME - (now - zuanpan.ts2)
  107. if msgRet.freeRefreshTime < 0 then
  108. msgRet.freeRefreshTime = 0
  109. end
  110. Grid.makeItem(msgRet.itemID, config.useItemID , 1)
  111. msgRet.itemCnt[0] = 2
  112. msgRet.itemCnt[1] = config.useItemCnt1
  113. msgRet.itemCnt[2] = config.useItemCnt2
  114. msgRet.drawCnt = config.drawCnt2
  115. Grid.makeItem(msgRet.zuanshiNeed, config.refreshCost[1][1], config.refreshCost[1][2])
  116. msgRet.needLv = config and config.lv or 0
  117. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  118. msgRet.needLv = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_GAOJI].lv
  119. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  120. msgRet.needLv = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_NORMAL].lv
  121. end
  122. msgRet.needVipLv = VipLogic.getPowerNeedLv(VipLogic.VIP_POWER15)
  123. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  124. local data = zuanpan[i]
  125. local dataID = data.id
  126. local tempConfig = zhuanpanConfig[dataID]
  127. Grid.makeItem(msgRet.list[i].item, data.itemID, data.itemCnt)
  128. msgRet.list[i].id = dataID
  129. msgRet.list[i].chance = tempConfig.chance
  130. msgRet.list[i].maxCnt = tempConfig.getCnt
  131. msgRet.list[i].getCnt = 0
  132. if data.getCnt then
  133. msgRet.list[i].getCnt = data.getCnt
  134. end
  135. end
  136. local luckConfig = getLuckConfig(mainType)
  137. msgRet.luck = zuanpan.luck or 0
  138. local len = 0
  139. for k, v in pairs(luckConfig) do
  140. len = len + 1
  141. local net = msgRet.luckList[len]
  142. net.id = k
  143. net.status = zuanpan.luckStatus[k] or 0
  144. Grid.makeItem(net.item, v.reward[1] , v.reward[2])
  145. end
  146. msgRet.luckList[0] = len
  147. len = 0
  148. if REWARD_RECORD[mainType] then
  149. for k, v in ipairs(REWARD_RECORD[mainType]) do
  150. len = len + 1
  151. local net = msgRet.record[len]
  152. net.id = k
  153. net.tips = v.tips
  154. net.timer = v.timer
  155. net.order = v.order
  156. end
  157. end
  158. msgRet.record[0] = len
  159. msgRet.list[0] = ZHUAN_PAN_ITEM_KIND_CNT
  160. len = 0
  161. local totalWeight = 0
  162. local chanceList = {}
  163. for k, v in pairs(zhuanpanConfig) do
  164. totalWeight = totalWeight + v.chance
  165. chanceList[k] = chanceList[k] or {}
  166. local rewardWeight = 0
  167. for a, b in ipairs(v.rewardID) do
  168. rewardWeight = rewardWeight + b[3]
  169. end
  170. chanceList[k].totalWeight = rewardWeight
  171. end
  172. -- 大全重占比
  173. local temp = {}
  174. for k, v in pairs(zhuanpanConfig) do
  175. local chance = chanceList[k]
  176. local scale = v.chance / totalWeight
  177. for a, b in ipairs(v.rewardID) do
  178. local itemID = b[1]
  179. if not temp[itemID] then
  180. local weight = b[3] / chance.totalWeight
  181. len = len + 1
  182. local net = msgRet.chanceList[len]
  183. local itemConfig = ItemDefine.getConfig(itemID)
  184. if not itemConfig then
  185. print(itemID)
  186. end
  187. net.tips = itemConfig and itemConfig.name or ""
  188. net.chance = weight * scale * 100
  189. temp[itemID] = 1
  190. end
  191. end
  192. end
  193. msgRet.chanceList[0] = len
  194. local red1 = red(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  195. msgRet.red1 = red1 == true and 1 or 0
  196. local red2 = red(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  197. msgRet.red2 = red2 == true and 1 or 0
  198. Msg.send(msgRet, human.fd)
  199. end
  200. function refresh(human, mainType, needTime, bRefreshLuck)
  201. if not ZhuanpanExcel[mainType] then
  202. return
  203. end
  204. local oldCnt = getCntByType(human, mainType)
  205. local oldLuck
  206. local tOldStatus = nil
  207. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  208. oldLuck = human.db.zhuanpan[mainType].luck
  209. if human.db.zhuanpan[mainType].luckStatus and not bRefreshLuck then
  210. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  211. end
  212. end
  213. -- 更新
  214. update(human, mainType)
  215. -- 初始化
  216. initDB(human, mainType, oldCnt,oldLuck,tOldStatus)
  217. -- 判断是否免费
  218. if needTime then
  219. local config = ZhuanpanExcel.define[mainType]
  220. local needCost = 0
  221. local now = os.time()
  222. local ts2 = now - human.db.zhuanpan[mainType].ts2
  223. local leftTime = DEFAULT_FREE_REFRESH_TIME - ts2
  224. if leftTime > 0 then
  225. needCost = config.refreshCost[1][2]
  226. end
  227. -- 判断消耗
  228. local cnt = BagLogic.getItemCnt(human, config.refreshCost[1][1])
  229. if cnt < needCost then
  230. return
  231. end
  232. -- 扣消耗
  233. if needCost > 0 then
  234. BagLogic.delItem(human, config.refreshCost[1][1], needCost, "zhuanpan_refresh")
  235. end
  236. -- 改db
  237. local dayStartTime = Util.getDayStartTime(now)
  238. human.db.zhuanpan[mainType].ts2 = now
  239. human.db.zhuanpan[mainType].ts1 = dayStartTime
  240. end
  241. addRandomReward(human, mainType)
  242. -- 通知客户端
  243. if needTime then
  244. query(human, mainType)
  245. end
  246. end
  247. -- 刷新数据
  248. function updateDaily(human)
  249. refresh(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  250. refresh(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  251. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free = 1
  252. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].freeCnt = 0
  253. ZhuanpanGift.updateDaily(human)
  254. end
  255. -- 抽奖
  256. function getReward(human, mainType, cnt)
  257. local config = ZhuanpanExcel.define[mainType]
  258. if not ZhuanpanExcel[mainType] then
  259. return
  260. end
  261. local needItemID = config.useItemID
  262. local needItemCnt = nil
  263. if cnt == 1 then
  264. needItemCnt = config.useItemCnt1
  265. elseif cnt == config.drawCnt2 then
  266. needItemCnt = config.useItemCnt2
  267. -- 多次抽奖需要达到指定vip等级
  268. --[[local needVipLv = VipLogic.getPowerNeedLv(VipLogic.VIP_POWER15)
  269. if VipLogic.getVipLv(human) < needVipLv then
  270. return Broadcast.sendErr(human, Util.format(Lang.ROLE_VIP_ERROR2, needVipLv))
  271. end]]
  272. end
  273. if needItemCnt == nil then
  274. return
  275. end
  276. local oldCnt = getCntByType(human, mainType)
  277. local oldLuck
  278. local tOldStatus = nil
  279. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  280. oldLuck = human.db.zhuanpan[mainType].luck
  281. if human.db.zhuanpan[mainType].luckStatus then
  282. tOldStatus = Util.copyTable(human.db.zhuanpan[mainType].luckStatus)
  283. end
  284. end
  285. -- 更新
  286. update(human, mainType)
  287. -- 初始化
  288. initDB(human, mainType, oldCnt,oldLuck, tOldStatus)
  289. local zhuanpan = human.db.zhuanpan[mainType]
  290. if not BagLogic.checkItemCnt(human, needItemID, needItemCnt) then
  291. return
  292. end
  293. -- 每日可抽取次数检测
  294. -- local dayTimes = ClutterDataLogic.GetZhuanPanTimes(human)
  295. -- if dayTimes < cnt then
  296. -- return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
  297. -- end
  298. -- --更新每次抽取次数
  299. -- dayTimes = dayTimes - cnt
  300. -- 改为只更新次数
  301. ClutterDataLogic.UpdateZhuanPanTimes(human, cnt)
  302. -- 扣消耗
  303. BagLogic.delItem(human, needItemID, needItemCnt, "zhuanpan_get")
  304. local msgRet = Msg.gc.GC_ZHUANPAN_GET_REWARD
  305. msgRet.type = mainType
  306. msgRet.list[0] = cnt
  307. local len = 0
  308. for i = 1, cnt do
  309. local result = getLuckDrawSingle(human, mainType)
  310. if result == nil then
  311. assert(nil)
  312. end
  313. -- 改db
  314. local data = zhuanpan[result]
  315. local zhuanpanConfig = ZhuanpanExcel[mainType]
  316. local tempConfig = zhuanpanConfig[data.id]
  317. local nowGetCnt = data.getCnt or 0
  318. local maxGetCnt = tempConfig.getCnt
  319. if maxGetCnt ~= 0 then
  320. data.getCnt = nowGetCnt + 1
  321. end
  322. -- 加道具
  323. local itemID = data.itemID
  324. local itemCnt = data.itemCnt
  325. BagLogic.addItem(human, itemID, itemCnt, "zhuanpan_get")
  326. local itemConfig = ItemExcel[itemID]
  327. local heroConfig = HeroExcel[itemID]
  328. -- 存在部分 英雄配表里面的id 和装备配表重合了
  329. if itemConfig and itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN and heroConfig and heroConfig.star == 5 then
  330. if mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  331. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE11, tempConfig.grade, itemID)
  332. else
  333. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE10, tempConfig.grade, itemID)
  334. end
  335. end
  336. if tempConfig.getCnt > 0 then
  337. if not REWARD_RECORD[mainType] then
  338. REWARD_RECORD[mainType] = {}
  339. end
  340. local record = REWARD_RECORD[mainType]
  341. local index = REWARD_RECORD.index[mainType] or 1
  342. local order = REWARD_RECORD.order[mainType] or 1
  343. record[index] = record[index] or {}
  344. record[index].tips = Util.format(Lang.ZHUANPAN_RECORD_TWO, human.db.name, ItemDefine.getValue(itemID,"name"), itemCnt)
  345. record[index].timer = os.time()
  346. record[index].order = order
  347. index = index + 1
  348. if index > REWARD_RECORD_CNT then
  349. index = 1
  350. end
  351. REWARD_RECORD.order[mainType] = order + 1
  352. REWARD_RECORD.index[mainType] = index
  353. end
  354. msgRet.list[i] = data.id
  355. len = len + 1
  356. if not ItemDefine.isEquip(itemID) then
  357. Grid.makeItem(msgRet.item[len], itemID, itemCnt)
  358. else
  359. EquipLogic.makeEquipItemOne(human, msgRet.item[len])
  360. end
  361. end
  362. msgRet.item[0] = len
  363. -- 增加积分
  364. local itemID = ItemDefine.ITEM_LUCK_ID
  365. local itemCnt = cnt * 10
  366. BagLogic.addItem(human, itemID, itemCnt, "zhuanpan_back")
  367. msgRet.luckItem[0] = 1
  368. Grid.makeItem(msgRet.luckItem[1], itemID, itemCnt)
  369. zhuanpan.cnt = zhuanpan.cnt or 0
  370. zhuanpan.cnt = zhuanpan.cnt + cnt
  371. zhuanpan.luck = zhuanpan.luck or 0
  372. zhuanpan.luck = zhuanpan.luck + cnt * 10
  373. zhuanpan.luckStatus = zhuanpan.luckStatus or {}
  374. local luckConfig = getLuckConfig(mainType)
  375. -- 集火幸运值 奖励
  376. local luckStatus = zhuanpan.luckStatus
  377. for k, v in pairs(luckConfig) do
  378. if zhuanpan.luck >= k then
  379. if luckStatus[k] == nil then
  380. luckStatus[k] = 1
  381. end
  382. end
  383. end
  384. Msg.send(msgRet, human.fd)
  385. query(human, mainType)
  386. YunYingLogic.onCallBack(human, "onFindStar", cnt)
  387. TriggerLogic.PublishEvent(TriggerDefine.EVENT_TYPE_FINDSTAR, human.db._id, cnt)
  388. --周任务,寻星
  389. WeekTaskLogic.recordWeekTaskFinishCnt(human, WeekTaskLogic.WEEK_TASK_ID_1, cnt)
  390. end
  391. -- 购买许愿珠
  392. function buyCnt(human, mainType, cnt)
  393. if cnt < 1 then return end
  394. if mainType ~= DEFAULT_ZHUANPAN_TYPE_NORMAL then
  395. return
  396. end
  397. local needZuanshi = 50 * cnt
  398. -- 判断消耗
  399. if not ObjHuman.checkRMB(human, needZuanshi) then
  400. return
  401. end
  402. -- 扣消耗
  403. ObjHuman.decZuanshi(human, -needZuanshi, "zhuanpan_buy_cnt", ItemDefine.ITEM_BASE_QIYUANZHU_ID, cnt)
  404. -- 增加物品
  405. BagLogic.addItem(human, ItemDefine.ITEM_BASE_QIYUANZHU_ID, cnt, "zhuanpan_buy_cnt")
  406. -- 通知客户端
  407. local msgRet = Msg.gc.GC_BUY_ZHUANPAN_CNT
  408. Msg.send(msgRet, human.fd)
  409. end
  410. ------------------------------功能函数--------------------------------------
  411. function addRandomReward(human, mainType)
  412. local zhuanpanConfig = ZhuanpanExcel[mainType]
  413. if not zhuanpanConfig then return end
  414. local nowLv = human.db.lv
  415. local itemID = nil
  416. local itemCnt = nil
  417. local cntIndex = 0
  418. for i = 1, #zhuanpanConfig do
  419. local tempConfig = zhuanpanConfig[i]
  420. if not tempConfig then return end
  421. if mainType == DEFAULT_ZHUANPAN_TYPE_NORMAL then
  422. if nowLv >= tempConfig.minLv and nowLv <= tempConfig.maxLv then
  423. itemID, itemCnt = getRandomItem(human, tempConfig)
  424. if itemID and itemCnt then
  425. cntIndex = cntIndex + 1
  426. human.db.zhuanpan[mainType][cntIndex] = {}
  427. human.db.zhuanpan[mainType][cntIndex].id = i
  428. human.db.zhuanpan[mainType][cntIndex].itemID = itemID
  429. human.db.zhuanpan[mainType][cntIndex].itemCnt = itemCnt
  430. end
  431. end
  432. elseif mainType == DEFAULT_ZHUANPAN_TYPE_GAOJI then
  433. itemID, itemCnt = getRandomItem(human, tempConfig)
  434. if itemID and itemCnt then
  435. cntIndex = cntIndex + 1
  436. human.db.zhuanpan[mainType][cntIndex] = {}
  437. human.db.zhuanpan[mainType][cntIndex].id = i
  438. human.db.zhuanpan[mainType][cntIndex].itemID = itemID
  439. human.db.zhuanpan[mainType][cntIndex].itemCnt = itemCnt
  440. end
  441. end
  442. end
  443. end
  444. function getLuckDrawSingle(human, mainType)
  445. local zhuanpanConfig = ZhuanpanExcel[mainType]
  446. if not zhuanpanConfig then return end
  447. local totalWeight = 0
  448. local recordData = nil
  449. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  450. local data = human.db.zhuanpan[mainType][i]
  451. local drawID = data.id
  452. local tempConfig = zhuanpanConfig[drawID]
  453. local nowGetCnt = data.getCnt or 0
  454. local maxGetCnt = tempConfig.getCnt
  455. if maxGetCnt == 0 or maxGetCnt > nowGetCnt then
  456. totalWeight = totalWeight + tempConfig.chance
  457. end
  458. end
  459. local randNum = math.random(1,totalWeight)
  460. for i = 1, ZHUAN_PAN_ITEM_KIND_CNT do
  461. local data = human.db.zhuanpan[mainType][i]
  462. local drawID = data.id
  463. local tempConfig = zhuanpanConfig[drawID]
  464. local nowGetCnt = data.getCnt or 0
  465. local maxGetCnt = tempConfig.getCnt
  466. if maxGetCnt == 0 or maxGetCnt > nowGetCnt then
  467. local tempWeight = tempConfig.chance
  468. if randNum <= tempWeight then
  469. return i
  470. end
  471. randNum = randNum - tempWeight
  472. end
  473. end
  474. end
  475. function initDB(human, mainType, cnt, oldluck, tOldStatus)
  476. if human.db.zhuanpan ~= nil and
  477. human.db.zhuanpan[mainType] ~= nil then
  478. return
  479. end
  480. local now = os.time()
  481. human.db.zhuanpan = human.db.zhuanpan or {}
  482. human.db.zhuanpan[mainType] = {}
  483. local dayStartTime = Util.getDayStartTime(now)
  484. human.db.zhuanpan[mainType].ts1 = dayStartTime
  485. human.db.zhuanpan[mainType].ts2 = now - DEFAULT_FREE_REFRESH_TIME
  486. human.db.zhuanpan[mainType].cnt = cnt
  487. human.db.zhuanpan[mainType].luck = oldluck
  488. human.db.zhuanpan[mainType].free = 1
  489. if tOldStatus then
  490. human.db.zhuanpan[mainType].luckStatus = tOldStatus
  491. else
  492. human.db.zhuanpan[mainType].luckStatus = {}
  493. end
  494. addRandomReward(human, mainType)
  495. end
  496. function update(human, mainType)
  497. if human.db.zhuanpan == nil or human.db.zhuanpan[mainType] == nil then
  498. return
  499. end
  500. if human.db.zhuanpan[mainType].ts1 then
  501. -- 防止前端 在结束倒计时结束 发送查询 , 这边还没有结束 返回一样的数据
  502. local now = os.time() + 1
  503. local ts1 = now - human.db.zhuanpan[mainType].ts1
  504. local leftTime = 24 * 60 * 60 - ts1
  505. if leftTime <= 0 then
  506. human.db.zhuanpan[mainType] = nil
  507. end
  508. end
  509. end
  510. function getRandomItem(human, tempConfig)
  511. local totalWeight = 0
  512. for k, v in ipairs(tempConfig.rewardID) do
  513. totalWeight = totalWeight + v[3]
  514. end
  515. local random = math.random(1, totalWeight)
  516. for k, v in ipairs(tempConfig.rewardID) do
  517. local weight = v[3]
  518. local itemID = v[1]
  519. local itemCnt = v[2]
  520. if random <= weight then
  521. return itemID, itemCnt
  522. end
  523. random = random - weight
  524. end
  525. end
  526. function zhuanpanByGm(human,mainType,val)
  527. if not mainType and not val then
  528. return
  529. end
  530. if mainType == 0 or mainType > 2 or val < 5 then
  531. return
  532. end
  533. if human.db.zhuanpan[mainType] then
  534. local now = os.time()
  535. human.db.zhuanpan[mainType].ts1 = now - 24 * 60 * 60 + val
  536. end
  537. end
  538. function getCntByType(human, mainType)
  539. local num = 0
  540. if human.db.zhuanpan and human.db.zhuanpan[mainType] then
  541. human.db.zhuanpan[mainType].cnt = human.db.zhuanpan[mainType].cnt or 0
  542. num = human.db.zhuanpan[mainType].cnt
  543. end
  544. return num
  545. end
  546. function getLuck(human, msg)
  547. local mainType = msg.type
  548. local config = getLuckConfig(mainType)
  549. if not config then
  550. return
  551. end
  552. local zhuanpan = human.db.zhuanpan[mainType]
  553. if zhuanpan == nil then
  554. return
  555. end
  556. if zhuanpan.luck <= 0 then
  557. return
  558. end
  559. local luckStatus = zhuanpan.luckStatus
  560. if luckStatus == nil then
  561. luckStatus = {}
  562. zhuanpan.luckStatus = luckStatus
  563. end
  564. -- 收集所有可领取的奖励(status == 1)
  565. local itemList = {}
  566. local itemListLen = 0
  567. -- 遍历所有配置的奖励,找到可领取的(status == 1)
  568. for k, v in pairs(config) do
  569. if luckStatus[k] == 1 then
  570. -- 标记为已领取
  571. luckStatus[k] = 2
  572. -- 添加奖励到背包
  573. BagLogic.addItem(human, v.reward[1], v.reward[2], "zhuanpan_back")
  574. -- 收集到奖励列表
  575. itemListLen = itemListLen + 1
  576. itemList[itemListLen] = {v.reward[1], v.reward[2]}
  577. end
  578. end
  579. -- 如果没有可领取的奖励,直接返回
  580. if itemListLen == 0 then
  581. return
  582. end
  583. -- 循环处理幸运值扣除和重新激活奖励(可能领取多轮)
  584. while true do
  585. -- 计算是否需要重新扣除幸运值 重新激活奖励
  586. local luck = zhuanpan.luck
  587. local calc = true
  588. for k, v in pairs(config) do
  589. if luckStatus[k] == nil or luckStatus[k] ~= 2 then
  590. calc = false
  591. end
  592. end
  593. -- 如果所有奖励都已领取,扣除幸运值并重新激活
  594. if calc == true then
  595. zhuanpan.luckStatus = {}
  596. luck = luck - DEFAULT_MAX_LUCK
  597. luck = luck < 0 and 0 or luck
  598. zhuanpan.luck = luck
  599. -- 重新激活幸运值奖励
  600. luckStatus = zhuanpan.luckStatus
  601. for k, v in pairs(config) do
  602. if zhuanpan.luck >= k then
  603. if luckStatus[k] == nil then
  604. luckStatus[k] = 1
  605. end
  606. end
  607. end
  608. -- 如果重新激活后还有可领取的奖励,继续循环领取
  609. local hasMore = false
  610. for k, v in pairs(config) do
  611. if luckStatus[k] == 1 then
  612. hasMore = true
  613. -- 领取这个奖励
  614. luckStatus[k] = 2
  615. BagLogic.addItem(human, v.reward[1], v.reward[2], "zhuanpan_back")
  616. itemListLen = itemListLen + 1
  617. itemList[itemListLen] = {v.reward[1], v.reward[2]}
  618. end
  619. end
  620. -- 如果没有更多可领取的奖励,退出循环
  621. if not hasMore then
  622. break
  623. end
  624. else
  625. -- 不是所有奖励都已领取,退出循环
  626. break
  627. end
  628. end
  629. -- 发送所有奖励列表
  630. if itemListLen > 0 then
  631. BagLogic.sendItemGetList(human, itemList, "zhuanpan_back")
  632. end
  633. query(human, msg.type)
  634. end
  635. function isDot(human)
  636. local red1 = red(human, DEFAULT_ZHUANPAN_TYPE_NORMAL)
  637. if red1 then return true end
  638. --local red2 = red(human, DEFAULT_ZHUANPAN_TYPE_GAOJI)
  639. --if red2 then return true end
  640. return false
  641. end
  642. function getFree(human)
  643. local oldLuck
  644. local tOldStatus = nil
  645. if human.db.zhuanpan and human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL] then
  646. oldLuck = human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luck
  647. if human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luckStatus then
  648. tOldStatus = Util.copyTable(human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].luckStatus)
  649. end
  650. end
  651. initDB(human,DEFAULT_ZHUANPAN_TYPE_NORMAL,nil, oldLuck,tOldStatus)
  652. if human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free ~= 1 then
  653. return
  654. end
  655. human.db.zhuanpan[DEFAULT_ZHUANPAN_TYPE_NORMAL].free = 0
  656. local zhuanpanConfig = ZhuanpanExcel.define[DEFAULT_ZHUANPAN_TYPE_NORMAL]
  657. local len = #zhuanpanConfig.freeCnt
  658. local totalWeight = 0
  659. for i = 1,len do
  660. totalWeight = totalWeight + zhuanpanConfig.freeCnt[i][2]
  661. end
  662. local itemCnt = nil
  663. local randomWeight = math.random(1,totalWeight)
  664. for i = 1,len do
  665. if randomWeight < zhuanpanConfig.freeCnt[i][2] then
  666. itemCnt = zhuanpanConfig.freeCnt[i][1]
  667. break
  668. else
  669. randomWeight = randomWeight - zhuanpanConfig.freeCnt[i][2]
  670. end
  671. end
  672. if itemCnt then
  673. local item = {}
  674. item[1] = {zhuanpanConfig.useItemID,itemCnt}
  675. item[2] = zhuanpanConfig.freeitem[1]
  676. BagLogic.addItemList(human, item, "zhuanpan_get")
  677. else
  678. assert()
  679. end
  680. end
  681. -- 检查账号下是否有任何角色已领取每日固定奖励(账号级别判断)
  682. local function checkAccountDailyFixedReward(account)
  683. if not account then
  684. return false
  685. end
  686. local QueryByAccount = {account = account}
  687. local fields = {zhuanpan = 1}
  688. LuaMongo.find(DB.db_char, QueryByAccount, fields)
  689. while true do
  690. local data = {}
  691. if not LuaMongo.next(data) then
  692. break
  693. end
  694. if data.zhuanpan and data.zhuanpan.dailyFixedReward then
  695. local getTime = data.zhuanpan.dailyFixedReward.getTime
  696. if getTime and Util.isSameDay(getTime) then
  697. return true -- 账号下已有角色今日领取过
  698. end
  699. end
  700. end
  701. return false -- 账号下没有角色今日领取过
  702. end
  703. -- 获取账号下最早创建的角色注册日期(账号级别判断)
  704. local function getAccountEarliestCreateTime(account)
  705. if not account then
  706. return nil
  707. end
  708. local QueryByAccount = {account = account}
  709. local fields = {createTime = 1}
  710. LuaMongo.find(DB.db_char, QueryByAccount, fields)
  711. local earliestTime = nil
  712. while true do
  713. local data = {}
  714. if not LuaMongo.next(data) then
  715. break
  716. end
  717. if data.createTime then
  718. if not earliestTime or data.createTime < earliestTime then
  719. earliestTime = data.createTime
  720. end
  721. end
  722. end
  723. return earliestTime
  724. end
  725. -- 检查渠道17是否已超过注册后15天(基于当前角色注册时间)
  726. local function isChannel18Over15Days(human)
  727. if not human or not human.db or not human.db.createTime then
  728. return false
  729. end
  730. local createTime = human.db.createTime
  731. local registerDayStart = Util.getDayStartTime(createTime)
  732. local now = os.time()
  733. local todayDayStart = Util.getDayStartTime(now)
  734. -- 计算从注册日期到今天已经过了多少天(注册日期是第1天)
  735. local daysSinceRegister = math.floor((todayDayStart - registerDayStart) / 86400) + 1
  736. -- 如果超过15天,则不能领取
  737. return daysSinceRegister > 7
  738. end
  739. -- 检查账号下是否有任何角色已领取一次性奖励(账号级别判断)
  740. local function checkAccountOnceReward(account)
  741. if not account then
  742. return false
  743. end
  744. local QueryByAccount = {account = account}
  745. local fields = {zhuanpan = 1}
  746. LuaMongo.find(DB.db_char, QueryByAccount, fields)
  747. while true do
  748. local data = {}
  749. if not LuaMongo.next(data) then
  750. break
  751. end
  752. if data.zhuanpan and data.zhuanpan.onceReward then
  753. local getTime = data.zhuanpan.onceReward.getTime
  754. if getTime then
  755. return true -- 账号下已有角色领取过
  756. end
  757. end
  758. end
  759. return false -- 账号下没有角色领取过
  760. end
  761. -- 检查账号下是否有任何角色已领取订阅奖励(账号级别判断)
  762. local function checkAccountSubscribeReward(account)
  763. if not account then
  764. return false
  765. end
  766. local QueryByAccount = {account = account}
  767. local fields = {zhuanpan = 1}
  768. LuaMongo.find(DB.db_char, QueryByAccount, fields)
  769. while true do
  770. local data = {}
  771. if not LuaMongo.next(data) then
  772. break
  773. end
  774. if data.zhuanpan and data.zhuanpan.subscribeReward then
  775. local getTime = data.zhuanpan.subscribeReward.getTime
  776. if getTime then
  777. return true -- 账号下已有角色领取过
  778. end
  779. end
  780. end
  781. return false -- 账号下没有角色领取过
  782. end
  783. -- 从newUniqueTag解析渠道ID (格式: "channelID|serverTag|account")
  784. local function getChannelIdFromNewUniqueTag(newUniqueTag)
  785. if not newUniqueTag then
  786. return nil
  787. end
  788. local parts = {}
  789. for part in string.gmatch(newUniqueTag, "([^|]+)") do
  790. table.insert(parts, part)
  791. end
  792. if #parts >= 1 then
  793. return tonumber(parts[1])
  794. end
  795. return nil
  796. end
  797. -- 获取每日固定奖励配置(根据渠道ID)
  798. local function getDailyFixedRewardConfig(channelId)
  799. if channelId == 17 then
  800. -- 渠道17(美图2楼):[[101,20000],[111,5000]]
  801. return {
  802. {101, 20000},
  803. {111, 5000}
  804. }
  805. else
  806. -- 渠道11(默认):[[102, 50]]
  807. return {
  808. {102, 50}
  809. }
  810. end
  811. end
  812. -- 每日固定奖励查询
  813. function dailyFixedRewardQuery(human)
  814. local msgRet = Msg.gc.GC_ZHUANPAN_DAILY_FIXED_QUERY
  815. if not msgRet then
  816. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardQuery] 错误: msgRet为nil")
  817. return
  818. end
  819. -- 每日更新检查
  820. ObjHuman.updateDaily(human)
  821. local account = human.db.account
  822. local isReceived = false
  823. -- 先读取当日是否已领取(必须在 or {} 初始化之前读,否则可能被空表覆盖导致刚写入的 getTime 丢失)
  824. local getTime = nil
  825. if human.db.zhuanpan and human.db.zhuanpan.dailyFixedReward then
  826. getTime = human.db.zhuanpan.dailyFixedReward.getTime
  827. end
  828. if getTime and Util.isSameDay(getTime) then
  829. isReceived = true
  830. else
  831. isReceived = checkAccountDailyFixedReward(account)
  832. end
  833. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardQuery] getTime=" .. tostring(getTime) .. ", isReceived=" .. tostring(isReceived) .. ", account=" .. tostring(account))
  834. -- 初始化数据库(放在读取 getTime 之后,避免覆盖已有 dailyFixedReward 导致 isReceived 错误)
  835. human.db.zhuanpan = human.db.zhuanpan or {}
  836. human.db.zhuanpan.dailyFixedReward = human.db.zhuanpan.dailyFixedReward or {}
  837. -- 渠道17(美团2楼)需要满足:
  838. -- 1) 当日从2楼登录(mtFrom2floor == 1,且 mtFrom2floorDay 是今天)
  839. -- 2) 订阅奖励已领取且在7日有效期内
  840. local newUniqueTag = human.db.newUniqueTag or human.newUniqueTag
  841. local channelId = getChannelIdFromNewUniqueTag(newUniqueTag)
  842. if channelId == 17 then
  843. -- 2楼登录状态(mtFrom2floor 可能为字符串;Util.isSameDay 同天返回 now 时间戳,否则 nil,转为布尔)
  844. local from2floorOk = false
  845. if tonumber(human.db.mtFrom2floor) == 1 and human.db.mtFrom2floorDay then
  846. from2floorOk = (Util.isSameDay(human.db.mtFrom2floorDay) and true or false)
  847. end
  848. -- 订阅相关状态(mtSubscribe 可能为字符串 "1",用 tonumber 统一判断)
  849. local isSubscribed = (tonumber(human.db.mtSubscribe) == 1)
  850. local zhuanpan = human.db.zhuanpan
  851. local hasSubscribeReward = zhuanpan and zhuanpan.subscribeReward and zhuanpan.subscribeReward.getTime
  852. local inValidPeriod = hasSubscribeReward and isSubscribeRewardInValidPeriod(human)
  853. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardQuery] channelId=17 from2floorOk=" .. tostring(from2floorOk) .. ", mtFrom2floor=" .. tostring(human.db.mtFrom2floor) .. ", mtFrom2floorDay=" .. tostring(human.db.mtFrom2floorDay) .. ", isSubscribed=" .. tostring(isSubscribed) .. ", hasSubscribeReward=" .. tostring(hasSubscribeReward) .. ", inValidPeriod=" .. tostring(inValidPeriod))
  854. if not isSubscribed then
  855. -- 未订阅:不可领取
  856. msgRet.status = 3 -- 未激活(未订阅)
  857. elseif isReceived then
  858. -- 当日已领取:固定为 2,不因 mtFrom2floor 被置 0 而变成 0
  859. msgRet.status = 2 -- 已领取(今日已领取)
  860. elseif not from2floorOk then
  861. -- 当天未从2楼进入:不可领取
  862. msgRet.status = 0
  863. else
  864. if not hasSubscribeReward or not inValidPeriod then
  865. -- 已订阅,但订阅奖励尚未领取,或已超过7日有效期
  866. msgRet.status = 3 -- 未激活(订阅奖励未领或15天已满)
  867. else
  868. msgRet.status = 1 -- 可领取(已激活且在7日内,且今日未领)
  869. end
  870. end
  871. else
  872. -- 其他渠道保持原有逻辑
  873. if isReceived then
  874. msgRet.status = 2 -- 已领取(账号级别)
  875. else
  876. msgRet.status = 1 -- 可领取
  877. end
  878. end
  879. -- 根据渠道ID设置奖励物品
  880. local newUniqueTag = human.db.newUniqueTag or human.newUniqueTag
  881. local channelId = getChannelIdFromNewUniqueTag(newUniqueTag)
  882. local rewardConfig = getDailyFixedRewardConfig(channelId or 11) -- 默认渠道11
  883. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardQuery] 查询奖励信息: rewardConfig",rewardConfig)
  884. -- 循环处理所有奖励物品
  885. for i = 1, #rewardConfig do
  886. Grid.makeItem(msgRet.reward[i], rewardConfig[i][1], rewardConfig[i][2])
  887. end
  888. msgRet.reward[0] = #rewardConfig -- 设置数组长度
  889. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardQuery] channelId=" .. tostring(channelId) .. ", status=" .. tostring(msgRet.status))
  890. if human.fd then
  891. Msg.send(msgRet, human.fd)
  892. else
  893. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardQuery] 错误: human.fd为nil,无法发送协议")
  894. end
  895. end
  896. -- 每日固定奖励领取
  897. function dailyFixedRewardGet(human, channelId)
  898. if not ALLOW_CHANNELS[channelId] then
  899. Log.write(
  900. Log.LOGID_DEBUG,
  901. "[dailyFixedRewardGet] 渠道不匹配: channelId=" .. (channelId or "nil") .. ", 允许=11,16,18"
  902. )
  903. return Broadcast.sendErr(human, "渠道不匹配")
  904. end
  905. -- 每日更新检查
  906. ObjHuman.updateDaily(human)
  907. -- 初始化数据库
  908. human.db.zhuanpan = human.db.zhuanpan or {}
  909. human.db.zhuanpan.dailyFixedReward = human.db.zhuanpan.dailyFixedReward or {}
  910. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  911. local account = human.db.account
  912. local isReceived = false
  913. -- 先检查当前角色的内存数据
  914. local getTime = human.db.zhuanpan.dailyFixedReward.getTime
  915. if getTime and Util.isSameDay(getTime) then
  916. isReceived = true
  917. else
  918. -- 再检查账号下其他角色(数据库)
  919. isReceived = checkAccountDailyFixedReward(account)
  920. end
  921. if isReceived then
  922. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 账号今日已领取: account="..(account or "nil"))
  923. return Broadcast.sendErr(human, "今日已领取")
  924. end
  925. -- 渠道17(美团2楼)额外条件:
  926. -- 1) 当日从2楼登录(mtFrom2floor == 1 且 mtFrom2floorDay 为今天)
  927. -- 2) 订阅奖励已领取且在7日有效期内
  928. if channelId == 17 then
  929. -- 2楼登录状态(mtFrom2floor 可能为字符串,Util.isSameDay 同天返回时间戳,转为布尔)
  930. local from2floorOk = false
  931. if tonumber(human.db.mtFrom2floor) == 1 and human.db.mtFrom2floorDay then
  932. from2floorOk = (Util.isSameDay(human.db.mtFrom2floorDay) and true or false)
  933. end
  934. if not from2floorOk then
  935. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 渠道17当日未从2楼进入,无法领取: account="..(account or "nil"))
  936. return Broadcast.sendErr(human, "条件不足,无法领取")
  937. end
  938. -- 订阅有效期检查(mtSubscribe 可能为字符串 "1")
  939. if tonumber(human.db.mtSubscribe) ~= 1 or not isSubscribeRewardInValidPeriod(human) then
  940. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 渠道17订阅未激活或已过期,无法领取: account="..(account or "nil"))
  941. return Broadcast.sendErr(human, "订阅未激活或已过期")
  942. end
  943. end
  944. BagLogic.cleanMomentItemList()
  945. -- 根据渠道ID发放不同的奖励(循环处理所有奖励)
  946. local rewardConfig = getDailyFixedRewardConfig(channelId)
  947. for i = 1, #rewardConfig do
  948. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, rewardConfig[i][1], rewardConfig[i][2])
  949. end
  950. -- 复用已有的日志类型,避免未定义的logType导致断言
  951. local success, err = pcall(function()
  952. BagLogic.addMomentItemList(human, "zhuanpan_once_reward")
  953. end)
  954. if not success then
  955. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 错误: 添加到背包失败: "..(err or "unknown"))
  956. return Broadcast.sendErr(human, "发放奖励失败")
  957. end
  958. -- 记录领取时间
  959. local nowTime = os.time()
  960. human.db.zhuanpan.dailyFixedReward.getTime = nowTime
  961. -- 渠道17:领取成功后,消耗当天的2楼登录资格,防止同日重复触发
  962. if channelId == 17 then
  963. human.db.mtFrom2floor = 0
  964. end
  965. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 领取成功 account=" .. tostring(human.db.account) .. " channelId=" .. tostring(channelId) .. " getTime=" .. tostring(nowTime) .. " mtFrom2floor=" .. tostring(human.db.mtFrom2floor))
  966. -- 领取成功后下发查询协议
  967. local querySuccess, queryErr = pcall(function()
  968. dailyFixedRewardQuery(human)
  969. end)
  970. if not querySuccess then
  971. Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 错误: 发送查询协议失败: "..(queryErr or "unknown"))
  972. -- 即使查询协议发送失败,奖励已经发放,所以不返回错误
  973. end
  974. -- Log.write(Log.LOGID_DEBUG, "[dailyFixedRewardGet] 领取完成")
  975. end
  976. -- 抖音渠道一次性奖励查询
  977. function onceRewardQuery(human)
  978. local msgRet = Msg.gc.GC_ZHUANPAN_ONCE_REWARD_QUERY
  979. if not msgRet then
  980. Log.write(Log.LOGID_DEBUG, "[onceRewardQuery] 错误: msgRet为nil")
  981. return
  982. end
  983. -- 初始化数据库
  984. human.db.zhuanpan = human.db.zhuanpan or {}
  985. human.db.zhuanpan.onceReward = human.db.zhuanpan.onceReward or {}
  986. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  987. local account = human.db.account
  988. local isReceived = false
  989. -- 先检查当前角色的内存数据
  990. local getTime = human.db.zhuanpan.onceReward.getTime
  991. if getTime then
  992. isReceived = true
  993. else
  994. -- 再检查账号下其他角色(数据库)
  995. isReceived = checkAccountOnceReward(account)
  996. end
  997. if isReceived then
  998. msgRet.status = 2 -- 已领取(账号级别)
  999. else
  1000. msgRet.status = 1 -- 可领取
  1001. end
  1002. -- 设置奖励物品列表:[[102,500],[118,10],[111,100000]]
  1003. local success, err = pcall(function()
  1004. Grid.makeItem(msgRet.reward[1], 102, 500)
  1005. Grid.makeItem(msgRet.reward[2], 118, 10)
  1006. Grid.makeItem(msgRet.reward[3], 111, 100000)
  1007. msgRet.reward[0] = 3 -- 设置数组长度
  1008. end)
  1009. if not success then
  1010. Log.write(Log.LOGID_DEBUG, "[onceRewardQuery] 错误: 设置奖励物品失败: "..tostring(err))
  1011. return
  1012. end
  1013. if human.fd then
  1014. Msg.send(msgRet, human.fd)
  1015. end
  1016. end
  1017. -- 抖音渠道一次性奖励领取
  1018. function onceRewardGet(human, channelId)
  1019. if not ALLOW_CHANNELS[channelId] then
  1020. Log.write(
  1021. Log.LOGID_DEBUG,
  1022. "[onceRewardGet] 渠道不匹配: channelId=" .. (channelId or "nil") .. ", 允许=11,12"
  1023. )
  1024. return Broadcast.sendErr(human, "渠道不匹配")
  1025. end
  1026. -- 初始化数据库
  1027. human.db.zhuanpan = human.db.zhuanpan or {}
  1028. human.db.zhuanpan.onceReward = human.db.zhuanpan.onceReward or {}
  1029. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  1030. local account = human.db.account
  1031. local isReceived = false
  1032. -- 先检查当前角色的内存数据
  1033. local getTime = human.db.zhuanpan.onceReward.getTime
  1034. if getTime then
  1035. isReceived = true
  1036. else
  1037. -- 再检查账号下其他角色(数据库)
  1038. isReceived = checkAccountOnceReward(account)
  1039. end
  1040. if isReceived then
  1041. Log.write(Log.LOGID_DEBUG, "[onceRewardGet] 账号已领取过,无法重复领取: account="..(account or "nil"))
  1042. return Broadcast.sendErr(human, "已领取过,无法重复领取")
  1043. end
  1044. BagLogic.cleanMomentItemList()
  1045. -- 发放奖励:[[102,500],[118,10],[111,100000]]
  1046. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 102, 500)
  1047. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 118, 10)
  1048. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 111, 100000)
  1049. local success, err = pcall(function()
  1050. BagLogic.addMomentItemList(human, "zhuanpan_once_reward")
  1051. end)
  1052. if not success then
  1053. Log.write(Log.LOGID_DEBUG, "[onceRewardGet] 错误: 添加到背包失败: "..(err or "unknown"))
  1054. return Broadcast.sendErr(human, "发放奖励失败")
  1055. end
  1056. -- 记录领取时间(永久记录)
  1057. human.db.zhuanpan.onceReward.getTime = os.time()
  1058. local querySuccess, queryErr = pcall(function()
  1059. onceRewardQuery(human)
  1060. end)
  1061. if not querySuccess then
  1062. Log.write(Log.LOGID_DEBUG, "[onceRewardGet] 错误: 发送查询协议失败: "..(queryErr or "unknown"))
  1063. -- 即使查询协议发送失败,奖励已经发放,所以不返回错误
  1064. end
  1065. end
  1066. -- 检查当前角色订阅奖励是否在7日有效期内(从订阅奖励领取时间起算)
  1067. function isSubscribeRewardInValidPeriod(human)
  1068. if not human or not human.db then
  1069. return false
  1070. end
  1071. local zhuanpan = human.db.zhuanpan
  1072. if not zhuanpan or not zhuanpan.subscribeReward or not zhuanpan.subscribeReward.getTime then
  1073. return false
  1074. end
  1075. local activateTime = zhuanpan.subscribeReward.getTime
  1076. local activateDayStart = Util.getDayStartTime(activateTime)
  1077. local nowTs = os.time()
  1078. local todayDayStart = Util.getDayStartTime(nowTs)
  1079. -- 激活日为第1天,7日内有效
  1080. local daysSinceActivate = math.floor((todayDayStart - activateDayStart) / 86400) + 1
  1081. return daysSinceActivate <= 7
  1082. end
  1083. -- 检查美团2楼玩家每日固定奖励领取情况(晚上11点调用)
  1084. function checkMtFrom2floorDailyReward()
  1085. if _G.is_middle == true then
  1086. return
  1087. end
  1088. local QueryMtFrom2floor = {mtFrom2floor = 1}
  1089. local fields = {zhuanpan = 1, createTime = 1, mtSubscribe = 1, account = 1}
  1090. LuaMongo.find(DB.db_char, QueryMtFrom2floor, fields)
  1091. local now = os.time()
  1092. local todayDayStart = Util.getDayStartTime(now)
  1093. local dailyMailSentCount = 0
  1094. local subscribeMailSentCount = 0
  1095. while true do
  1096. local data = {}
  1097. if not LuaMongo.next(data) then
  1098. break
  1099. end
  1100. -- 检查是否在订阅有效期内(如果从未激活订阅奖励,视为未激活,不发每日固定奖励)
  1101. if data.mtSubscribe ~= 1 then
  1102. goto continue
  1103. end
  1104. local inValidPeriod = false
  1105. if data.zhuanpan and data.zhuanpan.subscribeReward and data.zhuanpan.subscribeReward.getTime then
  1106. local activateTime = data.zhuanpan.subscribeReward.getTime
  1107. local activateDayStart = Util.getDayStartTime(activateTime)
  1108. local daysSinceActivate = math.floor((todayDayStart - activateDayStart) / 86400) + 1
  1109. inValidPeriod = (daysSinceActivate <= 7)
  1110. end
  1111. -- 订阅奖励未激活或已过期,则不发每日固定奖励
  1112. if not inValidPeriod then
  1113. goto continue
  1114. end
  1115. -- 检查今天是否已领取每日固定奖励
  1116. local hasReceivedToday = false
  1117. if data.zhuanpan and data.zhuanpan.dailyFixedReward then
  1118. local getTime = data.zhuanpan.dailyFixedReward.getTime
  1119. if getTime and Util.isSameDay(getTime) then
  1120. hasReceivedToday = true
  1121. end
  1122. end
  1123. -- 如果今天没领取,发送邮件,并将 mtFrom2floor 置为0,防止当天再次通过登录触发
  1124. if not hasReceivedToday then
  1125. local title = "每日固定奖励提醒"
  1126. local content = "您今日尚未领取每日固定奖励,请及时领取!"
  1127. local rewardItems = {{101, 20000}, {111, 5000}} -- 渠道17的奖励
  1128. local success, err = pcall(function()
  1129. MailManager.add(MailManager.SYSTEM, data._id, title, content, rewardItems)
  1130. -- 更新数据库:标记今日已通过邮件发放,并清除当天的2楼标记
  1131. local updateFields = {
  1132. ["zhuanpan.dailyFixedReward.getTime"] = now,
  1133. ["mtFrom2floor"] = 0,
  1134. }
  1135. LuaMongo.update(DB.db_char, {_id = data._id}, updateFields, false, false)
  1136. end)
  1137. if success then
  1138. dailyMailSentCount = dailyMailSentCount + 1
  1139. Log.write(Log.LOGID_DEBUG, "[checkMtFrom2floorDailyReward] 发送每日固定奖励邮件并更新状态成功: uuid="..data._id)
  1140. else
  1141. Log.write(Log.LOGID_DEBUG, "[checkMtFrom2floorDailyReward] 发送每日固定奖励邮件或更新状态失败: uuid="..data._id..", err="..tostring(err))
  1142. end
  1143. end
  1144. -- 检查订阅奖励:如果已订阅(mtSubscribe == 1)且未领取,发送邮件
  1145. if data.mtSubscribe == 1 then
  1146. -- 检查账号下是否已领取订阅奖励
  1147. local hasReceivedSubscribe = checkAccountSubscribeReward(data.account)
  1148. if not hasReceivedSubscribe then
  1149. local title = "订阅奖励提醒"
  1150. local content = "您已订阅但尚未领取订阅奖励,请及时领取!"
  1151. local rewardItems = {{101, 50000}, {112, 100}} -- 订阅奖励
  1152. local success, err = pcall(function()
  1153. MailManager.add(MailManager.SYSTEM, data._id, title, content, rewardItems)
  1154. end)
  1155. if success then
  1156. subscribeMailSentCount = subscribeMailSentCount + 1
  1157. Log.write(Log.LOGID_DEBUG, "[checkMtFrom2floorDailyReward] 发送订阅奖励邮件成功: uuid="..data._id)
  1158. else
  1159. Log.write(Log.LOGID_DEBUG, "[checkMtFrom2floorDailyReward] 发送订阅奖励邮件失败: uuid="..data._id..", err="..tostring(err))
  1160. end
  1161. end
  1162. end
  1163. ::continue::
  1164. end
  1165. Log.write(Log.LOGID_DEBUG, "[checkMtFrom2floorDailyReward] 检查完成,每日固定奖励邮件数量: "..dailyMailSentCount..", 订阅奖励邮件数量: "..subscribeMailSentCount)
  1166. end
  1167. -- 美团订阅奖励查询(同时接收并存储订阅状态)
  1168. function subscribeRewardQuery(human, msg)
  1169. local msgRet = Msg.gc.GC_ZHUANPAN_SUBSCRIBE_REWARD_QUERY
  1170. if not msgRet then
  1171. Log.write(Log.LOGID_DEBUG, "[subscribeRewardQuery] 错误: msgRet为nil")
  1172. return
  1173. end
  1174. -- 如果前端上传了订阅状态,则进行存储
  1175. if msg and msg.mtSubscribe ~= nil then
  1176. local val = tonumber(msg.mtSubscribe) or 0
  1177. -- 只从 0 -> 1,避免重复覆盖或回退
  1178. if val == 1 and human.db then
  1179. human.db.mtSubscribe = human.db.mtSubscribe or 0
  1180. if human.db.mtSubscribe ~= 1 then
  1181. human.db.mtSubscribe = 1
  1182. Log.write(Log.LOGID_DEBUG, "[subscribeRewardQuery] 更新订阅状态为1, account="..tostring(human.db.account))
  1183. end
  1184. end
  1185. end
  1186. -- 初始化数据库
  1187. human.db.zhuanpan = human.db.zhuanpan or {}
  1188. human.db.zhuanpan.subscribeReward = human.db.zhuanpan.subscribeReward or {}
  1189. -- 检查是否订阅(mtSubscribe == 1)
  1190. local isSubscribed = (human.db.mtSubscribe == 1)
  1191. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  1192. local account = human.db.account
  1193. local isReceived = false
  1194. if isSubscribed then
  1195. -- 先检查当前角色的内存数据
  1196. local getTime = human.db.zhuanpan.subscribeReward.getTime
  1197. if getTime then
  1198. isReceived = true
  1199. else
  1200. -- 再检查账号下其他角色(数据库)
  1201. isReceived = checkAccountSubscribeReward(account)
  1202. end
  1203. end
  1204. if not isSubscribed then
  1205. msgRet.status = 0 -- 不可领取(未订阅)
  1206. elseif isReceived then
  1207. msgRet.status = 2 -- 已领取
  1208. else
  1209. msgRet.status = 1 -- 可领取
  1210. end
  1211. -- 设置奖励物品列表:[[101,50000],[112,100]]
  1212. local success, err = pcall(function()
  1213. Grid.makeItem(msgRet.reward[1], 101, 50000)
  1214. Grid.makeItem(msgRet.reward[2], 112, 100)
  1215. msgRet.reward[0] = 2 -- 设置数组长度
  1216. end)
  1217. if not success then
  1218. Log.write(Log.LOGID_DEBUG, "[subscribeRewardQuery] 错误: 设置奖励物品失败: "..tostring(err))
  1219. return
  1220. end
  1221. if human.fd then
  1222. Msg.send(msgRet, human.fd)
  1223. end
  1224. end
  1225. -- 美团订阅奖励领取
  1226. function subscribeRewardGet(human, channelId)
  1227. -- 检查渠道,只有渠道ID=17(美团2楼)才能领取
  1228. if not channelId or channelId ~= 17 then
  1229. Log.write(Log.LOGID_DEBUG, "[subscribeRewardGet] 渠道不匹配: channelId="..(channelId or "nil")..", 需要=18")
  1230. return Broadcast.sendErr(human, "渠道不匹配")
  1231. end
  1232. -- 检查是否订阅(mtSubscribe == 1)
  1233. if human.db.mtSubscribe ~= 1 then
  1234. Log.write(Log.LOGID_DEBUG, "[subscribeRewardGet] 未订阅: mtSubscribe="..(human.db.mtSubscribe or "nil"))
  1235. return Broadcast.sendErr(human, "未订阅,无法领取")
  1236. end
  1237. -- 初始化数据库
  1238. human.db.zhuanpan = human.db.zhuanpan or {}
  1239. human.db.zhuanpan.subscribeReward = human.db.zhuanpan.subscribeReward or {}
  1240. -- 账号级别判断:先检查当前角色的内存数据,再检查数据库
  1241. local account = human.db.account
  1242. local isReceived = false
  1243. -- 先检查当前角色的内存数据
  1244. local getTime = human.db.zhuanpan.subscribeReward.getTime
  1245. if getTime then
  1246. isReceived = true
  1247. else
  1248. -- 再检查账号下其他角色(数据库)
  1249. isReceived = checkAccountSubscribeReward(account)
  1250. end
  1251. if isReceived then
  1252. Log.write(Log.LOGID_DEBUG, "[subscribeRewardGet] 账号已领取过,无法重复领取: account="..(account or "nil"))
  1253. return Broadcast.sendErr(human, "已领取过,无法重复领取")
  1254. end
  1255. BagLogic.cleanMomentItemList()
  1256. -- 发放奖励:[[101,50000],[112,100]]
  1257. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 101, 50000)
  1258. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, 112, 100)
  1259. -- 使用已存在的日志类型,避免日志定义缺失导致的断言
  1260. local success, err = pcall(function()
  1261. BagLogic.addMomentItemList(human, "zhuanpan_once_reward")
  1262. end)
  1263. if not success then
  1264. Log.write(Log.LOGID_DEBUG, "[subscribeRewardGet] 错误: 添加到背包失败: "..(err or "unknown"))
  1265. return Broadcast.sendErr(human, "发放奖励失败")
  1266. end
  1267. -- 记录领取时间(永久记录)
  1268. human.db.zhuanpan.subscribeReward.getTime = os.time()
  1269. local querySuccess, queryErr = pcall(function()
  1270. subscribeRewardQuery(human)
  1271. end)
  1272. if not querySuccess then
  1273. Log.write(Log.LOGID_DEBUG, "[subscribeRewardGet] 错误: 发送订阅奖励查询协议失败: "..(queryErr or "unknown"))
  1274. -- 即使查询协议发送失败,奖励已经发放,所以不返回错误
  1275. end
  1276. -- 领取订阅奖励后,同时下发每日固定奖励查询协议
  1277. local dailyQuerySuccess, dailyQueryErr = pcall(function()
  1278. dailyFixedRewardQuery(human)
  1279. end)
  1280. if not dailyQuerySuccess then
  1281. Log.write(Log.LOGID_DEBUG, "[subscribeRewardGet] 错误: 发送每日固定奖励查询协议失败: "..(dailyQueryErr or "unknown"))
  1282. -- 即使查询协议发送失败,奖励已经发放,所以不返回错误
  1283. end
  1284. end
  1285. -----------------------淘宝小程序桌面活动-----------------------
  1286. -- 奖励配置(占位,按需修改)
  1287. local TB_ENTER_REWARD = {{112, 100}} -- 桌面进入奖励(每日1次)
  1288. local TB_SECOND_FLOOR_REWARD = {{127, 10}} -- 二楼进入奖励(每日1次)
  1289. local TB_DESKTOP_REWARD = {{118, 5}} -- 添加桌面奖励(仅1次)
  1290. -- 渠道校验:仅 pf == "tbMiniGame" 的玩家可参与
  1291. local function isTbMiniApp(human)
  1292. return human.pf_info and human.pf_info.pf == "tbMiniGame"
  1293. end
  1294. -- 淘宝小程序桌面活动查询
  1295. -- status: 1=桌面进入 2=二楼进入
  1296. function tbminiappQuery(human, status)
  1297. Log.write(Log.LOGID_DEBUG, "[tbminiappQuery] 收到请求 account=" .. tostring(human and human.db and human.db.account)
  1298. .. " pf=" .. tostring(human and human.pf)
  1299. .. " status=" .. tostring(status))
  1300. local msgRet = Msg.gc.GC_ZHUANPAN_TB_QUERY
  1301. if not msgRet then
  1302. Log.write(Log.LOGID_DEBUG, "[tbminiappQuery] 错误: msgRet为nil")
  1303. return
  1304. end
  1305. if not isTbMiniApp(human) then
  1306. Log.write(Log.LOGID_DEBUG, "[tbminiappQuery] 渠道不匹配 pf=" .. tostring(human and human.pf))
  1307. return Broadcast.sendErr(human, "渠道不匹配")
  1308. end
  1309. ObjHuman.updateDaily(human)
  1310. human.db.zhuanpan = human.db.zhuanpan or {}
  1311. human.db.zhuanpan.tbminiapp = human.db.zhuanpan.tbminiapp or {}
  1312. local tb = human.db.zhuanpan.tbminiapp
  1313. -- 是否已添加桌面
  1314. msgRet.isDesktopAdded = tb.desktopAdded and 1 or 0
  1315. -- 是否已领取添加桌面奖励(一次性)
  1316. msgRet.isDesktopRewardReceived = (tb.desktopReward and tb.desktopReward.getTime) and 1 or 0
  1317. -- 是否已领取桌面进入奖励(每日1次)
  1318. local enterTime = tb.enterReward and tb.enterReward.getTime
  1319. msgRet.isEnterRewardReceived = (enterTime and Util.isSameDay(enterTime)) and 1 or 0
  1320. -- 是否已领取二楼进入奖励(每日1次)
  1321. local floorTime = tb.floorReward and tb.floorReward.getTime
  1322. msgRet.isFloorRewardReceived = (floorTime and Util.isSameDay(floorTime)) and 1 or 0
  1323. -- 填充奖励道具信息
  1324. local ok, err = pcall(function()
  1325. Grid.makeItem(msgRet.enterReward, TB_ENTER_REWARD[1][1], TB_ENTER_REWARD[1][2])
  1326. Grid.makeItem(msgRet.floorReward, TB_SECOND_FLOOR_REWARD[1][1], TB_SECOND_FLOOR_REWARD[1][2])
  1327. Grid.makeItem(msgRet.desktopReward, TB_DESKTOP_REWARD[1][1], TB_DESKTOP_REWARD[1][2])
  1328. end)
  1329. if not ok then
  1330. Log.write(Log.LOGID_DEBUG, "[tbminiappQuery] 错误: Grid.makeItem失败: " .. tostring(err))
  1331. return
  1332. end
  1333. Log.write(Log.LOGID_DEBUG, "[tbminiappQuery] 准备发送"
  1334. .. " isDesktopAdded=" .. tostring(msgRet.isDesktopAdded)
  1335. .. " isDesktopRewardReceived=" .. tostring(msgRet.isDesktopRewardReceived)
  1336. .. " isEnterRewardReceived=" .. tostring(msgRet.isEnterRewardReceived)
  1337. .. " isFloorRewardReceived=" .. tostring(msgRet.isFloorRewardReceived)
  1338. .. " fd=" .. tostring(human.fd))
  1339. if human.fd then
  1340. Msg.send(msgRet, human.fd)
  1341. Log.write(Log.LOGID_DEBUG, "[tbminiappQuery] 发送成功 account=" .. tostring(human.db.account))
  1342. else
  1343. Log.write(Log.LOGID_DEBUG, "[tbminiappQuery] 错误: human.fd为nil,无法发送协议 account=" .. tostring(human.db.account))
  1344. end
  1345. end
  1346. -- 淘宝小程序添加桌面上报:记录添加状态
  1347. function tbminiappAddDesktop(human)
  1348. if not isTbMiniApp(human) then
  1349. return Broadcast.sendErr(human, "渠道不匹配")
  1350. end
  1351. human.db.zhuanpan = human.db.zhuanpan or {}
  1352. human.db.zhuanpan.tbminiapp = human.db.zhuanpan.tbminiapp or {}
  1353. human.db.zhuanpan.tbminiapp.desktopAdded = true
  1354. Log.write(Log.LOGID_DEBUG, "[tbminiappAddDesktop] 添加桌面成功 account=" .. tostring(human.db.account))
  1355. local querySuccess, queryErr = pcall(function()
  1356. tbminiappQuery(human, 0)
  1357. end)
  1358. if not querySuccess then
  1359. Log.write(Log.LOGID_DEBUG, "[tbminiappAddDesktop] 错误: 发送查询协议失败: "..(queryErr or "unknown"))
  1360. end
  1361. end
  1362. -- 淘宝小程序活动奖励领取
  1363. -- status: 1=桌面进入奖励(每日1次)2=二楼进入奖励(每日1次)3=添加桌面奖励(仅1次)
  1364. function tbminiappRewardGet(human, status)
  1365. if not isTbMiniApp(human) then
  1366. return Broadcast.sendErr(human, "渠道不匹配")
  1367. end
  1368. ObjHuman.updateDaily(human)
  1369. human.db.zhuanpan = human.db.zhuanpan or {}
  1370. human.db.zhuanpan.tbminiapp = human.db.zhuanpan.tbminiapp or {}
  1371. local tb = human.db.zhuanpan.tbminiapp
  1372. local rewardConfig = nil
  1373. if status == 1 then
  1374. -- 桌面进入奖励(每日1次)
  1375. local enterTime = tb.enterReward and tb.enterReward.getTime
  1376. if enterTime and Util.isSameDay(enterTime) then
  1377. Log.write(Log.LOGID_DEBUG, "[tbminiappRewardGet] 今日已领取桌面进入奖励 account=" .. tostring(human.db.account))
  1378. return Broadcast.sendErr(human, "今日已领取桌面进入奖励")
  1379. end
  1380. rewardConfig = TB_ENTER_REWARD
  1381. elseif status == 2 then
  1382. -- 二楼进入奖励(每日1次)
  1383. local floorTime = tb.floorReward and tb.floorReward.getTime
  1384. if floorTime and Util.isSameDay(floorTime) then
  1385. Log.write(Log.LOGID_DEBUG, "[tbminiappRewardGet] 今日已领取二楼进入奖励 account=" .. tostring(human.db.account))
  1386. return Broadcast.sendErr(human, "今日已领取二楼进入奖励")
  1387. end
  1388. rewardConfig = TB_SECOND_FLOOR_REWARD
  1389. elseif status == 3 then
  1390. -- 添加桌面奖励(仅1次)
  1391. if not tb.desktopAdded then
  1392. Log.write(Log.LOGID_DEBUG, "[tbminiappRewardGet] 尚未添加桌面 account=" .. tostring(human.db.account))
  1393. return Broadcast.sendErr(human, "尚未添加桌面")
  1394. end
  1395. if tb.desktopReward and tb.desktopReward.getTime then
  1396. Log.write(Log.LOGID_DEBUG, "[tbminiappRewardGet] 添加桌面奖励已领取 account=" .. tostring(human.db.account))
  1397. return Broadcast.sendErr(human, "添加桌面奖励已领取")
  1398. end
  1399. rewardConfig = TB_DESKTOP_REWARD
  1400. else
  1401. return Broadcast.sendErr(human, "无效的status参数")
  1402. end
  1403. -- 发放奖励
  1404. BagLogic.cleanMomentItemList()
  1405. for _, v in ipairs(rewardConfig) do
  1406. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, v[1], v[2])
  1407. end
  1408. local success, err = pcall(function()
  1409. BagLogic.addMomentItemList(human, "zhuanpan_once_reward")
  1410. end)
  1411. if not success then
  1412. Log.write(Log.LOGID_DEBUG, "[tbminiappRewardGet] 错误: 添加到背包失败: "..(err or "unknown"))
  1413. return Broadcast.sendErr(human, "发放奖励失败")
  1414. end
  1415. -- 记录领取时间
  1416. local nowTime = os.time()
  1417. if status == 1 then
  1418. tb.enterReward = tb.enterReward or {}
  1419. tb.enterReward.getTime = nowTime
  1420. elseif status == 2 then
  1421. tb.floorReward = tb.floorReward or {}
  1422. tb.floorReward.getTime = nowTime
  1423. elseif status == 3 then
  1424. tb.desktopReward = tb.desktopReward or {}
  1425. tb.desktopReward.getTime = nowTime
  1426. end
  1427. Log.write(Log.LOGID_DEBUG, "[tbminiappRewardGet] 领取成功 status=" .. tostring(status) .. " account=" .. tostring(human.db.account))
  1428. -- 下发查询协议
  1429. local querySuccess, queryErr = pcall(function()
  1430. tbminiappQuery(human, 0)
  1431. end)
  1432. if not querySuccess then
  1433. Log.write(Log.LOGID_DEBUG, "[tbminiappRewardGet] 错误: 发送查询协议失败: "..(queryErr or "unknown"))
  1434. end
  1435. end
  1436. -----------------------游戏圈任务-----------------------
  1437. -- 游戏圈任务配置
  1438. -- reward: {itemId, count} target: 完成目标数量 type: "once"永久/"daily"每日/"weekly"每周
  1439. local YOUXI_TASK_CONFIG = {
  1440. [1] = { reward = {118, 10}, target = 1, taskType = "once" }, -- 首次加入游戏圈: 10连抽
  1441. [2] = { reward = {117, 1}, target = 1, taskType = "daily" }, -- 今日点赞1次: 金币100
  1442. [3] = { reward = {117, 1}, target = 1, taskType = "daily" }, -- 今日发表评论1次: 金币100
  1443. [4] = { reward = {117, 5}, target = 10, taskType = "weekly" }, -- 每周点赞10次: 金币100
  1444. [5] = { reward = {117, 5}, target = 20, taskType = "weekly" }, -- 每周发表评论20次: 金币100
  1445. }
  1446. -- 获取本周一0点时间戳
  1447. local function getWeekStartTime()
  1448. local now = os.time()
  1449. local t = os.date("*t", now)
  1450. local daysFromMonday = (t.wday - 2) % 7 -- wday: 1=Sun, 2=Mon
  1451. local todayStart = Util.getDayStartTime(now)
  1452. return todayStart - daysFromMonday * 86400
  1453. end
  1454. -- 判断时间戳是否在本周(本周一0点)之后
  1455. local function isCurrentWeek(timestamp)
  1456. if not timestamp then return false end
  1457. return timestamp >= getWeekStartTime()
  1458. end
  1459. -- 更新每日 SDK 进度(当天自然日,同天取最大值;跨天把昨天 count 累加入本周合计)
  1460. -- weekTotal 始终不含当天,getWeekProgress 统一加上今日 count
  1461. local function updateDailyCount(field, newCount)
  1462. field = field or {}
  1463. local now = os.time()
  1464. if field.dayTime and Util.isSameDay(field.dayTime) then
  1465. -- 同一天:仅在 newCount > 0 时更新;weekTotal 不动(不含今天)
  1466. if newCount > 0 then
  1467. field.count = newCount
  1468. end
  1469. else
  1470. -- 新的一天:把昨天的 count 累加到本周合计,重置今天的记录
  1471. local weekTotal = field.weekTotal or 0
  1472. if field.weekTime and isCurrentWeek(field.weekTime) then
  1473. -- 同一周:把昨天(或更早今天之前)的 count 并入 weekTotal
  1474. field.weekTotal = weekTotal + (field.count or 0)
  1475. else
  1476. -- 新的一周:重置,weekTotal 不含今天
  1477. field.weekTotal = 0
  1478. end
  1479. field.count = newCount
  1480. field.dayTime = now
  1481. field.weekTime = now
  1482. end
  1483. return field
  1484. end
  1485. -- 获取本周累计进度:weekTotal(不含今天)+ 今日 count
  1486. local function getWeekProgress(field)
  1487. if not field then return 0 end
  1488. local weekTotal = 0
  1489. if field.weekTime and isCurrentWeek(field.weekTime) then
  1490. weekTotal = field.weekTotal or 0
  1491. end
  1492. -- 今天的 count 始终不在 weekTotal 里,直接加上
  1493. if field.dayTime and Util.isSameDay(field.dayTime) then
  1494. return weekTotal + (field.count or 0)
  1495. end
  1496. return weekTotal
  1497. end
  1498. -- 检查账号下是否有任何角色已领取指定游戏圈任务(账号级别判断)
  1499. local function checkAccountYouxiTaskClaimed(account, taskId)
  1500. if not account then return false end
  1501. local config = YOUXI_TASK_CONFIG[taskId]
  1502. if not config then return false end
  1503. LuaMongo.find(DB.db_char, {account = account}, {zhuanpan = 1})
  1504. while true do
  1505. local data = {}
  1506. if not LuaMongo.next(data) then break end
  1507. local yt = data.zhuanpan and data.zhuanpan.youxiTask
  1508. if yt then
  1509. if config.taskType == "once" then
  1510. if yt.joinGetTime then return true end
  1511. elseif config.taskType == "daily" then
  1512. local field = (taskId == 2) and yt.dailyLike or yt.dailyComment
  1513. if field and field.getTime and Util.isSameDay(field.getTime) then return true end
  1514. elseif config.taskType == "weekly" then
  1515. local field = (taskId == 4) and yt.dailyLike or yt.dailyComment
  1516. if field and field.weekGetTime and isCurrentWeek(field.weekGetTime) then return true end
  1517. end
  1518. end
  1519. end
  1520. return false
  1521. end
  1522. -- 游戏圈任务查询(前端上传 SDK 数据,后端存储并计算进度)
  1523. function youxiTaskQuery(human, msg)
  1524. local msgRet = Msg.gc.GC_ZHUANPAN_YOUXI_TASK_QUERY
  1525. if not msgRet then
  1526. Log.write(Log.LOGID_DEBUG, "[youxiTaskQuery] 错误: msgRet为nil")
  1527. return
  1528. end
  1529. human.db.zhuanpan = human.db.zhuanpan or {}
  1530. human.db.zhuanpan.youxiTask = human.db.zhuanpan.youxiTask or {}
  1531. local yt = human.db.zhuanpan.youxiTask
  1532. -- 存储 SDK 上报数据并更新进度
  1533. if msg then
  1534. -- 任务1:加入游戏圈(joinTime > 0 表示已加入)
  1535. if msg.joinTime and msg.joinTime > 0 and not yt.joined then
  1536. yt.joined = true
  1537. end
  1538. -- 任务2/4:点赞(count=0 时也需调用,以便跨天时将昨日 count 累入 weekTotal)
  1539. if msg.likeCount ~= nil then
  1540. yt.dailyLike = updateDailyCount(yt.dailyLike, msg.likeCount)
  1541. end
  1542. -- 任务3/5:评论(同上)
  1543. if msg.commentCount ~= nil then
  1544. yt.dailyComment = updateDailyCount(yt.dailyComment, msg.commentCount)
  1545. end
  1546. end
  1547. local ok, err = pcall(function()
  1548. for taskId = 1, 5 do
  1549. local config = YOUXI_TASK_CONFIG[taskId]
  1550. local taskData = msgRet.tasks[taskId]
  1551. taskData.taskId = taskId
  1552. taskData.target = config.target
  1553. Grid.makeItem(taskData.reward, config.reward[1], config.reward[2])
  1554. if config.taskType == "once" then
  1555. taskData.progress = yt.joined and 1 or 0
  1556. local claimed = yt.joinGetTime or checkAccountYouxiTaskClaimed(human.db.account, taskId)
  1557. if claimed then
  1558. taskData.status = 2
  1559. elseif yt.joined then
  1560. taskData.status = 1
  1561. else
  1562. taskData.status = 0
  1563. end
  1564. elseif config.taskType == "daily" then
  1565. local field = (taskId == 2) and yt.dailyLike or yt.dailyComment
  1566. local dayCount = (field and field.dayTime and Util.isSameDay(field.dayTime)) and (field.count or 0) or 0
  1567. taskData.progress = math.min(dayCount, config.target)
  1568. local getTime = field and field.getTime
  1569. local claimed = (getTime and Util.isSameDay(getTime)) or checkAccountYouxiTaskClaimed(human.db.account, taskId)
  1570. if claimed then
  1571. taskData.status = 2
  1572. elseif dayCount >= config.target then
  1573. taskData.status = 1
  1574. else
  1575. taskData.status = 0
  1576. end
  1577. elseif config.taskType == "weekly" then
  1578. local field = (taskId == 4) and yt.dailyLike or yt.dailyComment
  1579. local weekCount = getWeekProgress(field)
  1580. taskData.progress = math.min(weekCount, config.target)
  1581. local weekGetTime = field and field.weekGetTime
  1582. local claimed = (weekGetTime and isCurrentWeek(weekGetTime)) or checkAccountYouxiTaskClaimed(human.db.account, taskId)
  1583. if claimed then
  1584. taskData.status = 2
  1585. elseif weekCount >= config.target then
  1586. taskData.status = 1
  1587. else
  1588. taskData.status = 0
  1589. end
  1590. end
  1591. end
  1592. msgRet.tasks[0] = 5
  1593. end)
  1594. if not ok then
  1595. Log.write(Log.LOGID_DEBUG, "[youxiTaskQuery] 错误: " .. tostring(err))
  1596. return
  1597. end
  1598. if human.fd then
  1599. Msg.send(msgRet, human.fd)
  1600. end
  1601. end
  1602. -- 游戏圈任务领取(前端协议调用)
  1603. -- taskId: 1-5
  1604. function youxiTaskGetReward(human, taskId)
  1605. local config = YOUXI_TASK_CONFIG[taskId]
  1606. if not config then
  1607. Log.write(Log.LOGID_DEBUG, "[youxiTaskGetReward] 无效taskId=" .. tostring(taskId))
  1608. return Broadcast.sendErr(human, "无效的任务ID")
  1609. end
  1610. human.db.zhuanpan = human.db.zhuanpan or {}
  1611. human.db.zhuanpan.youxiTask = human.db.zhuanpan.youxiTask or {}
  1612. local yt = human.db.zhuanpan.youxiTask
  1613. local now = os.time()
  1614. -- 校验条件 & 重复领取(账号级别判断,同账号多角色共享)
  1615. -- 先查当前角色内存,没有再查 DB 其他角色(与美团订阅奖励模式一致)
  1616. local account = human.db.account
  1617. if config.taskType == "once" then
  1618. local claimed = yt.joinGetTime or checkAccountYouxiTaskClaimed(account, taskId)
  1619. if claimed then
  1620. return Broadcast.sendErr(human, "已领取过")
  1621. end
  1622. if not yt.joined then
  1623. return Broadcast.sendErr(human, "尚未加入游戏圈")
  1624. end
  1625. elseif config.taskType == "daily" then
  1626. local field = (taskId == 2) and yt.dailyLike or yt.dailyComment
  1627. local getTime = field and field.getTime
  1628. local claimed = (getTime and Util.isSameDay(getTime)) or checkAccountYouxiTaskClaimed(account, taskId)
  1629. if claimed then
  1630. return Broadcast.sendErr(human, "今日已领取")
  1631. end
  1632. local dayCount = (field and field.dayTime and Util.isSameDay(field.dayTime)) and (field.count or 0) or 0
  1633. if dayCount < config.target then
  1634. return Broadcast.sendErr(human, "任务未完成")
  1635. end
  1636. elseif config.taskType == "weekly" then
  1637. local field = (taskId == 4) and yt.dailyLike or yt.dailyComment
  1638. local weekGetTime = field and field.weekGetTime
  1639. local claimed = (weekGetTime and isCurrentWeek(weekGetTime)) or checkAccountYouxiTaskClaimed(account, taskId)
  1640. if claimed then
  1641. return Broadcast.sendErr(human, "本周已领取")
  1642. end
  1643. if getWeekProgress(field) < config.target then
  1644. return Broadcast.sendErr(human, "任务未完成")
  1645. end
  1646. end
  1647. -- 发放奖励
  1648. BagLogic.cleanMomentItemList()
  1649. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, config.reward[1], config.reward[2])
  1650. local success, err = pcall(function()
  1651. BagLogic.addMomentItemList(human, "zhuanpan_once_reward")
  1652. end)
  1653. if not success then
  1654. Log.write(Log.LOGID_DEBUG, "[youxiTaskGetReward] 发放奖励失败 task=" .. taskId .. " err=" .. tostring(err))
  1655. return Broadcast.sendErr(human, "发放奖励失败")
  1656. end
  1657. -- 记录领取时间
  1658. if config.taskType == "once" then
  1659. yt.joinGetTime = now
  1660. elseif config.taskType == "daily" then
  1661. local key = (taskId == 2) and "dailyLike" or "dailyComment"
  1662. yt[key] = yt[key] or {}
  1663. yt[key].getTime = now
  1664. elseif config.taskType == "weekly" then
  1665. local key = (taskId == 4) and "dailyLike" or "dailyComment"
  1666. yt[key] = yt[key] or {}
  1667. yt[key].weekGetTime = now
  1668. end
  1669. Log.write(Log.LOGID_DEBUG, "[youxiTaskGetReward] 领取成功 task=" .. taskId .. " account=" .. tostring(human.db.account))
  1670. -- 领取后下发最新查询(不传 msg,不更新 SDK 数据)
  1671. youxiTaskQuery(human, nil)
  1672. end
  1673. -----------------------添加桌面(本地存储,账号级,同游戏圈写法)-----------------------
  1674. -- 检查账号下所有角色是否已添加过桌面(查本服MongoDB)
  1675. local function checkAccountDesktopAdded(account, channelId, platform)
  1676. if not account then return false end
  1677. local key = tostring(channelId) .. "_" .. tostring(platform)
  1678. LuaMongo.find(DB.db_char, {account = account}, {zhuanpan = 1})
  1679. while true do
  1680. local data = {}
  1681. if not LuaMongo.next(data) then break end
  1682. local da = data.zhuanpan and data.zhuanpan.desktopAdded
  1683. if da and da[key] then return true end
  1684. end
  1685. return false
  1686. end
  1687. -- 上报添加桌面
  1688. function dyDesktopSetLocal(human, channelId, platform)
  1689. if not channelId or not platform or platform == "" then
  1690. return Broadcast.sendErr(human, "参数缺失")
  1691. end
  1692. human.db.zhuanpan = human.db.zhuanpan or {}
  1693. human.db.zhuanpan.desktopAdded = human.db.zhuanpan.desktopAdded or {}
  1694. local key = tostring(channelId) .. "_" .. tostring(platform)
  1695. human.db.zhuanpan.desktopAdded[key] = true
  1696. Log.write(Log.LOGID_DEBUG, "[dyDesktopSetLocal] 添加桌面 account=" .. tostring(human.db.account)
  1697. .. " channelId=" .. tostring(channelId) .. " platform=" .. tostring(platform))
  1698. local ok, err = pcall(function()
  1699. dyDesktopQueryLocal(human, channelId, platform)
  1700. end)
  1701. if not ok then
  1702. Log.write(Log.LOGID_DEBUG, "[dyDesktopSetLocal] 错误: 下发查询失败: " .. tostring(err))
  1703. end
  1704. end
  1705. -- 查询桌面添加状态
  1706. function dyDesktopQueryLocal(human, channelId, platform)
  1707. if not channelId or not platform or platform == "" then
  1708. return Broadcast.sendErr(human, "参数缺失")
  1709. end
  1710. local msgRet = Msg.gc.GC_ZHUANPAN_DY_DESKTOP_QUERY
  1711. if not msgRet then return end
  1712. human.db.zhuanpan = human.db.zhuanpan or {}
  1713. human.db.zhuanpan.desktopAdded = human.db.zhuanpan.desktopAdded or {}
  1714. local key = tostring(channelId) .. "_" .. tostring(platform)
  1715. local added = human.db.zhuanpan.desktopAdded[key]
  1716. if not added then
  1717. added = checkAccountDesktopAdded(human.db.account, channelId, platform)
  1718. end
  1719. msgRet.channelId = channelId
  1720. msgRet.platform = platform
  1721. msgRet.isDesktopAdded = added and 1 or 0
  1722. Log.write(Log.LOGID_DEBUG, "[dyDesktopQueryLocal] account=" .. tostring(human.db.account)
  1723. .. " channelId=" .. tostring(channelId) .. " platform=" .. tostring(platform)
  1724. .. " isDesktopAdded=" .. tostring(msgRet.isDesktopAdded))
  1725. if human.fd then
  1726. Msg.send(msgRet, human.fd)
  1727. end
  1728. end
  1729. -----------------------添加桌面(跨服中心服存储,账号级多区服共享)-----------------------
  1730. --
  1731. ---- 从 newUniqueTag 解析出 serverId 和 account(格式: "channelID|serverTag|account")
  1732. --local function parseUniqueTag(newUniqueTag)
  1733. -- local parts = {}
  1734. -- for part in string.gmatch(newUniqueTag, "([^|]+)") do
  1735. -- table.insert(parts, part)
  1736. -- end
  1737. -- return tonumber(parts[2]), parts[3] -- serverId, account
  1738. --end
  1739. --
  1740. ---- 普通服:上报添加桌面 → 发消息给中心服写入
  1741. --function dyDesktopSet(human, channelId, platform)
  1742. -- if not channelId or not platform or platform == "" then
  1743. -- return Broadcast.sendErr(human, "参数缺失")
  1744. -- end
  1745. --
  1746. -- local newUniqueTag = human.db.newUniqueTag or human.newUniqueTag
  1747. -- if not newUniqueTag then
  1748. -- return Broadcast.sendErr(human, "账号标识缺失")
  1749. -- end
  1750. -- local serverId, account = parseUniqueTag(newUniqueTag)
  1751. --
  1752. -- local tMsg = InnerMsg.lw.LW_DESKTOP_SET
  1753. -- tMsg.nSrcServerID = Config.SVR_INDEX
  1754. -- tMsg.channelId = channelId
  1755. -- tMsg.serverId = serverId or 0
  1756. -- tMsg.account = account or ""
  1757. -- tMsg.platform = platform
  1758. -- InnerMsg.sendMsg(0, tMsg)
  1759. --
  1760. -- Log.write(Log.LOGID_DEBUG, "[dyDesktopSet] 发送中心服 channelId=" .. tostring(channelId)
  1761. -- .. " serverId=" .. tostring(serverId) .. " account=" .. tostring(account) .. " platform=" .. tostring(platform))
  1762. --end
  1763. --
  1764. ---- 普通服:查询桌面添加状态 → 发消息给中心服查询
  1765. --function dyDesktopQuery(human, channelId, platform)
  1766. -- if not channelId or not platform or platform == "" then
  1767. -- return Broadcast.sendErr(human, "参数缺失")
  1768. -- end
  1769. --
  1770. -- local newUniqueTag = human.db.newUniqueTag or human.newUniqueTag
  1771. -- if not newUniqueTag then
  1772. -- return Broadcast.sendErr(human, "账号标识缺失")
  1773. -- end
  1774. -- local serverId, account = parseUniqueTag(newUniqueTag)
  1775. --
  1776. -- local tMsg = InnerMsg.lw.LW_DESKTOP_QUERY
  1777. -- tMsg.nSrcServerID = Config.SVR_INDEX
  1778. -- tMsg.channelId = channelId
  1779. -- tMsg.serverId = serverId or 0
  1780. -- tMsg.account = account or ""
  1781. -- tMsg.platform = platform
  1782. -- InnerMsg.sendMsg(0, tMsg)
  1783. --
  1784. -- Log.write(Log.LOGID_DEBUG, "[dyDesktopQuery] 发送中心服 channelId=" .. tostring(channelId)
  1785. -- .. " serverId=" .. tostring(serverId) .. " account=" .. tostring(account) .. " platform=" .. tostring(platform))
  1786. --end
  1787. --
  1788. ---- 普通服:收到中心服查询回包,找到玩家下发 GC 协议
  1789. --function NS_DesktopQueryResult(msg)
  1790. -- -- 用三个字段重新拼出 newUniqueTag 来查找在线玩家
  1791. -- local newUniqueTag = tostring(msg.channelId) .. "|" .. tostring(msg.serverId) .. "|" .. tostring(msg.account)
  1792. -- local human = ObjHuman.onlineNewUniqueTag[newUniqueTag]
  1793. -- if not human or not human.fd then
  1794. -- Log.write(Log.LOGID_DEBUG, "[NS_DesktopQueryResult] 玩家不在线 newUniqueTag=" .. newUniqueTag)
  1795. -- return
  1796. -- end
  1797. --
  1798. -- local msgRet = Msg.gc.GC_ZHUANPAN_DY_DESKTOP_QUERY
  1799. -- if not msgRet then return end
  1800. --
  1801. -- msgRet.channelId = msg.channelId
  1802. -- msgRet.platform = msg.platform
  1803. -- msgRet.isDesktopAdded = msg.isDesktopAdded
  1804. --
  1805. -- Msg.send(msgRet, human.fd)
  1806. --end
  1807. --
  1808. ---- 中心服:收到普通服上报,写入 MongoDB 后回包最新状态
  1809. --function CS_DesktopSet(msg)
  1810. -- CommonDB.SetDesktopData(msg.channelId, msg.serverId, msg.account, msg.platform)
  1811. --
  1812. -- Log.write(Log.LOGID_DEBUG, "[CS_DesktopSet] 写入完成 channelId=" .. tostring(msg.channelId)
  1813. -- .. " serverId=" .. tostring(msg.serverId) .. " account=" .. tostring(msg.account) .. " platform=" .. tostring(msg.platform))
  1814. --
  1815. -- local tMsg = InnerMsg.wl.WL_DESKTOP_QUERY
  1816. -- tMsg.channelId = msg.channelId
  1817. -- tMsg.serverId = msg.serverId
  1818. -- tMsg.account = msg.account
  1819. -- tMsg.platform = msg.platform
  1820. -- tMsg.isDesktopAdded = 1
  1821. --
  1822. -- local fd = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
  1823. -- InnerMsg.sendMsg(fd, tMsg)
  1824. --end
  1825. --
  1826. ---- 中心服:收到普通服查询请求,读 MongoDB 后回包
  1827. --function CS_DesktopQuery(msg)
  1828. -- local added = CommonDB.IsDesktopAdded(msg.channelId, msg.serverId, msg.account, msg.platform)
  1829. --
  1830. -- local tMsg = InnerMsg.wl.WL_DESKTOP_QUERY
  1831. -- tMsg.channelId = msg.channelId
  1832. -- tMsg.serverId = msg.serverId
  1833. -- tMsg.account = msg.account
  1834. -- tMsg.platform = msg.platform
  1835. -- tMsg.isDesktopAdded = added and 1 or 0
  1836. --
  1837. -- local fd = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
  1838. -- InnerMsg.sendMsg(fd, tMsg)
  1839. --end
  1840. -- SDK 回调奖励发放(内部调用,与任务奖励独立)
  1841. -- items: {{itemId, count}, ...}
  1842. function youxiSdkReward(human, items)
  1843. if not items or #items == 0 then
  1844. Log.write(Log.LOGID_DEBUG, "[youxiSdkReward] items为空 account=" .. tostring(human.db.account))
  1845. return
  1846. end
  1847. BagLogic.cleanMomentItemList()
  1848. for _, v in ipairs(items) do
  1849. BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, v[1], v[2])
  1850. end
  1851. local success, err = pcall(function()
  1852. BagLogic.addMomentItemList(human, "zhuanpan_once_reward")
  1853. end)
  1854. if not success then
  1855. Log.write(Log.LOGID_DEBUG, "[youxiSdkReward] 发放失败 err=" .. tostring(err) .. " account=" .. tostring(human.db.account))
  1856. else
  1857. Log.write(Log.LOGID_DEBUG, "[youxiSdkReward] 发放成功 cnt=" .. #items .. " account=" .. tostring(human.db.account))
  1858. end
  1859. end