guild_battle.go 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314
  1. package model
  2. import (
  3. "encoding/base64"
  4. "math"
  5. "rocommon"
  6. "rocommon/service"
  7. "rocommon/util"
  8. "roserver/baseserver/model"
  9. "roserver/baseserver/set"
  10. model2 "roserver/db/model"
  11. "roserver/serverproto"
  12. "sort"
  13. "strconv"
  14. )
  15. func getDaoChang100GuildScore(val uint64, passTime uint64) uint64 {
  16. //score
  17. var scoreStr = val
  18. if scoreStr >= (1 << 28) {
  19. scoreStr = (1 << 28) - 1
  20. }
  21. scoreStr <<= 36
  22. if 0xfffffffff > passTime {
  23. scoreStr |= 0xfffffffff - passTime
  24. } else {
  25. scoreStr |= 0xfffffffff - passTime/10000
  26. }
  27. return scoreStr
  28. }
  29. func getValByDaoChang100GuildScore(score float64) uint32 {
  30. val := uint32(uint64(score) >> 36)
  31. return val
  32. }
  33. func UpdateDaoChang100GuildRank(uid uint64, guildId uint64, score uint32) {
  34. if uid <= 0 || guildId <= 0 || score <= 0 {
  35. return
  36. }
  37. // guildIdStr := strconv.FormatUint(uid, 10)
  38. guildIdStr := strconv.FormatUint(guildId, 10)
  39. tmpScore, err := service.GetRedis().ZScore(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
  40. if err != nil && err != service.NIL {
  41. util.ErrorF("uid=%v UpdateDaoChang100GuildRank guildid=%v score=%v err=%v", uid, guildId, score, err)
  42. return
  43. }
  44. if err == service.NIL {
  45. scoreStr := getDaoChang100GuildScore(uint64(score), util.GetTimeMilliseconds())
  46. _, err = service.GetRedis().ZAdd(model.RoleDaoChang100GuildRankPrefix, service.BaseZ{Score: float64(scoreStr), Member: guildIdStr}).Result()
  47. return
  48. }
  49. oldScore := getValByDaoChang100GuildScore(tmpScore)
  50. var maxScore uint32 = 1<<28 - 1
  51. if oldScore > 0 && oldScore < maxScore {
  52. score += oldScore
  53. if score > maxScore {
  54. score = maxScore
  55. }
  56. scoreStr := getDaoChang100GuildScore(uint64(score), util.GetTimeMilliseconds())
  57. _, err = service.GetRedis().ZAdd(model.RoleDaoChang100GuildRankPrefix, service.BaseZ{Score: float64(scoreStr), Member: guildIdStr}).Result()
  58. if err != nil {
  59. util.ErrorF("uid=%v UpdateDaoChang100GuildRank guildid=%v score=%v err=%v", uid, guildId, score, err)
  60. }
  61. }
  62. }
  63. func backUpDaoChang100GuildRank(guildBattleId int32) {
  64. battleIdStr := strconv.Itoa(int(guildBattleId))
  65. prefixStr := model.RoleDaoChang100GuildRankPrefix + battleIdStr
  66. _, err := service.GetRedis().ZUnionStore(prefixStr, service.BaseStore{}, model.RoleDaoChang100GuildRankPrefix).Result()
  67. if err != nil {
  68. util.ErrorF("backUpArenaRankData store rank record err=%v prefixStr=%v", err, prefixStr)
  69. return
  70. }
  71. }
  72. // 清除掉排行版数据
  73. func ClearDaoChang100GuildRank() {
  74. tempCount, err := service.GetRedis().ZCard(model.RoleDaoChang100GuildRankPrefix).Result()
  75. if err != nil {
  76. util.ErrorF("ClearDaoChang100GuildRank get tempCount err:%v", err)
  77. return
  78. }
  79. if tempCount <= 0 {
  80. return
  81. }
  82. _, err2 := service.GetRedis().ZRemRangeByRank(model.RoleDaoChang100GuildRankPrefix, 0, tempCount-1).Result()
  83. if err2 != nil {
  84. util.ErrorF("[ClearDaoChang100GuildRank] remove RoleDaoChang100GuildRankPrefix err:%v uid:%v", err)
  85. return
  86. }
  87. }
  88. const (
  89. Guild_Battle_State_Prepare = 1
  90. Guild_Battle_State_Fight = 2
  91. Guild_Battle_State_Finish = 3
  92. )
  93. // 删除这个公会在公会对阵表中的排名
  94. func OnDisbandGuild(guildId uint64) {
  95. guildIdStr := strconv.FormatUint(guildId, 10)
  96. service.GetRedis().ZRem(model.RoleDaoChang100GuildRankPrefix, guildIdStr)
  97. }
  98. func GetSelfGuildBattleData(guildId uint64, ackMsg *serverproto.SCGetSelfGuildInfoAck) {
  99. guildIdStr := strconv.FormatUint(guildId, 10)
  100. tmpScore, err2 := service.GetRedis().ZScore(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
  101. if err2 == nil {
  102. oldScore := getValByDaoChang100GuildScore(tmpScore)
  103. ackMsg.CpNum = oldScore
  104. }
  105. selfRank, rankErr := service.GetRedis().ZRevRank(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
  106. if rankErr == nil {
  107. ackMsg.GuildBattleRank = uint32(selfRank + 1)
  108. }
  109. if GuildBattleMag.banType == Guild_Battle_Baned {
  110. ackMsg.NextStateBegin = GuildBattleMag.nextGuildBattleBegin
  111. ackMsg.GuildBattleState = Guild_Battle_State_Prepare
  112. return
  113. }
  114. curTime := util.GetCurrentTime()
  115. if curTime < GuildBattleMag.curGuildBattleBegin {
  116. //准备阶段
  117. ackMsg.NextStateBegin = GuildBattleMag.curGuildBattleBegin
  118. ackMsg.GuildBattleState = Guild_Battle_State_Prepare
  119. } else if curTime < GuildBattleMag.finalEnd {
  120. //战斗阶段
  121. ackMsg.NextStateBegin = GuildBattleMag.finalEnd
  122. ackMsg.GuildBattleState = Guild_Battle_State_Fight
  123. } else if curTime < GuildBattleMag.showEnd {
  124. //结束(展示阶段)
  125. ackMsg.NextStateBegin = GuildBattleMag.showEnd
  126. ackMsg.GuildBattleState = Guild_Battle_State_Finish
  127. } else if curTime > GuildBattleMag.showEnd {
  128. //准备阶段
  129. ackMsg.NextStateBegin = GuildBattleMag.nextGuildBattleBegin
  130. ackMsg.GuildBattleState = Guild_Battle_State_Prepare
  131. }
  132. }
  133. const (
  134. //公会战战场相关数据
  135. GuildBattlePrefix = "guildbattle:" //key=guildbattle:1-1 field=uid:xxxxx
  136. GuildBattleRankPrefix = "guildbattle:rank:"
  137. GuildBattleLogPrefix = "guildbattle:log:"
  138. GuildBattleBasePrefix = "guildbattle:base:" //公会管理器战当前战斗数据
  139. GuildBattleTablePrefix = "guildbattle:table:" //公会管理器当前对阵表
  140. RankScoreListType = 1 //积分排行榜
  141. RankKillListType = 2 //击杀排行榜
  142. PosIdxMaxNum = 55
  143. PosIdxFightMaxTime = 60*1000 + 10 //1分钟
  144. MaxUidLogCount int64 = 100
  145. BattleWinNumTips = 3 //击杀超过3次
  146. PrepareTime = 10 * 60 * 1000
  147. )
  148. func getguildBattleScore(val uint64, passTime uint64) uint64 {
  149. //score
  150. var scoreStr = val
  151. if scoreStr >= (1 << 28) {
  152. scoreStr = (1 << 28) - 1
  153. }
  154. scoreStr <<= 36
  155. if 0xfffffffff > passTime {
  156. scoreStr |= 0xfffffffff - passTime
  157. } else {
  158. scoreStr |= 0xfffffffff - passTime/10000
  159. }
  160. return scoreStr
  161. }
  162. func getValByGuildBattleScore(score float64) uint32 {
  163. val := uint32(uint64(score) >> 36)
  164. return val
  165. }
  166. func setGuildBattlePlayerDataToRedis(battleId, battleIdx int32, playerData *serverproto.GuildBattleSelfData) {
  167. err, msgStr := model.GetEncodeMessage(playerData)
  168. if err != nil {
  169. return
  170. }
  171. //uidStr := strconv.FormatUint(playerData.Uid, 10)
  172. keyStr := GuildBattlePrefix + strconv.Itoa(int(battleId)) + "-" + strconv.Itoa(int(battleIdx))
  173. fieldStr := "uid:" + strconv.FormatUint(playerData.Uid, 10)
  174. service.GetRedis().HSet(keyStr, fieldStr, msgStr)
  175. }
  176. func setGuildBattlePosIdxDataToRedis(battleId, battleIdx int32, posIdxData *serverproto.GuildPosIdxData) {
  177. err, msgStr := model.GetEncodeMessage(posIdxData)
  178. if err != nil {
  179. return
  180. }
  181. //[prefix][battleId]-[battleIdx]
  182. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(battleId)) + "-" + strconv.Itoa(int(battleIdx))
  183. fieldStr := "idx:" + strconv.Itoa(int(posIdxData.PosIdx))
  184. service.GetRedis().HSet(battleKeyStr, fieldStr, msgStr)
  185. }
  186. func GetGuildBattleRankList(battleId, battleIdx int32, pagIdx, rankType int32, ackMsg *serverproto.SCGuildBattleRankListAck) {
  187. //[prefix][battleId]-[battleIdx]-[rankType]
  188. keyStr := GuildBattleRankPrefix + strconv.Itoa(int(battleId)) + "-" + strconv.Itoa(int(battleIdx)) + "-" + strconv.Itoa(int(rankType))
  189. if pagIdx <= 0 || pagIdx > 20 {
  190. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  191. return
  192. }
  193. if rankType != RankScoreListType && rankType != RankKillListType {
  194. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  195. return
  196. }
  197. endIdx := pagIdx * 5
  198. startIdx := endIdx - 5
  199. endIdx -= 1
  200. retList, err := service.GetRedis().ZRevRangeWithScores(keyStr, int64(startIdx), int64(endIdx)).Result()
  201. if err != nil {
  202. return
  203. }
  204. GuildBattleMag.initGuildBattleList()
  205. battleData := GuildBattleMag.getBattleByIdx(battleIdx)
  206. if battleData == nil {
  207. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  208. return
  209. }
  210. battleData.GetCountPartData(ackMsg)
  211. for idx := 0; idx < len(retList); idx++ {
  212. uid, _ := model.Str2NumU64(retList[idx].Member.(string))
  213. score := getValByGuildBattleScore(retList[idx].Score)
  214. brief := &serverproto.CommonPlayerBriefInfo{}
  215. err2 := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, uid, brief)
  216. if err2 != nil {
  217. continue
  218. }
  219. guildId := GuildMag.GetPlayerGuildId(uid)
  220. ackMsg.RankDataList = append(ackMsg.RankDataList, &serverproto.GuildBattleRank{
  221. Score: int32(score),
  222. Rank: int32(idx) + (startIdx + 1),
  223. Info: brief,
  224. GuildId: guildId,
  225. })
  226. }
  227. }
  228. func UpdateGuildBattleRankList_Kill(battleId, battleIdx, rankType int32, score int32, uid uint64) {
  229. if uid == 0 || score == 0 {
  230. return
  231. }
  232. //[prefix][battleId]-[battleIdx]-[rankType]
  233. keyStr := GuildBattleRankPrefix + strconv.Itoa(int(battleId)) + "-" + strconv.Itoa(int(battleIdx)) + "-" + strconv.Itoa(int(rankType))
  234. uidStr := strconv.FormatUint(uid, 10)
  235. scoreStr := getguildBattleScore(uint64(score), util.GetTimeMilliseconds())
  236. _, err := service.GetRedis().ZAdd(keyStr, service.BaseZ{Score: float64(scoreStr), Member: uidStr}).Result()
  237. if err != nil {
  238. util.ErrorF("uid=%v UpdateGuildBattleRankList score=%v err=%v", uid, score, err)
  239. }
  240. }
  241. // 上线调用
  242. func (this *GuildBattleManager) IsInBattleTime() bool {
  243. if this.banType == 1 {
  244. return false
  245. }
  246. if Guild_Battle_Start <= this.guildBattleState && this.guildBattleState < Guild_Battle_End {
  247. return true
  248. }
  249. return false
  250. }
  251. // 公会相关判定 公会是否在公会战中
  252. func (this *GuildBattleManager) IsInGuildBattle(guildId uint64) (int32, bool) {
  253. if this.guildBattleId == 0 {
  254. return 0, false
  255. }
  256. //当前不在公会战时间内
  257. if this.guildBattleState < Guild_Battle_Start || this.guildBattleState > Guild_Battle_Final {
  258. return 0, false
  259. }
  260. for idx, data := range this.battleIdxDataList {
  261. if data == nil || len(this.battleIdxDataList[idx].battleBaseData.ScoreList) <= 0 {
  262. continue
  263. }
  264. //公会是否在公会战三个阶段
  265. if Guild_Battle_Start <= this.guildBattleState && this.guildBattleState < Guild_Battle_OffMatch_TS {
  266. if idx < Guild_Battle_Tournament_Begin_Match || idx > Guild_Battle_Tournament_End_Match {
  267. continue
  268. }
  269. for _, matchGuild := range this.battleIdxDataList[idx].battleBaseData.ScoreList {
  270. if matchGuild.Key == guildId {
  271. return idx, true
  272. }
  273. }
  274. } else if Guild_Battle_OffMatch_TS <= this.guildBattleState && this.guildBattleState < Guild_Battle_OffMatch_SF {
  275. if idx < Guild_Battle_Seminal_Begin_Match || idx > Guild_Battle_Seminal_End_Match {
  276. continue
  277. }
  278. for _, matchGuild := range this.battleIdxDataList[idx].battleBaseData.ScoreList {
  279. if matchGuild.Key == guildId {
  280. return idx, true
  281. }
  282. }
  283. } else if Guild_Battle_OffMatch_SF <= this.guildBattleState && this.guildBattleState < Guild_Battle_Result {
  284. if idx != Guild_Battle_Final_Match {
  285. continue
  286. }
  287. for _, matchGuild := range this.battleIdxDataList[idx].battleBaseData.ScoreList {
  288. if matchGuild.Key == guildId {
  289. return idx, true
  290. }
  291. }
  292. }
  293. }
  294. return 0, false
  295. }
  296. const (
  297. Guild_Battle_Final_Match = 1
  298. Guild_Battle_Seminal_Begin_Match = 2
  299. Guild_Battle_Seminal_End_Match = 3
  300. Guild_Battle_Tournament_Begin_Match = 4
  301. Guild_Battle_Tournament_End_Match = 7
  302. )
  303. const (
  304. Guild_Battle_Reward_Final = 1 // 决赛奖励
  305. Guild_Battle_Reward_Semifinals = 2 // 半决赛奖励
  306. Guild_Battle_Reward_Tournament = 3 // 四强赛奖励
  307. )
  308. const (
  309. Guild_Battle_None = 0 //周二五点-周六五点
  310. Guild_Battle_Start = 1 //公会战阶段 周六五点-四强赛准备时间前
  311. Guild_Battle_Tournament_Prepare = 2 //公会战阶段 五点-四强赛准备时间
  312. Guild_Battle_Tournament = 3 //公会战阶段 四强(8-4)战斗
  313. Guild_Battle_OffMatch_TS = 4 //公会战阶段 四强-半决赛 休赛
  314. Guild_Battle_Semifinals_Prepare = 5 //公会战阶段 半决赛赛准备时间
  315. Guild_Battle_Semifinals = 6 //公会战阶段 半决(4-2)赛战斗
  316. Guild_Battle_OffMatch_SF = 7 //公会战阶段 半决赛-决赛 休赛
  317. Guild_Battle_Final_Prepare = 8 //公会战阶段 决赛准备时间
  318. Guild_Battle_Final = 9 //公会战阶段 决赛战斗
  319. Guild_Battle_Result = 10 //榜单展示阶段 周天9.30-周二5.00
  320. Guild_Battle_End = 11 //公会战结束
  321. )
  322. type GuildBattleManager struct {
  323. bInit bool
  324. operator int32
  325. isOpen int32
  326. //当前公会战信息
  327. guildBattleId int32 //当前是第几次公会战
  328. curGuildBattleBegin uint64 //当前公会战开始时间
  329. guildBattleState int32 //公会战状态
  330. nextStateBegin uint64 //下一阶段开始时间
  331. rewardState int32 //奖励发送阶段
  332. banType int32 //封停类型
  333. //四强赛时间
  334. tournamentPrepare uint64
  335. tournamentBegin uint64
  336. tournamentEnd uint64
  337. //半决赛时间
  338. semifinalsPrepare uint64
  339. semifinalsBegin uint64
  340. semifinalsEnd uint64
  341. //决赛时间
  342. finalPrepare uint64
  343. finalBegin uint64
  344. finalEnd uint64
  345. showEnd uint64
  346. championTeam uint64
  347. nextGuildBattleBegin uint64 //获取下一轮公会战开始时间
  348. countPartTable *serverproto.GuildCountPartTable
  349. updateTimer util.ServerTimer //主循环定时器
  350. battleIdxDataList map[int32]*GuildBattle //当前对应的7场战斗
  351. }
  352. func newGuildBattleManager() *GuildBattleManager {
  353. mag := &GuildBattleManager{
  354. battleIdxDataList: map[int32]*GuildBattle{},
  355. countPartTable: &serverproto.GuildCountPartTable{},
  356. banType: 0,
  357. }
  358. mag.updateTimer = util.NewDurationTimer(util.GetCurrentTime(), 200)
  359. return mag
  360. }
  361. func (this *GuildBattleManager) Update(ms uint64) {
  362. if !this.updateTimer.IsStart() || !this.updateTimer.IsExpired(ms) {
  363. return
  364. }
  365. for _, val := range this.battleIdxDataList {
  366. val.update(ms)
  367. }
  368. //初始化做完才能走Tick流程
  369. if this.bInit == true {
  370. this.SwitchGuildBattleState()
  371. } else {
  372. this.initGuildBattleList()
  373. util.InfoF("initGuildBattleList init")
  374. }
  375. }
  376. // 服务器初始化操作
  377. func (this *GuildBattleManager) initGuildBattleList() {
  378. if this.bInit {
  379. return
  380. }
  381. this.bInit = true
  382. err := this.loadGuildBattleBaseFromRedis()
  383. if err != nil {
  384. //加载失败
  385. if err == service.NIL {
  386. util.InfoF("init loadGuildBattleBaseFromRedis empty")
  387. } else {
  388. util.InfoF("init loadGuildBattleBaseFromRedis failed")
  389. }
  390. }
  391. //初始化battleIdxDataList
  392. err2 := this.loadCountTableFromRedis()
  393. if err2 != nil {
  394. if err2 == service.NIL {
  395. util.InfoF("init loadCountTableFromRedis empty")
  396. } else {
  397. util.InfoF("init loadCountTableFromRedis failed")
  398. }
  399. }
  400. //初始对阵表结构
  401. if len(this.countPartTable.Data) > 0 {
  402. for _, data := range this.countPartTable.Data {
  403. this.battleIdxDataList[data.BattleIndex] = newGuildBattle(this, data.BattleIndex, data.Guild1Id, data.Guild2Id, 0, 0)
  404. }
  405. }
  406. }
  407. // 对阵表存储
  408. func (this *GuildBattleManager) saveCountTableToRedis() error {
  409. keyStr := GuildBattleTablePrefix + "-" + strconv.Itoa(int(this.guildBattleId))
  410. msgData, err := rocommon.GetCodec().Marshal(this.countPartTable)
  411. if err != nil {
  412. return err
  413. }
  414. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  415. ret, err2 := service.GetRedis().Set(keyStr, msgStr, 0).Result()
  416. if err2 != nil {
  417. util.InfoF("saveCountTableToRedis err=%v ret=%v", keyStr, ret)
  418. return err2
  419. }
  420. util.InfoF("saveCountTableToRedis save countPartTable success : %v", this.countPartTable)
  421. return nil
  422. }
  423. // 对阵表加载
  424. func (this *GuildBattleManager) loadCountTableFromRedis() error {
  425. keyStr := GuildBattleTablePrefix + "-" + strconv.Itoa(int(this.guildBattleId))
  426. ret, err := service.GetRedis().Get(keyStr).Result()
  427. if err != nil {
  428. util.InfoF("loadCountTableFromRedis get guildId key=%v err=%v\n", keyStr, err)
  429. return err
  430. }
  431. if ret == "" {
  432. util.InfoF("loadCountTableFromRedis key(%v)->val empty", keyStr)
  433. return err
  434. }
  435. str, err := base64.StdEncoding.DecodeString(ret)
  436. if err != nil {
  437. util.InfoF("loadCountTableFromRedis key(%v)->val DecodeString error", keyStr)
  438. return err
  439. }
  440. countTable := &serverproto.GuildCountPartTable{}
  441. err = rocommon.GetCodec().Unmarshal(str, countTable)
  442. if err == nil {
  443. this.countPartTable = countTable
  444. util.InfoF("loadCountTableFromRedis success:%v", countTable)
  445. return nil
  446. }
  447. util.InfoF("loadCountTableFromRedis failed")
  448. return err
  449. }
  450. // 公会战基础信息,只要有变动就保存下来
  451. func (this *GuildBattleManager) saveGuildBattleBaseToRedis() error {
  452. keyStr := GuildBattleBasePrefix
  453. guildBattleBase := &serverproto.GuildBattleBase{
  454. GuildBattleId: this.guildBattleId,
  455. CurGuildStart: this.curGuildBattleBegin,
  456. GuildBattleState: this.guildBattleState,
  457. NextStageBegin: this.nextStateBegin,
  458. NextBattleBegin: this.nextGuildBattleBegin,
  459. TournamentPrepare: this.tournamentPrepare,
  460. TournamentBegin: this.tournamentBegin,
  461. TournamentEnd: this.tournamentEnd,
  462. SemifinalsPrepare: this.semifinalsPrepare,
  463. SemifinalsBegin: this.semifinalsBegin,
  464. SemifinalsEnd: this.semifinalsEnd,
  465. FinalsPrepare: this.finalPrepare,
  466. FinalsBegin: this.finalBegin,
  467. FinalsEnd: this.finalEnd,
  468. ShowEnd: this.showEnd,
  469. IsOpen: this.isOpen,
  470. RewardState: this.rewardState,
  471. BanType: this.banType,
  472. }
  473. msgData, err := rocommon.GetCodec().Marshal(guildBattleBase)
  474. if err != nil {
  475. return err
  476. }
  477. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  478. ret, err2 := service.GetRedis().Set(keyStr, msgStr, 0).Result()
  479. if err2 != nil {
  480. util.InfoF("saveGuildBattleBase err=%v ret=%v", keyStr, ret)
  481. return err2
  482. }
  483. util.InfoF("saveGuildBattleBase save guildbase success : %v", guildBattleBase)
  484. return nil
  485. }
  486. // 服务器启动时加载
  487. func (this *GuildBattleManager) loadGuildBattleBaseFromRedis() error {
  488. keyStr := GuildBattleBasePrefix
  489. ret, err := service.GetRedis().Get(keyStr).Result()
  490. if err != nil {
  491. util.InfoF("loadGuildBattleBase get guildId key=%v err=%v\n", keyStr, err)
  492. return err
  493. }
  494. if ret == "" {
  495. util.InfoF("loadGuildBattleBase key(%v)->val empty", keyStr)
  496. return err
  497. }
  498. str, err := base64.StdEncoding.DecodeString(ret)
  499. if err != nil {
  500. util.InfoF("loadGuildBattleBase key(%v)->val DecodeString error", keyStr)
  501. return err
  502. }
  503. guildBase := &serverproto.GuildBattleBase{}
  504. err = rocommon.GetCodec().Unmarshal(str, guildBase)
  505. if err == nil {
  506. this.guildBattleId = guildBase.GuildBattleId
  507. this.curGuildBattleBegin = guildBase.CurGuildStart
  508. this.guildBattleState = guildBase.GuildBattleState
  509. this.nextStateBegin = guildBase.NextStageBegin
  510. this.nextGuildBattleBegin = guildBase.NextBattleBegin
  511. this.tournamentPrepare = guildBase.TournamentPrepare
  512. this.tournamentBegin = guildBase.TournamentBegin
  513. this.tournamentEnd = guildBase.TournamentEnd
  514. this.semifinalsPrepare = guildBase.SemifinalsPrepare
  515. this.semifinalsBegin = guildBase.SemifinalsBegin
  516. this.semifinalsEnd = guildBase.SemifinalsEnd
  517. this.finalPrepare = guildBase.FinalsPrepare
  518. this.finalBegin = guildBase.FinalsBegin
  519. this.finalEnd = guildBase.FinalsEnd
  520. this.showEnd = guildBase.ShowEnd
  521. this.isOpen = guildBase.IsOpen
  522. this.rewardState = guildBase.RewardState
  523. this.banType = guildBase.BanType
  524. util.InfoF("loadGuildBattleBase success:%v", guildBase)
  525. return nil
  526. }
  527. util.InfoF("loadGuildBattleBase failed")
  528. return err
  529. }
  530. const firstGuildBattleIdHour = 5 * 86400 * 1000
  531. // 第一次初始化公会战时间
  532. func (this *GuildBattleManager) InitFirstGuildBattleBegin() uint64 {
  533. nextWeekBegin := util.GetLatestWeek5Hour(0) + firstGuildBattleIdHour //下个周一早上五点。再加上5天整
  534. return nextWeekBegin
  535. }
  536. // 第一轮公会战之后,校验下一轮公会战时间
  537. const TwoDayMilliseconds = 2 * 24 * 3600 * 1000
  538. func (this *GuildBattleManager) GetCurWeekGuildBattleBeginTime(curWeekDay, curHour int32) uint64 {
  539. //如果当前是周六五点之后,或者周天的话,则初始化到下一周的周六五点 //6 :周六 0:周天
  540. if curWeekDay == 0 || (curWeekDay == 6 && curHour >= 5) {
  541. return this.InitFirstGuildBattleBegin()
  542. }
  543. //如果是已经到了周一之后,但是周六五点之前
  544. //则设定下一轮的公会战开始时间为,下周一凌晨5点的前两天整
  545. return util.GetLatestWeek5Hour(0) - TwoDayMilliseconds
  546. }
  547. func (this *GuildBattleManager) ResetNextGuildBattleBegin() uint64 {
  548. curMilSecond := util.GetCurrentTime()
  549. //如果当前时间还没有到 下一轮公会战 的8.30。则认为可以继续开启公会战
  550. if curMilSecond < this.nextGuildBattleBegin+Tournament_Begin_Hour+Tournament_End_Hour {
  551. return 0
  552. }
  553. //已经超过 this.nextGuildBattleBegin 时间对应的周六 8.30,则认为当前轮次的公会战已经过了,
  554. nowTime := util.GetCurrentTimeNow()
  555. return this.GetCurWeekGuildBattleBeginTime(int32(nowTime.Weekday()), int32(nowTime.Hour()))
  556. }
  557. func (this *GuildBattleManager) SetGuildBattleFinish() {
  558. this.guildBattleState = Guild_Battle_End
  559. }
  560. const (
  561. Tournament_Prepare_Hour = 60 * 10 * 1000
  562. Tournament_Begin_Hour = 15 * 3600 * 1000
  563. Tournament_End_Hour = 1800 * 1000
  564. Semifinals_Prepare_Hour = 60 * 10 * 1000
  565. Semifinals_Begin_Hour = 16 * 3600 * 1000
  566. Semifinals_End_Hour = 1800 * 1000
  567. Final_Prepare_Hour = 60 * 10 * 1000
  568. Final_Begin_Hour = 86400*1000 + 16*3600*1000
  569. Final_End_Hour = 1800 * 1000
  570. Show_End_Hour = 3 * 86400 * 1000
  571. Next_Guild_Battle_Begin = 7 * 86400 * 1000
  572. Guild_Battle_Baned = 1
  573. )
  574. // 公会战状态切换
  575. func (this *GuildBattleManager) SwitchGuildBattleState() {
  576. if this.banType == Guild_Battle_Baned {
  577. return
  578. }
  579. if this.nextGuildBattleBegin == 0 {
  580. nowTime := util.GetCurrentTimeNow()
  581. this.nextGuildBattleBegin = this.GetCurWeekGuildBattleBeginTime(int32(nowTime.Weekday()), int32(nowTime.Hour()))
  582. // this.nextGuildBattleBegin = this.InitFirstGuildBattleBegin()
  583. err := this.saveGuildBattleBaseToRedis()
  584. if err != nil {
  585. util.InfoF("saveGuildBattleBaseToRedis save nextGuildBattleBegin failed : %v", err)
  586. }
  587. } else {
  588. //如果公会战时间不为零。,则要考虑nextGuildBattleBegin已经过期,切当前不在公会战时间内
  589. //即当前时间是不是在周二5点到周六5点之间,如果是,则初始化下一轮公会战时间为本周六的凌晨五点
  590. nextGuildBattleBegin := this.ResetNextGuildBattleBegin()
  591. if nextGuildBattleBegin != 0 {
  592. this.nextGuildBattleBegin = nextGuildBattleBegin
  593. err := this.saveGuildBattleBaseToRedis()
  594. if err != nil {
  595. util.InfoF("saveGuildBattleBaseToRedis init save new nextGuildBattleBegin failed : %v", err)
  596. }
  597. }
  598. }
  599. curTime := util.GetCurrentTime()
  600. //公会战时间到
  601. if this.nextGuildBattleBegin <= curTime {
  602. //开启公会战//记录当前公会战开启时间
  603. this.guildBattleId += 1
  604. util.InfoF("SwitchGuildBattleState init begin : %v", this.guildBattleId)
  605. //备份这一轮的百人道场贡献点
  606. backUpDaoChang100GuildRank(this.guildBattleId)
  607. //设置下一次公会战的时间
  608. this.curGuildBattleBegin = this.nextGuildBattleBegin
  609. this.nextGuildBattleBegin += Next_Guild_Battle_Begin
  610. //初始化:周六8.00-8.30 周六9.00-9.30 周日9.00-9.00。
  611. //四强赛
  612. this.tournamentBegin = this.curGuildBattleBegin + Tournament_Begin_Hour
  613. this.tournamentPrepare = this.tournamentBegin - Tournament_Prepare_Hour
  614. this.tournamentEnd = this.tournamentBegin + Tournament_End_Hour
  615. //半决赛
  616. this.semifinalsBegin = this.curGuildBattleBegin + Semifinals_Begin_Hour
  617. this.semifinalsPrepare = this.semifinalsBegin - Semifinals_Prepare_Hour
  618. this.semifinalsEnd = this.semifinalsBegin + Semifinals_End_Hour
  619. //决赛
  620. this.finalBegin = this.curGuildBattleBegin + Final_Begin_Hour
  621. this.finalPrepare = this.finalBegin - Final_Prepare_Hour
  622. this.finalEnd = this.finalBegin + Final_End_Hour
  623. //初始化周二凌晨5定
  624. this.showEnd = this.curGuildBattleBegin + Show_End_Hour
  625. this.rewardState = Guild_Battle_Start
  626. //清空当前对阵表,和对阵数据
  627. this.countPartTable = &serverproto.GuildCountPartTable{}
  628. this.battleIdxDataList = make(map[int32]*GuildBattle) //
  629. //初始化对阵表
  630. this.initGuildBattleList()
  631. if this.curGuildBattleBegin <= curTime && curTime < this.tournamentBegin {
  632. this.initBattle()
  633. }
  634. this.GuildBattleStateChangeNtf(true)
  635. err := this.saveGuildBattleBaseToRedis()
  636. if err != nil {
  637. util.InfoF("saveGuildBattleBaseToRedis save failed err: %v", err)
  638. }
  639. err2 := this.saveCountTableToRedis()
  640. if err2 != nil {
  641. util.InfoF("saveCountTableToRedis save failed err2: %v", err2)
  642. }
  643. //百人道场贡献点清空,重新记录新的
  644. ClearDaoChang100GuildRank()
  645. }
  646. if this.guildBattleState == Guild_Battle_End {
  647. return
  648. }
  649. //战斗中
  650. var needSave bool = false
  651. oldState := this.guildBattleState
  652. switch this.guildBattleState {
  653. case Guild_Battle_Start:
  654. if this.tournamentPrepare <= curTime && curTime < this.tournamentBegin {
  655. this.guildBattleState = Guild_Battle_Tournament_Prepare
  656. } else if this.tournamentEnd <= curTime { //四强赛结束(表示四强塞没打过)
  657. //公会战结束
  658. this.SetGuildBattleFinish()
  659. } else if curTime > this.tournamentBegin {
  660. this.guildBattleState = Guild_Battle_Tournament_Prepare
  661. }
  662. case Guild_Battle_Tournament_Prepare:
  663. if this.tournamentBegin <= curTime && curTime < this.tournamentEnd {
  664. this.guildBattleState = Guild_Battle_Tournament
  665. } else if this.tournamentEnd <= curTime { //四强赛结束(表示四强塞没打过)
  666. //公会战结束
  667. this.SetGuildBattleFinish()
  668. }
  669. case Guild_Battle_Tournament:
  670. if this.tournamentEnd <= curTime && curTime < this.semifinalsPrepare { //四强赛结束
  671. this.guildBattleState = Guild_Battle_OffMatch_TS
  672. } else if this.semifinalsEnd <= curTime { //半决赛赛结束(表示半决赛没打过)
  673. //公会战结束
  674. this.SetGuildBattleFinish()
  675. } else if curTime > this.semifinalsPrepare {
  676. this.guildBattleState = Guild_Battle_OffMatch_TS
  677. }
  678. case Guild_Battle_OffMatch_TS:
  679. if this.rewardState < Guild_Battle_Tournament {
  680. this.CheckTournamentReward()
  681. needSave = true
  682. }
  683. if this.semifinalsPrepare <= curTime && curTime < this.semifinalsBegin { //四强赛结束
  684. this.guildBattleState = Guild_Battle_Semifinals_Prepare
  685. } else if this.semifinalsEnd <= curTime { //半决赛赛结束(表示半决赛没打过)
  686. //公会战结束
  687. this.SetGuildBattleFinish()
  688. } else if curTime > this.semifinalsBegin {
  689. this.guildBattleState = Guild_Battle_Semifinals_Prepare
  690. }
  691. case Guild_Battle_Semifinals_Prepare:
  692. if this.rewardState < Guild_Battle_Tournament {
  693. this.CheckTournamentReward()
  694. needSave = true
  695. }
  696. if this.semifinalsBegin <= curTime && curTime <= this.semifinalsEnd { //半决赛开始
  697. this.guildBattleState = Guild_Battle_Semifinals
  698. } else if this.semifinalsEnd <= curTime { //半决赛赛结束(表示半决赛没打过)
  699. //公会战结束
  700. this.SetGuildBattleFinish()
  701. }
  702. case Guild_Battle_Semifinals:
  703. if this.semifinalsEnd <= curTime && curTime < this.finalPrepare { //半决赛结束
  704. this.guildBattleState = Guild_Battle_OffMatch_SF
  705. } else if this.finalEnd <= curTime { //决赛赛结束(表示没有进入过决塞)
  706. //公会战结束
  707. this.SetGuildBattleFinish()
  708. } else if curTime > this.finalPrepare {
  709. this.guildBattleState = Guild_Battle_OffMatch_SF
  710. }
  711. if this.rewardState < Guild_Battle_Tournament {
  712. this.CheckTournamentReward()
  713. needSave = true
  714. }
  715. case Guild_Battle_OffMatch_SF:
  716. if this.rewardState < Guild_Battle_Semifinals {
  717. this.CheckSemiFinalReward()
  718. needSave = true
  719. }
  720. if this.finalPrepare <= curTime && curTime <= this.finalBegin { //四强赛结束
  721. this.guildBattleState = Guild_Battle_Final_Prepare
  722. } else if this.finalEnd <= curTime { //半决赛赛结束(表示半决赛没打过)
  723. //公会战结束
  724. this.SetGuildBattleFinish()
  725. } else if curTime > this.finalBegin {
  726. this.guildBattleState = Guild_Battle_Final_Prepare
  727. }
  728. case Guild_Battle_Final_Prepare:
  729. if this.rewardState < Guild_Battle_Semifinals {
  730. this.CheckSemiFinalReward()
  731. needSave = true
  732. }
  733. if this.finalBegin <= curTime && curTime <= this.finalEnd {
  734. this.guildBattleState = Guild_Battle_Final
  735. } else if this.finalEnd <= curTime { //决赛赛结束(表示没有进入过决塞)
  736. //公会战结束
  737. this.SetGuildBattleFinish()
  738. }
  739. case Guild_Battle_Final:
  740. if this.rewardState < Guild_Battle_Semifinals {
  741. this.CheckSemiFinalReward()
  742. needSave = true
  743. }
  744. if this.finalEnd < curTime && curTime <= this.showEnd { //决赛结束
  745. //结算决赛奖励,给胜负两个队伍发送奖励
  746. this.guildBattleState = Guild_Battle_Result
  747. this.rewardState = Guild_Battle_Result
  748. this.sendGuildBattleFinalsReward()
  749. this.sendGuildBattleMvpReward(Guild_Battle_Reward_Final, 1, 1)
  750. needSave = true
  751. } else if curTime > this.showEnd {
  752. this.guildBattleState = Guild_Battle_Result
  753. }
  754. case Guild_Battle_Result:
  755. if this.showEnd < curTime && curTime <= this.nextGuildBattleBegin {
  756. this.guildBattleState = Guild_Battle_End
  757. this.GuildBattleStateChangeNtf(false)
  758. }
  759. default:
  760. }
  761. if oldState != this.guildBattleState || needSave == true {
  762. err2 := this.saveGuildBattleBaseToRedis()
  763. if err2 != nil {
  764. util.InfoF("saveGuildBattleBaseToRedis save failed : %v", err2)
  765. }
  766. }
  767. }
  768. func (this *GuildBattleManager) GuildBattleStateChangeNtf(inBattle bool) {
  769. ntfMsg := &serverproto.SSGuildBattleStageChangeNtf{
  770. InGuildBattle: inBattle,
  771. }
  772. SendToAllGame(ntfMsg)
  773. }
  774. func (this *GuildBattleManager) CheckTournamentReward() bool {
  775. this.initSemifinalsBattle()
  776. this.sendGuildBattleReward(Guild_Battle_Reward_Tournament, 4, 7)
  777. this.sendGuildBattleMvpReward(Guild_Battle_Reward_Tournament, 4, 7)
  778. this.rewardState = Guild_Battle_Tournament
  779. return true
  780. }
  781. func (this *GuildBattleManager) CheckSemiFinalReward() bool {
  782. this.initFinalsBattle()
  783. this.sendGuildBattleReward(Guild_Battle_Reward_Semifinals, 2, 3)
  784. this.sendGuildBattleMvpReward(Guild_Battle_Reward_Semifinals, 2, 3)
  785. this.rewardState = Guild_Battle_Semifinals
  786. return true
  787. }
  788. func (this *GuildBattleManager) ExchangeGuildPre(oldPreUid uint64, curPreUid uint64, guildId uint64) {
  789. if this.guildBattleState <= Guild_Battle_Tournament {
  790. this.handleExchangeTitle(oldPreUid, curPreUid, guildId, 4, 7)
  791. } else if this.guildBattleState <= Guild_Battle_Semifinals {
  792. this.handleExchangeTitle(oldPreUid, curPreUid, guildId, 2, 3)
  793. } else if this.guildBattleState <= Guild_Battle_Final {
  794. this.handleExchangeTitle(oldPreUid, curPreUid, guildId, 1, 1)
  795. }
  796. }
  797. func (this *GuildBattleManager) handleExchangeTitle(oldPreUid, curPreUid, guildId uint64, begin, end int32) {
  798. for index, data := range this.battleIdxDataList {
  799. if begin <= index && end <= index {
  800. data.ExchangeGuildPre(oldPreUid, curPreUid, guildId)
  801. }
  802. }
  803. }
  804. const MaxBattleTeams = 7
  805. // 初始化四强赛队伍
  806. func (this *GuildBattleManager) initBattle() {
  807. topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model.RoleDaoChang100GuildRankPrefix, 0, 7).Result()
  808. if err != nil {
  809. util.ErrorF("UpdateDaoChang100GuildRank")
  810. }
  811. this.battleIdxDataList = make(map[int32]*GuildBattle)
  812. //获取八强的名单。塞入8进4的对阵表中
  813. if len(topListWithScore) >= 5 {
  814. //四强赛公会ID
  815. var seedTeams []uint64
  816. var randTeams = map[uint64]int32{}
  817. for index, _ := range topListWithScore {
  818. guildId, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  819. if index < Guild_Battle_Tournament_Begin_Match {
  820. seedTeams = append(seedTeams, guildId)
  821. } else if index >= Guild_Battle_Tournament_Begin_Match {
  822. randTeams[guildId] = 1
  823. }
  824. }
  825. var randTeamsList []uint64
  826. for key, _ := range randTeams {
  827. randTeamsList = append(randTeamsList, key)
  828. }
  829. //填充四强赛的具体数据4-7 4个队伍
  830. var battleIndex int32 = Guild_Battle_Tournament_Begin_Match
  831. for idx, seedGuildId := range seedTeams {
  832. var guildId2 uint64 = 0
  833. if len(randTeamsList) >= idx+1 {
  834. guildId2 = randTeamsList[idx]
  835. }
  836. this.battleIdxDataList[battleIndex] = newGuildBattle(this, battleIndex, seedGuildId, guildId2, this.tournamentBegin, this.tournamentEnd)
  837. this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
  838. BattleIndex: battleIndex,
  839. Guild1Id: seedGuildId,
  840. Guild2Id: guildId2,
  841. })
  842. battleIndex++
  843. }
  844. this.guildBattleState = Guild_Battle_Start //开始状态
  845. } else if len(topListWithScore) >= Guild_Battle_Seminal_End_Match {
  846. //初始化半决赛
  847. var seedTeams []uint64
  848. var randTeams = map[uint64]int32{} //打乱顺序
  849. for index, _ := range topListWithScore {
  850. guildId, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  851. if index < Guild_Battle_Seminal_Begin_Match {
  852. seedTeams = append(seedTeams, guildId)
  853. } else if index >= Guild_Battle_Seminal_Begin_Match {
  854. randTeams[guildId] = 1
  855. }
  856. }
  857. var randTeamsList []uint64 ///有序排放
  858. for key, _ := range randTeams {
  859. randTeamsList = append(randTeamsList, key)
  860. }
  861. //填充四强赛的具体数据4-7 4个队伍
  862. var battleIndex int32 = Guild_Battle_Seminal_Begin_Match
  863. for idx, seedGuildId := range seedTeams {
  864. var guildId2 uint64 = 0
  865. if len(randTeamsList) >= idx+1 {
  866. guildId2 = randTeamsList[idx]
  867. }
  868. this.battleIdxDataList[battleIndex] = newGuildBattle(this, battleIndex, seedGuildId, guildId2, this.semifinalsBegin, this.semifinalsEnd)
  869. this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
  870. BattleIndex: battleIndex,
  871. Guild1Id: seedGuildId,
  872. Guild2Id: guildId2,
  873. })
  874. battleIndex++
  875. }
  876. this.guildBattleState = Guild_Battle_OffMatch_TS //开始状态
  877. this.rewardState = Guild_Battle_Tournament
  878. } else if len(topListWithScore) == Guild_Battle_Seminal_Begin_Match {
  879. //初始化决赛
  880. guildId, _ := strconv.ParseUint(topListWithScore[0].Member.(string), 10, 64)
  881. guildId2, _ := strconv.ParseUint(topListWithScore[1].Member.(string), 10, 64)
  882. this.battleIdxDataList[1] = newGuildBattle(this, 1, guildId, guildId2, this.finalBegin, this.finalEnd)
  883. this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
  884. BattleIndex: 1,
  885. Guild1Id: guildId,
  886. Guild2Id: guildId2,
  887. })
  888. this.guildBattleState = Guild_Battle_OffMatch_SF //开始状态
  889. this.rewardState = Guild_Battle_Semifinals
  890. } else if len(topListWithScore) == Guild_Battle_Final_Match {
  891. //直接结算冠军
  892. guildId, _ := strconv.ParseUint(topListWithScore[0].Member.(string), 10, 64)
  893. this.battleIdxDataList[1] = newGuildBattle(this, 1, guildId, 0, this.finalBegin, this.finalEnd)
  894. // this.championTeam = guildId
  895. this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
  896. BattleIndex: 1,
  897. Guild1Id: guildId,
  898. Guild2Id: 0,
  899. })
  900. this.guildBattleState = Guild_Battle_Result
  901. this.rewardState = Guild_Battle_Result
  902. this.sendGuildBattleFinalsReward()
  903. //如果只有一个队伍。则当前所有阶段时间都设置为当前的时间
  904. this.ResetStageTime()
  905. } else {
  906. //直接结束
  907. this.guildBattleState = Guild_Battle_Result
  908. }
  909. }
  910. func (this *GuildBattleManager) ResetStageTime() {
  911. nowTime := util.GetCurrentTime()
  912. //四强赛
  913. this.tournamentBegin = nowTime
  914. this.tournamentPrepare = nowTime
  915. this.tournamentEnd = nowTime
  916. //半决赛
  917. this.semifinalsBegin = nowTime
  918. this.semifinalsPrepare = nowTime
  919. this.semifinalsEnd = nowTime
  920. //决赛
  921. this.finalBegin = nowTime
  922. this.finalPrepare = nowTime
  923. this.finalEnd = nowTime
  924. }
  925. // 初始化半决赛队伍 8-4必定有至少3个队伍(前面初始化规则)
  926. func (this *GuildBattleManager) initSemifinalsBattle() {
  927. _, ok := this.battleIdxDataList[3]
  928. if !ok {
  929. //获取八强的名单。塞入4进2的对阵表中
  930. team5, ok5 := this.battleIdxDataList[5]
  931. team6, ok6 := this.battleIdxDataList[6]
  932. if ok6 && ok5 {
  933. winGuildId6, _, bRet6 := team6.GetBattleWinState()
  934. winGuildId5, _, bRet5 := team5.GetBattleWinState()
  935. if bRet6 && bRet5 {
  936. this.battleIdxDataList[3] = newGuildBattle(this, 3, winGuildId5, winGuildId6, this.semifinalsBegin, this.semifinalsEnd)
  937. this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
  938. BattleIndex: 3,
  939. Guild1Id: winGuildId5,
  940. Guild2Id: winGuildId6,
  941. })
  942. }
  943. }
  944. }
  945. _, ok2 := this.battleIdxDataList[2]
  946. if !ok2 {
  947. team4, ok4 := this.battleIdxDataList[4]
  948. team7, ok7 := this.battleIdxDataList[7]
  949. if ok4 {
  950. winGuildId4, _, bRet4 := team4.GetBattleWinState()
  951. if bRet4 {
  952. var guildId7 uint64 = 0
  953. if ok7 {
  954. winGuildId7, _, bRet7 := team7.GetBattleWinState()
  955. if bRet7 {
  956. guildId7 = winGuildId7
  957. this.battleIdxDataList[2] = newGuildBattle(this, 2, winGuildId4, winGuildId7, this.semifinalsBegin, this.semifinalsEnd)
  958. }
  959. } else {
  960. this.battleIdxDataList[2] = newGuildBattle(this, 2, winGuildId4, 0, this.semifinalsBegin, this.semifinalsEnd)
  961. }
  962. this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
  963. BattleIndex: 2,
  964. Guild1Id: winGuildId4,
  965. Guild2Id: guildId7,
  966. })
  967. }
  968. }
  969. }
  970. err2 := this.saveCountTableToRedis()
  971. if err2 != nil {
  972. }
  973. }
  974. // 初始化决赛队伍 4-2必定有2个队伍(前面初始化规则)
  975. func (this *GuildBattleManager) initFinalsBattle() {
  976. _, ok := this.battleIdxDataList[1]
  977. if !ok {
  978. //获取八强的名单。塞入决赛的对阵表中
  979. team2, ok2 := this.battleIdxDataList[2]
  980. team3, ok3 := this.battleIdxDataList[3]
  981. if ok2 && ok3 {
  982. winGuildId2, _, bRet2 := team2.GetBattleWinState()
  983. winGuildId3, _, bRet3 := team3.GetBattleWinState()
  984. if bRet2 && bRet3 {
  985. this.battleIdxDataList[1] = newGuildBattle(this, 1, winGuildId2, winGuildId3, this.finalBegin, this.finalEnd)
  986. this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
  987. BattleIndex: 1,
  988. Guild1Id: winGuildId2,
  989. Guild2Id: winGuildId3,
  990. })
  991. }
  992. }
  993. }
  994. err2 := this.saveCountTableToRedis()
  995. if err2 != nil {
  996. }
  997. }
  998. const (
  999. GuileBattle_Reward_Rank_First = 1 //冠军奖励
  1000. GuileBattle_Reward_Rank_Second = 2 //亚军奖励
  1001. GuileBattle_Reward_Rank_Semifinals = 3 //4-2
  1002. GuileBattle_Reward_Rank_Tournament = 4 //8-4
  1003. )
  1004. func (this *GuildBattleManager) SendGuildBattleReward(rewardType int32, guildId uint64) {
  1005. //发送公会成员奖励
  1006. guild := GuildMag.GetGuild(guildId)
  1007. if guild == nil || guild.GuildMember == nil || guild.GuildBase == nil {
  1008. util.ErrorF("SendGuildBattleReward, guild not found : %v", guildId)
  1009. return
  1010. }
  1011. ntfMsg := &serverproto.SSGuildBattleRewardNtf{
  1012. RewardType: rewardType,
  1013. GuildRound: this.guildBattleId,
  1014. }
  1015. for _, data := range guild.GuildMember.MemberInfo {
  1016. if data.Title == Guild_Title_Pre {
  1017. ntfMsg.GuildPre = data.MemberId
  1018. } else {
  1019. ntfMsg.UidList = append(ntfMsg.UidList, data.MemberId)
  1020. }
  1021. }
  1022. //发送公会会长特殊奖励
  1023. SendToAllGame(ntfMsg)
  1024. }
  1025. // 发送四强赛失败奖励
  1026. func (this *GuildBattleManager) sendGuildBattleReward(rewardType int32, beginIdx, endIdx int32) {
  1027. for i := beginIdx; i <= endIdx; i++ {
  1028. team, ok := this.battleIdxDataList[i]
  1029. if !ok {
  1030. //打印日志
  1031. continue
  1032. }
  1033. winGuild, lostGuild, ok := team.GetBattleWinState()
  1034. if !ok {
  1035. continue
  1036. }
  1037. util.InfoF("SendGuildBattleReward, lost guildId:%v, winGuildId:%v, battleIndex:%v", lostGuild, winGuild, i)
  1038. //发送奖励给LostGuild
  1039. if rewardType == Guild_Battle_Reward_Tournament {
  1040. this.SendGuildBattleReward(GuileBattle_Reward_Rank_Tournament, lostGuild)
  1041. } else if rewardType == Guild_Battle_Reward_Semifinals {
  1042. this.SendGuildBattleReward(GuileBattle_Reward_Rank_Semifinals, lostGuild)
  1043. }
  1044. }
  1045. }
  1046. // 发送决赛奖励
  1047. func (this *GuildBattleManager) sendGuildBattleFinalsReward() {
  1048. team, ok := this.battleIdxDataList[1]
  1049. if !ok {
  1050. //打印日志
  1051. util.ErrorF("SendGuildBattleFinalsReward, team not found : %v", this.battleIdxDataList)
  1052. return
  1053. }
  1054. winGuild, lostGuild, ok := team.GetBattleWinState()
  1055. if !ok {
  1056. util.ErrorF("SendGuildBattleFinalsReward, team not found2 : %v, %v", winGuild, lostGuild)
  1057. return
  1058. }
  1059. if team != nil {
  1060. if team.battleBaseData != nil {
  1061. util.ErrorF("send battle final reward:%v,win:%v,lost:%v", team.battleBaseData, winGuild, lostGuild)
  1062. }
  1063. }
  1064. //发送奖励给
  1065. this.SendGuildBattleReward(GuileBattle_Reward_Rank_Second, lostGuild)
  1066. this.SendGuildBattleReward(GuileBattle_Reward_Rank_First, winGuild)
  1067. }
  1068. const (
  1069. Rank_Type_Score = 1
  1070. Rank_Type_Killer = 2
  1071. )
  1072. func (this *GuildBattleManager) GuildBattleMvpReward(ackMsg *serverproto.SSGuildBattleSendMvpRewardAck) {
  1073. if ackMsg == nil {
  1074. return
  1075. }
  1076. if len(ackMsg.ScoreMvp) <= 0 && len(ackMsg.KillerMvp) <= 0 {
  1077. return
  1078. }
  1079. //设置领奖标记
  1080. for _, scoreData := range ackMsg.ScoreMvp {
  1081. battle, ok := this.battleIdxDataList[scoreData.BattleIndex]
  1082. if !ok {
  1083. //打印日志,给了奖励,但是没有做记录
  1084. continue
  1085. }
  1086. battle.battleBaseData.ScoreMvp = append(battle.battleBaseData.ScoreMvp, scoreData.Uid)
  1087. battle.baseChange = true
  1088. }
  1089. for _, killerData := range ackMsg.KillerMvp {
  1090. battle, ok := this.battleIdxDataList[killerData.BattleIndex]
  1091. if !ok {
  1092. //打印日志,给了奖励,但是没有做记录
  1093. continue
  1094. }
  1095. battle.battleBaseData.KillerMvp = append(battle.battleBaseData.KillerMvp, killerData.Uid)
  1096. battle.baseChange = true
  1097. }
  1098. }
  1099. func (this *GuildBattleManager) checkRewardDraw(mvpType, battleIndex int32, uid uint64) bool {
  1100. guildBattle := this.getBattleByIdx(battleIndex)
  1101. if guildBattle == nil || guildBattle.battleBaseData == nil {
  1102. return true //找不到这场战斗,不给奖励
  1103. }
  1104. if len(guildBattle.battleBaseData.ScoreMvp) > 0 && mvpType == Rank_Type_Score {
  1105. for _, rewardUid := range guildBattle.battleBaseData.ScoreMvp {
  1106. if rewardUid == uid {
  1107. return true
  1108. }
  1109. }
  1110. } else if len(guildBattle.battleBaseData.KillerMvp) > 0 && mvpType == Rank_Type_Killer {
  1111. for _, rewardUid := range guildBattle.battleBaseData.KillerMvp {
  1112. if rewardUid == uid {
  1113. return true
  1114. }
  1115. }
  1116. }
  1117. return false
  1118. }
  1119. // 场次结束结束发送排名奖励
  1120. func (this *GuildBattleManager) sendGuildBattleMvpReward(rewardType int32, startBattle, endBattle int32) {
  1121. ntfMsg := &serverproto.SSGuildBattleSendMvpRewardReq{
  1122. GuildBattleRound: this.guildBattleId,
  1123. }
  1124. for i := startBattle; i <= endBattle; i++ {
  1125. //打包 积分Mvp
  1126. scoreStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(i)) + "-" + strconv.Itoa(int(Rank_Type_Score))
  1127. retList, err := service.GetRedis().ZRevRangeWithScores(scoreStr, int64(0), int64(0)).Result()
  1128. if err == nil {
  1129. for idx := 0; idx < len(retList); {
  1130. uid, _ := model.Str2NumU64(retList[idx].Member.(string))
  1131. bFind := this.checkRewardDraw(Rank_Type_Score, i, uid)
  1132. if bFind {
  1133. break
  1134. }
  1135. ntfMsg.ScoreMvp = append(ntfMsg.ScoreMvp, &serverproto.MvpRewardInfo{
  1136. Uid: uid,
  1137. RewardType: int32(rewardType),
  1138. BattleIndex: int32(i),
  1139. })
  1140. break
  1141. }
  1142. }
  1143. //打包 击杀Mvp
  1144. killerStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(i)) + "-" + strconv.Itoa(int(Rank_Type_Killer))
  1145. retList2, err2 := service.GetRedis().ZRevRangeWithScores(killerStr, int64(0), int64(0)).Result()
  1146. if err2 == nil {
  1147. for idx := 0; idx < len(retList2); {
  1148. uid, _ := model.Str2NumU64(retList2[idx].Member.(string))
  1149. bFind := this.checkRewardDraw(Rank_Type_Killer, i, uid)
  1150. if bFind {
  1151. break
  1152. }
  1153. ntfMsg.KillerMvp = append(ntfMsg.KillerMvp, &serverproto.MvpRewardInfo{
  1154. Uid: uid,
  1155. RewardType: int32(rewardType),
  1156. BattleIndex: i,
  1157. })
  1158. break
  1159. }
  1160. }
  1161. }
  1162. SendToAllGame(ntfMsg)
  1163. }
  1164. // 对阵数据启动服务器加载
  1165. func (this *GuildBattleManager) getBattleByIdx(battleIdx int32) *GuildBattle {
  1166. if this.banType == Guild_Battle_Baned {
  1167. return nil
  1168. }
  1169. if battle, ok := this.battleIdxDataList[battleIdx]; ok {
  1170. return battle
  1171. }
  1172. //TODO 查看数据库中是否存在对阵表,存在则构建战斗数据
  1173. // 并初始化战斗信息
  1174. //guildBattleKey := GuildBattlePrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(battleIdx))
  1175. return nil
  1176. }
  1177. func (this *GuildBattleManager) GMGuildBattleOperator(operator int32) {
  1178. this.operator = operator
  1179. }
  1180. func (this *GuildBattleManager) GMAddGuildBattleScore(battleIdx int32, guild_index int32, score int32) {
  1181. guildBattle := this.getBattleByIdx(battleIdx)
  1182. if guildBattle != nil {
  1183. if guild_index == 0 {
  1184. guildBattle.battleBaseData.ScoreList[0].Value += score
  1185. } else if guild_index == 1 {
  1186. guildBattle.battleBaseData.ScoreList[1].Value += score
  1187. }
  1188. }
  1189. }
  1190. // 公会战开关
  1191. func (this *GuildBattleManager) WebGMGuildBattle() {
  1192. var inBattle bool
  1193. if this.banType == 0 {
  1194. //原先开着的,则关闭
  1195. this.banType = 1
  1196. inBattle = false
  1197. util.InfoF("WebGMGuildBattle, guild battle current state close")
  1198. } else {
  1199. //原先关闭的,则开着
  1200. this.banType = 0
  1201. inBattle = true
  1202. util.InfoF("WebGMGuildBattle, guild battle current state open")
  1203. }
  1204. this.GuildBattleStateChangeNtf(inBattle)
  1205. err := this.saveGuildBattleBaseToRedis()
  1206. if err != nil {
  1207. }
  1208. }
  1209. // todo wangzhaocan 测试用 待删除
  1210. func (this *GuildBattleManager) GMGuildBattleReward() {
  1211. this.sendGuildBattleReward(Guild_Battle_Reward_Semifinals, 2, 3)
  1212. }
  1213. // 获取对阵双方信息(打开界面时的信息)
  1214. func (this *GuildBattleManager) GuildBattleInfo(selfGuildId, uid uint64, battleIdx int32, ackMsg *serverproto.SCGuildBattleInfoAck) {
  1215. if this.banType == Guild_Battle_Baned {
  1216. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_GUILD_SPECIAL_STATE)
  1217. return
  1218. }
  1219. this.initGuildBattleList()
  1220. battleData := this.getBattleByIdx(battleIdx)
  1221. if battleData == nil {
  1222. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1223. return
  1224. }
  1225. battleData.BattleInfoReq(uid, selfGuildId, ackMsg)
  1226. }
  1227. func (this *GuildBattleManager) GuildBattleSetting(battleIdx int32, uid, guildId uint64, autoChallenge, autoReborn bool, ackMsg *serverproto.SCGuildBattleSettingAck) {
  1228. this.initGuildBattleList()
  1229. battleData := this.getBattleByIdx(battleIdx)
  1230. if battleData == nil {
  1231. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1232. return
  1233. }
  1234. battleData.BattleSetting(uid, guildId, autoChallenge, autoReborn, ackMsg)
  1235. }
  1236. // 是否在购买时间内//购买时间之前/之后,做区分
  1237. func (this *GuildBattleManager) CheckInBuyTime(battleData *GuildBattle) serverproto.ErrorCode {
  1238. if battleData == nil {
  1239. return serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST
  1240. }
  1241. if Guild_Battle_Tournament_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Tournament_End_Match { //四强
  1242. if this.guildBattleState < Guild_Battle_Tournament_Prepare {
  1243. return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
  1244. }
  1245. } else if Guild_Battle_Seminal_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Seminal_End_Match { //半决赛
  1246. if this.guildBattleState < Guild_Battle_Semifinals_Prepare {
  1247. return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
  1248. }
  1249. } else if Guild_Battle_Tournament_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Tournament_End_Match { //决赛
  1250. if this.guildBattleState < Guild_Battle_Final_Prepare {
  1251. return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
  1252. }
  1253. }
  1254. _, _, ok := battleData.GetBattleWinState()
  1255. if ok {
  1256. return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_FINISH
  1257. }
  1258. return serverproto.ErrorCode_ERROR_OK
  1259. }
  1260. func (this *GuildBattleManager) BuyChallengeNum(battleIdx int32, uid, guildId uint64, ackMsg *serverproto.SCGuildBattleBuyChallengeAck) {
  1261. this.initGuildBattleList()
  1262. battleData := this.getBattleByIdx(battleIdx)
  1263. if battleData == nil {
  1264. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1265. return
  1266. }
  1267. bRet := this.CheckInBuyTime(battleData)
  1268. if bRet != serverproto.ErrorCode_ERROR_OK {
  1269. ackMsg.Error = int32(bRet)
  1270. return
  1271. }
  1272. battleData.BuyChallengeNum(uid, guildId, ackMsg)
  1273. }
  1274. func (this *GuildBattleManager) BuyBattleBuff(battleIdx int32, uid uint64, buffList []int32, ackMsg *serverproto.SSGuildBattleBuyBuffAck) {
  1275. this.initGuildBattleList()
  1276. battleData := this.getBattleByIdx(battleIdx)
  1277. if battleData == nil {
  1278. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1279. return
  1280. }
  1281. bRet := this.CheckInBuyTime(battleData)
  1282. if bRet != serverproto.ErrorCode_ERROR_OK {
  1283. ackMsg.Error = int32(bRet)
  1284. return
  1285. }
  1286. guildIdx := GuildMag.RoleToGuilds[uid]
  1287. if guildIdx != nil {
  1288. battleData.BuyBattleBuff(uid, guildIdx.GuildId, buffList, ackMsg)
  1289. }
  1290. }
  1291. func (this *GuildBattleManager) ResetRebornCD(battleIdx int32, uid, guildId uint64, ackMsg *serverproto.SCGuildBattleRebornAck) {
  1292. this.initGuildBattleList()
  1293. battleData := this.getBattleByIdx(battleIdx)
  1294. if battleData == nil {
  1295. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1296. return
  1297. }
  1298. bRet := this.CheckInBuyTime(battleData)
  1299. if bRet != serverproto.ErrorCode_ERROR_OK {
  1300. ackMsg.Error = int32(bRet)
  1301. return
  1302. }
  1303. battleData.ResetRebornCD(uid, guildId, ackMsg)
  1304. }
  1305. func (this *GuildBattleManager) GetBattleRankList(battleIdx, pageIdx, rankType int32, ackMsg *serverproto.SCGuildBattleRankListAck) {
  1306. this.initGuildBattleList()
  1307. battleData := this.getBattleByIdx(battleIdx)
  1308. if battleData == nil {
  1309. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1310. return
  1311. }
  1312. GetGuildBattleRankList(this.guildBattleId, battleIdx, pageIdx, rankType, ackMsg)
  1313. }
  1314. // limit 20 items
  1315. func (this *GuildBattleManager) GetBattlePosIdxList(battleIdx int32, posIdxList []int32, ackMsg *serverproto.SCGuildBattlePosIdxListAck) {
  1316. if len(posIdxList) <= 0 || len(posIdxList) > 20 {
  1317. return
  1318. }
  1319. this.initGuildBattleList()
  1320. battleData := this.getBattleByIdx(battleIdx)
  1321. if battleData == nil {
  1322. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1323. return
  1324. }
  1325. bRet := this.CheckInBuyTime(battleData)
  1326. if bRet != serverproto.ErrorCode_ERROR_OK {
  1327. ackMsg.Error = int32(bRet)
  1328. return
  1329. }
  1330. battleData.GetBattlePosIdxList(posIdxList, ackMsg)
  1331. }
  1332. func (this *GuildBattleManager) GetPosIdxLog(battleIdx int32, beginTime uint64, ackMsg *serverproto.SCGuildBattleLogAck) {
  1333. this.initGuildBattleList()
  1334. battleData := this.getBattleByIdx(battleIdx)
  1335. if battleData == nil {
  1336. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1337. return
  1338. }
  1339. battleData.GetPosIdxLog(beginTime, ackMsg)
  1340. }
  1341. func (this *GuildBattleManager) BattleChallenge(battleIdx, posIdx int32, selfGuildId uint64, buyNum int32, bfInfo *serverproto.CommonPlayerBriefInfo,
  1342. curRmb uint64, ackMsg *serverproto.SSGuildBattleChallengeAck) {
  1343. this.initGuildBattleList()
  1344. battleData := this.getBattleByIdx(battleIdx)
  1345. if battleData == nil {
  1346. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1347. return
  1348. }
  1349. bRet := this.CheckInBuyTime(battleData)
  1350. if bRet != serverproto.ErrorCode_ERROR_OK {
  1351. ackMsg.Error = int32(bRet)
  1352. return
  1353. }
  1354. battleData.BattleChallengePre(posIdx, selfGuildId, bfInfo, curRmb, buyNum, ackMsg)
  1355. }
  1356. func (this *GuildBattleManager) BattleChallengeResult(msg *serverproto.CSGuildBattleChallengeResultReq,
  1357. ssAckMsg *serverproto.SCGuildBattleChallengeResultAck, ev rocommon.ProcEvent) {
  1358. this.initGuildBattleList()
  1359. battleData := this.getBattleByIdx(msg.GuildBattleIdx)
  1360. if battleData == nil {
  1361. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1362. return
  1363. }
  1364. bRet := this.CheckInBuyTime(battleData)
  1365. if bRet != serverproto.ErrorCode_ERROR_OK {
  1366. ssAckMsg.Error = int32(bRet)
  1367. return
  1368. }
  1369. battleData.BattleChallengeResult(msg.PosIdx, msg.SelfGuildId, msg.BfInfo, msg.BattleResult, ssAckMsg, ev)
  1370. }
  1371. func (this *GuildBattleManager) BattleChallengePing(challengeUid uint64, battleIdx, posIdx int32,
  1372. ackMsg *serverproto.SCGuildBattleChallengePingAck) {
  1373. this.initGuildBattleList()
  1374. battleData := this.getBattleByIdx(battleIdx)
  1375. if battleData == nil {
  1376. return
  1377. }
  1378. battleData.BattleChallengePing(challengeUid, posIdx, ackMsg)
  1379. }
  1380. func (this *GuildBattleManager) BattlePKData(battleIdx int32, ssAckMsg *serverproto.SCGuildBattlePKDataAck) {
  1381. this.initGuildBattleList()
  1382. battleData := this.getBattleByIdx(battleIdx)
  1383. if battleData == nil {
  1384. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1385. return
  1386. }
  1387. bRet := this.CheckInBuyTime(battleData)
  1388. if bRet != serverproto.ErrorCode_ERROR_OK {
  1389. ssAckMsg.Error = int32(bRet)
  1390. // return
  1391. }
  1392. battleData.BattlePKData(ssAckMsg)
  1393. }
  1394. func (this *GuildBattleManager) GetGuildBattleCPRank(guildId uint64, ackMsg *serverproto.SSGuildBattleCPRankAck) {
  1395. topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model.RoleDaoChang100GuildRankPrefix, 0, 19).Result()
  1396. if err != nil {
  1397. util.ErrorF("GetGuildBattleCPRank")
  1398. return
  1399. }
  1400. util.InfoF("GetGuildBattleCPRank len:%v", len(topListWithScore))
  1401. for index, _ := range topListWithScore {
  1402. guildId, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  1403. guild := GuildMag.GetGuild(guildId)
  1404. if guild == nil {
  1405. continue
  1406. }
  1407. cpNum := getValByDaoChang100GuildScore(topListWithScore[index].Score)
  1408. ackMsg.CpList = append(ackMsg.CpList, &serverproto.GuildBattleCP{
  1409. Brief: guild.GuildBase.GuildBrief,
  1410. MemCount: int32(len(guild.GuildMember.MemberInfo)),
  1411. CpNum: uint32(cpNum),
  1412. Rank: int32(index + 1),
  1413. })
  1414. }
  1415. if guildId != 0 {
  1416. guildIdStr := strconv.FormatUint(guildId, 10)
  1417. tmpScore, err2 := service.GetRedis().ZScore(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
  1418. if err2 == nil {
  1419. oldScore := getValByDaoChang100GuildScore(tmpScore)
  1420. ackMsg.SelfGuildCp = int32(oldScore)
  1421. }
  1422. selfRank, rankErr := service.GetRedis().ZRevRank(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
  1423. if rankErr == nil {
  1424. ackMsg.SelfGuildRank = int32(selfRank + 1)
  1425. }
  1426. }
  1427. }
  1428. func (this *GuildBattleManager) GetBattleState(guildId1, guildId2 uint64, battle *GuildBattle) (bool, int32) {
  1429. if battle == nil {
  1430. return false, 0
  1431. }
  1432. bRet := false
  1433. retState := int32(0)
  1434. winGuild, _, ok := battle.GetBattleWinState()
  1435. if ok {
  1436. if winGuild == guildId1 {
  1437. bRet = true
  1438. } else if winGuild == guildId2 {
  1439. bRet = false
  1440. }
  1441. retState = Battle_State_End
  1442. } else {
  1443. nowTime := util.GetCurrentTime()
  1444. if Guild_Battle_Tournament_Begin_Match <= battle.guildBattleIdx && battle.guildBattleIdx <= Guild_Battle_Tournament_End_Match {
  1445. if this.tournamentPrepare <= nowTime && nowTime <= this.tournamentEnd {
  1446. retState = Battle_State_Fight
  1447. } else {
  1448. retState = Battle_State_Prepare
  1449. }
  1450. } else if Guild_Battle_Seminal_Begin_Match <= battle.guildBattleIdx && battle.guildBattleIdx <= Guild_Battle_Seminal_End_Match {
  1451. if this.semifinalsPrepare <= nowTime && nowTime <= this.semifinalsEnd {
  1452. retState = Battle_State_Fight
  1453. } else {
  1454. retState = Battle_State_Prepare
  1455. }
  1456. } else if Guild_Battle_Final_Match == battle.guildBattleIdx {
  1457. if this.finalPrepare <= nowTime && nowTime <= this.finalEnd {
  1458. retState = Battle_State_Fight
  1459. } else {
  1460. retState = Battle_State_Prepare
  1461. }
  1462. }
  1463. }
  1464. return bRet, retState
  1465. }
  1466. const (
  1467. Battle_State_Prepare = 1
  1468. Battle_State_Fight = 2
  1469. Battle_State_End = 3
  1470. )
  1471. func (this *GuildBattleManager) GetGuildBattleCountPart(ackMsg *serverproto.SSGuildBattleCountPartAck) {
  1472. if this.banType == Guild_Battle_Baned {
  1473. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_GUILD_SPECIAL_STATE)
  1474. return
  1475. }
  1476. if this.guildBattleId == 0 || this.showEnd <= util.GetCurrentTime() {
  1477. return
  1478. }
  1479. ackMsg.Table = &serverproto.GuildCountPartTable{}
  1480. for _, data := range this.countPartTable.Data {
  1481. battle := this.battleIdxDataList[data.BattleIndex]
  1482. if battle == nil {
  1483. continue
  1484. }
  1485. countTable := &serverproto.CountTableData{}
  1486. countTable.BattleIndex = data.BattleIndex
  1487. countTable.Guild1Id = data.Guild1Id
  1488. countTable.Guild2Id = data.Guild2Id
  1489. if len(battle.battleBaseData.ScoreList) >= 1 {
  1490. countTable.Guild1Name = battle.battleBaseData.ScoreList[0].StrVal
  1491. countTable.Guild1Badge = battle.battleBaseData.ScoreList[0].Value3
  1492. }
  1493. if len(battle.battleBaseData.ScoreList) >= 2 {
  1494. countTable.Guild2Name = battle.battleBaseData.ScoreList[1].StrVal
  1495. countTable.Guild2Badge = battle.battleBaseData.ScoreList[1].Value3
  1496. }
  1497. retWin, retState := this.GetBattleState(data.Guild1Id, data.Guild2Id, battle)
  1498. countTable.WinGuild = retWin
  1499. countTable.State = retState
  1500. ackMsg.Table.Data = append(ackMsg.Table.Data, countTable)
  1501. }
  1502. // ackMsg.Table = this.countPartTable
  1503. ackMsg.CurStge = this.guildBattleState
  1504. ackMsg.TournamentPrepare = this.tournamentPrepare
  1505. ackMsg.TournamentBegin = this.tournamentBegin
  1506. ackMsg.TournamentEnd = this.tournamentEnd
  1507. ackMsg.SemifinalsPrepare = this.semifinalsPrepare
  1508. ackMsg.SemifinalsBegin = this.semifinalsBegin
  1509. ackMsg.SemifinalsEnd = this.semifinalsEnd
  1510. ackMsg.FinalsPrepare = this.finalPrepare
  1511. ackMsg.FinalsBegin = this.finalBegin
  1512. ackMsg.FinalsEnd = this.finalEnd
  1513. }
  1514. func (this *GuildBattleManager) GetGuildBattleMvpInfo(battleIdx int32, ackMsg *serverproto.SSGuildBattleMvpInfoAck) {
  1515. if ackMsg == nil {
  1516. return
  1517. }
  1518. data, ok := this.battleIdxDataList[battleIdx]
  1519. if !ok {
  1520. return
  1521. }
  1522. var guildId1 uint64 = 0
  1523. for idx, scoreList := range data.battleBaseData.ScoreList {
  1524. ackMsg.Detail = append(ackMsg.Detail, &serverproto.GuildBattleDetail{
  1525. GuildId: scoreList.Key,
  1526. SlotCount: 0,
  1527. Score: scoreList.Value,
  1528. })
  1529. guild := GuildMag.GetGuild(scoreList.Key)
  1530. if guild != nil {
  1531. if idx == 1 {
  1532. guildId1 = scoreList.Key
  1533. ackMsg.CountPart.Guild1Id = scoreList.Key
  1534. ackMsg.CountPart.Guild1Name = guild.GuildBase.GuildBrief.GuildName
  1535. } else if idx == 2 {
  1536. ackMsg.CountPart.Guild2Id = scoreList.Key
  1537. ackMsg.CountPart.Guild2Name = guild.GuildBase.GuildBrief.GuildName
  1538. }
  1539. }
  1540. }
  1541. winGuild, _, bRet := this.GetBattleWinState(battleIdx)
  1542. if bRet {
  1543. if winGuild == guildId1 {
  1544. ackMsg.CountPart.WinGuild = true
  1545. } else {
  1546. ackMsg.CountPart.WinGuild = false
  1547. }
  1548. }
  1549. }
  1550. func (this *GuildBattleManager) GetGuildBattleMvpDetail(battleIdx int32, mvpType int32, reqRank int32, ackMsg *serverproto.SSGuildBattleMvpDetailAck) {
  1551. keyStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(battleIdx)) + "-" + strconv.Itoa(int(mvpType))
  1552. topListWithScore, err := service.GetRedis().ZRevRangeWithScores(keyStr, int64(reqRank), int64(reqRank+9)).Result()
  1553. if err != nil {
  1554. util.ErrorF("UpdateDaoChang100GuildRank")
  1555. }
  1556. for index, data := range topListWithScore {
  1557. tmpScore := getValByGuildBattleScore(data.Score)
  1558. uid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  1559. mvp := &serverproto.GuildBattleMvp{
  1560. Uid: uid,
  1561. Score: int32(tmpScore),
  1562. }
  1563. mvp.GuildId = GuildMag.GetPlayerGuildId(uid)
  1564. guild := GuildMag.GetGuild(mvp.GuildId)
  1565. if guild != nil {
  1566. mvp.Name = guild.GuildBase.GuildBrief.GuildName
  1567. }
  1568. ackMsg.Mvp = append(ackMsg.Mvp, mvp)
  1569. }
  1570. }
  1571. func (this *GuildBattleManager) GetBattleWinStateByBaseData(baseData *serverproto.GuildBattleBaseData) (uint64, uint64, bool) {
  1572. scoreList := baseData.ScoreList
  1573. if len(scoreList) == 2 { //满编队伍
  1574. if scoreList[0].Value == scoreList[1].Value {
  1575. if scoreList[0].Value2 == scoreList[0].Value2 {
  1576. return scoreList[0].Key, scoreList[1].Key, true
  1577. } else if scoreList[0].Value2 < scoreList[0].Value2 {
  1578. return scoreList[0].Key, scoreList[1].Key, true
  1579. } else {
  1580. return scoreList[1].Key, scoreList[0].Key, true
  1581. }
  1582. } else if scoreList[0].Value > scoreList[1].Value {
  1583. return scoreList[0].Key, scoreList[1].Key, true
  1584. } else {
  1585. return scoreList[1].Key, scoreList[0].Key, true
  1586. }
  1587. } else if len(scoreList) == 1 { //轮空的情况下
  1588. return scoreList[0].Key, scoreList[1].Key, true
  1589. }
  1590. return 0, 0, false
  1591. }
  1592. func (this *GuildBattleManager) CheckCurRewardRound(uid uint64) bool {
  1593. for key, battle := range this.battleIdxDataList {
  1594. _, lostGuild, ret := this.GetBattleWinState(key)
  1595. if !ret {
  1596. continue
  1597. }
  1598. playerData := battle.getPlayerData(uid, lostGuild)
  1599. if playerData == nil {
  1600. continue
  1601. }
  1602. //战斗结束//且有这个玩家,如果key是决赛,那就可以给奖励了,不是决赛且这场战斗输了也给奖励
  1603. if key == 1 || key != 1 && playerData.GuildId == lostGuild {
  1604. return true
  1605. }
  1606. }
  1607. return false
  1608. }
  1609. func (this *GuildBattleManager) PackPlayerGuildBattleReward(uid uint64, round, index int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) serverproto.ErrorCode {
  1610. keyStr := GuildBattlePrefix + strconv.Itoa(int(round)) + "-" + strconv.Itoa(int(index))
  1611. fieldStr := "uid:" + strconv.FormatUint(uid, 10)
  1612. resultData, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
  1613. if err != nil {
  1614. util.ErrorF("GuildBattleSelfData err uid:%v round: %v battleindex: %v, err3: %v", uid, round, index, err)
  1615. return serverproto.ErrorCode_ERROR_FAIL
  1616. }
  1617. playerData := &serverproto.GuildBattleSelfData{}
  1618. err2 := model.GetDecodeMessage(playerData, resultData)
  1619. if err2 != nil {
  1620. util.ErrorF("GuildBattleSelfData not decode uid:%v round: %v battleindex: %v, err3: %v", uid, round, index, err2)
  1621. return serverproto.ErrorCode_ERROR_FAIL
  1622. }
  1623. isPre := false
  1624. if playerData.GuildPre == true {
  1625. isPre = true
  1626. }
  1627. rewardType := 0
  1628. var winGuild uint64 = 0
  1629. var lostGuild uint64 = 0
  1630. //如果是本轮活动,则判定是否战斗结束://
  1631. if round == this.guildBattleId {
  1632. win, lost, ok := this.GetBattleWinState(int32(index))
  1633. if !ok { //没结束就不管了
  1634. return serverproto.ErrorCode_ERROR_FAIL
  1635. }
  1636. //不是冠军赛 则要看是否是战斗失败的公会
  1637. if index != 1 && playerData.GuildId != lost {
  1638. return serverproto.ErrorCode_ERROR_FAIL
  1639. }
  1640. winGuild = win
  1641. lostGuild = lost
  1642. } else {
  1643. //获取比赛的信息
  1644. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(round)) + "-" + strconv.Itoa(int(index))
  1645. retStr, err3 := service.GetRedis().HGet(battleKeyStr, "base").Result()
  1646. if err3 != nil {
  1647. util.ErrorF("GuildBattleBaseData err round: %v battleindex: %v, err3: %v", round, index, err3)
  1648. return serverproto.ErrorCode_ERROR_FAIL
  1649. }
  1650. //获取比赛的基础数据
  1651. battleBaseData := &serverproto.GuildBattleBaseData{}
  1652. err4 := model.GetDecodeMessage(battleBaseData, retStr)
  1653. if err4 != nil {
  1654. util.ErrorF("GuildBattleBaseData not decode round: %v battleindex:%v: err: %v", round, index, err4)
  1655. return serverproto.ErrorCode_ERROR_FAIL
  1656. }
  1657. //比赛未结束,则后续不处理
  1658. win2, lost2, ret := this.GetBattleWinStateByBaseData(battleBaseData)
  1659. if !ret {
  1660. return serverproto.ErrorCode_ERROR_FAIL
  1661. }
  1662. winGuild = win2
  1663. lostGuild = lost2
  1664. }
  1665. if index == 1 {
  1666. if playerData.GuildId == winGuild {
  1667. rewardType = GuileBattle_Reward_Rank_First
  1668. } else if playerData.GuildId == lostGuild {
  1669. rewardType = GuileBattle_Reward_Rank_Second
  1670. }
  1671. } else if index == 2 || index == 3 {
  1672. rewardType = GuileBattle_Reward_Rank_Semifinals
  1673. } else if 4 <= index && index <= 7 {
  1674. rewardType = GuileBattle_Reward_Rank_Tournament
  1675. }
  1676. ackMsg.RoundReward = append(ackMsg.RoundReward, &serverproto.GuildBattleReward{
  1677. IsPre: isPre,
  1678. RewardType: int32(rewardType),
  1679. })
  1680. util.InfoF("uid=%v GuildBattleBaseData online get reward curround :%v, round: %v battleindex: %v guildid:%v", uid, this.guildBattleId, round, index, playerData.GuildId)
  1681. return serverproto.ErrorCode_ERROR_OK
  1682. }
  1683. func (this *GuildBattleManager) PackPlayerGuildBattleMvpReward(uid uint64, mvpRound, index int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) {
  1684. //获取本场战斗的base
  1685. var battleBaseData *serverproto.GuildBattleBaseData = nil
  1686. if mvpRound == this.guildBattleId {
  1687. battle, ok := this.battleIdxDataList[int32(index)]
  1688. if !ok {
  1689. return
  1690. }
  1691. battleBaseData = battle.battleBaseData
  1692. //公会战是否结束,未结束就不管
  1693. _, _, ok2 := this.GetBattleWinState(int32(index))
  1694. if !ok2 { //没结束就不管了
  1695. return
  1696. }
  1697. } else {
  1698. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(mvpRound)) + "-" + strconv.Itoa(int(index))
  1699. retStr, err3 := service.GetRedis().HGet(battleKeyStr, "base").Result()
  1700. if err3 != nil {
  1701. util.ErrorF("MvpReward GuildBattleBaseData err round: %v battleindex: %v, err3: %v", mvpRound, index, err3)
  1702. return
  1703. }
  1704. if err3 == service.NIL {
  1705. util.ErrorF("MvpReward GuildBattleBaseData not found round: %v battleindex: %v, err3: %v", mvpRound, index)
  1706. return
  1707. }
  1708. battleBaseData = &serverproto.GuildBattleBaseData{}
  1709. //获取比赛的基础数据
  1710. err4 := model.GetDecodeMessage(battleBaseData, retStr)
  1711. if err4 != nil {
  1712. util.ErrorF("MvpReward GuildBattleBaseData not decode round: %v battleindex:%v: err: %v", mvpRound, index, err4)
  1713. return
  1714. }
  1715. }
  1716. if battleBaseData == nil {
  1717. return
  1718. }
  1719. //遍历所有领奖记录,是否领取过奖励
  1720. var bScoreReward bool = false
  1721. var bKillerReward bool = false
  1722. if len(battleBaseData.ScoreMvp) >= 0 {
  1723. for _, rewardUid := range battleBaseData.ScoreMvp {
  1724. if rewardUid == uid {
  1725. bScoreReward = true
  1726. break
  1727. }
  1728. }
  1729. }
  1730. if len(battleBaseData.KillerMvp) >= 0 {
  1731. for _, rewardUid := range battleBaseData.KillerMvp {
  1732. if rewardUid == uid {
  1733. bKillerReward = true
  1734. }
  1735. break
  1736. }
  1737. }
  1738. if bScoreReward == true && bKillerReward == true {
  1739. return
  1740. }
  1741. //获取奖励类型
  1742. rewardType := Guild_Battle_Reward_Tournament
  1743. if index == 1 {
  1744. rewardType = Guild_Battle_Reward_Final
  1745. } else if index == 2 || index == 3 {
  1746. rewardType = Guild_Battle_Reward_Semifinals
  1747. }
  1748. //打包 积分Mvp
  1749. if bScoreReward == false {
  1750. scoreStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(index)) + "-" + strconv.Itoa(int(Rank_Type_Score))
  1751. retList, err3 := service.GetRedis().ZRevRangeWithScores(scoreStr, int64(0), int64(0)).Result()
  1752. if err3 == nil && len(retList) > 0 {
  1753. firstUid, _ := model.Str2NumU64(retList[0].Member.(string))
  1754. if uid == firstUid {
  1755. ackMsg.ScoreMvp = append(ackMsg.ScoreMvp, &serverproto.MvpRewardInfo{
  1756. Uid: uid,
  1757. RewardType: int32(rewardType),
  1758. })
  1759. battleBaseData.ScoreMvp = append(battleBaseData.ScoreMvp, uid)
  1760. }
  1761. }
  1762. }
  1763. //打包 击杀Mvp
  1764. if bKillerReward == false {
  1765. killerStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(index)) + "-" + strconv.Itoa(int(Rank_Type_Killer))
  1766. retList2, err4 := service.GetRedis().ZRevRangeWithScores(killerStr, int64(0), int64(0)).Result()
  1767. if err4 == nil && len(retList2) > 0 {
  1768. firstUid, _ := model.Str2NumU64(retList2[0].Member.(string))
  1769. if uid == firstUid {
  1770. ackMsg.KillerMvp = append(ackMsg.KillerMvp, &serverproto.MvpRewardInfo{
  1771. Uid: uid,
  1772. RewardType: int32(rewardType),
  1773. })
  1774. battleBaseData.KillerMvp = append(battleBaseData.KillerMvp, uid)
  1775. }
  1776. }
  1777. }
  1778. //保存
  1779. if mvpRound == this.guildBattleId {
  1780. battle, ok := this.battleIdxDataList[int32(index)]
  1781. if !ok {
  1782. return
  1783. }
  1784. battle.saveBattleBaseData()
  1785. } else {
  1786. err, msgStr := model.GetEncodeMessage(battleBaseData)
  1787. if err != nil {
  1788. return
  1789. }
  1790. keyStr := GuildBattlePrefix + strconv.Itoa(int(mvpRound)) + "-" + strconv.Itoa(int(index))
  1791. service.GetRedis().HSet(keyStr, "base", msgStr)
  1792. }
  1793. }
  1794. // 玩家上线查看当前领奖//最多打包最近的5场
  1795. func (this *GuildBattleManager) OnlineGetRoleGuildBattleReward(uid uint64, curRewardId, mvpRewardId int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) {
  1796. if ackMsg == nil || this.guildBattleState == 0 {
  1797. return
  1798. }
  1799. var beginRound int32 = curRewardId + 1
  1800. //计算领奖轮次//判定当前轮次是否有奖励可领取。有则领取,无则从(当前轮次-1)开始领取
  1801. bRet := this.CheckCurRewardRound(uid)
  1802. endRound := this.guildBattleId
  1803. if !bRet {
  1804. endRound = this.guildBattleId - 1
  1805. }
  1806. ackMsg.CurBattleRound = endRound
  1807. //获取公会战奖励
  1808. if endRound <= 0 || endRound < beginRound {
  1809. //这里暂不处理,不需要打包数据
  1810. } else {
  1811. //curRewardId 已经领取轮次
  1812. for round := endRound; beginRound <= round && (endRound-round <= 5); round-- {
  1813. for index := 1; index <= MaxBattleTeams; index++ {
  1814. //遍历七场战斗数据//查看有无该玩家。有则给奖励//从上到下,只要找到一场有该玩家的数据,则给这一档次的奖励
  1815. bRet := this.PackPlayerGuildBattleReward(uid, round, int32(index), ackMsg)
  1816. //找到奖励,当前轮就不在查找
  1817. if bRet == serverproto.ErrorCode_ERROR_OK {
  1818. break
  1819. }
  1820. }
  1821. }
  1822. }
  1823. //设定领奖轮次
  1824. mvpFinishRound := this.guildBattleId
  1825. mvpBeginRound := mvpRewardId
  1826. ackMsg.CurMvpRound = mvpFinishRound
  1827. if mvpFinishRound < mvpBeginRound {
  1828. return
  1829. }
  1830. //公会战MVP奖励
  1831. for mvpRound := mvpFinishRound; mvpBeginRound <= mvpRound && (mvpFinishRound-mvpRound <= 5) && mvpRound > 0; mvpRound-- {
  1832. for index := 1; index <= MaxBattleTeams; index++ {
  1833. this.PackPlayerGuildBattleMvpReward(uid, mvpRound, int32(index), ackMsg)
  1834. }
  1835. }
  1836. }
  1837. // 获取战场当前的获胜状态
  1838. func (this *GuildBattleManager) GetBattleWinState(battleIdx int32) (uint64, uint64, bool) {
  1839. this.initGuildBattleList()
  1840. battleData := this.getBattleByIdx(battleIdx)
  1841. if battleData == nil {
  1842. return 0, 0, false
  1843. }
  1844. return battleData.GetBattleWinState()
  1845. }
  1846. type GuildBattle struct {
  1847. updateTimer util.ServerTimer //主循环定时器
  1848. mag *GuildBattleManager
  1849. guildBattleIdx int32 //1-7中的第几场战斗
  1850. battleBaseData *serverproto.GuildBattleBaseData
  1851. scoreUpdateMs uint64
  1852. //参战玩家信息
  1853. playerDataList map[uint64]*serverproto.GuildBattleSelfData
  1854. //战斗日志
  1855. logList []*serverproto.GuildBattleLog
  1856. //占位信息
  1857. posIdxDataList map[int32]*serverproto.GuildPosIdxData
  1858. posIdxDataUidList map[uint64]*serverproto.GuildPosIdxData
  1859. changePosIdxList set.Interface
  1860. changePlayerDataList set.Interface
  1861. baseChange bool //积分变化
  1862. bPosInit bool
  1863. }
  1864. func newGuildBattle(mag *GuildBattleManager, battleIdx int32, guildId1, guildId2 uint64, startTime, endTime uint64) *GuildBattle {
  1865. battle := &GuildBattle{
  1866. playerDataList: map[uint64]*serverproto.GuildBattleSelfData{},
  1867. posIdxDataList: map[int32]*serverproto.GuildPosIdxData{},
  1868. posIdxDataUidList: map[uint64]*serverproto.GuildPosIdxData{},
  1869. changePosIdxList: set.New(set.NonThreadSafe),
  1870. changePlayerDataList: set.New(set.NonThreadSafe),
  1871. battleBaseData: &serverproto.GuildBattleBaseData{},
  1872. guildBattleIdx: battleIdx,
  1873. mag: mag,
  1874. }
  1875. if startTime != 0 && endTime != 0 {
  1876. battle.battleBaseData.BattleStartTime = startTime
  1877. battle.battleBaseData.BattleEndTime = endTime
  1878. }
  1879. battle.updateTimer = util.NewDurationTimer(util.GetCurrentTime(), 1000)
  1880. util.InfoF("GuildBattle battleIndex:%v guildId1=%v guildId2=%v startTime:%v, endTime:%v", battleIdx, guildId1, guildId2, startTime, endTime)
  1881. battle.init(guildId1, guildId2, startTime, endTime)
  1882. return battle
  1883. }
  1884. // 加载战斗数据(如果存在)
  1885. func (this *GuildBattle) init(guildId1, guildId2 uint64, startTime, endTime uint64) {
  1886. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  1887. retStr, err := service.GetRedis().HGet(battleKeyStr, "base").Result()
  1888. if err == nil {
  1889. err2 := model.GetDecodeMessage(this.battleBaseData, retStr)
  1890. if err2 != nil {
  1891. util.ErrorF("GuildBattle id=%v idx=%v base err", this.mag.guildBattleId, this.guildBattleIdx)
  1892. }
  1893. }
  1894. if len(this.battleBaseData.ScoreList) <= 0 {
  1895. this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
  1896. &serverproto.KeyValueType64{Key: guildId1}, &serverproto.KeyValueType64{Key: guildId2})
  1897. //初始化公会数据
  1898. for _, data := range this.battleBaseData.ScoreList {
  1899. guild := GuildMag.GetGuild(data.Key)
  1900. if guild != nil {
  1901. data.Value3 = guild.GuildBase.GuildBrief.GuildBadge
  1902. data.StrVal = guild.GuildBase.GuildBrief.GuildName
  1903. }
  1904. }
  1905. this.initGuildPlayerData(guildId1, guildId2)
  1906. this.saveBattleBaseData()
  1907. util.ErrorF("GuildBattle id=%v idx=%v init battle data ", this.mag.guildBattleId, this.guildBattleIdx)
  1908. } else {
  1909. util.InfoF("GuildBattle battleIndex 2 :%v guildId1=%v guildId2=%v startTime:%v, endTime:%v", this.guildBattleIdx, guildId1, guildId2, this.battleBaseData.BattleStartTime, this.battleBaseData.BattleEndTime)
  1910. }
  1911. this.initPosListInfo()
  1912. }
  1913. func (this *GuildBattle) saveBattleBaseData() {
  1914. err, msgStr := model.GetEncodeMessage(this.battleBaseData)
  1915. if err != nil {
  1916. return
  1917. }
  1918. keyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  1919. service.GetRedis().HSet(keyStr, "base", msgStr)
  1920. }
  1921. func (this *GuildBattle) saveBattlePlayerData() {
  1922. for _, val := range this.changePlayerDataList.List() {
  1923. uid := val.(uint64)
  1924. if playerData, ok := this.playerDataList[uid]; ok {
  1925. setGuildBattlePlayerDataToRedis(this.mag.guildBattleId, this.guildBattleIdx, playerData)
  1926. }
  1927. }
  1928. this.changePlayerDataList.Clear()
  1929. }
  1930. func (this *GuildBattle) update(ms uint64) {
  1931. if this.mag == nil || this.mag.banType == Guild_Battle_Baned {
  1932. return
  1933. }
  1934. if !this.updateTimer.IsStart() || !this.updateTimer.IsExpired(ms) {
  1935. return
  1936. }
  1937. //不在战斗中
  1938. if ms < this.battleBaseData.BattleStartTime || ms > this.battleBaseData.BattleEndTime {
  1939. return
  1940. }
  1941. //积分处理
  1942. this.updateScore(ms)
  1943. if this.baseChange {
  1944. this.baseChange = false
  1945. this.saveBattleBaseData()
  1946. }
  1947. this.saveBattlePlayerData()
  1948. for _, val := range this.changePosIdxList.List() {
  1949. posIdx := val.(int32)
  1950. if posIdxData, ok := this.posIdxDataList[posIdx]; ok {
  1951. setGuildBattlePosIdxDataToRedis(this.mag.guildBattleId, this.guildBattleIdx, posIdxData)
  1952. }
  1953. }
  1954. this.changePosIdxList.Clear()
  1955. }
  1956. func (this *GuildBattle) updateScore(ms uint64) {
  1957. if this.scoreUpdateMs < 0 {
  1958. this.scoreUpdateMs = ms
  1959. return
  1960. }
  1961. //5s
  1962. if this.scoreUpdateMs+model.GlobalGuildBattleRewardTime > ms {
  1963. return
  1964. }
  1965. this.scoreUpdateMs = ms
  1966. var scoreMap = map[uint64]int32{}
  1967. var playerScoreMap = map[uint64]uint32{}
  1968. for _, v := range this.posIdxDataList {
  1969. if warPoint, ok := model.ConvertGuildBattleList[v.PosIdx]; ok {
  1970. if v.GuildId > 0 {
  1971. scoreMap[v.GuildId] += warPoint
  1972. }
  1973. playerScoreMap[v.Uid] += uint32(warPoint)
  1974. }
  1975. }
  1976. for k, v := range scoreMap {
  1977. var scoreVal int32 = 0
  1978. bFind := false
  1979. for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
  1980. if this.battleBaseData.ScoreList[idx].Key == k {
  1981. this.battleBaseData.ScoreList[idx].Value += v
  1982. bFind = true
  1983. scoreVal = this.battleBaseData.ScoreList[idx].Value
  1984. break
  1985. }
  1986. }
  1987. if !bFind {
  1988. this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
  1989. &serverproto.KeyValueType64{Key: k, Value: v})
  1990. scoreVal = v
  1991. }
  1992. this.baseChange = true
  1993. //先到达胜利最大积分的为胜利队伍
  1994. if scoreVal >= model.GlobalGuildBattleWinMaxScore {
  1995. this.battleBaseData.BattleEndTime = ms
  1996. util.InfoF("GuildBattle updateScore id=%v idx=%v guildid=%v score=%v",
  1997. this.mag.guildBattleId, this.guildBattleIdx, k, scoreVal)
  1998. break
  1999. }
  2000. }
  2001. keyStr := GuildBattleRankPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" +
  2002. strconv.Itoa(int(this.guildBattleIdx)) + "-" + strconv.Itoa(int(RankScoreListType))
  2003. for k, v := range playerScoreMap {
  2004. if k == 0 || v == 0 {
  2005. continue
  2006. }
  2007. uidStr := strconv.FormatUint(k, 10)
  2008. oldScore, err := service.GetRedis().ZScore(keyStr, uidStr).Result()
  2009. if err == nil { //找到则变更数据
  2010. tmpScore := getValByGuildBattleScore(oldScore)
  2011. tmpScore += v
  2012. scoreStr := getguildBattleScore(uint64(tmpScore), ms)
  2013. util.InfoF("GuildBattle updateScore uid=%v addscore=%v score=%v", k, v, tmpScore)
  2014. service.GetRedis().ZAdd(keyStr, service.BaseZ{Score: float64(scoreStr), Member: uidStr})
  2015. } else if err == service.NIL { //未找到此人,则新插入一条记录
  2016. scoreStr := getguildBattleScore(uint64(v), ms)
  2017. service.GetRedis().ZAdd(keyStr, service.BaseZ{Score: float64(scoreStr), Member: uidStr})
  2018. }
  2019. }
  2020. }
  2021. func (this *GuildBattle) initGuildPlayerData(guildId1, guildId2 uint64) {
  2022. guild1 := GuildMag.GetGuild(guildId1)
  2023. if guild1 != nil {
  2024. for _, member := range guild1.GuildMember.MemberInfo {
  2025. playerData := &serverproto.GuildBattleSelfData{
  2026. Uid: member.MemberId,
  2027. GuildId: guildId1,
  2028. ChallengeNum: model.GlobalGuildBattleChallengeNum,
  2029. }
  2030. if member.Title == Guild_Title_Pre {
  2031. playerData.GuildPre = true
  2032. }
  2033. this.playerDataList[member.MemberId] = playerData
  2034. this.changePlayerDataList.Add(member.MemberId)
  2035. }
  2036. }
  2037. guild2 := GuildMag.GetGuild(guildId2)
  2038. if guild2 != nil {
  2039. for _, member := range guild2.GuildMember.MemberInfo {
  2040. playerData := &serverproto.GuildBattleSelfData{
  2041. Uid: member.MemberId,
  2042. GuildId: guildId2,
  2043. ChallengeNum: model.GlobalGuildBattleChallengeNum,
  2044. }
  2045. if member.Title == Guild_Title_Pre {
  2046. playerData.GuildPre = true
  2047. }
  2048. this.playerDataList[member.MemberId] = playerData
  2049. this.changePlayerDataList.Add(member.MemberId)
  2050. }
  2051. }
  2052. this.saveBattlePlayerData()
  2053. }
  2054. func (this *GuildBattle) getPlayerData(uid, guildId uint64) *serverproto.GuildBattleSelfData {
  2055. playerData, ok := this.playerDataList[uid]
  2056. if !ok {
  2057. //从数据库中加载
  2058. keyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2059. fieldStr := "uid:" + strconv.FormatUint(uid, 10)
  2060. retData, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
  2061. if err == nil {
  2062. playerData = &serverproto.GuildBattleSelfData{}
  2063. err2 := model.GetDecodeMessage(playerData, retData)
  2064. if err2 == nil {
  2065. this.playerDataList[uid] = playerData
  2066. util.ErrorF("GuildBattle id=%v idx=%v base err", this.mag.guildBattleId, this.guildBattleIdx)
  2067. } else {
  2068. playerData = nil
  2069. }
  2070. }
  2071. /*
  2072. if playerData == nil {
  2073. playerData = &serverproto.GuildBattleSelfData{
  2074. Uid: uid,
  2075. GuildId: guildId,
  2076. ChallengeNum: model.GlobalGuildBattleChallengeNum,
  2077. }
  2078. }
  2079. */
  2080. }
  2081. return playerData
  2082. }
  2083. func (this *GuildBattle) checkBattleFinish() bool {
  2084. nowTime := util.GetTimeMilliseconds()
  2085. //是否固定时间结束
  2086. if Guild_Battle_Tournament_Begin_Match <= this.guildBattleIdx && this.guildBattleIdx <= Guild_Battle_Tournament_End_Match {
  2087. if this.mag.tournamentEnd < nowTime {
  2088. return true
  2089. }
  2090. } else if Guild_Battle_Seminal_Begin_Match == this.guildBattleIdx || this.guildBattleIdx == Guild_Battle_Seminal_End_Match {
  2091. if this.mag.semifinalsEnd < nowTime {
  2092. return true
  2093. }
  2094. } else if Guild_Battle_Final_Match == this.guildBattleIdx {
  2095. if this.mag.finalEnd < nowTime {
  2096. return true
  2097. }
  2098. }
  2099. //是否固定时间结束
  2100. if this.battleBaseData.BattleEndTime < nowTime && this.battleBaseData.BattleEndTime > 0 {
  2101. return true
  2102. }
  2103. return false
  2104. }
  2105. func (this *GuildBattle) BattleInfoReq(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleInfoAck) {
  2106. //参赛者和观战者区分
  2107. bWatch := true
  2108. for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
  2109. if this.battleBaseData.ScoreList[idx].Key == guildId {
  2110. bWatch = false
  2111. break
  2112. }
  2113. }
  2114. if !bWatch {
  2115. playerData := this.getPlayerData(uid, guildId)
  2116. if playerData == nil {
  2117. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2118. return
  2119. }
  2120. ackMsg.SelfData = playerData
  2121. }
  2122. ackMsg.GuildBattleIdx = this.guildBattleIdx
  2123. ackMsg.BattleStartTime = this.battleBaseData.BattleStartTime
  2124. ackMsg.BattleEndTime = this.battleBaseData.BattleEndTime
  2125. posIdxData := this.getPosInfoByUid(uid)
  2126. if posIdxData != nil {
  2127. ackMsg.ChallengePosData = posIdxData
  2128. }
  2129. for _, countPart := range this.battleBaseData.ScoreList {
  2130. keyValue := &serverproto.KeyValueType64{
  2131. Key: countPart.Key,
  2132. Value: countPart.Value,
  2133. Value2: countPart.Value2,
  2134. Value3: countPart.Value3,
  2135. StrVal: countPart.StrVal,
  2136. }
  2137. ackMsg.BattleDataList = append(ackMsg.BattleDataList, keyValue)
  2138. }
  2139. }
  2140. func (this *GuildBattle) BattleSetting(uid, guildId uint64, autoChallenge, autoReborn bool, ackMsg *serverproto.SCGuildBattleSettingAck) {
  2141. playerData := this.getPlayerData(uid, guildId)
  2142. if playerData == nil {
  2143. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2144. return
  2145. }
  2146. playerData.AutoChallenge = autoChallenge
  2147. playerData.AutoReborn = autoReborn
  2148. this.changePlayerDataList.Add(uid)
  2149. ackMsg.SelfData = playerData
  2150. }
  2151. func (this *GuildBattle) BuyChallengeNum(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleBuyChallengeAck) {
  2152. playerData := this.getPlayerData(uid, guildId)
  2153. if playerData == nil {
  2154. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2155. return
  2156. }
  2157. if this.checkBattleFinish() {
  2158. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2159. return
  2160. }
  2161. this.changePlayerDataList.Add(uid)
  2162. ackMsg.SelfData = playerData
  2163. }
  2164. func (this *GuildBattle) BuyBattleBuff(uid, guildId uint64, buffList []int32, ackMsg *serverproto.SSGuildBattleBuyBuffAck) {
  2165. playerData := this.getPlayerData(uid, guildId)
  2166. if playerData == nil {
  2167. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2168. return
  2169. }
  2170. if this.checkBattleFinish() {
  2171. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2172. return
  2173. }
  2174. for _, data := range buffList {
  2175. bFind := false
  2176. for index, playerBuff := range playerData.BuffList {
  2177. if playerBuff.Key == data {
  2178. bFind = true
  2179. level := playerBuff.Value + 1
  2180. //判定表中是否存在level这一级的buff
  2181. buffData := model.GuildBattleBuffList[data]
  2182. if buffData == nil {
  2183. continue
  2184. }
  2185. //存在level级buff,判定金币够不够,不够直接回退
  2186. buyInfo := buffData.BuyData[level]
  2187. if buyInfo == nil {
  2188. continue
  2189. }
  2190. ackMsg.NeedRes = append(ackMsg.NeedRes, &serverproto.KeyValueType{
  2191. Key: buyInfo.Key,
  2192. Value: buyInfo.Value,
  2193. })
  2194. //设置
  2195. playerData.BuffList[index].Value = level
  2196. break
  2197. }
  2198. }
  2199. //没找到对应的BUFF,则新购买
  2200. if bFind == false {
  2201. buffData := model.GuildBattleBuffList[data]
  2202. if buffData == nil {
  2203. continue
  2204. }
  2205. //存在level级buff,判定金币够不够,不够直接回退
  2206. buyInfo := buffData.BuyData[1]
  2207. if buyInfo == nil {
  2208. continue
  2209. }
  2210. //购买一级的
  2211. playerData.BuffList = append(playerData.BuffList, &serverproto.KeyValueType{
  2212. Key: data,
  2213. Value: 1,
  2214. })
  2215. ackMsg.NeedRes = append(ackMsg.NeedRes, &serverproto.KeyValueType{
  2216. Key: buyInfo.Key,
  2217. Value: buyInfo.Value,
  2218. })
  2219. }
  2220. }
  2221. ackMsg.SelfData = playerData
  2222. this.changePlayerDataList.Add(uid)
  2223. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_OK)
  2224. }
  2225. func (this *GuildBattle) ResetRebornCD(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleRebornAck) {
  2226. playerData := this.getPlayerData(uid, guildId)
  2227. if playerData == nil {
  2228. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2229. return
  2230. }
  2231. nowTime := util.GetTimeMilliseconds()
  2232. if playerData.RebornEndTime <= nowTime {
  2233. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_HAS_REBORN)
  2234. return
  2235. }
  2236. playerData.RebornEndTime = 0
  2237. this.changePlayerDataList.Add(uid)
  2238. ackMsg.SelfData = playerData
  2239. }
  2240. func (this *GuildBattle) initPosListInfo() {
  2241. if this.bPosInit {
  2242. return
  2243. }
  2244. this.bPosInit = true
  2245. var guildPosIdxNum = map[uint64]int32{}
  2246. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2247. for idx := 1; idx <= PosIdxMaxNum; idx++ {
  2248. fieldStr := "idx:" + strconv.Itoa(idx)
  2249. retData, err := service.GetRedis().HGet(battleKeyStr, fieldStr).Result()
  2250. if err != nil {
  2251. continue
  2252. }
  2253. posIdxData := &serverproto.GuildPosIdxData{}
  2254. err = model.GetDecodeMessage(posIdxData, retData)
  2255. if err != nil {
  2256. continue
  2257. }
  2258. this.posIdxDataList[int32(posIdxData.PosIdx)] = posIdxData
  2259. if posIdxData.OwnerBrief != nil {
  2260. this.posIdxDataUidList[posIdxData.OwnerBrief.Uid] = posIdxData
  2261. }
  2262. if posIdxData.GuildId > 0 {
  2263. guildPosIdxNum[posIdxData.GuildId]++
  2264. }
  2265. }
  2266. for k, v := range guildPosIdxNum {
  2267. bFind := false
  2268. for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
  2269. if this.battleBaseData.ScoreList[idx].Key == k {
  2270. //value=积分数量
  2271. //valu2=道场数量
  2272. this.battleBaseData.ScoreList[idx].Value2 = v
  2273. this.baseChange = true
  2274. bFind = true
  2275. break
  2276. }
  2277. }
  2278. if !bFind {
  2279. this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
  2280. &serverproto.KeyValueType64{Key: k, Value2: v})
  2281. this.baseChange = true
  2282. }
  2283. }
  2284. }
  2285. func (this *GuildBattle) getPosInfo(posIdx int32) *serverproto.GuildPosIdxData {
  2286. this.initPosListInfo()
  2287. posIdxData, ok := this.posIdxDataList[posIdx]
  2288. if ok {
  2289. return posIdxData
  2290. } else {
  2291. //无公会占领地块
  2292. posIdxData := &serverproto.GuildPosIdxData{
  2293. PosIdx: posIdx,
  2294. }
  2295. this.posIdxDataList[posIdx] = posIdxData
  2296. return posIdxData
  2297. }
  2298. }
  2299. func (this *GuildBattle) getPosInfoByUid(uid uint64) *serverproto.GuildPosIdxData {
  2300. this.initPosListInfo()
  2301. posIdxData, ok := this.posIdxDataUidList[uid]
  2302. if ok {
  2303. return posIdxData
  2304. }
  2305. return nil
  2306. }
  2307. func (this *GuildBattle) GetBuffTotalLevel(playerData *serverproto.GuildBattleSelfData) int32 {
  2308. if playerData == nil || len(playerData.BuffList) <= 0 {
  2309. return 0
  2310. }
  2311. totalLevel := int32(0)
  2312. for _, data := range playerData.BuffList {
  2313. totalLevel += data.Value
  2314. }
  2315. return totalLevel
  2316. }
  2317. func (this *GuildBattle) GetBattlePosIdxList(posIdxList []int32, ackMsg *serverproto.SCGuildBattlePosIdxListAck) {
  2318. if this.checkBattleFinish() {
  2319. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2320. return
  2321. }
  2322. for idx := 0; idx < len(posIdxList); idx++ {
  2323. if posIdxList[idx] <= 0 || posIdxList[idx] > int32(len(model.ConvertGuildBattleList)) {
  2324. continue
  2325. }
  2326. tmpPosIdxData := serverproto.GuildPosIdxData{}
  2327. posIdxData := this.getPosInfo(posIdxList[idx])
  2328. if posIdxData == nil {
  2329. continue
  2330. }
  2331. tmpPosIdxData = *posIdxData
  2332. //玩家不在该位置但是也也能属于该玩家
  2333. if posIdxData.OwnerBrief != nil {
  2334. posIdxData.OwnerBrief = model2.GetPlayerBriefInfoWithoutCache(posIdxData.OwnerBrief.Uid)
  2335. }
  2336. if posIdxData.OwnerBrief != nil {
  2337. var brief serverproto.CommonPlayerBriefInfo
  2338. brief = *posIdxData.OwnerBrief
  2339. playerData := this.getPlayerData(posIdxData.OwnerBrief.Uid, posIdxData.GuildId)
  2340. if playerData != nil {
  2341. totalLevel := this.GetBuffTotalLevel(playerData)
  2342. if totalLevel > 0 {
  2343. brief.FightPower += brief.FightPower * totalLevel * model.GlobalGuildBattleFightPower / 100
  2344. }
  2345. }
  2346. tmpPosIdxData.OwnerBrief = &brief
  2347. }
  2348. ackMsg.PosDataList = append(ackMsg.PosDataList, &tmpPosIdxData)
  2349. }
  2350. }
  2351. func (this *GuildBattle) GetPosIdxLog(beginTime uint64, ackMsg *serverproto.SCGuildBattleLogAck) {
  2352. /*
  2353. if this.checkBattleFinish() {
  2354. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2355. return
  2356. }
  2357. */
  2358. //加载日志
  2359. if len(this.logList) <= 0 {
  2360. keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2361. retList, err := service.GetRedis().LRange(keyStr, 0, -1).Result()
  2362. if err != nil {
  2363. return
  2364. }
  2365. for idx := 0; idx < len(retList); idx++ {
  2366. logData := &serverproto.GuildBattleLog{}
  2367. err = model.GetDecodeMessage(logData, retList[idx])
  2368. if err != nil {
  2369. continue
  2370. }
  2371. this.logList = append(this.logList, logData)
  2372. }
  2373. sort.Slice(this.logList, func(i, j int) bool {
  2374. return this.logList[i].RecordTime < this.logList[j].RecordTime
  2375. })
  2376. }
  2377. //获取最新列表
  2378. if beginTime <= 0 {
  2379. starIdx := len(this.logList)
  2380. if starIdx > 0 {
  2381. for idx := starIdx - 1; idx >= 0; idx-- {
  2382. ackMsg.LogList = append(ackMsg.LogList, this.logList[idx])
  2383. if len(ackMsg.LogList) >= 10 {
  2384. break
  2385. }
  2386. }
  2387. }
  2388. } else {
  2389. for idx := len(this.logList) - 1; idx >= 0; idx-- {
  2390. if this.logList[idx].RecordTime < beginTime {
  2391. ackMsg.LogList = append(ackMsg.LogList, this.logList[idx])
  2392. if len(ackMsg.LogList) >= 10 {
  2393. break
  2394. }
  2395. }
  2396. }
  2397. }
  2398. if len(ackMsg.LogList) < 10 {
  2399. ackMsg.IsEnd = true
  2400. }
  2401. }
  2402. func (this *GuildBattle) outPosIdx(bFight bool, uid uint64, nowTime uint64) bool {
  2403. posIdxData := this.getPosInfoByUid(uid)
  2404. if posIdxData == nil {
  2405. return false
  2406. }
  2407. if posIdxData.OwnerBrief != nil && posIdxData.OwnerBrief.Uid == uid {
  2408. if bFight {
  2409. posIdxData.GuildId = 0
  2410. posIdxData.Uid = 0
  2411. //因为被打败离开,才要清,如果是主动离开。很可能这块地已经存在战斗了。不能清战斗数据
  2412. posIdxData.FightEndTime = 0
  2413. posIdxData.FightStartTime = 0
  2414. posIdxData.ChallengeUid = 0
  2415. posIdxData.FightPingTime = 0
  2416. }
  2417. posIdxData.OwnerBrief = nil
  2418. this.changePosIdxList.Add(posIdxData.PosIdx)
  2419. delete(this.posIdxDataUidList, uid)
  2420. return true
  2421. }
  2422. return false
  2423. }
  2424. func (this *GuildBattle) inPosIdx(posIdxData *serverproto.GuildPosIdxData, uid, selfGuildId uint64, nowTime uint64) bool {
  2425. posIdxData.OwnerBrief = &serverproto.CommonPlayerBriefInfo{Uid: uid} //需要重新获取
  2426. posIdxData.FightEndTime = 0
  2427. posIdxData.FightStartTime = 0
  2428. posIdxData.ChallengeUid = 0
  2429. posIdxData.FightPingTime = 0
  2430. posIdxData.GuildId = selfGuildId
  2431. posIdxData.Uid = uid //当前地块标记为该玩家
  2432. this.posIdxDataUidList[uid] = posIdxData
  2433. this.changePosIdxList.Add(posIdxData.PosIdx)
  2434. return true
  2435. }
  2436. func (this *GuildBattle) BattleChallengePre(posIdx int32, selfGuildId uint64, bfInfo *serverproto.CommonPlayerBriefInfo,
  2437. curRmb uint64, buyNum int32, ssAckMsg *serverproto.SSGuildBattleChallengeAck) {
  2438. ssAckMsg.PosIdx = posIdx
  2439. ssAckMsg.GuildBattleIdx = this.guildBattleIdx
  2440. if this.checkBattleFinish() {
  2441. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2442. return
  2443. }
  2444. posIdxData := this.getPosInfo(posIdx)
  2445. if posIdxData == nil {
  2446. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2447. return
  2448. }
  2449. nowTime := util.GetTimeMilliseconds()
  2450. playerData := this.getPlayerData(bfInfo.Uid, selfGuildId)
  2451. if playerData == nil {
  2452. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_DAOCHANG100_NO_CHALLENGE_COUNT)
  2453. return
  2454. }
  2455. var costRmb uint64 = 0
  2456. //判断是否处于虚弱状态
  2457. if playerData.RebornEndTime > 0 && playerData.RebornEndTime > nowTime {
  2458. if playerData.AutoReborn {
  2459. if curRmb >= costRmb+model.GlobalGuildBattleRebornCost {
  2460. costRmb += model.GlobalGuildBattleRebornCost
  2461. playerData.RebornEndTime = 0
  2462. this.changePlayerDataList.Add(bfInfo.Uid)
  2463. ssAckMsg.CostRmb = costRmb
  2464. ssAckMsg.SelfData = playerData
  2465. } else {
  2466. playerData.AutoReborn = false
  2467. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_CHALLENGE_NO_RMB)
  2468. return
  2469. }
  2470. } else {
  2471. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_REBORN_TIME)
  2472. return
  2473. }
  2474. }
  2475. //当前正在战斗
  2476. if posIdxData.FightEndTime > 0 && posIdxData.FightEndTime > nowTime {
  2477. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FIGHTING)
  2478. ssAckMsg.ChallengePosData = posIdxData
  2479. return
  2480. }
  2481. //该玩家是否已经占领该位置
  2482. if posIdxData.OwnerBrief != nil && posIdxData.OwnerBrief.Uid == bfInfo.Uid {
  2483. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_HAS)
  2484. ssAckMsg.ChallengePosData = posIdxData
  2485. return
  2486. }
  2487. //同公会无法进行挑战
  2488. if posIdxData.OwnerBrief != nil && posIdxData.GuildId == selfGuildId {
  2489. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_SAME_GUILD)
  2490. ssAckMsg.ChallengePosData = posIdxData
  2491. return
  2492. }
  2493. //直接占领
  2494. if posIdxData.GuildId == 0 || posIdxData.OwnerBrief == nil {
  2495. //处理地块数量
  2496. //占领着地块自加
  2497. guildId := GuildMag.GetPlayerGuildId(bfInfo.Uid)
  2498. if posIdxData.GuildId == 0 || posIdxData.GuildId != 0 && posIdxData.GuildId != guildId {
  2499. //抵款自家
  2500. for idx, data := range this.battleBaseData.ScoreList {
  2501. if data.Key == guildId {
  2502. this.battleBaseData.ScoreList[idx].Value2++
  2503. }
  2504. }
  2505. }
  2506. //原有地块持有者地块自减
  2507. if posIdxData.GuildId != 0 && posIdxData.GuildId != guildId {
  2508. for idx, data := range this.battleBaseData.ScoreList {
  2509. if data.Key == posIdxData.GuildId {
  2510. this.battleBaseData.ScoreList[idx].Value2--
  2511. }
  2512. }
  2513. }
  2514. //离开玩家当前战力的地块(不在位置但还是属于该玩家所在公会)
  2515. this.outPosIdx(false, bfInfo.Uid, nowTime)
  2516. this.inPosIdx(posIdxData, bfInfo.Uid, selfGuildId, nowTime)
  2517. ssAckMsg.SelfData = playerData
  2518. ssAckMsg.ChallengePosData = posIdxData
  2519. } else {
  2520. //挑战次数判断//优先用购买次数
  2521. if buyNum > 0 {
  2522. ssAckMsg.UseBuyNum = true
  2523. util.InfoF("BattleChallengePre fight use buy num ")
  2524. } else if playerData.ChallengeNum > 0 {
  2525. playerData.ChallengeNum--
  2526. this.changePlayerDataList.Add(bfInfo.Uid)
  2527. util.InfoF("BattleChallengePre fight use init num ")
  2528. } else {
  2529. if playerData.AutoChallenge {
  2530. if curRmb < costRmb+model.GlobalGuildBattleBuyChallengeCost {
  2531. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_CHALLENGE_NO_RMB)
  2532. //todo wangzhaocan 挑战次数不够,并且金币也不够,取消自动购买
  2533. playerData.AutoChallenge = false
  2534. this.changePlayerDataList.Add(bfInfo.Uid)
  2535. return
  2536. }
  2537. //挑战消耗的金币(自动消耗)
  2538. costRmb += model.GlobalGuildBattleBuyChallengeCost
  2539. util.InfoF("BattleChallengePre fight use gold ")
  2540. } else {
  2541. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_DAOCHANG100_NO_CHALLENGE_COUNT)
  2542. return
  2543. }
  2544. }
  2545. util.InfoF("BattleChallengePre fight use buy num cost %v", costRmb)
  2546. ssAckMsg.SelfData = playerData
  2547. ssAckMsg.CostRmb = costRmb
  2548. //挑战玩家
  2549. if posIdxData.OwnerBrief != nil {
  2550. fightInfo := model2.GetRoleGuildBattleFightInfoWithoutCache(posIdxData.OwnerBrief.Uid)
  2551. if fightInfo != nil {
  2552. posIdxData.FightEndTime = nowTime + PosIdxFightMaxTime
  2553. posIdxData.FightStartTime = nowTime
  2554. posIdxData.ChallengeUid = bfInfo.Uid
  2555. posIdxData.FightPingTime = nowTime
  2556. this.changePosIdxList.Add(posIdxData.PosIdx)
  2557. ssAckMsg.FightInfo = fightInfo
  2558. //bufflist
  2559. fightPlayerData := this.getPlayerData(posIdxData.OwnerBrief.Uid, posIdxData.GuildId)
  2560. if fightPlayerData != nil {
  2561. ssAckMsg.FightBuffList = fightPlayerData.BuffList
  2562. }
  2563. ssAckMsg.SelfBuffList = playerData.BuffList
  2564. return
  2565. }
  2566. }
  2567. posIdxData.OwnerBrief = nil
  2568. posIdxData.FightEndTime = 0
  2569. posIdxData.FightStartTime = 0
  2570. posIdxData.GuildId = 0
  2571. posIdxData.ChallengeUid = 0
  2572. posIdxData.FightPingTime = 0
  2573. }
  2574. }
  2575. func (this *GuildBattle) BattleChallengePing(challengeUid uint64, posIdx int32, ssAckMsg *serverproto.SCGuildBattleChallengePingAck) {
  2576. if this.checkBattleFinish() {
  2577. return
  2578. }
  2579. posIdxData := this.getPosInfo(posIdx)
  2580. if posIdxData == nil {
  2581. return
  2582. }
  2583. //当前玩家没有挑战该位置
  2584. if posIdxData.ChallengeUid != challengeUid {
  2585. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
  2586. return
  2587. }
  2588. nowTime := util.GetTimeMilliseconds()
  2589. if posIdxData.FightPingTime > 0 && posIdxData.FightPingTime+60*1000 < nowTime {
  2590. //战斗无效
  2591. posIdxData.ChallengeUid = 0
  2592. posIdxData.FightPingTime = 0
  2593. posIdxData.FightStartTime = 0
  2594. posIdxData.FightEndTime = 0
  2595. this.changePosIdxList.Add(posIdxData.PosIdx)
  2596. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
  2597. return
  2598. }
  2599. posIdxData.FightPingTime = nowTime
  2600. posIdxData.FightEndTime = nowTime + PosIdxFightMaxTime
  2601. }
  2602. func (this *GuildBattle) battleResultValidCheck(selfUid uint64, selfFightPower int32, selfGuildId uint64, beChallengeFighter *serverproto.GuildPosIdxData) bool {
  2603. if beChallengeFighter.Uid <= 0 {
  2604. return true
  2605. }
  2606. if len(model.GlobalDaoChang100BattleCheck) >= 3 {
  2607. playerData := this.getPlayerData(selfUid, selfGuildId)
  2608. beChallengePlayerData := this.getPlayerData(beChallengeFighter.Uid, beChallengeFighter.GuildId)
  2609. if playerData == nil || beChallengePlayerData == nil {
  2610. //找不到对应的玩家数据,这个应该不让过了。
  2611. return false
  2612. }
  2613. var challengePower int32 = 0
  2614. //拉取一下最新的战力算了。。
  2615. tmpBf := model2.GetPlayerBriefInfoWithoutCache(beChallengeFighter.Uid)
  2616. if tmpBf != nil {
  2617. challengePower = tmpBf.FightPower
  2618. }
  2619. //进攻方的战力
  2620. playerTotalLevel := this.GetBuffTotalLevel(playerData)
  2621. if playerTotalLevel > 0 {
  2622. selfFightPower += selfFightPower * playerTotalLevel * model.GlobalGuildBattleFightPower / 100
  2623. }
  2624. //防守方的战力
  2625. beChallengeTotalLevel := this.GetBuffTotalLevel(beChallengePlayerData)
  2626. if beChallengeTotalLevel > 0 {
  2627. challengePower += challengePower * beChallengeTotalLevel * model.GlobalGuildBattleFightPower / 100
  2628. }
  2629. p1 := (float64(challengePower) - model.GlobalDaoChang100BattleCheck[0]) * model.GlobalDaoChang100BattleCheck[1]
  2630. p2 := float64(challengePower) * model.GlobalDaoChang100BattleCheck[2]
  2631. p := int32(math.Max(p1, p2) + 0.5)
  2632. util.InfoF("uid=%v battleResultValidCheckDaoChang100 selfFightPower=%v bechallengePower=%v", selfUid, selfFightPower, challengePower)
  2633. if selfFightPower <= p {
  2634. util.ErrorF("uid=%v battleResultValidCheckDaoChang100 selfFightPower=%v bechallengePower=%v invalid", selfUid, selfFightPower, challengePower)
  2635. return false
  2636. }
  2637. }
  2638. return true
  2639. }
  2640. func (this *GuildBattle) BattleChallengeResult(posIdx int32, challengeGuildId uint64, bfInfo *serverproto.CommonPlayerBriefInfo,
  2641. battleResult bool, ssAckMsg *serverproto.SCGuildBattleChallengeResultAck, ev rocommon.ProcEvent) {
  2642. if this.checkBattleFinish() {
  2643. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2644. return
  2645. }
  2646. posIdxData := this.getPosInfo(posIdx)
  2647. if posIdxData == nil {
  2648. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2649. return
  2650. }
  2651. //当前位置的挑战者不是该玩家
  2652. if posIdxData.ChallengeUid != bfInfo.Uid {
  2653. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
  2654. return
  2655. }
  2656. targetPosIdxDataBfInfo := &serverproto.CommonPlayerBriefInfo{}
  2657. if posIdxData.OwnerBrief != nil {
  2658. err := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.OwnerBrief.Uid, targetPosIdxDataBfInfo)
  2659. if err == nil {
  2660. posIdxData.OwnerBrief = targetPosIdxDataBfInfo
  2661. }
  2662. }
  2663. //战力校验
  2664. if posIdxData.Uid > 0 && battleResult {
  2665. if !this.battleResultValidCheck(bfInfo.Uid, bfInfo.FightPower, challengeGuildId, posIdxData) {
  2666. battleResult = false
  2667. }
  2668. }
  2669. nowTime := util.GetTimeMilliseconds()
  2670. posIdxData.FightEndTime = 0
  2671. posIdxData.FightStartTime = 0
  2672. posIdxData.FightPingTime = 0
  2673. //挑战失败处理
  2674. if !battleResult {
  2675. if posIdxData.OwnerBrief != nil {
  2676. //添加占领日志
  2677. tmpSt := posIdxLogSt{
  2678. logType: 2,
  2679. posIdx: posIdx,
  2680. challengeGuild: challengeGuildId,
  2681. challengeNickName: bfInfo.NickName,
  2682. challengeUid: bfInfo.Uid,
  2683. posIdxUid: posIdxData.OwnerBrief.Uid,
  2684. posIdxGuild: posIdxData.GuildId,
  2685. }
  2686. if posIdxData.OwnerBrief.NickName != "" {
  2687. tmpSt.posIdxNickName = posIdxData.OwnerBrief.NickName
  2688. }
  2689. this.addPosIdxLog(tmpSt)
  2690. } else if posIdxData.Uid != 0 {
  2691. //添加占领日志
  2692. ownerBrief := &serverproto.CommonPlayerBriefInfo{}
  2693. err2 := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.Uid, ownerBrief)
  2694. if err2 == nil {
  2695. tmpSt := posIdxLogSt{
  2696. logType: 2,
  2697. posIdx: posIdx,
  2698. challengeGuild: challengeGuildId,
  2699. challengeNickName: bfInfo.NickName,
  2700. challengeUid: bfInfo.Uid,
  2701. posIdxUid: posIdxData.Uid,
  2702. posIdxGuild: posIdxData.GuildId,
  2703. }
  2704. if ownerBrief.NickName != "" {
  2705. tmpSt.posIdxNickName = ownerBrief.NickName
  2706. }
  2707. this.addPosIdxLog(tmpSt)
  2708. }
  2709. }
  2710. //进攻失败处理//进攻玩家清除连胜,重置复活时间
  2711. playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
  2712. if playerData != nil {
  2713. playerData.WinStrak = 0
  2714. playerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
  2715. this.changePlayerDataList.Add(bfInfo.Uid)
  2716. }
  2717. return
  2718. }
  2719. if posIdxData.OwnerBrief != nil {
  2720. //原有地块持有者地块自减
  2721. for idx, data := range this.battleBaseData.ScoreList {
  2722. if data.Key == posIdxData.GuildId {
  2723. this.battleBaseData.ScoreList[idx].Value2--
  2724. } else if data.Key == challengeGuildId {
  2725. this.battleBaseData.ScoreList[idx].Value2++
  2726. }
  2727. }
  2728. //挑战玩家
  2729. //老玩家离开占位
  2730. oldUid := posIdxData.OwnerBrief.Uid
  2731. oldUidNIckName := posIdxData.OwnerBrief.NickName
  2732. oldUidGuildId := posIdxData.GuildId
  2733. this.outPosIdx(true, oldUid, nowTime)
  2734. oldPlayerData := this.getPlayerData(oldUid, oldUidGuildId)
  2735. if oldPlayerData != nil {
  2736. /* 策划说,防守失败,不清连胜
  2737. if oldPlayerData.WinStrak > 0 {
  2738. oldPlayerData.WinStrak = 0
  2739. this.changePlayerDataList.Add(oldUid)
  2740. }
  2741. */
  2742. //被击杀玩家虚弱状态通知
  2743. oldPlayerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
  2744. ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
  2745. GuildBattleIdx: this.guildBattleIdx,
  2746. NtfUid: oldUid,
  2747. SelfData: oldPlayerData,
  2748. }
  2749. SendSocial(ntfMsg)
  2750. this.changePlayerDataList.Add(oldUid)
  2751. // model.ServiceReplay(ev, ntfMsg)
  2752. }
  2753. //当前挑战的玩家离开之前的占位
  2754. this.outPosIdx(false, bfInfo.Uid, nowTime)
  2755. //当前挑战的玩家进入挑战成功的占位
  2756. this.inPosIdx(posIdxData, bfInfo.Uid, challengeGuildId, nowTime)
  2757. //击杀次数记录
  2758. playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
  2759. if playerData != nil {
  2760. playerData.WinNum++
  2761. playerData.WinStrak++
  2762. this.changePlayerDataList.Add(bfInfo.Uid)
  2763. //玩家击杀排行榜
  2764. UpdateGuildBattleRankList_Kill(this.mag.guildBattleId, this.guildBattleIdx,
  2765. RankKillListType, playerData.WinNum, bfInfo.Uid)
  2766. //通知玩家自己
  2767. ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
  2768. GuildBattleIdx: this.guildBattleIdx,
  2769. NtfUid: bfInfo.Uid,
  2770. SelfData: playerData,
  2771. PosData: posIdxData,
  2772. }
  2773. model.ServiceReplay(ev, ntfMsg)
  2774. }
  2775. //添加占领日志
  2776. tmpSt := posIdxLogSt{
  2777. logType: 1,
  2778. posIdx: posIdx,
  2779. battleResult: true,
  2780. challengeGuild: challengeGuildId,
  2781. challengeNickName: bfInfo.NickName,
  2782. challengeUid: bfInfo.Uid,
  2783. posIdxUid: oldUid,
  2784. posIdxGuild: oldUidGuildId,
  2785. posIdxNickName: oldUidNIckName,
  2786. }
  2787. if playerData != nil && playerData.WinStrak > BattleWinNumTips {
  2788. tmpSt.WinSteak = playerData.WinStrak
  2789. }
  2790. this.addPosIdxLog(tmpSt)
  2791. } else {
  2792. util.InfoF("BattleChallengeResult posIdxData.OwnerBrief is nil uid: %v", posIdxData.Uid)
  2793. playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
  2794. if posIdxData.Uid != 0 && playerData != nil {
  2795. for idx, data := range this.battleBaseData.ScoreList {
  2796. if data.Key == posIdxData.GuildId {
  2797. this.battleBaseData.ScoreList[idx].Value2--
  2798. } else if data.Key == challengeGuildId {
  2799. this.battleBaseData.ScoreList[idx].Value2++
  2800. }
  2801. }
  2802. //找不到老玩家数据,非法战斗
  2803. brief := &serverproto.CommonPlayerBriefInfo{}
  2804. err2 := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.Uid, brief)
  2805. if err2 != nil {
  2806. playerData.ChallengeNum++
  2807. //您在战斗时已被他人击败,本场战斗无效,本次挑战无消耗
  2808. this.changePlayerDataList.Add(bfInfo.Uid)
  2809. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_RESULT_INVALID)
  2810. return
  2811. }
  2812. //挑战玩家
  2813. //老玩家离开占位
  2814. oldUid := posIdxData.Uid
  2815. oldUidNIckName := brief.NickName
  2816. oldUidGuildId := posIdxData.GuildId
  2817. oldPlayerData := this.getPlayerData(oldUid, oldUidGuildId)
  2818. if oldPlayerData != nil {
  2819. //被击杀玩家虚弱状态通知
  2820. oldPlayerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
  2821. ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
  2822. GuildBattleIdx: this.guildBattleIdx,
  2823. NtfUid: oldUid,
  2824. SelfData: oldPlayerData,
  2825. }
  2826. SendSocial(ntfMsg)
  2827. this.changePlayerDataList.Add(oldUid)
  2828. }
  2829. //当前挑战的玩家离开之前的占位
  2830. this.outPosIdx(false, bfInfo.Uid, nowTime)
  2831. //当前挑战的玩家进入挑战成功的占位
  2832. this.inPosIdx(posIdxData, bfInfo.Uid, challengeGuildId, nowTime)
  2833. //击杀次数记录
  2834. playerData.WinNum++
  2835. playerData.WinStrak++
  2836. this.changePlayerDataList.Add(bfInfo.Uid)
  2837. //玩家击杀排行榜
  2838. UpdateGuildBattleRankList_Kill(this.mag.guildBattleId, this.guildBattleIdx,
  2839. RankKillListType, playerData.WinNum, bfInfo.Uid)
  2840. //通知玩家自己
  2841. ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
  2842. GuildBattleIdx: this.guildBattleIdx,
  2843. NtfUid: bfInfo.Uid,
  2844. SelfData: playerData,
  2845. PosData: posIdxData,
  2846. }
  2847. model.ServiceReplay(ev, ntfMsg)
  2848. //添加占领日志
  2849. tmpSt := posIdxLogSt{
  2850. logType: 1,
  2851. posIdx: posIdx,
  2852. battleResult: true,
  2853. challengeGuild: challengeGuildId,
  2854. challengeNickName: bfInfo.NickName,
  2855. challengeUid: bfInfo.Uid,
  2856. posIdxUid: oldUid,
  2857. posIdxGuild: oldUidGuildId,
  2858. posIdxNickName: oldUidNIckName,
  2859. }
  2860. if playerData.WinStrak > BattleWinNumTips {
  2861. tmpSt.WinSteak = playerData.WinStrak
  2862. }
  2863. this.addPosIdxLog(tmpSt)
  2864. return
  2865. }
  2866. //挑战成功但是该位置已经先被其他玩家占领
  2867. //本次挑战无效
  2868. if playerData != nil {
  2869. playerData.ChallengeNum++
  2870. //您在战斗时已被他人击败,本场战斗无效,本次挑战无消耗
  2871. this.changePlayerDataList.Add(bfInfo.Uid)
  2872. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_RESULT_INVALID)
  2873. }
  2874. }
  2875. }
  2876. func (this *GuildBattle) BattlePKData(ssAckMsg *serverproto.SCGuildBattlePKDataAck) {
  2877. ssAckMsg.BattleDataList = this.battleBaseData.ScoreList
  2878. }
  2879. func (this *GuildBattle) GetBattleWinState() (uint64, uint64, bool) {
  2880. scoreList := this.battleBaseData.ScoreList
  2881. if len(scoreList) == 2 { //满编队伍
  2882. if scoreList[1].Key == 0 {
  2883. return scoreList[0].Key, 0, true
  2884. }
  2885. }
  2886. if !this.checkBattleFinish() {
  2887. //战斗未结束无法获取胜利/失败状态
  2888. return 0, 0, false
  2889. }
  2890. if len(scoreList) == 2 { //满编队伍
  2891. if scoreList[0].Value == scoreList[1].Value {
  2892. if scoreList[0].Value2 == scoreList[0].Value2 {
  2893. return scoreList[0].Key, scoreList[1].Key, true
  2894. } else if scoreList[0].Value2 < scoreList[0].Value2 {
  2895. return scoreList[0].Key, scoreList[1].Key, true
  2896. } else {
  2897. return scoreList[1].Key, scoreList[0].Key, true
  2898. }
  2899. } else if scoreList[0].Value > scoreList[1].Value {
  2900. return scoreList[0].Key, scoreList[1].Key, true
  2901. } else {
  2902. return scoreList[1].Key, scoreList[0].Key, true
  2903. }
  2904. }
  2905. return 0, 0, false
  2906. }
  2907. type posIdxLogSt struct {
  2908. logType int32
  2909. posIdx int32
  2910. battleResult bool
  2911. challengeUid uint64
  2912. challengeNickName string
  2913. challengeGuild uint64
  2914. posIdxUid uint64
  2915. posIdxNickName string
  2916. posIdxGuild uint64
  2917. WinSteak int32 //连杀次数
  2918. }
  2919. // =1、{0}公会{1}击败了{2}公会的{3}的防守。{4}连杀!
  2920. // =2、{0}公会{1}成功防守了{2}公会{3}的进攻。
  2921. func (this *GuildBattle) addPosIdxLog(st posIdxLogSt) {
  2922. logData := &serverproto.GuildBattleLog{
  2923. Type: st.logType,
  2924. PosIdx: st.posIdx,
  2925. }
  2926. if st.battleResult {
  2927. logData.State = true
  2928. }
  2929. logData.RecordTime = util.GetTimeMilliseconds()
  2930. logData.ChallengePlayerUid = st.challengeUid
  2931. logData.ChallengeGuildId = st.challengeGuild
  2932. logData.ChallengePlayerName = st.challengeNickName
  2933. logData.TargetPlayerUid = st.posIdxUid
  2934. logData.TargetGuildId = st.posIdxGuild
  2935. logData.TargetPlayerName = st.posIdxNickName
  2936. logData.WinSteak = st.WinSteak
  2937. //加载日志
  2938. keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2939. if len(this.logList) <= 0 {
  2940. //keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2941. retList, err := service.GetRedis().LRange(keyStr, 0, -1).Result()
  2942. if err != nil {
  2943. return
  2944. }
  2945. for idx := 0; idx < len(retList); idx++ {
  2946. logData := &serverproto.GuildBattleLog{}
  2947. err = model.GetDecodeMessage(logData, retList[idx])
  2948. if err != nil {
  2949. continue
  2950. }
  2951. this.logList = append(this.logList, logData)
  2952. }
  2953. sort.Slice(this.logList, func(i, j int) bool {
  2954. return this.logList[i].RecordTime < this.logList[j].RecordTime
  2955. })
  2956. }
  2957. this.logList = append(this.logList, logData)
  2958. if len(this.logList) > int(MaxUidLogCount) {
  2959. this.logList = append(this.logList[:0], this.logList[1:]...)
  2960. }
  2961. err, msgStr := model.GetEncodeMessage(logData)
  2962. if err != nil {
  2963. return
  2964. }
  2965. ret, err1 := service.GetRedis().LPush(keyStr, msgStr).Result()
  2966. if err1 == nil && ret > MaxUidLogCount {
  2967. service.GetRedis().LTrim(keyStr, 0, MaxUidLogCount-1)
  2968. }
  2969. }
  2970. func (this *GuildBattle) GetCountPartData(ackMsg *serverproto.SCGuildBattleRankListAck) {
  2971. if ackMsg == nil {
  2972. return
  2973. }
  2974. winGuild, _, ok := this.GetBattleWinState()
  2975. if ok {
  2976. if winGuild == this.battleBaseData.ScoreList[0].Key {
  2977. ackMsg.WinGuild = true
  2978. } else {
  2979. ackMsg.WinGuild = false
  2980. }
  2981. ackMsg.IsFinish = true
  2982. } else {
  2983. ackMsg.IsFinish = false
  2984. }
  2985. for _, countPart := range this.battleBaseData.ScoreList {
  2986. keyValue := &serverproto.KeyValueType64{
  2987. Key: countPart.Key,
  2988. Value: countPart.Value,
  2989. Value2: countPart.Value2,
  2990. Value3: countPart.Value3,
  2991. StrVal: countPart.StrVal,
  2992. }
  2993. ackMsg.BattleDataList = append(ackMsg.BattleDataList, keyValue)
  2994. }
  2995. }
  2996. func (this *GuildBattle) ExchangeGuildPre(oldPreUid, curPreUid, guildId uint64) {
  2997. oldPrePlayer := this.getPlayerData(oldPreUid, guildId)
  2998. curPrePlayer := this.getPlayerData(curPreUid, guildId)
  2999. if oldPrePlayer == nil || curPrePlayer == nil {
  3000. return
  3001. }
  3002. oldPrePlayer.GuildPre = false
  3003. curPrePlayer.GuildPre = true
  3004. this.changePlayerDataList.Add(oldPrePlayer.Uid)
  3005. this.changePlayerDataList.Add(curPrePlayer.Uid)
  3006. }