YjTreasureDB.lua 17 KB

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