ValleyLogic.lua 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. --------------------------------------------------------------
  2. -- 荣耀峡谷(龙族战场)
  3. -- 因为活动以服为单位,建议合服时候直接清空活动数据
  4. -- 休战->报名->匹配->侦查->战斗->休战
  5. -- 规则说明 notice.xlsx-helpMisc-85
  6. -- common db
  7. -- db.state 当前阶段
  8. -- db.startTime 阶段开始时间戳
  9. -- db.endTime 阶段结束时间戳
  10. -- db.round 战斗阶段轮次
  11. -- db.roundState 战斗阶段轮次状态
  12. -- db.signList [本服]报名列表 [uuid]={roadIndex,zhandouli}
  13. -- db.roadList [本服]各路入驻情况 [roadIndex]={[index]=uuid}
  14. -- db.roadListSend [本服]同步列表
  15. -- db.mainInfos [跨服]活动服基础信息
  16. -- db.matchList [跨服]分组列表 [index]={svrIndex1, svrIndex2...}
  17. --------------------------------------------------------------
  18. local LuaMongo = _G.lua_mongo
  19. local DB = require("common.DB")
  20. local Config = require("Config")
  21. local ValleyExcel = require("excel.valley")
  22. local ValleyDefine = ValleyExcel.define[1]
  23. local MailExcel = require("excel.mail")
  24. local CommonDB = require("common.CommonDB")
  25. local Util = require("common.Util")
  26. local Lang = require("common.Lang")
  27. local Msg = require("core.Msg")
  28. local InnerMsg = require("core.InnerMsg")
  29. local ObjHuman = require("core.ObjHuman")
  30. local Broadcast = require("broadcast.Broadcast")
  31. local Grid = require("bag.Grid")
  32. local BagLogic = require("bag.BagLogic")
  33. local CombatLogic = require("combat.CombatLogic")
  34. local CombatDefine = require("combat.CombatDefine")
  35. local CombatPosLogic = require("combat.CombatPosLogic")
  36. local HeroGrid = require("hero.HeroGrid")
  37. local JjcActLogic = require("jjc.JjcActLogic")
  38. local MailManager = require("mail.MailManager")
  39. local MiddleConnect = require("middle.MiddleConnect")
  40. local RoleDBLogic = require("role.RoleDBLogic")
  41. local RoleLogic = require("role.RoleLogic")
  42. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  43. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  44. local ValleyTask = require("valley.ValleyTask")
  45. local ValleyMiddle = require("valley.ValleyMiddle")
  46. local HeroLogic = require("hero.HeroLogic")
  47. -- 活动状态
  48. STATE_ACT_NO_OPEN = 0 -- 未开放
  49. STATE_ACT_READY = 1 -- 休战
  50. STATE_ACT_SIGN = 2 -- 报名
  51. STATE_ACT_MATCH = 3 -- 匹配
  52. STATE_ACT_EXPLORE = 4 -- 侦查
  53. STATE_ACT_FIGHT = 5 -- 战斗(行军->准备->战斗)
  54. -- 战斗阶段的状态
  55. STATE_FIGHT_END = 0 -- 结束
  56. STATE_FIGHT_MOVE = 1 -- 行军
  57. STATE_FIGHT_READY = 2 -- 准备/鼓舞
  58. STATE_FIGHT_FIGHT = 3 -- 战斗
  59. ROAD_MAX_CNT = 3 -- 一共有x路营地
  60. ROAD_POINT_CNT = 3 -- 每路有x个目标点
  61. MAX_INSPIRE_CNT = 100 -- 鼓舞进度条上限
  62. TIMER_SEND_DATA = 3 -- 每次发送x个玩家数据到跨服
  63. local fakeHuman = {}
  64. -- 获得活动最新状态 state, stateEndTime, stateStartTime
  65. local function getNewState()
  66. local nowTime = os.time()
  67. local weekStartTime = Util.getWeekStartTime(nowTime)
  68. local scf = ValleyDefine.startTime
  69. local startTime = weekStartTime + (scf.week - 1) * 86400 + scf.hour * 3600 + scf.min * 60
  70. local ecf = ValleyDefine.endTime
  71. local endTime = weekStartTime + (ecf.week - 1) * 86400 + ecf.hour * 3600 + ecf.min * 60
  72. if startTime <= nowTime and nowTime < endTime then
  73. local signEndTime = startTime + ValleyDefine.signTime
  74. if nowTime < signEndTime then
  75. return STATE_ACT_SIGN, signEndTime, startTime
  76. end
  77. local matchEndTime = signEndTime + ValleyDefine.matchTime
  78. if nowTime < matchEndTime then
  79. return STATE_ACT_MATCH, matchEndTime, signEndTime
  80. end
  81. local exploreEndTime = matchEndTime + ValleyDefine.exploreTime
  82. if nowTime < exploreEndTime then
  83. return STATE_ACT_EXPLORE, exploreEndTime, matchEndTime
  84. end
  85. return STATE_ACT_FIGHT, endTime, exploreEndTime
  86. end
  87. if nowTime < startTime then
  88. return STATE_ACT_READY, startTime, endTime - 7 * 86400
  89. end
  90. return STATE_ACT_READY, startTime + 7 * 86400, endTime
  91. end
  92. -- 获取活动当前状态(显示用)
  93. function getState()
  94. if not _G.is_middle and MiddleConnect.IS_MIDDLE_CONNECT ~= true then
  95. return STATE_ACT_NO_OPEN, 0
  96. end
  97. local commonDB = getCommonDB()
  98. if not commonDB.state then
  99. onTimer()
  100. commonDB = getCommonDB()
  101. end
  102. local leftTime = math.max(commonDB.endTime - os.time(), 0)
  103. return commonDB.state, leftTime, commonDB.startTime
  104. end
  105. -- 获取轮次信息 第x轮 阶段
  106. local function getNewRound(startTime)
  107. local time = math.max(os.time() - startTime, 1)
  108. local roundMaxTime = ValleyDefine.roundMoveTime + ValleyDefine.roundReadyTime + ValleyDefine.roundFightTime
  109. local round = math.ceil(time / roundMaxTime)
  110. local roundTime = time % roundMaxTime
  111. local roundStateEndTime = startTime + (round - 1) * roundMaxTime
  112. if roundTime < ValleyDefine.roundMoveTime then
  113. return round, STATE_FIGHT_MOVE, roundStateEndTime + ValleyDefine.roundMoveTime
  114. end
  115. if roundTime < ValleyDefine.roundMoveTime + ValleyDefine.roundReadyTime then
  116. return round, STATE_FIGHT_READY, roundStateEndTime + ValleyDefine.roundMoveTime + ValleyDefine.roundReadyTime
  117. end
  118. return round, STATE_FIGHT_FIGHT, roundStateEndTime + roundMaxTime
  119. end
  120. -- 获取活动当前轮次
  121. function getRound()
  122. local commonDB = getCommonDB()
  123. if not commonDB.state then
  124. onTimer()
  125. commonDB = getCommonDB()
  126. end
  127. return commonDB.round, commonDB.roundState, commonDB.roundStateEndTime
  128. end
  129. -- 是否开放
  130. function isOpen(human)
  131. local state, leftTime, startTime = getState()
  132. if state == STATE_ACT_NO_OPEN then -- 跨服未启动
  133. return Broadcast.sendErr(human, Lang.MIDDLE_SVR_ERR_CONNECT)
  134. end
  135. -- 判断开放等级+开服天数
  136. -- if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_10013, true) then
  137. -- return
  138. -- end
  139. return state, leftTime, startTime
  140. end
  141. ----------------------------------------------- db --------------------------------------------------
  142. -- 清空
  143. function cleanDB()
  144. local commonDB = getCommonDB()
  145. commonDB.round = nil
  146. commonDB.roundState = nil
  147. commonDB.signList = nil
  148. commonDB.roadList = nil
  149. commonDB.roadListSend = nil
  150. commonDB.mainInfos = nil
  151. commonDB.matchList = nil
  152. saveCommonDB(commonDB)
  153. LuaMongo.remove(DB.db_valley)
  154. LuaMongo.remove(DB.db_valley_record)
  155. end
  156. function saveCommonDB(data)
  157. data = data or {}
  158. CommonDB.updateValue(CommonDB.KEY_VALLEY, data)
  159. end
  160. local TEMP_COMMONDB = {}
  161. function getCommonDB()
  162. return CommonDB.getValueByKey(CommonDB.KEY_VALLEY) or TEMP_COMMONDB
  163. end
  164. -- 是否报名
  165. function getSignData(uuid)
  166. local commonDB = getCommonDB()
  167. if not commonDB.signList then
  168. return
  169. end
  170. return commonDB.signList[uuid]
  171. end
  172. -- 设置报名
  173. function addSignData(human)
  174. local uuid = human.db._id
  175. local commonDB = getCommonDB()
  176. commonDB.signList = commonDB.signList or {}
  177. if commonDB.signList[uuid] then
  178. return
  179. end
  180. local signData = {}
  181. signData.roadIndex = 0 -- 入驻x路
  182. signData.exploreCnt = 0 -- 侦查次数
  183. signData.exploreItems = nil -- 侦查未领取道具 [itemID] = itemCnt
  184. signData.exploreLogs = {} -- 侦查日志 [index] = id
  185. signData.zhandouli = human.db.zhandouli
  186. commonDB.signList[uuid] = signData
  187. saveCommonDB(commonDB)
  188. return signData
  189. end
  190. -- 获取入驻哪一路
  191. function getRoadIndex(uuid)
  192. local signData = getSignData(uuid)
  193. if not signData then return end
  194. return signData.roadIndex
  195. end
  196. -- 获取某路的入驻列表
  197. function getRoadUuids(roadIndex)
  198. local commonDB = getCommonDB()
  199. if not commonDB.roadList then
  200. return
  201. end
  202. return commonDB.roadList[roadIndex]
  203. end
  204. -- 从某路移除
  205. function delFromRoad(uuid, noSave)
  206. local signData = getSignData(uuid)
  207. if not signData or signData.roadIndex < 1 then
  208. return
  209. end
  210. local roadIndex = signData.roadIndex
  211. local uuids = getRoadUuids(roadIndex)
  212. local uuidsLen = uuids and #uuids or 0
  213. local cnt = 0
  214. for i = 1, uuidsLen do
  215. local tuuid = uuids[i]
  216. uuids[i] = nil
  217. if tuuid ~= uuid then
  218. cnt = cnt + 1
  219. uuids[cnt] = tuuid
  220. end
  221. end
  222. signData.roadIndex = 0
  223. if not noSave then
  224. saveCommonDB(commonDB)
  225. end
  226. return roadIndex
  227. end
  228. -- 入驻某路
  229. function addToRoad(uuid, roadIndex, noSave)
  230. delFromRoad(uuid, true)
  231. local signData = getSignData(uuid)
  232. if not signData then return end
  233. if signData.roadIndex > 0 then
  234. return
  235. end
  236. if roadIndex < 1 then return end
  237. signData.roadIndex = roadIndex
  238. local commonDB = getCommonDB()
  239. commonDB.roadList = commonDB.roadList or {}
  240. if not commonDB.roadList[roadIndex] then
  241. commonDB.roadList[roadIndex] = {}
  242. end
  243. local cnt = #commonDB.roadList[roadIndex]
  244. commonDB.roadList[roadIndex][cnt + 1] = uuid
  245. if not noSave then
  246. saveCommonDB(commonDB)
  247. end
  248. return roadIndex
  249. end
  250. -- 生成侦查道具
  251. function randomExploreID()
  252. local weight = 0
  253. for _, config in pairs(ValleyExcel.explore) do
  254. weight = weight + config.weight
  255. end
  256. if weight < 1 then return end
  257. local r = math.random(1, weight)
  258. for id, config in pairs(ValleyExcel.explore) do
  259. if r <= config.weight then
  260. return id, config
  261. end
  262. r = r - config.weight
  263. end
  264. end
  265. -- 平均战力
  266. local function getMainInfoZDL()
  267. local commonDB = getCommonDB()
  268. if not commonDB.signList then
  269. return 0
  270. end
  271. local cnt = 0
  272. local sum = 0
  273. for _, signData in pairs(commonDB.signList) do
  274. cnt = cnt + 1
  275. sum = sum + signData.zhandouli
  276. end
  277. if cnt > 0 then
  278. return math.floor(sum / cnt)
  279. end
  280. return 0
  281. end
  282. -- 创建服基础信息
  283. function createMainInfo()
  284. local mainInfo = {}
  285. mainInfo.svrIndex = Config.SVR_INDEX
  286. mainInfo.svrName = Config.SVR_NAME
  287. mainInfo.zhandouli = getMainInfoZDL()
  288. mainInfo.inspireCnt = nil -- 鼓舞次数
  289. mainInfo.inspireList = nil -- 鼓舞列表
  290. mainInfo.targetSvrIndex = nil -- 对手服务器 0表示没有对手
  291. mainInfo.roadList = {} -- 入驻列表 [roadIndex][index] = uuid
  292. mainInfo.fightIndexList = {} -- 出战索引 [roadIndex] = index
  293. mainInfo.fightIndexChange = nil -- 本轮结束是否换人 [roadIndex] = boolean (true表示换人)
  294. mainInfo.fightWinCnt = 0 -- 本轮胜利场数
  295. mainInfo.fightFailCnt = 0 -- 本轮失败场数
  296. mainInfo.fightResult = nil -- 本轮战斗结果 [roadIndex] = 胜负
  297. mainInfo.fightVideo = nil -- 本轮战斗录像 [roadIndex] = videoUuid
  298. mainInfo.moveList = {} -- 移动列表 [uuid] = pos
  299. mainInfo.logReads = {} -- 日志读取列表 [uuid] = boolean
  300. mainInfo.logs = {} -- 日志列表 [index] = videoUuid
  301. mainInfo.board = {} -- 排行榜 [rank] = uuid
  302. return mainInfo
  303. end
  304. -- 创建角色数据
  305. function createPlayerData(uuid, roadIndex)
  306. local db = RoleDBLogic.getDb(uuid, RoleLogic.getCombatField())
  307. if not db then return end
  308. fakeHuman.db = db
  309. local combatType = CombatDefine.COMBAT_TYPE17
  310. local objList, helpList, rolebase, formation = CombatLogic.getHumanObjList(fakeHuman, combatType)
  311. if not objList then return end
  312. rolebase.tili = ValleyDefine.initTili -- 初始体力 每打完一场扣1
  313. rolebase.winCnt = 0 -- 胜利场数
  314. rolebase.name = "[" .. Config.SVR_NAME .. "]" .. rolebase.name -- 名字拼接上服务名前缀
  315. rolebase.body = CombatLogic.getRoleBody(fakeHuman, combatType) -- 外显形象
  316. rolebase.time = os.time()
  317. local playerData = {}
  318. playerData._id = uuid
  319. playerData.roadIndex = roadIndex
  320. playerData.rolebase = rolebase
  321. playerData.objList = objList
  322. playerData.helpList = helpList
  323. playerData.formation = formation
  324. return playerData
  325. end
  326. -- 根据鼓舞次数获取额外属性
  327. function getAttrsUp(inspireCnt1, inspireCnt2, camp)
  328. local cnt = 0
  329. if camp == CombatDefine.ATTACK_SIDE then
  330. cnt = inspireCnt1 - inspireCnt2
  331. else
  332. cnt = inspireCnt2 - inspireCnt1
  333. end
  334. cnt = math.min(cnt, ValleyDefine.inspireAttrCnt)
  335. if cnt < 1 then
  336. return
  337. end
  338. local attrsUp = {}
  339. for _, attr in ipairs(ValleyDefine.inspireAttr) do
  340. local attrUp = {}
  341. attrUp[1] = attr[1]
  342. attrUp[2] = attr[2] * cnt
  343. attrsUp[#attrsUp + 1] = attrUp
  344. end
  345. return attrsUp
  346. end
  347. ------------------------------------------------ msg --------------------------------------------------
  348. -- 默认信息
  349. local TEMP_MAIN_INFO = nil
  350. local function getMainInfoCache()
  351. if not TEMP_MAIN_INFO then
  352. TEMP_MAIN_INFO = createMainInfo()
  353. end
  354. return TEMP_MAIN_INFO
  355. end
  356. -- 营地是否被攻破
  357. function isRoadDie(data, roadIndex)
  358. if not data then return true end
  359. local fightIndex = data.fightIndexList[roadIndex] or 1
  360. local list = data.roadList[roadIndex]
  361. local listLen = list and #list or 0
  362. return fightIndex > listLen
  363. end
  364. -- 获取破营数
  365. function getMainStar(data1, data2)
  366. local star = 0
  367. for i = 1, ROAD_MAX_CNT do
  368. -- (敌方阵营被攻破)敌方某路没人,己方还有人
  369. if isRoadDie(data2, i) and not isRoadDie(data1, i) then
  370. star = star + 1
  371. end
  372. end
  373. return star
  374. end
  375. -- 封装主界面基础信息
  376. local function fontMainInfo(net, data1, data2)
  377. net.svrName = data1 and data1.svrName or ""
  378. net.star = getMainStar(data1, data2)
  379. net.roadStates[0] = ROAD_MAX_CNT
  380. for i = 1, ROAD_MAX_CNT do
  381. net.roadStates[i] = 0
  382. -- (己方阵营被攻破)己方某路没人,敌方还有人
  383. if isRoadDie(data1, i) and not isRoadDie(data2, i) then
  384. net.roadStates[i] = 1
  385. end
  386. end
  387. end
  388. -- 主界面查询
  389. local TEMP_QUERY_DATA = {}
  390. function query(human)
  391. local state, leftTime = isOpen(human)
  392. if not state then return end
  393. if state ~= STATE_ACT_FIGHT then
  394. return sendQuery(human.db._id, TEMP_QUERY_DATA)
  395. end
  396. local msgInner = InnerMsg.lw.LW_VALLEY_QUERY
  397. msgInner.uuid = uuid
  398. InnerMsg.sendMsg(0, msgInner)
  399. end
  400. -- 发送主界面信息
  401. function sendQuery(uuid, data)
  402. local human = ObjHuman.onlineUuid[uuid]
  403. if not human then return end
  404. local state, leftTime = data.state, data.leftTime
  405. if not state or not leftTime then
  406. state, leftTime = getState()
  407. end
  408. if not state then return end
  409. local msgRet = Msg.gc.GC_VALLEY_QUERY
  410. local dataNet = msgRet.data
  411. dataNet.state = state
  412. dataNet.leftTime = leftTime
  413. dataNet.taskRed = ValleyTask.isRed(human) and 1 or 0
  414. dataNet.logRed = data.logRed and 1 or 0
  415. local mainInfo1 = data.mainInfo1 or getMainInfoCache()
  416. fontMainInfo(dataNet.info1, mainInfo1, data.mainInfo2)
  417. fontMainInfo(dataNet.info2, data.mainInfo2, mainInfo1)
  418. -- 是否报名
  419. local signData = getSignData(uuid)
  420. dataNet.isSign = signData and 1 or 0
  421. dataNet.roadIndex = signData and signData.roadIndex or 0
  422. -- 探索阶段额外信息
  423. dataNet.exploreData[0] = 0
  424. if state == STATE_ACT_EXPLORE then
  425. dataNet.exploreData[0] = 1
  426. local exploreDataNet = dataNet.exploreData[1]
  427. exploreDataNet.time = ValleyDefine.exploreTime - leftTime
  428. exploreDataNet.maxTime = ValleyDefine.exploreTime
  429. exploreDataNet.exploreRed = isRedExplore(human) and 1 or 0
  430. end
  431. -- 战斗阶段额外信息
  432. dataNet.fightData[0] = 0
  433. if state == STATE_ACT_FIGHT then
  434. dataNet.fightData[0] = 1
  435. local fightDataNet = dataNet.fightData[1]
  436. fightDataNet.fightState = data.roundState or 0
  437. fightDataNet.leftTime = (data.roundStateEndTime - os.time()) or 0
  438. fightDataNet.players[0] = data.players and #data.players or 0
  439. for i = 1, fightDataNet.players[0] do
  440. Util.copyTableSimple(data.players[i], fightDataNet.players[i])
  441. end
  442. end
  443. -- Msg.trace(msgRet)
  444. Msg.send(msgRet, human.fd)
  445. end
  446. -- 报名
  447. function sign(human)
  448. local state = isOpen(human)
  449. if not state then return end
  450. -- 是否报名阶段
  451. if state ~= STATE_ACT_SIGN then
  452. return Broadcast.sendErr(human, Lang.VALLEY_SIGN_ERR_TIME)
  453. end
  454. -- 判断是否已报名
  455. if getSignData(human.db._id) then
  456. return Broadcast.sendErr(human, Lang.VALLEY_SIGN_ERR_HAD)
  457. end
  458. -- 未报名生成报名信息
  459. addSignData(human)
  460. -- 初始队伍取征战的阵容
  461. CombatPosLogic.copyCombatHeros(human, CombatDefine.COMBAT_TYPE1, CombatDefine.COMBAT_TYPE17)
  462. Broadcast.sendErr(human, Lang.VALLEY_SIGN_OK)
  463. Msg.send(Msg.gc.GC_VALLEY_SIGN, human.fd)
  464. end
  465. -- 封装各路入驻角色信息
  466. function fontRoadPlayer(net, uuid)
  467. local db = RoleDBLogic.getDb(uuid, RoleLogic.getCombatField())
  468. if not db then return end
  469. fakeHuman.db = db
  470. net.uuid = uuid
  471. net.name = "[" .. Config.SVR_NAME .. "]" .. db.name
  472. net.tili = ValleyDefine.initTili
  473. net.heroList[0] = 0
  474. -- 报名了才显示默认队伍
  475. local heroIndexs = CombatPosLogic.getCombatHeros(fakeHuman, CombatDefine.COMBAT_TYPE17)
  476. if heroIndexs and getSignData(uuid) then
  477. for pos = 1, CombatDefine.COMBAT_HERO_CNT do
  478. local uuid = heroIndexs[pos]
  479. local heroGrid = HeroLogic.getHeroGridByUuid(human, uuid)
  480. if heroGrid and type(heroGrid) == "table" then
  481. net.heroList[0] = net.heroList[0] + 1
  482. HeroGrid.makeHeroSimple(net.heroList[net.heroList[0]], heroGrid, pos)
  483. end
  484. end
  485. end
  486. net.zhandouli = CombatPosLogic.getCombatHeroZDL(fakeHuman, CombatDefine.COMBAT_TYPE17)
  487. return true
  488. end
  489. -- 某路的入驻信息
  490. function queryRoad(human, camp, roadIndex)
  491. local state = isOpen(human)
  492. if not state then return end
  493. if roadIndex < 1 or roadIndex > ROAD_MAX_CNT then
  494. return
  495. end
  496. if camp ~= 1 and camp ~= 2 then return end
  497. if state == STATE_ACT_READY then -- 休战阶段是看不了的
  498. return Broadcast.sendErr(human, Lang.VALLEY_ERR_READY)
  499. end
  500. -- 只有战斗阶段可以查看敌方营地信息
  501. if state ~= STATE_ACT_FIGHT and camp ~= CombatDefine.ATTACK_SIDE then
  502. returnBroadcast.sendErr(human, Lang.VALLEY_ROAD_ERR_CAMP)
  503. end
  504. if state == STATE_ACT_SIGN then -- 报名阶段显示本服数据即可
  505. return sendRoadQuery(human.db._id, camp, roadIndex)
  506. end
  507. local msgInner = InnerMsg.lw.LW_VALLEY_ROAD_QUERY
  508. msgInner.uuid = human.db._id
  509. msgInner.camp = camp
  510. msgInner.roadIndex = roadIndex
  511. InnerMsg.sendMsg(0, msgInner)
  512. end
  513. -- 发送入驻信息
  514. function sendRoadQuery(uuid, camp, roadIndex, playerList, myData)
  515. local human = ObjHuman.onlineUuid[uuid]
  516. if not human then return end
  517. local msgRet = Msg.gc.GC_VALLEY_ROAD_QUERY
  518. msgRet.state = getState()
  519. msgRet.camp = camp
  520. msgRet.roadIndex = roadIndex
  521. msgRet.myRoadIndex = getRoadIndex(uuid) or 0
  522. msgRet.list[0] = 0
  523. if playerList then
  524. msgRet.cnt = playerList.cnt or 0
  525. msgRet.list[0] = #playerList
  526. for i = 1, msgRet.list[0] do
  527. Util.copyTableSimple(playerList[i], msgRet.list[i])
  528. end
  529. else
  530. local uuids = getRoadUuids(roadIndex)
  531. local uuidsLen = uuids and #uuids or 0
  532. msgRet.cnt = uuidsLen
  533. for i = 1, uuidsLen do
  534. if msgRet.list[0] >= #msgRet.list then
  535. break
  536. end
  537. local uuid = uuids[i]
  538. if fontRoadPlayer(msgRet.list[msgRet.list[0] + 1], uuid) then
  539. msgRet.list[0] = msgRet.list[0] + 1
  540. end
  541. end
  542. end
  543. if myData and myData.uuid then
  544. Util.copyTableSimple(myData, msgRet.myData)
  545. else
  546. fontRoadPlayer(msgRet.myData, uuid)
  547. end
  548. msgRet.isSign = getSignData(uuid) and 1 or 0
  549. -- Msg.trace(msgRet)
  550. Msg.send(msgRet, human.fd)
  551. end
  552. -- 发送入驻/退出路线
  553. local function sendRoadChange(human, roadIndex)
  554. local msgRet = Msg.gc.GC_VALLEY_ROAD_CHANGE
  555. msgRet.roadIndex = roadIndex
  556. Msg.send(msgRet, human.fd)
  557. end
  558. -- 修改入驻路线
  559. function changeRoad(human, roadIndex)
  560. local state = isOpen(human)
  561. if not state then return end
  562. if state ~= STATE_ACT_SIGN then
  563. return Broadcast.sendErr(human, Lang.VALLEY_CHANGE_ROAD_ERR_TIME)
  564. end
  565. if roadIndex < 1 or roadIndex > ROAD_MAX_CNT then
  566. return
  567. end
  568. if not getSignData(human.db._id) then --先报名
  569. return Broadcast.sendErr(human, Lang.VALLEY_ERR_SIGN)
  570. end
  571. if roadIndex == 0 then
  572. -- 退出入驻
  573. local oldRoadIndex = delFromRoad(human.db._id)
  574. if oldRoadIndex and oldRoadIndex > 0 then
  575. sendRoadChange(human, roadIndex)
  576. end
  577. else
  578. -- 入驻某路
  579. if addToRoad(human.db._id, roadIndex) then
  580. sendRoadQuery(human, roadIndex)
  581. sendRoadChange(human, roadIndex)
  582. end
  583. end
  584. end
  585. -- 自动入驻路线 优先选人最少的
  586. -- 报名了,但还没入驻
  587. function autoChangeRoad(uuid)
  588. if getRoadIndex(uuid) ~= 0 then
  589. return
  590. end
  591. local commonDB = getCommonDB()
  592. local minCnt = nil
  593. local minCntRoadInex = nil
  594. for i = 1, ROAD_MAX_CNT do
  595. local roadList = commonDB.roadList[i]
  596. local cnt = roadList and #roadList or 0
  597. if minCnt == nil or cnt < minCnt then
  598. minCnt = cnt
  599. minCntRoadInex = i
  600. end
  601. end
  602. if not minCntRoadInex then return end
  603. addToRoad(uuid, minCntRoadInex, true)
  604. end
  605. -- 刷新侦查列表
  606. function refreshExplore(signData, exploreCnt)
  607. local isChange = nil
  608. for i = signData.exploreCnt + 1, exploreCnt do
  609. signData.exploreCnt = i
  610. local id, config = randomExploreID()
  611. if id and config then
  612. signData.exploreLogs[#signData.exploreLogs + 1] = id
  613. signData.exploreItems = signData.exploreItems or {}
  614. local oldItemCnt = signData.exploreItems[config.itemID] or 0
  615. signData.exploreItems[config.itemID] = oldItemCnt + config.itemCnt
  616. end
  617. isChange = true
  618. end
  619. if isChange then
  620. saveCommonDB(getCommonDB())
  621. end
  622. end
  623. -- 是否有侦查红点(有奖励可领取)
  624. function isRedExplore(human)
  625. local state, _, startTime = isOpen(human)
  626. if not state then return end
  627. if state ~= STATE_ACT_EXPLORE then return end
  628. local signData = getSignData(human.db._id)
  629. if not signData then return end
  630. local exploreCnt = math.floor((os.time() - startTime) / ValleyDefine.exploreItemTime)
  631. refreshExplore(signData, exploreCnt)
  632. if signData.exploreItems then
  633. return true
  634. end
  635. end
  636. -- 侦查界面查询
  637. function sendExploreQuery(human)
  638. local state, _, startTime = isOpen(human)
  639. if not state then return end
  640. if state ~= STATE_ACT_EXPLORE then
  641. return Broadcast.sendErr(human, Lang.VALLEY_EXPLORE_ERR_TIME)
  642. end
  643. local signData = getSignData(human.db._id)
  644. if not signData then
  645. return Broadcast.sendErr(human, Lang.VALLEY_ERR_SIGN)
  646. end
  647. local exploreCnt = math.floor((os.time() - startTime) / ValleyDefine.exploreItemTime)
  648. refreshExplore(signData, exploreCnt)
  649. local msgRet = Msg.gc.GC_VALLEY_EXPLORE_QUERY
  650. msgRet.time = os.time()
  651. msgRet.logs[0] = #signData.exploreLogs
  652. for i = 1, msgRet.logs[0] do
  653. local net = msgRet.logs[i]
  654. local id = signData.exploreLogs[i]
  655. local config = ValleyExcel.explore[id]
  656. net.time = startTime + ValleyDefine.exploreItemTime * i
  657. net.content = Util.format(config.content, ItemDefine.getValue(config.itemID, "name"), config.itemCnt)
  658. end
  659. msgRet.items[0] = 0
  660. if signData.exploreItems then
  661. for itemID, itemCnt in pairs(signData.exploreItems) do
  662. msgRet.items[0] = msgRet.items[0] + 1
  663. Grid.makeItem(msgRet.items[msgRet.items[0]], itemID, itemCnt)
  664. end
  665. end
  666. Msg.send(msgRet, human.fd)
  667. end
  668. -- 领取侦查奖励
  669. function getExplore(human)
  670. local state = isOpen(human)
  671. if not state then return end
  672. if state ~= STATE_ACT_EXPLORE then
  673. return Broadcast.sendErr(human, Lang.VALLEY_EXPLORE_ERR_TIME)
  674. end
  675. local signData = getSignData(human.db._id)
  676. if not signData then
  677. return Broadcast.sendErr(human, Lang.VALLEY_ERR_SIGN)
  678. end
  679. local exploreCnt = math.floor((os.time() - startTime) / ValleyDefine.exploreItemTime)
  680. refreshExplore(signData, exploreCnt)
  681. local exploreItems = signData.exploreItems
  682. if not exploreItems then
  683. return Broadcast.sendErr(human, Lang.VALLEY_EXPLORE_GET_ERR_ITEMS)
  684. end
  685. signData.exploreItems = nil
  686. saveCommonDB(getCommonDB())
  687. for itemID, itemCnt in pairs(exploreItems) do
  688. BagLogic.addItem(human, itemID, itemCnt, "valley")
  689. end
  690. BagLogic.sendItemGetList(human, exploreItems, "valley")
  691. sendExploreQuery(human)
  692. end
  693. -- 封装鼓舞基本信息
  694. local function fontInspireBase(net, mainInfo)
  695. net.svrName = mainInfo and mainInfo.svrName or ""
  696. net.cnt = mainInfo and mainInfo.inspireCnt or 0
  697. net.maxCnt = MAX_INSPIRE_CNT
  698. end
  699. local function fontInspireData(dataNet, data)
  700. dataNet.round = data.round
  701. fontInspireBase(dataNet.base1, data.mainInfo1)
  702. fontInspireBase(dataNet.base2, data.mainInfo2)
  703. dataNet.inspireItems[0] = 2
  704. Grid.makeItem(dataNet.inspireItems[1], ValleyDefine.inspireItem1[1], ValleyDefine.inspireItem1[2])
  705. Grid.makeItem(dataNet.inspireItems[2], ValleyDefine.inspireItem2[1], ValleyDefine.inspireItem2[2])
  706. dataNet.selectIndex = data.selectIndex or 0
  707. end
  708. -- 鼓舞查询
  709. function queryInspire(human)
  710. local state = isOpen(human)
  711. if not state then return end
  712. if state ~= STATE_ACT_FIGHT then
  713. return Broadcast.sendErr(human, Lang.VALLEY_INSPIRE_ERR_TIME)
  714. end
  715. local msgInner = InnerMsg.lw.LW_VALLEY_INSPIRE_QUERY
  716. msgInner.uuid = human.db._id
  717. InnerMsg.sendMsg(0, msgInner)
  718. end
  719. -- 鼓舞
  720. function selectInspire(human, selectIndex)
  721. local state = isOpen(human)
  722. if not state then return end
  723. if state ~= STATE_ACT_FIGHT then
  724. return Broadcast.sendErr(human, Lang.VALLEY_INSPIRE_ERR_TIME)
  725. end
  726. -- 判断花费
  727. local useItem = ValleyDefine["inspireItem" .. selectIndex]
  728. if not useItem then return end -- 选择鼓舞不存在
  729. local itemID = useItem[1]
  730. local itemCnt = useItem[2]
  731. if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
  732. return
  733. end
  734. -- 通知跨服
  735. local msgInner = InnerMsg.lw.LW_VALLEY_INSPIRE
  736. msgInner.uuid = human.db._id
  737. InnerMsg.sendMsg(0, msgInner)
  738. end
  739. -- 观战面板查询
  740. function queryFight(human, roadIndex)
  741. local state = isOpen(human)
  742. if not state then return end
  743. if state ~= STATE_ACT_FIGHT then
  744. return Broadcast.sendErr(human, Lang.VALLEY_FIGHT_ERR_TIME)
  745. end
  746. if roadIndex < 1 or roadIndex > ROAD_MAX_CNT then
  747. return
  748. end
  749. local msgInner = InnerMsg.lw.LW_VALLEY_FIGHT_QUERY
  750. msgInner.uuid = human.db._id
  751. msgInner.roadIndex = roadIndex
  752. InnerMsg.sendMsg(0, msgInner)
  753. end
  754. -- 观战
  755. function playFight(human, videoUuid)
  756. local state = isOpen(human)
  757. if not state then return end
  758. if state ~= STATE_ACT_FIGHT then
  759. return Broadcast.sendErr(human, Lang.VALLEY_FIGHT_ERR_TIME)
  760. end
  761. if videoUuid == "" then
  762. return Broadcast.sendErr(human, Lang.VALLEY_FIGHT_ERR_DOING)
  763. end
  764. local msgInner = InnerMsg.lw.LW_VALLEY_PLAY_FIGHT
  765. msgInner.uuid = human.db._id
  766. msgInner.videoUuid = videoUuid
  767. InnerMsg.sendMsg(0, msgInner)
  768. end
  769. -- 日志查看
  770. function queryLog(human, roadIndex)
  771. local state = isOpen(human)
  772. if not state then return end
  773. if roadIndex < 0 or roadIndex > ROAD_MAX_CNT then
  774. return
  775. end
  776. local msgInner = InnerMsg.lw.LW_VALLEY_LOG_QUERY
  777. msgInner.uuid = human.db._id
  778. msgInner.roadIndex = roadIndex
  779. InnerMsg.sendMsg(0, msgInner)
  780. end
  781. -- 排行榜查看
  782. function queryBoard(human, camp)
  783. local state = isOpen(human)
  784. if not state then return end
  785. if camp ~= 1 and camp ~= 2 then
  786. return
  787. end
  788. local msgInner = InnerMsg.lw.LW_VALLEY_BOARD_QUERY
  789. msgInner.uuid = human.db._id
  790. msgInner.camp = camp
  791. InnerMsg.sendMsg(0, msgInner)
  792. end
  793. -- 可否打开上阵面板
  794. function checkCombatPos(human)
  795. local state = isOpen(human)
  796. if not state then return end -- 未达到开放条件
  797. if state == STATE_ACT_FIGHT then -- 开打了,不可以
  798. return Broadcast.sendErr(human, Lang.VALLEY_COMBAT_POS_ERR_TIME)
  799. end
  800. if state ~= STATE_ACT_SIGN then
  801. local roadIndex = getRoadIndex(uuid)
  802. if not roadIndex then -- 错过报名阶段,还没报名,不让改阵容
  803. return Broadcast.sendErr(human, Lang.VALLEY_COMBAT_POS_ERR_SIGN)
  804. end
  805. if roadIndex == 0 then -- 错过报名阶段,没入驻,不让改阵容
  806. return Broadcast.sendErr(human, Lang.VALLEY_COMBAT_POS_ERR_ROAD)
  807. end
  808. end
  809. return true
  810. end
  811. -- 刷新上阵信息
  812. function onUpdate(human)
  813. local uuid = human.db._id
  814. local roadIndex = getRoadIndex(uuid)
  815. if not roadIndex then return end
  816. local state = getState()
  817. if state == STATE_ACT_FIGHT then
  818. return
  819. end
  820. sendLWRoadPlayer(uuid, roadIndex)
  821. end
  822. -------------------------------------- middle -------------------------------------------
  823. -- 主界面
  824. function WL_VALLEY_QUERY(uuid, data)
  825. sendQuery(uuid, data)
  826. end
  827. -- 营地查询
  828. function WL_VALLEY_ROAD_QUERY(uuid, camp, roadIndex, playerList, myData)
  829. sendRoadQuery(uuid, camp, roadIndex, playerList, myData)
  830. end
  831. -- 鼓舞界面返回
  832. function WL_VALLEY_INSPIRE_QUERY(uuid, data)
  833. local human = ObjHuman.onlineUuid[uuid]
  834. if not human then return end
  835. local msgRet = Msg.gc.GC_VALLEY_INSPIRE_QUERY
  836. msgRet.leftTime = data.leftTime
  837. fontInspireData(msgRet.data, data)
  838. Msg.send(msgRet, human.fd)
  839. end
  840. -- 鼓舞
  841. function WL_VALLEY_INSPIRE(uuid, selectIndex)
  842. local human = ObjHuman.onlineUuid[uuid]
  843. if not human then return end
  844. -- 判断花费
  845. local useItem = ValleyDefine["inspireItem" .. selectIndex]
  846. if not useItem then return end -- 选择鼓舞不存在
  847. local itemID = useItem[1]
  848. local itemCnt = useItem[2]
  849. if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
  850. return
  851. end
  852. BagLogic.delItem(human, itemID, itemCnt, "valley_inspire")
  853. end
  854. -- 观战界面
  855. function WL_VALLEY_FIGHT_QUERY(uuid, data)
  856. local human = ObjHuman.onlineUuid[uuid]
  857. if not human then return end
  858. local msgRet = Msg.gc.GC_VALLEY_FIGHT_QUERY
  859. msgRet.fightState = data.fightState or 0
  860. fontInspireData(msgRet.inspireData, data)
  861. msgRet.fightPlayers[0] = data.fightPlayers and #data.fightPlayers or 0
  862. for i = 1, msgRet.fightPlayers[0] do
  863. Util.copyTableSimple(data.fightPlayers[i], msgRet.fightPlayers[i])
  864. end
  865. msgRet.fightBase[0] = data.fightBase and #data.fightBase or 0
  866. for i = 1, msgRet.fightBase[0] do
  867. Util.copyTableSimple(data.fightBase[i], msgRet.fightBase[i])
  868. end
  869. msgRet.videoUuid = data.videoUuid or ""
  870. Msg.send(msgRet, human.fd)
  871. end
  872. -- 观战
  873. function WL_VALLEY_PLAY_FIGHT(uuid, combatInfo)
  874. local human = ObjHuman.onlineUuid[uuid]
  875. if not human then return end
  876. CombatLogic.repeatCombat(human, combatInfo)
  877. end
  878. -- 开打
  879. function WL_VALLEY_COMBAT_BEGIN(svrIndex, roadIndex, round, inspireCnt1, inspireCnt2, playerData1, playerData2)
  880. -- 开打
  881. local cbParam = {}
  882. cbParam.svrIndex = svrIndex
  883. cbParam.roadIndex = roadIndex
  884. cbParam.inspireCnt1 = inspireCnt1
  885. cbParam.inspireCnt2 = inspireCnt2
  886. cbParam.round = round
  887. cbParam.hpRateListAtk = playerData1.hpRateList
  888. cbParam.hpRateListDef = playerData2.hpRateList
  889. local param = {}
  890. param.attacker = playerData1.objList
  891. param.atkHelp = playerData1.helpList
  892. param.atkRBase = playerData1.rolebase
  893. param.atkFormation = playerData1.formation
  894. param.defender = playerData2.objList
  895. param.defHelp = playerData2.helpList
  896. param.defRBase = playerData2.rolebase
  897. param.defFormation = playerData2.formation
  898. fakeHuman.db = nil
  899. CombatLogic.combatBegin(fakeHuman, 1001, param, CombatDefine.COMBAT_TYPE17, cbParam, true)
  900. end
  901. -- 战斗前加属性
  902. function onFightBegin(human, cbParam)
  903. local attrsUp1 = getAttrsUp(cbParam.inspireCnt1, cbParam.inspireCnt2, CombatDefine.ATTACK_SIDE)
  904. local attrsUp2 = getAttrsUp(cbParam.inspireCnt1, cbParam.inspireCnt2, CombatDefine.DEFEND_SIDE)
  905. for index = 1, CombatDefine.COMBAT_HERO_CNT do
  906. -- 攻击
  907. local atkPos = CombatLogic.getPos(CombatDefine.ATTACK_SIDE, index)
  908. local atkObj = CombatImpl.objList[atkPos]
  909. if atkObj then
  910. atkObj.isSysAttrChange = true
  911. if attrsUp1 then
  912. local initAttr = atkObj.initAttr --基础属性
  913. for _, attrUp in ipairs(attrsUp1) do
  914. initAttr[attrUp[1]] = initAttr[attrUp[1]] + attrUp[2]
  915. end
  916. end
  917. end
  918. -- 防守
  919. local defPos = CombatLogic.getPos(CombatDefine.DEFEND_SIDE, index)
  920. local defObj = CombatImpl.objList[defPos]
  921. if defObj then
  922. defObj.isSysAttrChange = true
  923. if attrsUp2 then
  924. local initAttr = defObj.initAttr --基础属性
  925. for _, attrUp in ipairs(attrsUp2) do
  926. initAttr[attrUp[1]] = initAttr[attrUp[1]] + attrUp[2]
  927. end
  928. end
  929. end
  930. end
  931. end
  932. -- 战斗结束回调
  933. function onFightEnd(human, result, combatType, cbParam, combatInfo, param, isSaodang)
  934. print("fight end", result, combatInfo, result)
  935. -- 如果两边都没死,按照
  936. calcCombatResult(combatInfo)
  937. combatInfo.inspireCnt1 = cbParam.inspireCnt1
  938. combatInfo.inspireCnt2 = cbParam.inspireCnt2
  939. combatInfo.roadIndex = roadIndex
  940. combatInfo.round = round
  941. local msgInner = InnerMsg.lw.LW_VALLEY_COMBATINFO
  942. msgInner.svrIndex = cbParam.svrIndex
  943. msgInner.combatInfo = combatInfo
  944. InnerMsg.sendMsg(0, msgInner)
  945. end
  946. -- 刷新记录的血量+双方未死时,重新定义胜负(血量多胜出)
  947. function calcCombatResult(combatInfo)
  948. local hpRateListAtk = {}
  949. local hpRateListDef = {}
  950. local hpSumAtk = 0
  951. local hpSumDef = 0
  952. for index = 1, CombatDefine.COMBAT_HERO_CNT do
  953. -- 更新攻击方血量
  954. local atkPos = CombatLogic.getPos(CombatDefine.ATTACK_SIDE, index)
  955. local atkObj = combatInfo.objList[atkPos]
  956. if atkObj ~= nil then
  957. local hp = atkObj.hp
  958. local hpMax = CombatObj.getHpMax(atkObj)
  959. local hpRate = hp / hpMax
  960. hpRateListAtk[index] = hpRate
  961. hpSumAtk = math.max(hp, 0) + hpSumAtk
  962. end
  963. -- 更新防守方血量
  964. local defPos = CombatLogic.getPos(CombatDefine.DEFEND_SIDE, index)
  965. local defObj = combatInfo.objList[defPos]
  966. if defObj ~= nil then
  967. local hp = defObj.hp
  968. local hpMax = CombatObj.getHpMax(defObj)
  969. local hpRate = hp / hpMax
  970. hpRateListDef[index] = hpRate
  971. hpSumDef = math.max(hp, 0) + hpSumDef
  972. end
  973. end
  974. combatInfo.isWin = hpSumAtk >= hpSumDef
  975. combatInfo.hpRateListAtk = hpRateListAtk
  976. combatInfo.hpRateListDef = hpRateListDef
  977. end
  978. -- 所有属性计算完毕,重新设置血量(攻击)
  979. function calcAttr(side, index, obj, cbParam)
  980. local hpRateList = nil
  981. if side == CombatDefine.ATTACK_SIDE then
  982. hpRateList = cbParam.hpRateListAtk
  983. else
  984. hpRateList = cbParam.hpRateListDef
  985. end
  986. if not hpRateList then return end
  987. local hpRate = hpRateList[index]
  988. if not hpRate then return end
  989. local hpMax = CombatObj.getHpMax(obj)
  990. obj.hp = math.ceil(hpRate * hpMax)
  991. end
  992. -- 战斗刷新 如果客户端处于观战面板且对应营地 重新请求CG_VALLEY_FIGHT_QUERY
  993. function WL_VALLEY_FIGHT_QUERY_REFRESH(roadIndex)
  994. local msgRet = Msg.gc.GC_VALLEY_FIGHT_QUERY_REFRESH
  995. msgRet.roadIndex = roadIndex
  996. Msg.sendWorld(msgRet)
  997. end
  998. -- 日志
  999. function WL_VALLEY_LOG_QUERY(uuid, roadIndex, logs)
  1000. local human = ObjHuman.onlineUuid[uuid]
  1001. if not human then return end
  1002. local msgRet = Msg.gc.GC_VALLEY_LOG_QUERY
  1003. msgRet.roadIndex = roadIndex
  1004. msgRet.logs[0] = #logs
  1005. for i = 1, msgRet.logs[0] do
  1006. Util.copyTableSimple(logs[i], msgRet.logs[i])
  1007. end
  1008. -- Msg.trace(msgRet)
  1009. Msg.send(msgRet, human.fd)
  1010. end
  1011. -- 排行榜
  1012. function WL_VALLEY_BOARD_QUERY(uuid, camp, list, myData)
  1013. local human = ObjHuman.onlineUuid[uuid]
  1014. if not human then return end
  1015. local msgRet = Msg.gc.GC_VALLEY_BOARD_QUERY
  1016. msgRet.camp = camp
  1017. Util.copyTableSimple(myData, msgRet.myData)
  1018. if myData.roleBase == nil then
  1019. msgRet.myData.killCnt = 0
  1020. RoleLogic.makeRoleBase(human.db, msgRet.myData.roleBase)
  1021. end
  1022. msgRet.list[0] = #list
  1023. for i = 1, msgRet.list[0] do
  1024. Util.copyTableSimple(list[i], msgRet.list[i])
  1025. end
  1026. -- Msg.trace(msgRet)
  1027. Msg.send(msgRet, human.fd)
  1028. end
  1029. -- 计算破营奖励
  1030. function calcRoadItems(roadItems, items, cnt)
  1031. if cnt < 1 then return end
  1032. for _, item in pairs(items) do
  1033. local itemID = item[1]
  1034. local itemCnt = item[2] * cnt
  1035. local selectIndex = #roadItems + 1
  1036. for index, item2 in ipairs(roadItems) do
  1037. if item2[1] == itemID then
  1038. selectIndex = index
  1039. break
  1040. end
  1041. end
  1042. roadItems[selectIndex] = roadItems[selectIndex] or {}
  1043. roadItems[selectIndex][1] = itemID
  1044. roadItems[selectIndex][2] = (roadItems[selectIndex][2] or 0) + itemCnt
  1045. end
  1046. return roadItems
  1047. end
  1048. -- 破营+杀敌奖励+刷新成就
  1049. function WL_VALLEY_FINAL_RESULT(killList, winCnt, failCnt, tieCnt)
  1050. local roadMailConfig = MailExcel.mail[114]
  1051. local roadContent = Util.format(roadMailConfig.content, winCnt, failCnt, tieCnt)
  1052. local killMailConfig = MailExcel.mail[115]
  1053. local roadItems = {}
  1054. calcRoadItems(roadItems, ValleyDefine.roadWinItem, winCnt)
  1055. calcRoadItems(roadItems, ValleyDefine.roadFailItem, failCnt)
  1056. calcRoadItems(roadItems, ValleyDefine.roadTieItem, tieCnt)
  1057. for uuid, killCnt in pairs(killList) do
  1058. -- 杀敌奖励
  1059. local killConfig = ValleyExcel.kill[killCnt]
  1060. if killConfig then
  1061. local content = Util.format(killMailConfig.content, killCnt)
  1062. MailManager.add(MailManager.SYSTEM, uuid, killMailConfig.title, content, killConfig.reward, killMailConfig.senderName)
  1063. end
  1064. -- 破营奖励
  1065. MailManager.add(MailManager.SYSTEM, uuid, roadMailConfig.title, roadContent, roadItems, roadMailConfig.senderName)
  1066. -- 成就刷新
  1067. ValleyTask.updateValue(uuid, killCnt, winCnt)
  1068. end
  1069. end
  1070. -- 每轮鼓舞奖励
  1071. function WL_VALLEY_INSPIRE_RESULT(inspireList, winCnt, failCnt)
  1072. local config = ValleyExcel.inspire[winCnt]
  1073. if not config then return end
  1074. local msgRet = Msg.gc.GC_VALLEY_INSPIRE_RESULT
  1075. msgRet.winCnt = winCnt
  1076. msgRet.failCnt = failCnt
  1077. for uuid, selectIndex in pairs(inspireList) do
  1078. local human = ObjHuman.onlineUuid[uuid]
  1079. local inspireItem = ValleyDefine["inspireItem" .. selectIndex]
  1080. if human and inspireItem then
  1081. BagLogic.addItem(human, inspireItem[1], inspireItem[2], "valley_inspire")
  1082. msgRet.items[0] = 1
  1083. Grid.makeItem(msgRet.items[1], inspireItem[1], inspireItem[2])
  1084. Msg.send(msgRet, human.fd)
  1085. end
  1086. end
  1087. end
  1088. ------------------------------------- 活动状态变化 ------------------------------------------
  1089. -- 发送角色数据
  1090. function sendLWRoadPlayer(uuid, roadIndex)
  1091. local playerData = createPlayerData(uuid, roadIndex)
  1092. if not playerData then return end
  1093. local msgInner = InnerMsg.lw.LW_VALLEY_ROAD_PLAYER
  1094. msgInner.playerData = playerData
  1095. InnerMsg.sendMsg(0, msgInner)
  1096. end
  1097. -- 数据上传跨服+匹配对手
  1098. function onActMatch()
  1099. if _G.is_middle then return end
  1100. local commonDB = getCommonDB()
  1101. -- 未入驻的自动入驻
  1102. if commonDB.signList then
  1103. for uuid, roadIndex in pairs(commonDB.signList) do
  1104. if roadIndex == 0 then
  1105. autoChangeRoad(uuid)
  1106. end
  1107. end
  1108. end
  1109. commonDB.roadListSend = {}
  1110. if commonDB.roadList then
  1111. for roadIndex, list in pairs(commonDB.roadList) do
  1112. for _, uuid in ipairs(list) do
  1113. local data = {}
  1114. data.uuid = uuid
  1115. data.roadIndex = roadIndex
  1116. commonDB.roadListSend[#commonDB.roadListSend + 1] = data
  1117. end
  1118. end
  1119. end
  1120. saveCommonDB(commonDB)
  1121. local msgInner = InnerMsg.lw.LW_VALLEY_MAIN_INFO
  1122. msgInner.data = createMainInfo()
  1123. InnerMsg.sendMsg(0, msgInner)
  1124. end
  1125. -- 匹配阶段定时器
  1126. function onTimerActMatch(commonDB)
  1127. if not commonDB.roadListSend then return end
  1128. local cnt = 0
  1129. local len = #commonDB.roadListSend
  1130. for i = len, 1, -1 do
  1131. if cnt >= TIMER_SEND_DATA then
  1132. break
  1133. end
  1134. local data = commonDB.roadListSend[i]
  1135. commonDB.roadListSend[i] = nil
  1136. sendLWRoadPlayer(data.uuid, data.roadIndex)
  1137. cnt = cnt + 1
  1138. end
  1139. if #commonDB.roadListSend < 1 then
  1140. commonDB.roadListSend = nil
  1141. end
  1142. saveCommonDB(commonDB)
  1143. end
  1144. -- 侦查开始/匹配结束
  1145. function onActExplore()
  1146. if _G.is_middle then
  1147. ValleyMiddle.onActExplore()
  1148. end
  1149. end
  1150. -- 战斗开始
  1151. function onActFight()
  1152. local commonDB = getCommonDB()
  1153. commonDB.round = nil
  1154. commonDB.roundState = nil
  1155. commonDB.roundStateEndTime = nil
  1156. saveCommonDB(commonDB)
  1157. end
  1158. -- 战斗阶段轮次状态切换
  1159. function onActRound(round, roundState)
  1160. print("onActRound", round, roundState)
  1161. if _G.is_middle then
  1162. return ValleyMiddle.onActRound(round, roundState)
  1163. end
  1164. end
  1165. -- 战斗阶段定时器
  1166. function onTimerActFight(commonDB)
  1167. local round, roundState, roundStateEndTime = getNewRound(commonDB.startTime)
  1168. commonDB.round = commonDB.round or 1
  1169. commonDB.roundState = commonDB.roundState or 0
  1170. if commonDB.round > round then
  1171. return
  1172. end
  1173. if commonDB.round == round and commonDB.roundState >= roundState then
  1174. return
  1175. end
  1176. local newRoundState = commonDB.roundState + 1
  1177. local newRound = commonDB.round
  1178. if newRoundState > STATE_FIGHT_FIGHT then
  1179. newRoundState = STATE_FIGHT_MOVE
  1180. newRound = newRound + 1
  1181. end
  1182. commonDB.round = newRound
  1183. commonDB.roundState = newRoundState
  1184. commonDB.roundStateEndTime = roundStateEndTime
  1185. saveCommonDB(commonDB)
  1186. onActRound(newRound, newRoundState)
  1187. end
  1188. -- 活动结束
  1189. function onActEnd()
  1190. if _G.is_middle then
  1191. return ValleyMiddle.onActEnd()
  1192. end
  1193. cleanDB()
  1194. end
  1195. -- 活动开始
  1196. function onActStart()
  1197. if _G.is_middle then
  1198. return ValleyMiddle.onActStart()
  1199. end
  1200. cleanDB()
  1201. end
  1202. -- 定时器
  1203. local TEMP_TIMER_STAMPTIME = nil
  1204. local TEMP_TIMER_STAMPTIME2 = nil
  1205. function onTimer()
  1206. if not TEMP_TIMER_STAMPTIME then
  1207. _, TEMP_TIMER_STAMPTIME, TEMP_TIMER_STAMPTIME2 = getNewState()
  1208. end
  1209. local nowTime = os.time()
  1210. local commonDB = getCommonDB()
  1211. if (not commonDB.endTime) or
  1212. (commonDB.endTime <= nowTime) or
  1213. (commonDB.endTime ~= TEMP_TIMER_STAMPTIME) or
  1214. (commonDB.startTime ~= TEMP_TIMER_STAMPTIME2) then
  1215. local oldState = commonDB.state
  1216. local state, endTime, startTime = getNewState()
  1217. commonDB.state = state
  1218. commonDB.endTime = endTime
  1219. commonDB.startTime = startTime
  1220. TEMP_TIMER_STAMPTIME = endTime
  1221. TEMP_TIMER_STAMPTIME2 = startTime
  1222. saveCommonDB(commonDB)
  1223. print("valley state",state, oldState, endTime)
  1224. if state == STATE_ACT_READY then
  1225. onActEnd()
  1226. elseif state == STATE_ACT_MATCH then
  1227. onActMatch()
  1228. elseif state == STATE_ACT_FIGHT then
  1229. onActFight()
  1230. elseif state == STATE_ACT_EXPLORE then
  1231. onActExplore()
  1232. elseif state == STATE_ACT_SIGN then
  1233. onActStart()
  1234. end
  1235. end
  1236. -- 战斗阶段
  1237. if commonDB.state == STATE_ACT_FIGHT then
  1238. onTimerActFight(commonDB)
  1239. elseif commonDB.state == STATE_ACT_MATCH then
  1240. onTimerActMatch(commonDB)
  1241. end
  1242. end
  1243. -- 返回活动列表的状态
  1244. function getActState()
  1245. if MiddleConnect.IS_MIDDLE_CONNECT ~= true then
  1246. return JjcActLogic.STATE_NOOPEN, 0
  1247. end
  1248. local state, leftTime = getState()
  1249. if state == STATE_ACT_READY then
  1250. return JjcActLogic.STATE_READY, leftTime
  1251. end
  1252. return JjcActLogic.STATE_START, leftTime
  1253. end
  1254. -- 描述
  1255. function getActDesc(desc)
  1256. if MiddleConnect.IS_MIDDLE_CONNECT then
  1257. return Lang.MIDDLE_SVR_ERR_CONNECT
  1258. end
  1259. return desc
  1260. end