YjTreasureDB.lua 17 KB

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