YjTreasureDB.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. local LuaMongo = _G.lua_mongo
  2. local DB = require("common.DB")
  3. local Util = require("common.Util")
  4. local YjTreasureExcel = require("excel.yjTreasure")
  5. local RoleDefine = require("role.RoleDefine")
  6. local CombatLogic = require("combat.CombatLogic")
  7. local YjTreasureLogic = require("yjTreasure.YjTreasureLogic")
  8. local CombatDefine = require("combat.CombatDefine")
  9. local PanelDefine = require("broadcast.PanelDefine")
  10. local Log = require("common.Log")
  11. local Config = require("Config")
  12. local CommonDB = require("common.CommonDB")
  13. local DBUpdate = { _id = nil }
  14. local DBUpdateField = {} --更新域
  15. YJ_TREASURE_DB = YJ_TREASURE_DB or {}
  16. YJ_Rank2Uuid = YJ_Rank2Uuid or {}
  17. YJ_Uuid2Role = YJ_Uuid2Role or {}
  18. function changeOldToNewDB()
  19. if _G.is_middle == true then return end
  20. LuaMongo.find(DB.db_yjTreasure)
  21. local yjTreasureDB = {}
  22. if not LuaMongo.next(yjTreasureDB) then
  23. return
  24. end
  25. YJ_TREASURE_DB = yjTreasureDB
  26. -- 老数据库中有数据
  27. if YJ_TREASURE_DB.time then
  28. -- 改成新数据
  29. CommonDB.setYjTreasureEndTime(YJ_TREASURE_DB.time)
  30. -- 初始化新库
  31. --initDB()
  32. -- 插入原有数据
  33. for uuid, roledata in pairs(YJ_TREASURE_DB.uuid2Roles) do
  34. roledata.uuid = uuid
  35. insertUuid2Role(roledata)
  36. YJ_Rank2Uuid[#YJ_Rank2Uuid + 1] = uuid
  37. end
  38. end
  39. sortYJboard()
  40. local removeDate = {}
  41. removeDate._id = yjTreasureDB._id
  42. LuaMongo.remove(DB.db_yjTreasure, removeDate)
  43. end
  44. function initAfterStart()
  45. if _G.is_middle == true then return end
  46. LuaMongo.find(DB.db_yjTreasure_role)
  47. while true do
  48. local roleDB = {}
  49. if not LuaMongo.next(roleDB) then
  50. break
  51. end
  52. if not YJ_Uuid2Role[roleDB.uuid] then
  53. YJ_Uuid2Role[roleDB.uuid] = roleDB
  54. YJ_Rank2Uuid[#YJ_Rank2Uuid + 1] = roleDB.uuid
  55. end
  56. end
  57. sortYJboard()
  58. end
  59. function getDBUuid2Roles()
  60. return YJ_Uuid2Role
  61. end
  62. function getDBrank2Uuid()
  63. return YJ_Rank2Uuid
  64. end
  65. function getRoleDataByUuid(uuid)
  66. local uuid2Roles = getDBUuid2Roles()
  67. if not uuid2Roles then return end
  68. return uuid2Roles[uuid]
  69. end
  70. function getLayerDataByUuid(uuid)
  71. local roleData = getRoleDataByUuid(uuid)
  72. if roleData and roleData.layerData then
  73. return roleData.layerData
  74. else
  75. print("[getLayerDataByUuid] 不存在对应的数据")
  76. return
  77. end
  78. end
  79. -- 获取出战英雄
  80. function getFightHeroObj(heroList)
  81. if not heroList then return end
  82. for pos in pairs(heroList) do
  83. local obj = heroList[pos]
  84. if obj and obj.isFight == 1 and getCombatObjHp(obj) > 0 then
  85. return obj, pos
  86. end
  87. end
  88. end
  89. -- 获取当前血
  90. function getCombatObjHp(obj)
  91. if obj.hp then return obj.hp end
  92. return obj.attrs[RoleDefine.HP] or 0
  93. end
  94. -- 获取最大血量
  95. function getCombatObjHpMax(obj)
  96. if obj.hpMax then return obj.hpMax end
  97. return obj.attrs[RoleDefine.HP] or 0
  98. end
  99. -- 获取allshop数据
  100. function getAllShopData(uuid)
  101. local roleData = getRoleDataByUuid(uuid)
  102. return roleData.allShop
  103. end
  104. function getGridsData(uuid)
  105. local layerData = getLayerDataByUuid(uuid)
  106. if not layerData then return end
  107. return layerData.grids
  108. end
  109. function getGridDataByIndex(uuid,gridIndex)
  110. local gridData = getGridsData(uuid)
  111. if not gridData then return end
  112. return gridData[gridIndex]
  113. end
  114. function getBuffData(uuid)
  115. local roleData = getRoleDataByUuid(uuid)
  116. return roleData.buff
  117. end
  118. function getKillMonsterData(uuid)
  119. local roleData = getRoleDataByUuid(uuid)
  120. return roleData.killMonster
  121. end
  122. function getRankByUuid(uuid)
  123. local rank2Uuid = getDBrank2Uuid()
  124. for i=1,#rank2Uuid do
  125. if uuid == rank2Uuid[i] then return i end
  126. end
  127. return 0
  128. end
  129. function getObjList(uuid)
  130. local roleData = getRoleDataByUuid(uuid)
  131. return roleData.objList
  132. end
  133. function insertUuid2Role(roleDB)
  134. LuaMongo.insert(DB.db_yjTreasure_role,roleDB)
  135. YJ_Uuid2Role[roleDB.uuid] = roleDB
  136. end
  137. function updateUuid2Role(roleDB,uuid)
  138. DBUpdate = {}
  139. DBUpdate.uuid = roleDB.uuid
  140. LuaMongo.update(DB.db_yjTreasure_role, DBUpdate, roleDB)
  141. YJ_Uuid2Role[roleDB.uuid] = roleDB
  142. end
  143. function updateRank2Uuid(rankDB)
  144. YJ_Uuid2Role[rankDB.rank] = rankDB.uuid
  145. end
  146. --更新楼层信息
  147. function updateLayerData(uuid,layerData)
  148. local roleData = getRoleDataByUuid(uuid)
  149. roleData.layerData = layerData
  150. updateUuid2Role(roleData)
  151. end
  152. --更新格子信息
  153. function updateGridsData(uuid,gridsData)
  154. local roleData = getRoleDataByUuid(uuid)
  155. roleData.layerData.grids = gridsData
  156. updateUuid2Role(roleData)
  157. end
  158. --更新db中排名信息
  159. function updateYJRank(rank2Uuid)
  160. YJ_Rank2Uuid = rank2Uuid
  161. end
  162. -- 设置出战
  163. function setFightHero(heroList, tpos)
  164. if not heroList then return end
  165. if tpos and heroList[tpos] and getCombatObjHp(heroList[tpos]) <= 0 then
  166. return -- 手动设置的出战对象不存在或者血量小于0
  167. end
  168. for pos in pairs(heroList) do
  169. local obj = heroList[pos]
  170. if obj then
  171. obj.isFight = 0
  172. end
  173. if obj and tpos == nil and getCombatObjHp(obj) > 0 then
  174. tpos = pos -- 自动选择出战
  175. end
  176. end
  177. if not tpos then return end
  178. local obj = heroList[tpos]
  179. obj.isFight = 1
  180. return true
  181. end
  182. --活动db中增加玩家
  183. function addPlayer(objList,rolebase,uuid)
  184. local uuid2Roles = YJ_Uuid2Role or {}
  185. local rank2Uuid = YJ_Rank2Uuid or {}
  186. local roleData = {
  187. objList = Util.copyTable(objList), --上阵英雄
  188. rolebase = Util.copyTable(rolebase), --人物信息
  189. layerData = nil, --当层信息(层数+格子信息)
  190. layerMax = nil, --历史最高层
  191. tansuo = nil, --探索度(已翻开格子数量)
  192. allShop = nil, --探险商城(包含神秘商人和商店所出现的所有道具)
  193. yjShoperCnt = nil, --遗迹商人
  194. killMonster = nil, --达标奖励 killMonster.killCnt = 0 killMonster.getList[id]
  195. yaojis = nil, --药剂(生命+死亡)yaojis[type1].nowCnt yaojis[type1].useCnt
  196. buff = nil, --属性加成 buff[key] = value
  197. sangdang = nil, --已扫荡次数
  198. time = os.time(), --参加时间
  199. item = nil, -- 累计道具
  200. uuid = uuid, -- uuid
  201. }
  202. creatLayerData(roleData,1)
  203. setFightHero(roleData.objList)
  204. uuid2Roles[uuid] = roleData
  205. insertUuid2Role(roleData)
  206. rank2Uuid[#rank2Uuid + 1] = uuid
  207. return uuid2Roles[uuid]
  208. end
  209. --根据格子类型获得当前已生成格子中已有该类型的数量
  210. local function getCntByGridType(grids,gridType)
  211. if not grids then return 0 end
  212. local cnt = 0
  213. for index,data in pairs(grids) do
  214. if data.gridType == gridType then
  215. cnt = cnt + 1
  216. end
  217. end
  218. return cnt
  219. end
  220. local function getOpenGroupByIndex(gridIndex)
  221. local killOpenGridsCnf = YjTreasureExcel.define[1].killOpenGrids
  222. for i=1,#killOpenGridsCnf do
  223. for j=1,#killOpenGridsCnf[i] do
  224. if gridIndex == killOpenGridsCnf[i][j] then
  225. return i
  226. end
  227. end
  228. end
  229. end
  230. local function getKeyIndexByGroupID(openGroupID)
  231. local keyGroup = nil
  232. if openGroupID == 4 then
  233. keyGroup = 1
  234. elseif openGroupID == 1 then
  235. keyGroup = 4
  236. elseif openGroupID == 2 then
  237. keyGroup = 3
  238. elseif openGroupID == 3 then
  239. keyGroup = 2
  240. end
  241. local killOpenGridsCnf = YjTreasureExcel.define[1].killOpenGrids[keyGroup]
  242. local random = math.random(1,#killOpenGridsCnf)
  243. return killOpenGridsCnf[random]
  244. end
  245. --根据层数创造格子信息
  246. function createGrids(layer,grids)
  247. local defCnf = YjTreasureExcel.define[1]
  248. local layCnf = YjTreasureExcel.layer[layer]
  249. local gridCnf = YjTreasureExcel.grid
  250. local keyIndex = nil
  251. for gridIndex=1,YjTreasureLogic.YJ_TREASURE_GRID_MAX do
  252. local grid = {
  253. gridType = nil, --格子类型
  254. monsterObjList = nil, --格子为怪物或boss时,记录outID中的objList
  255. monsterRBase = nil, --怪物基本信息
  256. item = nil, --格子为道具时,记录道具{id,cnt}
  257. soul = nil, --灵魂拷问 soulID
  258. question = nil, --博闻强识
  259. treasureShop = nil, --商店
  260. isOpen = nil, --是否打开
  261. }
  262. for monsterGrid=1,#defCnf.monstersGrid do
  263. if gridIndex == defCnf.monstersGrid[monsterGrid] then
  264. grid.gridType = YjTreasureLogic.YJ_TREASURE_GRID_MONSTER
  265. local objList,_,rolebase = CombatLogic.getMonsterObjList(layCnf.monsters[monsterGrid])
  266. grid.monsterObjList = Util.copyTable(objList)
  267. grid.monsterRBase = Util.copyTable(rolebase)
  268. grid.isOpen = 1
  269. end
  270. end
  271. if gridIndex == defCnf.bossGrid then
  272. grid.gridType = YjTreasureLogic.YJ_TREASURE_GRID_BOSS
  273. local objList,_,rolebase = CombatLogic.getMonsterObjList(layCnf.boss)
  274. grid.monsterObjList = Util.copyTable(objList)
  275. grid.monsterRBase = Util.copyTable(rolebase)
  276. grid.isOpen = nil
  277. end
  278. if keyIndex and keyIndex == gridIndex then
  279. grid.gridType = YjTreasureLogic.YJ_TREASURE_GRID_KEY
  280. keyIndex = nil
  281. end
  282. if not grid.gridType then
  283. local totalWeight = 0
  284. for i=1,#gridCnf do
  285. local nowCnt = getCntByGridType(grids,i)
  286. if nowCnt < gridCnf[i].openMax or gridCnf[i].openMax == 0 then
  287. totalWeight = totalWeight + gridCnf[i].weight
  288. end
  289. end
  290. local random = math.random(1,totalWeight)
  291. local randWeight = 0
  292. for i=1,#gridCnf do
  293. local nowCnt = getCntByGridType(grids,i)
  294. if nowCnt < gridCnf[i].openMax or gridCnf[i].openMax == 0 then
  295. if gridCnf[i].weight ~= 0 then
  296. randWeight = randWeight + gridCnf[i].weight
  297. if random <= randWeight then
  298. grid.gridType = i
  299. if grid.gridType == YjTreasureLogic.YJ_TREASURE_GRID_BOX then --宝箱特殊处理 安置钥匙
  300. local openGroupID = getOpenGroupByIndex(gridIndex)
  301. keyIndex = getKeyIndexByGroupID(openGroupID)
  302. --print("keyIndex",keyIndex)
  303. if keyIndex < gridIndex then
  304. grids[keyIndex] = {}
  305. grids[keyIndex].gridType = YjTreasureLogic.YJ_TREASURE_GRID_KEY
  306. end
  307. end
  308. if grid.gridType == YjTreasureLogic.YJ_TREASURE_GRID_ITEM then --权重道具生成
  309. local layerItemsCnf = layCnf.layerItems
  310. local totalItemWeight = 0
  311. for j=1,#layerItemsCnf do
  312. totalItemWeight = totalItemWeight + layerItemsCnf[j][3]
  313. end
  314. local randItem = math.random(1,totalItemWeight)
  315. local randItemWight = 0
  316. for g=1,#layerItemsCnf do
  317. randItemWight = randItemWight + layerItemsCnf[g][3]
  318. if randItem <= randItemWight then
  319. grid.item = {}
  320. grid.item.itemID = layerItemsCnf[g][1]
  321. grid.item.itemCnt = layerItemsCnf[g][2]
  322. break
  323. end
  324. end
  325. end
  326. break
  327. end
  328. end
  329. end
  330. end
  331. end
  332. grids[gridIndex] = grid
  333. -- if grids[gridIndex].gridType == YjTreasureLogic.YJ_TREASURE_GRID_ITEM then
  334. -- require("common.Util").printTable(grids[gridIndex])
  335. -- end
  336. end
  337. return grids
  338. end
  339. --创建层的信息
  340. function creatLayerData(roleData,layer)
  341. local layerData = {
  342. layer = layer, --层数
  343. grids = {}, --格子信息
  344. getItems = nil, --本层中所获得的物品 getItems[ID] = cnt
  345. }
  346. createGrids(layer,layerData.grids)
  347. roleData.layerData = layerData
  348. return layerData
  349. end
  350. function creatLayerDataByUuid(uuid,layer)
  351. local roleData = getRoleDataByUuid(uuid)
  352. local layerData = creatLayerData(roleData,layer)
  353. updateUuid2Role(roleData)
  354. return layerData
  355. end
  356. --神秘商人刷出的道具 和 商店刷出的道具 都会加入列表中
  357. function addItems2AllShop(uuid,shopType,items)
  358. local roleData = getRoleDataByUuid(uuid)
  359. local allShopIDList = {}
  360. roleData.allShop = roleData.allShop or {}
  361. for i=1,#items do
  362. local shopItem = {
  363. shopType = shopType,
  364. shopConfID = items[i],
  365. isBuy = nil, --已购买
  366. }
  367. local index = #roleData.allShop + 1
  368. roleData.allShop[index] = shopItem
  369. allShopIDList[#allShopIDList + 1] = index
  370. end
  371. return allShopIDList
  372. end
  373. --创建商店三个权重物品
  374. function makeTreasureShop(uuid,gridIndex)
  375. local roleData = getRoleDataByUuid(uuid)
  376. local layerData = roleData.layerData
  377. local gridData = layerData.grids[gridIndex]
  378. local treasureShopCnf = YjTreasureExcel.treasureShop
  379. local randItem = {}
  380. local randCnt = math.random(1,3)
  381. for rand=1,randCnt do
  382. local totalWeight = 0
  383. for i=1,#treasureShopCnf do
  384. if not randItem[i] then
  385. totalWeight = totalWeight + treasureShopCnf[i].weight
  386. end
  387. end
  388. local randWeight = math.random(1,totalWeight)
  389. local weight = 0
  390. for i=1,#treasureShopCnf do
  391. if not randItem[i] then
  392. weight = weight + treasureShopCnf[i].weight
  393. if randWeight <= weight then
  394. randItem[i] = i
  395. break
  396. end
  397. end
  398. end
  399. end
  400. local items = {}
  401. for treasureShopID in pairs(randItem) do
  402. items[#items + 1] = treasureShopID
  403. end
  404. local allShopIDList = addItems2AllShop(uuid,YjTreasureLogic.YJ_SHOP_TREASURE_SHOP,items)
  405. gridData.treasureShop = {}
  406. for i=1,#allShopIDList do
  407. gridData.treasureShop[#gridData.treasureShop + 1] = allShopIDList[i]
  408. end
  409. updateUuid2Role(roleData)
  410. end
  411. --设置玩家购买商品
  412. function setShopItemBuy(uuid,allShopID)
  413. local roleData = getRoleDataByUuid(uuid)
  414. roleData.allShop[allShopID].isBuy = allShopID
  415. updateUuid2Role(roleData)
  416. end
  417. --创造灵魂拷问题
  418. function makeSoul(uuid,gridIndex)
  419. local gridsData = getGridsData(uuid)
  420. local soulCnf = YjTreasureExcel.soul
  421. local totalWeight = 0
  422. for i=1,#soulCnf do
  423. totalWeight = totalWeight + soulCnf[i].weight
  424. end
  425. local rand = math.random(1,totalWeight)
  426. local weight = 0
  427. for i=1,#soulCnf do
  428. weight = weight + soulCnf[i].weight
  429. if rand <= weight then
  430. gridsData[gridIndex].soul = i
  431. updateGridsData(uuid,gridsData)
  432. return i
  433. end
  434. end
  435. end
  436. --创造博闻强识问题
  437. function makeQuestion(uuid,gridIndex)
  438. local gridsData = getGridsData(uuid)
  439. local questionData = {
  440. rightCnt = nil, --答对次数
  441. answer = nil, --已回答 answer[id] = select
  442. list = {}, --问题列表
  443. }
  444. local questionCnf = YjTreasureExcel.question
  445. local randList = {}
  446. for i=1,3 do
  447. local totalWeight = 0
  448. for question=1,#questionCnf do
  449. if not randList[question] then
  450. totalWeight = totalWeight + questionCnf[i].weight
  451. end
  452. end
  453. local rand = math.random(1,totalWeight)
  454. local weight = 0
  455. for question=1,#questionCnf do
  456. if not randList[question] then
  457. weight = weight + questionCnf[question].weight
  458. if rand <= weight then
  459. randList[question] = true
  460. questionData.list[i] = question
  461. break
  462. end
  463. end
  464. end
  465. end
  466. gridsData[gridIndex].question = questionData
  467. updateGridsData(uuid,gridsData)
  468. return questionData
  469. end
  470. --权重遗迹商人商品
  471. function makeYjShoperItem(uuid)
  472. local roleData = getRoleDataByUuid(uuid)
  473. local yjShoperCnf = YjTreasureExcel.yjShoper
  474. local totalWeight = 0
  475. for i=1,#yjShoperCnf do
  476. totalWeight = totalWeight + yjShoperCnf[i].weight
  477. end
  478. local rand = math.random(1,totalWeight)
  479. local weight = 0
  480. local item = {}
  481. for yjShoperCnfID=1,#yjShoperCnf do
  482. weight = weight + yjShoperCnf[yjShoperCnfID].weight
  483. if rand <= weight then
  484. item[1] = yjShoperCnfID
  485. break
  486. end
  487. end
  488. local allShopIDList = addItems2AllShop(uuid,YjTreasureLogic.YJ_SHOP_YJSHOPER,item)
  489. return allShopIDList[1],item[1]
  490. end
  491. function makeBuff(uuid)
  492. local roleData = getRoleDataByUuid(uuid)
  493. local buffConf = YjTreasureExcel.buff
  494. local totalWeight = 0
  495. for i=1,#buffConf do
  496. totalWeight = totalWeight + buffConf[i].weight
  497. end
  498. local buffAttr = nil
  499. local rand = math.random(1,totalWeight)
  500. local weight = 0
  501. for i=1,#buffConf do
  502. weight = weight + buffConf[i].weight
  503. if rand <= weight then
  504. roleData.buff = roleData.buff or {}
  505. local key = buffConf[i].gap[1]
  506. local value = buffConf[i].gap[2]
  507. roleData.buff[key] = (roleData.buff[key] or 0) + value
  508. buffAttr = {key,value}
  509. break
  510. end
  511. end
  512. return buffAttr
  513. end
  514. --排行榜
  515. local function cmpYJRank(auuid, buuid)
  516. local aRoleData = getRoleDataByUuid(auuid)
  517. local bRoleData = getRoleDataByUuid(buuid)
  518. local aLayerData = aRoleData.layerData
  519. local bLayerData = bRoleData.layerData
  520. if not aLayerData or not bLayerData then
  521. if aLayerData and not bLayerData then
  522. return true
  523. end
  524. return false
  525. end
  526. if aLayerData.layer ~= bLayerData.layer then
  527. return aLayerData.layer > bLayerData.layer
  528. end
  529. local aTansuo = aRoleData.tansuo or 0
  530. local bTansuo = bRoleData.tansuo or 0
  531. if aTansuo ~= bTansuo then
  532. return aTansuo > bTansuo
  533. end
  534. return aRoleData.time > bRoleData.time
  535. end
  536. function sortYJboard()
  537. local rank2Uuid = getDBrank2Uuid()
  538. if not rank2Uuid then return end
  539. table.sort(rank2Uuid, cmpYJRank)
  540. updateYJRank(rank2Uuid)
  541. end
  542. --改变格子属性
  543. function gridTypeChange(uuid,gridsData,gridIndex,gridType,isOpen)
  544. gridsData[gridIndex] = {}
  545. gridsData[gridIndex].gridType = gridType
  546. if isOpen then
  547. isOpen = 1
  548. end
  549. gridsData[gridIndex].isOpen = isOpen
  550. updateGridsData(uuid,gridsData)
  551. return gridsData[gridIndex]
  552. end
  553. function addItem(item,uuid)
  554. local roleData = getRoleDataByUuid(uuid)
  555. roleData.item = roleData.item or {}
  556. for k,v in pairs(item) do
  557. roleData.item[k] = roleData.item[k] or 0
  558. roleData.item[k] = roleData.item[k] + v
  559. end
  560. updateUuid2Role(roleData)
  561. end