guild_battle.go 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309
  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. //发送奖励给
  1060. this.SendGuildBattleReward(GuileBattle_Reward_Rank_Second, lostGuild)
  1061. this.SendGuildBattleReward(GuileBattle_Reward_Rank_First, winGuild)
  1062. }
  1063. const (
  1064. Rank_Type_Score = 1
  1065. Rank_Type_Killer = 2
  1066. )
  1067. func (this *GuildBattleManager) GuildBattleMvpReward(ackMsg *serverproto.SSGuildBattleSendMvpRewardAck) {
  1068. if ackMsg == nil {
  1069. return
  1070. }
  1071. if len(ackMsg.ScoreMvp) <= 0 && len(ackMsg.KillerMvp) <= 0 {
  1072. return
  1073. }
  1074. //设置领奖标记
  1075. for _, scoreData := range ackMsg.ScoreMvp {
  1076. battle, ok := this.battleIdxDataList[scoreData.BattleIndex]
  1077. if !ok {
  1078. //打印日志,给了奖励,但是没有做记录
  1079. continue
  1080. }
  1081. battle.battleBaseData.ScoreMvp = append(battle.battleBaseData.ScoreMvp, scoreData.Uid)
  1082. battle.baseChange = true
  1083. }
  1084. for _, killerData := range ackMsg.KillerMvp {
  1085. battle, ok := this.battleIdxDataList[killerData.BattleIndex]
  1086. if !ok {
  1087. //打印日志,给了奖励,但是没有做记录
  1088. continue
  1089. }
  1090. battle.battleBaseData.KillerMvp = append(battle.battleBaseData.KillerMvp, killerData.Uid)
  1091. battle.baseChange = true
  1092. }
  1093. }
  1094. func (this *GuildBattleManager) checkRewardDraw(mvpType, battleIndex int32, uid uint64) bool {
  1095. guildBattle := this.getBattleByIdx(battleIndex)
  1096. if guildBattle == nil || guildBattle.battleBaseData == nil {
  1097. return true //找不到这场战斗,不给奖励
  1098. }
  1099. if len(guildBattle.battleBaseData.ScoreMvp) > 0 && mvpType == Rank_Type_Score {
  1100. for _, rewardUid := range guildBattle.battleBaseData.ScoreMvp {
  1101. if rewardUid == uid {
  1102. return true
  1103. }
  1104. }
  1105. } else if len(guildBattle.battleBaseData.KillerMvp) > 0 && mvpType == Rank_Type_Killer {
  1106. for _, rewardUid := range guildBattle.battleBaseData.KillerMvp {
  1107. if rewardUid == uid {
  1108. return true
  1109. }
  1110. }
  1111. }
  1112. return false
  1113. }
  1114. // 场次结束结束发送排名奖励
  1115. func (this *GuildBattleManager) sendGuildBattleMvpReward(rewardType int32, startBattle, endBattle int32) {
  1116. ntfMsg := &serverproto.SSGuildBattleSendMvpRewardReq{
  1117. GuildBattleRound: this.guildBattleId,
  1118. }
  1119. for i := startBattle; i <= endBattle; i++ {
  1120. //打包 积分Mvp
  1121. scoreStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(i)) + "-" + strconv.Itoa(int(Rank_Type_Score))
  1122. retList, err := service.GetRedis().ZRevRangeWithScores(scoreStr, int64(0), int64(0)).Result()
  1123. if err == nil {
  1124. for idx := 0; idx < len(retList); {
  1125. uid, _ := model.Str2NumU64(retList[idx].Member.(string))
  1126. bFind := this.checkRewardDraw(Rank_Type_Score, i, uid)
  1127. if bFind {
  1128. break
  1129. }
  1130. ntfMsg.ScoreMvp = append(ntfMsg.ScoreMvp, &serverproto.MvpRewardInfo{
  1131. Uid: uid,
  1132. RewardType: int32(rewardType),
  1133. BattleIndex: int32(i),
  1134. })
  1135. break
  1136. }
  1137. }
  1138. //打包 击杀Mvp
  1139. killerStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(i)) + "-" + strconv.Itoa(int(Rank_Type_Killer))
  1140. retList2, err2 := service.GetRedis().ZRevRangeWithScores(killerStr, int64(0), int64(0)).Result()
  1141. if err2 == nil {
  1142. for idx := 0; idx < len(retList2); {
  1143. uid, _ := model.Str2NumU64(retList2[idx].Member.(string))
  1144. bFind := this.checkRewardDraw(Rank_Type_Killer, i, uid)
  1145. if bFind {
  1146. break
  1147. }
  1148. ntfMsg.KillerMvp = append(ntfMsg.KillerMvp, &serverproto.MvpRewardInfo{
  1149. Uid: uid,
  1150. RewardType: int32(rewardType),
  1151. BattleIndex: i,
  1152. })
  1153. break
  1154. }
  1155. }
  1156. }
  1157. SendToAllGame(ntfMsg)
  1158. }
  1159. // 对阵数据启动服务器加载
  1160. func (this *GuildBattleManager) getBattleByIdx(battleIdx int32) *GuildBattle {
  1161. if this.banType == Guild_Battle_Baned {
  1162. return nil
  1163. }
  1164. if battle, ok := this.battleIdxDataList[battleIdx]; ok {
  1165. return battle
  1166. }
  1167. //TODO 查看数据库中是否存在对阵表,存在则构建战斗数据
  1168. // 并初始化战斗信息
  1169. //guildBattleKey := GuildBattlePrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(battleIdx))
  1170. return nil
  1171. }
  1172. func (this *GuildBattleManager) GMGuildBattleOperator(operator int32) {
  1173. this.operator = operator
  1174. }
  1175. func (this *GuildBattleManager) GMAddGuildBattleScore(battleIdx int32, guild_index int32, score int32) {
  1176. guildBattle := this.getBattleByIdx(battleIdx)
  1177. if guildBattle != nil {
  1178. if guild_index == 0 {
  1179. guildBattle.battleBaseData.ScoreList[0].Value += score
  1180. } else if guild_index == 1 {
  1181. guildBattle.battleBaseData.ScoreList[1].Value += score
  1182. }
  1183. }
  1184. }
  1185. // 公会战开关
  1186. func (this *GuildBattleManager) WebGMGuildBattle() {
  1187. var inBattle bool
  1188. if this.banType == 0 {
  1189. //原先开着的,则关闭
  1190. this.banType = 1
  1191. inBattle = false
  1192. util.InfoF("WebGMGuildBattle, guild battle current state close")
  1193. } else {
  1194. //原先关闭的,则开着
  1195. this.banType = 0
  1196. inBattle = true
  1197. util.InfoF("WebGMGuildBattle, guild battle current state open")
  1198. }
  1199. this.GuildBattleStateChangeNtf(inBattle)
  1200. err := this.saveGuildBattleBaseToRedis()
  1201. if err != nil {
  1202. }
  1203. }
  1204. // todo wangzhaocan 测试用 待删除
  1205. func (this *GuildBattleManager) GMGuildBattleReward() {
  1206. this.sendGuildBattleReward(Guild_Battle_Reward_Semifinals, 2, 3)
  1207. }
  1208. // 获取对阵双方信息(打开界面时的信息)
  1209. func (this *GuildBattleManager) GuildBattleInfo(selfGuildId, uid uint64, battleIdx int32, ackMsg *serverproto.SCGuildBattleInfoAck) {
  1210. if this.banType == Guild_Battle_Baned {
  1211. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_GUILD_SPECIAL_STATE)
  1212. return
  1213. }
  1214. this.initGuildBattleList()
  1215. battleData := this.getBattleByIdx(battleIdx)
  1216. if battleData == nil {
  1217. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1218. return
  1219. }
  1220. battleData.BattleInfoReq(uid, selfGuildId, ackMsg)
  1221. }
  1222. func (this *GuildBattleManager) GuildBattleSetting(battleIdx int32, uid, guildId uint64, autoChallenge, autoReborn bool, ackMsg *serverproto.SCGuildBattleSettingAck) {
  1223. this.initGuildBattleList()
  1224. battleData := this.getBattleByIdx(battleIdx)
  1225. if battleData == nil {
  1226. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1227. return
  1228. }
  1229. battleData.BattleSetting(uid, guildId, autoChallenge, autoReborn, ackMsg)
  1230. }
  1231. // 是否在购买时间内//购买时间之前/之后,做区分
  1232. func (this *GuildBattleManager) CheckInBuyTime(battleData *GuildBattle) serverproto.ErrorCode {
  1233. if battleData == nil {
  1234. return serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST
  1235. }
  1236. if Guild_Battle_Tournament_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Tournament_End_Match { //四强
  1237. if this.guildBattleState < Guild_Battle_Tournament_Prepare {
  1238. return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
  1239. }
  1240. } else if Guild_Battle_Seminal_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Seminal_End_Match { //半决赛
  1241. if this.guildBattleState < Guild_Battle_Semifinals_Prepare {
  1242. return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
  1243. }
  1244. } else if Guild_Battle_Tournament_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Tournament_End_Match { //决赛
  1245. if this.guildBattleState < Guild_Battle_Final_Prepare {
  1246. return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
  1247. }
  1248. }
  1249. _, _, ok := battleData.GetBattleWinState()
  1250. if ok {
  1251. return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_FINISH
  1252. }
  1253. return serverproto.ErrorCode_ERROR_OK
  1254. }
  1255. func (this *GuildBattleManager) BuyChallengeNum(battleIdx int32, uid, guildId uint64, ackMsg *serverproto.SCGuildBattleBuyChallengeAck) {
  1256. this.initGuildBattleList()
  1257. battleData := this.getBattleByIdx(battleIdx)
  1258. if battleData == nil {
  1259. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1260. return
  1261. }
  1262. bRet := this.CheckInBuyTime(battleData)
  1263. if bRet != serverproto.ErrorCode_ERROR_OK {
  1264. ackMsg.Error = int32(bRet)
  1265. return
  1266. }
  1267. battleData.BuyChallengeNum(uid, guildId, ackMsg)
  1268. }
  1269. func (this *GuildBattleManager) BuyBattleBuff(battleIdx int32, uid uint64, buffList []int32, ackMsg *serverproto.SSGuildBattleBuyBuffAck) {
  1270. this.initGuildBattleList()
  1271. battleData := this.getBattleByIdx(battleIdx)
  1272. if battleData == nil {
  1273. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1274. return
  1275. }
  1276. bRet := this.CheckInBuyTime(battleData)
  1277. if bRet != serverproto.ErrorCode_ERROR_OK {
  1278. ackMsg.Error = int32(bRet)
  1279. return
  1280. }
  1281. guildIdx := GuildMag.RoleToGuilds[uid]
  1282. if guildIdx != nil {
  1283. battleData.BuyBattleBuff(uid, guildIdx.GuildId, buffList, ackMsg)
  1284. }
  1285. }
  1286. func (this *GuildBattleManager) ResetRebornCD(battleIdx int32, uid, guildId uint64, ackMsg *serverproto.SCGuildBattleRebornAck) {
  1287. this.initGuildBattleList()
  1288. battleData := this.getBattleByIdx(battleIdx)
  1289. if battleData == nil {
  1290. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1291. return
  1292. }
  1293. bRet := this.CheckInBuyTime(battleData)
  1294. if bRet != serverproto.ErrorCode_ERROR_OK {
  1295. ackMsg.Error = int32(bRet)
  1296. return
  1297. }
  1298. battleData.ResetRebornCD(uid, guildId, ackMsg)
  1299. }
  1300. func (this *GuildBattleManager) GetBattleRankList(battleIdx, pageIdx, rankType int32, ackMsg *serverproto.SCGuildBattleRankListAck) {
  1301. this.initGuildBattleList()
  1302. battleData := this.getBattleByIdx(battleIdx)
  1303. if battleData == nil {
  1304. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1305. return
  1306. }
  1307. GetGuildBattleRankList(this.guildBattleId, battleIdx, pageIdx, rankType, ackMsg)
  1308. }
  1309. // limit 20 items
  1310. func (this *GuildBattleManager) GetBattlePosIdxList(battleIdx int32, posIdxList []int32, ackMsg *serverproto.SCGuildBattlePosIdxListAck) {
  1311. if len(posIdxList) <= 0 || len(posIdxList) > 20 {
  1312. return
  1313. }
  1314. this.initGuildBattleList()
  1315. battleData := this.getBattleByIdx(battleIdx)
  1316. if battleData == nil {
  1317. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1318. return
  1319. }
  1320. bRet := this.CheckInBuyTime(battleData)
  1321. if bRet != serverproto.ErrorCode_ERROR_OK {
  1322. ackMsg.Error = int32(bRet)
  1323. return
  1324. }
  1325. battleData.GetBattlePosIdxList(posIdxList, ackMsg)
  1326. }
  1327. func (this *GuildBattleManager) GetPosIdxLog(battleIdx int32, beginTime uint64, ackMsg *serverproto.SCGuildBattleLogAck) {
  1328. this.initGuildBattleList()
  1329. battleData := this.getBattleByIdx(battleIdx)
  1330. if battleData == nil {
  1331. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1332. return
  1333. }
  1334. battleData.GetPosIdxLog(beginTime, ackMsg)
  1335. }
  1336. func (this *GuildBattleManager) BattleChallenge(battleIdx, posIdx int32, selfGuildId uint64, buyNum int32, bfInfo *serverproto.CommonPlayerBriefInfo,
  1337. curRmb uint64, ackMsg *serverproto.SSGuildBattleChallengeAck) {
  1338. this.initGuildBattleList()
  1339. battleData := this.getBattleByIdx(battleIdx)
  1340. if battleData == nil {
  1341. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1342. return
  1343. }
  1344. bRet := this.CheckInBuyTime(battleData)
  1345. if bRet != serverproto.ErrorCode_ERROR_OK {
  1346. ackMsg.Error = int32(bRet)
  1347. return
  1348. }
  1349. battleData.BattleChallengePre(posIdx, selfGuildId, bfInfo, curRmb, buyNum, ackMsg)
  1350. }
  1351. func (this *GuildBattleManager) BattleChallengeResult(msg *serverproto.CSGuildBattleChallengeResultReq,
  1352. ssAckMsg *serverproto.SCGuildBattleChallengeResultAck, ev rocommon.ProcEvent) {
  1353. this.initGuildBattleList()
  1354. battleData := this.getBattleByIdx(msg.GuildBattleIdx)
  1355. if battleData == nil {
  1356. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1357. return
  1358. }
  1359. bRet := this.CheckInBuyTime(battleData)
  1360. if bRet != serverproto.ErrorCode_ERROR_OK {
  1361. ssAckMsg.Error = int32(bRet)
  1362. return
  1363. }
  1364. battleData.BattleChallengeResult(msg.PosIdx, msg.SelfGuildId, msg.BfInfo, msg.BattleResult, ssAckMsg, ev)
  1365. }
  1366. func (this *GuildBattleManager) BattleChallengePing(challengeUid uint64, battleIdx, posIdx int32,
  1367. ackMsg *serverproto.SCGuildBattleChallengePingAck) {
  1368. this.initGuildBattleList()
  1369. battleData := this.getBattleByIdx(battleIdx)
  1370. if battleData == nil {
  1371. return
  1372. }
  1373. battleData.BattleChallengePing(challengeUid, posIdx, ackMsg)
  1374. }
  1375. func (this *GuildBattleManager) BattlePKData(battleIdx int32, ssAckMsg *serverproto.SCGuildBattlePKDataAck) {
  1376. this.initGuildBattleList()
  1377. battleData := this.getBattleByIdx(battleIdx)
  1378. if battleData == nil {
  1379. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
  1380. return
  1381. }
  1382. bRet := this.CheckInBuyTime(battleData)
  1383. if bRet != serverproto.ErrorCode_ERROR_OK {
  1384. ssAckMsg.Error = int32(bRet)
  1385. // return
  1386. }
  1387. battleData.BattlePKData(ssAckMsg)
  1388. }
  1389. func (this *GuildBattleManager) GetGuildBattleCPRank(guildId uint64, ackMsg *serverproto.SSGuildBattleCPRankAck) {
  1390. topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model.RoleDaoChang100GuildRankPrefix, 0, 19).Result()
  1391. if err != nil {
  1392. util.ErrorF("GetGuildBattleCPRank")
  1393. return
  1394. }
  1395. util.InfoF("GetGuildBattleCPRank len:%v", len(topListWithScore))
  1396. for index, _ := range topListWithScore {
  1397. guildId, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  1398. guild := GuildMag.GetGuild(guildId)
  1399. if guild == nil {
  1400. continue
  1401. }
  1402. cpNum := getValByDaoChang100GuildScore(topListWithScore[index].Score)
  1403. ackMsg.CpList = append(ackMsg.CpList, &serverproto.GuildBattleCP{
  1404. Brief: guild.GuildBase.GuildBrief,
  1405. MemCount: int32(len(guild.GuildMember.MemberInfo)),
  1406. CpNum: uint32(cpNum),
  1407. Rank: int32(index + 1),
  1408. })
  1409. }
  1410. if guildId != 0 {
  1411. guildIdStr := strconv.FormatUint(guildId, 10)
  1412. tmpScore, err2 := service.GetRedis().ZScore(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
  1413. if err2 == nil {
  1414. oldScore := getValByDaoChang100GuildScore(tmpScore)
  1415. ackMsg.SelfGuildCp = int32(oldScore)
  1416. }
  1417. selfRank, rankErr := service.GetRedis().ZRevRank(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
  1418. if rankErr == nil {
  1419. ackMsg.SelfGuildRank = int32(selfRank + 1)
  1420. }
  1421. }
  1422. }
  1423. func (this *GuildBattleManager) GetBattleState(guildId1, guildId2 uint64, battle *GuildBattle) (bool, int32) {
  1424. if battle == nil {
  1425. return false, 0
  1426. }
  1427. bRet := false
  1428. retState := int32(0)
  1429. winGuild, _, ok := battle.GetBattleWinState()
  1430. if ok {
  1431. if winGuild == guildId1 {
  1432. bRet = true
  1433. } else if winGuild == guildId2 {
  1434. bRet = false
  1435. }
  1436. retState = Battle_State_End
  1437. } else {
  1438. nowTime := util.GetCurrentTime()
  1439. if Guild_Battle_Tournament_Begin_Match <= battle.guildBattleIdx && battle.guildBattleIdx <= Guild_Battle_Tournament_End_Match {
  1440. if this.tournamentPrepare <= nowTime && nowTime <= this.tournamentEnd {
  1441. retState = Battle_State_Fight
  1442. } else {
  1443. retState = Battle_State_Prepare
  1444. }
  1445. } else if Guild_Battle_Seminal_Begin_Match <= battle.guildBattleIdx && battle.guildBattleIdx <= Guild_Battle_Seminal_End_Match {
  1446. if this.semifinalsPrepare <= nowTime && nowTime <= this.semifinalsEnd {
  1447. retState = Battle_State_Fight
  1448. } else {
  1449. retState = Battle_State_Prepare
  1450. }
  1451. } else if Guild_Battle_Final_Match == battle.guildBattleIdx {
  1452. if this.finalPrepare <= nowTime && nowTime <= this.finalEnd {
  1453. retState = Battle_State_Fight
  1454. } else {
  1455. retState = Battle_State_Prepare
  1456. }
  1457. }
  1458. }
  1459. return bRet, retState
  1460. }
  1461. const (
  1462. Battle_State_Prepare = 1
  1463. Battle_State_Fight = 2
  1464. Battle_State_End = 3
  1465. )
  1466. func (this *GuildBattleManager) GetGuildBattleCountPart(ackMsg *serverproto.SSGuildBattleCountPartAck) {
  1467. if this.banType == Guild_Battle_Baned {
  1468. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_GUILD_SPECIAL_STATE)
  1469. return
  1470. }
  1471. if this.guildBattleId == 0 || this.showEnd <= util.GetCurrentTime() {
  1472. return
  1473. }
  1474. ackMsg.Table = &serverproto.GuildCountPartTable{}
  1475. for _, data := range this.countPartTable.Data {
  1476. battle := this.battleIdxDataList[data.BattleIndex]
  1477. if battle == nil {
  1478. continue
  1479. }
  1480. countTable := &serverproto.CountTableData{}
  1481. countTable.BattleIndex = data.BattleIndex
  1482. countTable.Guild1Id = data.Guild1Id
  1483. countTable.Guild2Id = data.Guild2Id
  1484. if len(battle.battleBaseData.ScoreList) >= 1 {
  1485. countTable.Guild1Name = battle.battleBaseData.ScoreList[0].StrVal
  1486. countTable.Guild1Badge = battle.battleBaseData.ScoreList[0].Value3
  1487. }
  1488. if len(battle.battleBaseData.ScoreList) >= 2 {
  1489. countTable.Guild2Name = battle.battleBaseData.ScoreList[1].StrVal
  1490. countTable.Guild2Badge = battle.battleBaseData.ScoreList[1].Value3
  1491. }
  1492. retWin, retState := this.GetBattleState(data.Guild1Id, data.Guild2Id, battle)
  1493. countTable.WinGuild = retWin
  1494. countTable.State = retState
  1495. ackMsg.Table.Data = append(ackMsg.Table.Data, countTable)
  1496. }
  1497. // ackMsg.Table = this.countPartTable
  1498. ackMsg.CurStge = this.guildBattleState
  1499. ackMsg.TournamentPrepare = this.tournamentPrepare
  1500. ackMsg.TournamentBegin = this.tournamentBegin
  1501. ackMsg.TournamentEnd = this.tournamentEnd
  1502. ackMsg.SemifinalsPrepare = this.semifinalsPrepare
  1503. ackMsg.SemifinalsBegin = this.semifinalsBegin
  1504. ackMsg.SemifinalsEnd = this.semifinalsEnd
  1505. ackMsg.FinalsPrepare = this.finalPrepare
  1506. ackMsg.FinalsBegin = this.finalBegin
  1507. ackMsg.FinalsEnd = this.finalEnd
  1508. }
  1509. func (this *GuildBattleManager) GetGuildBattleMvpInfo(battleIdx int32, ackMsg *serverproto.SSGuildBattleMvpInfoAck) {
  1510. if ackMsg == nil {
  1511. return
  1512. }
  1513. data, ok := this.battleIdxDataList[battleIdx]
  1514. if !ok {
  1515. return
  1516. }
  1517. var guildId1 uint64 = 0
  1518. for idx, scoreList := range data.battleBaseData.ScoreList {
  1519. ackMsg.Detail = append(ackMsg.Detail, &serverproto.GuildBattleDetail{
  1520. GuildId: scoreList.Key,
  1521. SlotCount: 0,
  1522. Score: scoreList.Value,
  1523. })
  1524. guild := GuildMag.GetGuild(scoreList.Key)
  1525. if guild != nil {
  1526. if idx == 1 {
  1527. guildId1 = scoreList.Key
  1528. ackMsg.CountPart.Guild1Id = scoreList.Key
  1529. ackMsg.CountPart.Guild1Name = guild.GuildBase.GuildBrief.GuildName
  1530. } else if idx == 2 {
  1531. ackMsg.CountPart.Guild2Id = scoreList.Key
  1532. ackMsg.CountPart.Guild2Name = guild.GuildBase.GuildBrief.GuildName
  1533. }
  1534. }
  1535. }
  1536. winGuild, _, bRet := this.GetBattleWinState(battleIdx)
  1537. if bRet {
  1538. if winGuild == guildId1 {
  1539. ackMsg.CountPart.WinGuild = true
  1540. } else {
  1541. ackMsg.CountPart.WinGuild = false
  1542. }
  1543. }
  1544. }
  1545. func (this *GuildBattleManager) GetGuildBattleMvpDetail(battleIdx int32, mvpType int32, reqRank int32, ackMsg *serverproto.SSGuildBattleMvpDetailAck) {
  1546. keyStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(battleIdx)) + "-" + strconv.Itoa(int(mvpType))
  1547. topListWithScore, err := service.GetRedis().ZRevRangeWithScores(keyStr, int64(reqRank), int64(reqRank+9)).Result()
  1548. if err != nil {
  1549. util.ErrorF("UpdateDaoChang100GuildRank")
  1550. }
  1551. for index, data := range topListWithScore {
  1552. tmpScore := getValByGuildBattleScore(data.Score)
  1553. uid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  1554. mvp := &serverproto.GuildBattleMvp{
  1555. Uid: uid,
  1556. Score: int32(tmpScore),
  1557. }
  1558. mvp.GuildId = GuildMag.GetPlayerGuildId(uid)
  1559. guild := GuildMag.GetGuild(mvp.GuildId)
  1560. if guild != nil {
  1561. mvp.Name = guild.GuildBase.GuildBrief.GuildName
  1562. }
  1563. ackMsg.Mvp = append(ackMsg.Mvp, mvp)
  1564. }
  1565. }
  1566. func (this *GuildBattleManager) GetBattleWinStateByBaseData(baseData *serverproto.GuildBattleBaseData) (uint64, uint64, bool) {
  1567. scoreList := baseData.ScoreList
  1568. if len(scoreList) == 2 { //满编队伍
  1569. if scoreList[0].Value == scoreList[1].Value {
  1570. if scoreList[0].Value2 == scoreList[0].Value2 {
  1571. return scoreList[0].Key, scoreList[1].Key, true
  1572. } else if scoreList[0].Value2 < scoreList[0].Value2 {
  1573. return scoreList[0].Key, scoreList[1].Key, true
  1574. } else {
  1575. return scoreList[1].Key, scoreList[0].Key, true
  1576. }
  1577. } else if scoreList[0].Value > scoreList[1].Value {
  1578. return scoreList[0].Key, scoreList[1].Key, true
  1579. } else {
  1580. return scoreList[1].Key, scoreList[0].Key, true
  1581. }
  1582. } else if len(scoreList) == 1 { //轮空的情况下
  1583. return scoreList[0].Key, scoreList[1].Key, true
  1584. }
  1585. return 0, 0, false
  1586. }
  1587. func (this *GuildBattleManager) CheckCurRewardRound(uid uint64) bool {
  1588. for key, battle := range this.battleIdxDataList {
  1589. _, lostGuild, ret := this.GetBattleWinState(key)
  1590. if !ret {
  1591. continue
  1592. }
  1593. playerData := battle.getPlayerData(uid, lostGuild)
  1594. if playerData == nil {
  1595. continue
  1596. }
  1597. //战斗结束//且有这个玩家,如果key是决赛,那就可以给奖励了,不是决赛且这场战斗输了也给奖励
  1598. if key == 1 || key != 1 && playerData.GuildId == lostGuild {
  1599. return true
  1600. }
  1601. }
  1602. return false
  1603. }
  1604. func (this *GuildBattleManager) PackPlayerGuildBattleReward(uid uint64, round, index int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) serverproto.ErrorCode {
  1605. keyStr := GuildBattlePrefix + strconv.Itoa(int(round)) + "-" + strconv.Itoa(int(index))
  1606. fieldStr := "uid:" + strconv.FormatUint(uid, 10)
  1607. resultData, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
  1608. if err != nil {
  1609. util.ErrorF("GuildBattleSelfData err uid:%v round: %v battleindex: %v, err3: %v", uid, round, index, err)
  1610. return serverproto.ErrorCode_ERROR_FAIL
  1611. }
  1612. playerData := &serverproto.GuildBattleSelfData{}
  1613. err2 := model.GetDecodeMessage(playerData, resultData)
  1614. if err2 != nil {
  1615. util.ErrorF("GuildBattleSelfData not decode uid:%v round: %v battleindex: %v, err3: %v", uid, round, index, err2)
  1616. return serverproto.ErrorCode_ERROR_FAIL
  1617. }
  1618. isPre := false
  1619. if playerData.GuildPre == true {
  1620. isPre = true
  1621. }
  1622. rewardType := 0
  1623. var winGuild uint64 = 0
  1624. var lostGuild uint64 = 0
  1625. //如果是本轮活动,则判定是否战斗结束://
  1626. if round == this.guildBattleId {
  1627. win, lost, ok := this.GetBattleWinState(int32(index))
  1628. if !ok { //没结束就不管了
  1629. return serverproto.ErrorCode_ERROR_FAIL
  1630. }
  1631. //不是冠军赛 则要看是否是战斗失败的公会
  1632. if index != 1 && playerData.GuildId != lost {
  1633. return serverproto.ErrorCode_ERROR_FAIL
  1634. }
  1635. winGuild = win
  1636. lostGuild = lost
  1637. } else {
  1638. //获取比赛的信息
  1639. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(round)) + "-" + strconv.Itoa(int(index))
  1640. retStr, err3 := service.GetRedis().HGet(battleKeyStr, "base").Result()
  1641. if err3 != nil {
  1642. util.ErrorF("GuildBattleBaseData err round: %v battleindex: %v, err3: %v", round, index, err3)
  1643. return serverproto.ErrorCode_ERROR_FAIL
  1644. }
  1645. //获取比赛的基础数据
  1646. battleBaseData := &serverproto.GuildBattleBaseData{}
  1647. err4 := model.GetDecodeMessage(battleBaseData, retStr)
  1648. if err4 != nil {
  1649. util.ErrorF("GuildBattleBaseData not decode round: %v battleindex:%v: err: %v", round, index, err4)
  1650. return serverproto.ErrorCode_ERROR_FAIL
  1651. }
  1652. //比赛未结束,则后续不处理
  1653. win2, lost2, ret := this.GetBattleWinStateByBaseData(battleBaseData)
  1654. if !ret {
  1655. return serverproto.ErrorCode_ERROR_FAIL
  1656. }
  1657. winGuild = win2
  1658. lostGuild = lost2
  1659. }
  1660. if index == 1 {
  1661. if playerData.GuildId == winGuild {
  1662. rewardType = GuileBattle_Reward_Rank_First
  1663. } else if playerData.GuildId == lostGuild {
  1664. rewardType = GuileBattle_Reward_Rank_Second
  1665. }
  1666. } else if index == 2 || index == 3 {
  1667. rewardType = GuileBattle_Reward_Rank_Semifinals
  1668. } else if 4 <= index && index <= 7 {
  1669. rewardType = GuileBattle_Reward_Rank_Tournament
  1670. }
  1671. ackMsg.RoundReward = append(ackMsg.RoundReward, &serverproto.GuildBattleReward{
  1672. IsPre: isPre,
  1673. RewardType: int32(rewardType),
  1674. })
  1675. util.InfoF("uid=%v GuildBattleBaseData online get reward curround :%v, round: %v battleindex: %v guildid:%v", uid, this.guildBattleId, round, index, playerData.GuildId)
  1676. return serverproto.ErrorCode_ERROR_OK
  1677. }
  1678. func (this *GuildBattleManager) PackPlayerGuildBattleMvpReward(uid uint64, mvpRound, index int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) {
  1679. //获取本场战斗的base
  1680. var battleBaseData *serverproto.GuildBattleBaseData = nil
  1681. if mvpRound == this.guildBattleId {
  1682. battle, ok := this.battleIdxDataList[int32(index)]
  1683. if !ok {
  1684. return
  1685. }
  1686. battleBaseData = battle.battleBaseData
  1687. //公会战是否结束,未结束就不管
  1688. _, _, ok2 := this.GetBattleWinState(int32(index))
  1689. if !ok2 { //没结束就不管了
  1690. return
  1691. }
  1692. } else {
  1693. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(mvpRound)) + "-" + strconv.Itoa(int(index))
  1694. retStr, err3 := service.GetRedis().HGet(battleKeyStr, "base").Result()
  1695. if err3 != nil {
  1696. util.ErrorF("MvpReward GuildBattleBaseData err round: %v battleindex: %v, err3: %v", mvpRound, index, err3)
  1697. return
  1698. }
  1699. if err3 == service.NIL {
  1700. util.ErrorF("MvpReward GuildBattleBaseData not found round: %v battleindex: %v, err3: %v", mvpRound, index)
  1701. return
  1702. }
  1703. battleBaseData = &serverproto.GuildBattleBaseData{}
  1704. //获取比赛的基础数据
  1705. err4 := model.GetDecodeMessage(battleBaseData, retStr)
  1706. if err4 != nil {
  1707. util.ErrorF("MvpReward GuildBattleBaseData not decode round: %v battleindex:%v: err: %v", mvpRound, index, err4)
  1708. return
  1709. }
  1710. }
  1711. if battleBaseData == nil {
  1712. return
  1713. }
  1714. //遍历所有领奖记录,是否领取过奖励
  1715. var bScoreReward bool = false
  1716. var bKillerReward bool = false
  1717. if len(battleBaseData.ScoreMvp) >= 0 {
  1718. for _, rewardUid := range battleBaseData.ScoreMvp {
  1719. if rewardUid == uid {
  1720. bScoreReward = true
  1721. break
  1722. }
  1723. }
  1724. }
  1725. if len(battleBaseData.KillerMvp) >= 0 {
  1726. for _, rewardUid := range battleBaseData.KillerMvp {
  1727. if rewardUid == uid {
  1728. bKillerReward = true
  1729. }
  1730. break
  1731. }
  1732. }
  1733. if bScoreReward == true && bKillerReward == true {
  1734. return
  1735. }
  1736. //获取奖励类型
  1737. rewardType := Guild_Battle_Reward_Tournament
  1738. if index == 1 {
  1739. rewardType = Guild_Battle_Reward_Final
  1740. } else if index == 2 || index == 3 {
  1741. rewardType = Guild_Battle_Reward_Semifinals
  1742. }
  1743. //打包 积分Mvp
  1744. if bScoreReward == false {
  1745. scoreStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(index)) + "-" + strconv.Itoa(int(Rank_Type_Score))
  1746. retList, err3 := service.GetRedis().ZRevRangeWithScores(scoreStr, int64(0), int64(0)).Result()
  1747. if err3 == nil && len(retList) > 0 {
  1748. firstUid, _ := model.Str2NumU64(retList[0].Member.(string))
  1749. if uid == firstUid {
  1750. ackMsg.ScoreMvp = append(ackMsg.ScoreMvp, &serverproto.MvpRewardInfo{
  1751. Uid: uid,
  1752. RewardType: int32(rewardType),
  1753. })
  1754. battleBaseData.ScoreMvp = append(battleBaseData.ScoreMvp, uid)
  1755. }
  1756. }
  1757. }
  1758. //打包 击杀Mvp
  1759. if bKillerReward == false {
  1760. killerStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(index)) + "-" + strconv.Itoa(int(Rank_Type_Killer))
  1761. retList2, err4 := service.GetRedis().ZRevRangeWithScores(killerStr, int64(0), int64(0)).Result()
  1762. if err4 == nil && len(retList2) > 0 {
  1763. firstUid, _ := model.Str2NumU64(retList2[0].Member.(string))
  1764. if uid == firstUid {
  1765. ackMsg.KillerMvp = append(ackMsg.KillerMvp, &serverproto.MvpRewardInfo{
  1766. Uid: uid,
  1767. RewardType: int32(rewardType),
  1768. })
  1769. battleBaseData.KillerMvp = append(battleBaseData.KillerMvp, uid)
  1770. }
  1771. }
  1772. }
  1773. //保存
  1774. if mvpRound == this.guildBattleId {
  1775. battle, ok := this.battleIdxDataList[int32(index)]
  1776. if !ok {
  1777. return
  1778. }
  1779. battle.saveBattleBaseData()
  1780. } else {
  1781. err, msgStr := model.GetEncodeMessage(battleBaseData)
  1782. if err != nil {
  1783. return
  1784. }
  1785. keyStr := GuildBattlePrefix + strconv.Itoa(int(mvpRound)) + "-" + strconv.Itoa(int(index))
  1786. service.GetRedis().HSet(keyStr, "base", msgStr)
  1787. }
  1788. }
  1789. // 玩家上线查看当前领奖//最多打包最近的5场
  1790. func (this *GuildBattleManager) OnlineGetRoleGuildBattleReward(uid uint64, curRewardId, mvpRewardId int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) {
  1791. if ackMsg == nil || this.guildBattleState == 0 {
  1792. return
  1793. }
  1794. var beginRound int32 = curRewardId + 1
  1795. //计算领奖轮次//判定当前轮次是否有奖励可领取。有则领取,无则从(当前轮次-1)开始领取
  1796. bRet := this.CheckCurRewardRound(uid)
  1797. endRound := this.guildBattleId
  1798. if !bRet {
  1799. endRound = this.guildBattleId - 1
  1800. }
  1801. ackMsg.CurBattleRound = endRound
  1802. //获取公会战奖励
  1803. if endRound <= 0 || endRound < beginRound {
  1804. //这里暂不处理,不需要打包数据
  1805. } else {
  1806. //curRewardId 已经领取轮次
  1807. for round := endRound; beginRound <= round && (endRound-round <= 5); round-- {
  1808. for index := 1; index <= MaxBattleTeams; index++ {
  1809. //遍历七场战斗数据//查看有无该玩家。有则给奖励//从上到下,只要找到一场有该玩家的数据,则给这一档次的奖励
  1810. bRet := this.PackPlayerGuildBattleReward(uid, round, int32(index), ackMsg)
  1811. //找到奖励,当前轮就不在查找
  1812. if bRet == serverproto.ErrorCode_ERROR_OK {
  1813. break
  1814. }
  1815. }
  1816. }
  1817. }
  1818. //设定领奖轮次
  1819. mvpFinishRound := this.guildBattleId
  1820. mvpBeginRound := mvpRewardId
  1821. ackMsg.CurMvpRound = mvpFinishRound
  1822. if mvpFinishRound < mvpBeginRound {
  1823. return
  1824. }
  1825. //公会战MVP奖励
  1826. for mvpRound := mvpFinishRound; mvpBeginRound <= mvpRound && (mvpFinishRound-mvpRound <= 5) && mvpRound > 0; mvpRound-- {
  1827. for index := 1; index <= MaxBattleTeams; index++ {
  1828. this.PackPlayerGuildBattleMvpReward(uid, mvpRound, int32(index), ackMsg)
  1829. }
  1830. }
  1831. }
  1832. // 获取战场当前的获胜状态
  1833. func (this *GuildBattleManager) GetBattleWinState(battleIdx int32) (uint64, uint64, bool) {
  1834. this.initGuildBattleList()
  1835. battleData := this.getBattleByIdx(battleIdx)
  1836. if battleData == nil {
  1837. return 0, 0, false
  1838. }
  1839. return battleData.GetBattleWinState()
  1840. }
  1841. type GuildBattle struct {
  1842. updateTimer util.ServerTimer //主循环定时器
  1843. mag *GuildBattleManager
  1844. guildBattleIdx int32 //1-7中的第几场战斗
  1845. battleBaseData *serverproto.GuildBattleBaseData
  1846. scoreUpdateMs uint64
  1847. //参战玩家信息
  1848. playerDataList map[uint64]*serverproto.GuildBattleSelfData
  1849. //战斗日志
  1850. logList []*serverproto.GuildBattleLog
  1851. //占位信息
  1852. posIdxDataList map[int32]*serverproto.GuildPosIdxData
  1853. posIdxDataUidList map[uint64]*serverproto.GuildPosIdxData
  1854. changePosIdxList set.Interface
  1855. changePlayerDataList set.Interface
  1856. baseChange bool //积分变化
  1857. bPosInit bool
  1858. }
  1859. func newGuildBattle(mag *GuildBattleManager, battleIdx int32, guildId1, guildId2 uint64, startTime, endTime uint64) *GuildBattle {
  1860. battle := &GuildBattle{
  1861. playerDataList: map[uint64]*serverproto.GuildBattleSelfData{},
  1862. posIdxDataList: map[int32]*serverproto.GuildPosIdxData{},
  1863. posIdxDataUidList: map[uint64]*serverproto.GuildPosIdxData{},
  1864. changePosIdxList: set.New(set.NonThreadSafe),
  1865. changePlayerDataList: set.New(set.NonThreadSafe),
  1866. battleBaseData: &serverproto.GuildBattleBaseData{},
  1867. guildBattleIdx: battleIdx,
  1868. mag: mag,
  1869. }
  1870. if startTime != 0 && endTime != 0 {
  1871. battle.battleBaseData.BattleStartTime = startTime
  1872. battle.battleBaseData.BattleEndTime = endTime
  1873. }
  1874. battle.updateTimer = util.NewDurationTimer(util.GetCurrentTime(), 1000)
  1875. util.InfoF("GuildBattle battleIndex:%v guildId1=%v guildId2=%v startTime:%v, endTime:%v", battleIdx, guildId1, guildId2, startTime, endTime)
  1876. battle.init(guildId1, guildId2, startTime, endTime)
  1877. return battle
  1878. }
  1879. // 加载战斗数据(如果存在)
  1880. func (this *GuildBattle) init(guildId1, guildId2 uint64, startTime, endTime uint64) {
  1881. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  1882. retStr, err := service.GetRedis().HGet(battleKeyStr, "base").Result()
  1883. if err == nil {
  1884. err2 := model.GetDecodeMessage(this.battleBaseData, retStr)
  1885. if err2 != nil {
  1886. util.ErrorF("GuildBattle id=%v idx=%v base err", this.mag.guildBattleId, this.guildBattleIdx)
  1887. }
  1888. }
  1889. if len(this.battleBaseData.ScoreList) <= 0 {
  1890. this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
  1891. &serverproto.KeyValueType64{Key: guildId1}, &serverproto.KeyValueType64{Key: guildId2})
  1892. //初始化公会数据
  1893. for _, data := range this.battleBaseData.ScoreList {
  1894. guild := GuildMag.GetGuild(data.Key)
  1895. if guild != nil {
  1896. data.Value3 = guild.GuildBase.GuildBrief.GuildBadge
  1897. data.StrVal = guild.GuildBase.GuildBrief.GuildName
  1898. }
  1899. }
  1900. this.initGuildPlayerData(guildId1, guildId2)
  1901. this.saveBattleBaseData()
  1902. util.ErrorF("GuildBattle id=%v idx=%v init battle data ", this.mag.guildBattleId, this.guildBattleIdx)
  1903. } else {
  1904. util.InfoF("GuildBattle battleIndex 2 :%v guildId1=%v guildId2=%v startTime:%v, endTime:%v", this.guildBattleIdx, guildId1, guildId2, this.battleBaseData.BattleStartTime, this.battleBaseData.BattleEndTime)
  1905. }
  1906. this.initPosListInfo()
  1907. }
  1908. func (this *GuildBattle) saveBattleBaseData() {
  1909. err, msgStr := model.GetEncodeMessage(this.battleBaseData)
  1910. if err != nil {
  1911. return
  1912. }
  1913. keyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  1914. service.GetRedis().HSet(keyStr, "base", msgStr)
  1915. }
  1916. func (this *GuildBattle) saveBattlePlayerData() {
  1917. for _, val := range this.changePlayerDataList.List() {
  1918. uid := val.(uint64)
  1919. if playerData, ok := this.playerDataList[uid]; ok {
  1920. setGuildBattlePlayerDataToRedis(this.mag.guildBattleId, this.guildBattleIdx, playerData)
  1921. }
  1922. }
  1923. this.changePlayerDataList.Clear()
  1924. }
  1925. func (this *GuildBattle) update(ms uint64) {
  1926. if this.mag == nil || this.mag.banType == Guild_Battle_Baned {
  1927. return
  1928. }
  1929. if !this.updateTimer.IsStart() || !this.updateTimer.IsExpired(ms) {
  1930. return
  1931. }
  1932. //不在战斗中
  1933. if ms < this.battleBaseData.BattleStartTime || ms > this.battleBaseData.BattleEndTime {
  1934. return
  1935. }
  1936. //积分处理
  1937. this.updateScore(ms)
  1938. if this.baseChange {
  1939. this.baseChange = false
  1940. this.saveBattleBaseData()
  1941. }
  1942. this.saveBattlePlayerData()
  1943. for _, val := range this.changePosIdxList.List() {
  1944. posIdx := val.(int32)
  1945. if posIdxData, ok := this.posIdxDataList[posIdx]; ok {
  1946. setGuildBattlePosIdxDataToRedis(this.mag.guildBattleId, this.guildBattleIdx, posIdxData)
  1947. }
  1948. }
  1949. this.changePosIdxList.Clear()
  1950. }
  1951. func (this *GuildBattle) updateScore(ms uint64) {
  1952. if this.scoreUpdateMs < 0 {
  1953. this.scoreUpdateMs = ms
  1954. return
  1955. }
  1956. //5s
  1957. if this.scoreUpdateMs+model.GlobalGuildBattleRewardTime > ms {
  1958. return
  1959. }
  1960. this.scoreUpdateMs = ms
  1961. var scoreMap = map[uint64]int32{}
  1962. var playerScoreMap = map[uint64]uint32{}
  1963. for _, v := range this.posIdxDataList {
  1964. if warPoint, ok := model.ConvertGuildBattleList[v.PosIdx]; ok {
  1965. if v.GuildId > 0 {
  1966. scoreMap[v.GuildId] += warPoint
  1967. }
  1968. playerScoreMap[v.Uid] += uint32(warPoint)
  1969. }
  1970. }
  1971. for k, v := range scoreMap {
  1972. var scoreVal int32 = 0
  1973. bFind := false
  1974. for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
  1975. if this.battleBaseData.ScoreList[idx].Key == k {
  1976. this.battleBaseData.ScoreList[idx].Value += v
  1977. bFind = true
  1978. scoreVal = this.battleBaseData.ScoreList[idx].Value
  1979. break
  1980. }
  1981. }
  1982. if !bFind {
  1983. this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
  1984. &serverproto.KeyValueType64{Key: k, Value: v})
  1985. scoreVal = v
  1986. }
  1987. this.baseChange = true
  1988. //先到达胜利最大积分的为胜利队伍
  1989. if scoreVal >= model.GlobalGuildBattleWinMaxScore {
  1990. this.battleBaseData.BattleEndTime = ms
  1991. util.InfoF("GuildBattle updateScore id=%v idx=%v guildid=%v score=%v",
  1992. this.mag.guildBattleId, this.guildBattleIdx, k, scoreVal)
  1993. break
  1994. }
  1995. }
  1996. keyStr := GuildBattleRankPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" +
  1997. strconv.Itoa(int(this.guildBattleIdx)) + "-" + strconv.Itoa(int(RankScoreListType))
  1998. for k, v := range playerScoreMap {
  1999. if k == 0 || v == 0 {
  2000. continue
  2001. }
  2002. uidStr := strconv.FormatUint(k, 10)
  2003. oldScore, err := service.GetRedis().ZScore(keyStr, uidStr).Result()
  2004. if err == nil { //找到则变更数据
  2005. tmpScore := getValByGuildBattleScore(oldScore)
  2006. tmpScore += v
  2007. scoreStr := getguildBattleScore(uint64(tmpScore), ms)
  2008. util.InfoF("GuildBattle updateScore uid=%v addscore=%v score=%v", k, v, tmpScore)
  2009. service.GetRedis().ZAdd(keyStr, service.BaseZ{Score: float64(scoreStr), Member: uidStr})
  2010. } else if err == service.NIL { //未找到此人,则新插入一条记录
  2011. scoreStr := getguildBattleScore(uint64(v), ms)
  2012. service.GetRedis().ZAdd(keyStr, service.BaseZ{Score: float64(scoreStr), Member: uidStr})
  2013. }
  2014. }
  2015. }
  2016. func (this *GuildBattle) initGuildPlayerData(guildId1, guildId2 uint64) {
  2017. guild1 := GuildMag.GetGuild(guildId1)
  2018. if guild1 != nil {
  2019. for _, member := range guild1.GuildMember.MemberInfo {
  2020. playerData := &serverproto.GuildBattleSelfData{
  2021. Uid: member.MemberId,
  2022. GuildId: guildId1,
  2023. ChallengeNum: model.GlobalGuildBattleChallengeNum,
  2024. }
  2025. if member.Title == Guild_Title_Pre {
  2026. playerData.GuildPre = true
  2027. }
  2028. this.playerDataList[member.MemberId] = playerData
  2029. this.changePlayerDataList.Add(member.MemberId)
  2030. }
  2031. }
  2032. guild2 := GuildMag.GetGuild(guildId2)
  2033. if guild2 != nil {
  2034. for _, member := range guild2.GuildMember.MemberInfo {
  2035. playerData := &serverproto.GuildBattleSelfData{
  2036. Uid: member.MemberId,
  2037. GuildId: guildId2,
  2038. ChallengeNum: model.GlobalGuildBattleChallengeNum,
  2039. }
  2040. if member.Title == Guild_Title_Pre {
  2041. playerData.GuildPre = true
  2042. }
  2043. this.playerDataList[member.MemberId] = playerData
  2044. this.changePlayerDataList.Add(member.MemberId)
  2045. }
  2046. }
  2047. this.saveBattlePlayerData()
  2048. }
  2049. func (this *GuildBattle) getPlayerData(uid, guildId uint64) *serverproto.GuildBattleSelfData {
  2050. playerData, ok := this.playerDataList[uid]
  2051. if !ok {
  2052. //从数据库中加载
  2053. keyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2054. fieldStr := "uid:" + strconv.FormatUint(uid, 10)
  2055. retData, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
  2056. if err == nil {
  2057. playerData = &serverproto.GuildBattleSelfData{}
  2058. err2 := model.GetDecodeMessage(playerData, retData)
  2059. if err2 == nil {
  2060. this.playerDataList[uid] = playerData
  2061. util.ErrorF("GuildBattle id=%v idx=%v base err", this.mag.guildBattleId, this.guildBattleIdx)
  2062. } else {
  2063. playerData = nil
  2064. }
  2065. }
  2066. /*
  2067. if playerData == nil {
  2068. playerData = &serverproto.GuildBattleSelfData{
  2069. Uid: uid,
  2070. GuildId: guildId,
  2071. ChallengeNum: model.GlobalGuildBattleChallengeNum,
  2072. }
  2073. }
  2074. */
  2075. }
  2076. return playerData
  2077. }
  2078. func (this *GuildBattle) checkBattleFinish() bool {
  2079. nowTime := util.GetTimeMilliseconds()
  2080. //是否固定时间结束
  2081. if Guild_Battle_Tournament_Begin_Match <= this.guildBattleIdx && this.guildBattleIdx <= Guild_Battle_Tournament_End_Match {
  2082. if this.mag.tournamentEnd < nowTime {
  2083. return true
  2084. }
  2085. } else if Guild_Battle_Seminal_Begin_Match == this.guildBattleIdx || this.guildBattleIdx == Guild_Battle_Seminal_End_Match {
  2086. if this.mag.semifinalsEnd < nowTime {
  2087. return true
  2088. }
  2089. } else if Guild_Battle_Final_Match == this.guildBattleIdx {
  2090. if this.mag.finalEnd < nowTime {
  2091. return true
  2092. }
  2093. }
  2094. //是否固定时间结束
  2095. if this.battleBaseData.BattleEndTime < nowTime && this.battleBaseData.BattleEndTime > 0 {
  2096. return true
  2097. }
  2098. return false
  2099. }
  2100. func (this *GuildBattle) BattleInfoReq(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleInfoAck) {
  2101. //参赛者和观战者区分
  2102. bWatch := true
  2103. for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
  2104. if this.battleBaseData.ScoreList[idx].Key == guildId {
  2105. bWatch = false
  2106. break
  2107. }
  2108. }
  2109. if !bWatch {
  2110. playerData := this.getPlayerData(uid, guildId)
  2111. if playerData == nil {
  2112. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2113. return
  2114. }
  2115. ackMsg.SelfData = playerData
  2116. }
  2117. ackMsg.GuildBattleIdx = this.guildBattleIdx
  2118. ackMsg.BattleStartTime = this.battleBaseData.BattleStartTime
  2119. ackMsg.BattleEndTime = this.battleBaseData.BattleEndTime
  2120. posIdxData := this.getPosInfoByUid(uid)
  2121. if posIdxData != nil {
  2122. ackMsg.ChallengePosData = posIdxData
  2123. }
  2124. for _, countPart := range this.battleBaseData.ScoreList {
  2125. keyValue := &serverproto.KeyValueType64{
  2126. Key: countPart.Key,
  2127. Value: countPart.Value,
  2128. Value2: countPart.Value2,
  2129. Value3: countPart.Value3,
  2130. StrVal: countPart.StrVal,
  2131. }
  2132. ackMsg.BattleDataList = append(ackMsg.BattleDataList, keyValue)
  2133. }
  2134. }
  2135. func (this *GuildBattle) BattleSetting(uid, guildId uint64, autoChallenge, autoReborn bool, ackMsg *serverproto.SCGuildBattleSettingAck) {
  2136. playerData := this.getPlayerData(uid, guildId)
  2137. if playerData == nil {
  2138. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2139. return
  2140. }
  2141. playerData.AutoChallenge = autoChallenge
  2142. playerData.AutoReborn = autoReborn
  2143. this.changePlayerDataList.Add(uid)
  2144. ackMsg.SelfData = playerData
  2145. }
  2146. func (this *GuildBattle) BuyChallengeNum(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleBuyChallengeAck) {
  2147. playerData := this.getPlayerData(uid, guildId)
  2148. if playerData == nil {
  2149. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2150. return
  2151. }
  2152. if this.checkBattleFinish() {
  2153. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2154. return
  2155. }
  2156. this.changePlayerDataList.Add(uid)
  2157. ackMsg.SelfData = playerData
  2158. }
  2159. func (this *GuildBattle) BuyBattleBuff(uid, guildId uint64, buffList []int32, ackMsg *serverproto.SSGuildBattleBuyBuffAck) {
  2160. playerData := this.getPlayerData(uid, guildId)
  2161. if playerData == nil {
  2162. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2163. return
  2164. }
  2165. if this.checkBattleFinish() {
  2166. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2167. return
  2168. }
  2169. for _, data := range buffList {
  2170. bFind := false
  2171. for index, playerBuff := range playerData.BuffList {
  2172. if playerBuff.Key == data {
  2173. bFind = true
  2174. level := playerBuff.Value + 1
  2175. //判定表中是否存在level这一级的buff
  2176. buffData := model.GuildBattleBuffList[data]
  2177. if buffData == nil {
  2178. continue
  2179. }
  2180. //存在level级buff,判定金币够不够,不够直接回退
  2181. buyInfo := buffData.BuyData[level]
  2182. if buyInfo == nil {
  2183. continue
  2184. }
  2185. ackMsg.NeedRes = append(ackMsg.NeedRes, &serverproto.KeyValueType{
  2186. Key: buyInfo.Key,
  2187. Value: buyInfo.Value,
  2188. })
  2189. //设置
  2190. playerData.BuffList[index].Value = level
  2191. break
  2192. }
  2193. }
  2194. //没找到对应的BUFF,则新购买
  2195. if bFind == false {
  2196. buffData := model.GuildBattleBuffList[data]
  2197. if buffData == nil {
  2198. continue
  2199. }
  2200. //存在level级buff,判定金币够不够,不够直接回退
  2201. buyInfo := buffData.BuyData[1]
  2202. if buyInfo == nil {
  2203. continue
  2204. }
  2205. //购买一级的
  2206. playerData.BuffList = append(playerData.BuffList, &serverproto.KeyValueType{
  2207. Key: data,
  2208. Value: 1,
  2209. })
  2210. ackMsg.NeedRes = append(ackMsg.NeedRes, &serverproto.KeyValueType{
  2211. Key: buyInfo.Key,
  2212. Value: buyInfo.Value,
  2213. })
  2214. }
  2215. }
  2216. ackMsg.SelfData = playerData
  2217. this.changePlayerDataList.Add(uid)
  2218. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_OK)
  2219. }
  2220. func (this *GuildBattle) ResetRebornCD(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleRebornAck) {
  2221. playerData := this.getPlayerData(uid, guildId)
  2222. if playerData == nil {
  2223. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2224. return
  2225. }
  2226. nowTime := util.GetTimeMilliseconds()
  2227. if playerData.RebornEndTime <= nowTime {
  2228. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_HAS_REBORN)
  2229. return
  2230. }
  2231. playerData.RebornEndTime = 0
  2232. this.changePlayerDataList.Add(uid)
  2233. ackMsg.SelfData = playerData
  2234. }
  2235. func (this *GuildBattle) initPosListInfo() {
  2236. if this.bPosInit {
  2237. return
  2238. }
  2239. this.bPosInit = true
  2240. var guildPosIdxNum = map[uint64]int32{}
  2241. battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2242. for idx := 1; idx <= PosIdxMaxNum; idx++ {
  2243. fieldStr := "idx:" + strconv.Itoa(idx)
  2244. retData, err := service.GetRedis().HGet(battleKeyStr, fieldStr).Result()
  2245. if err != nil {
  2246. continue
  2247. }
  2248. posIdxData := &serverproto.GuildPosIdxData{}
  2249. err = model.GetDecodeMessage(posIdxData, retData)
  2250. if err != nil {
  2251. continue
  2252. }
  2253. this.posIdxDataList[int32(posIdxData.PosIdx)] = posIdxData
  2254. if posIdxData.OwnerBrief != nil {
  2255. this.posIdxDataUidList[posIdxData.OwnerBrief.Uid] = posIdxData
  2256. }
  2257. if posIdxData.GuildId > 0 {
  2258. guildPosIdxNum[posIdxData.GuildId]++
  2259. }
  2260. }
  2261. for k, v := range guildPosIdxNum {
  2262. bFind := false
  2263. for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
  2264. if this.battleBaseData.ScoreList[idx].Key == k {
  2265. //value=积分数量
  2266. //valu2=道场数量
  2267. this.battleBaseData.ScoreList[idx].Value2 = v
  2268. this.baseChange = true
  2269. bFind = true
  2270. break
  2271. }
  2272. }
  2273. if !bFind {
  2274. this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
  2275. &serverproto.KeyValueType64{Key: k, Value2: v})
  2276. this.baseChange = true
  2277. }
  2278. }
  2279. }
  2280. func (this *GuildBattle) getPosInfo(posIdx int32) *serverproto.GuildPosIdxData {
  2281. this.initPosListInfo()
  2282. posIdxData, ok := this.posIdxDataList[posIdx]
  2283. if ok {
  2284. return posIdxData
  2285. } else {
  2286. //无公会占领地块
  2287. posIdxData := &serverproto.GuildPosIdxData{
  2288. PosIdx: posIdx,
  2289. }
  2290. this.posIdxDataList[posIdx] = posIdxData
  2291. return posIdxData
  2292. }
  2293. }
  2294. func (this *GuildBattle) getPosInfoByUid(uid uint64) *serverproto.GuildPosIdxData {
  2295. this.initPosListInfo()
  2296. posIdxData, ok := this.posIdxDataUidList[uid]
  2297. if ok {
  2298. return posIdxData
  2299. }
  2300. return nil
  2301. }
  2302. func (this *GuildBattle) GetBuffTotalLevel(playerData *serverproto.GuildBattleSelfData) int32 {
  2303. if playerData == nil || len(playerData.BuffList) <= 0 {
  2304. return 0
  2305. }
  2306. totalLevel := int32(0)
  2307. for _, data := range playerData.BuffList {
  2308. totalLevel += data.Value
  2309. }
  2310. return totalLevel
  2311. }
  2312. func (this *GuildBattle) GetBattlePosIdxList(posIdxList []int32, ackMsg *serverproto.SCGuildBattlePosIdxListAck) {
  2313. if this.checkBattleFinish() {
  2314. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2315. return
  2316. }
  2317. for idx := 0; idx < len(posIdxList); idx++ {
  2318. if posIdxList[idx] <= 0 || posIdxList[idx] > int32(len(model.ConvertGuildBattleList)) {
  2319. continue
  2320. }
  2321. tmpPosIdxData := serverproto.GuildPosIdxData{}
  2322. posIdxData := this.getPosInfo(posIdxList[idx])
  2323. if posIdxData == nil {
  2324. continue
  2325. }
  2326. tmpPosIdxData = *posIdxData
  2327. //玩家不在该位置但是也也能属于该玩家
  2328. if posIdxData.OwnerBrief != nil {
  2329. posIdxData.OwnerBrief = model2.GetPlayerBriefInfoWithoutCache(posIdxData.OwnerBrief.Uid)
  2330. }
  2331. if posIdxData.OwnerBrief != nil {
  2332. var brief serverproto.CommonPlayerBriefInfo
  2333. brief = *posIdxData.OwnerBrief
  2334. playerData := this.getPlayerData(posIdxData.OwnerBrief.Uid, posIdxData.GuildId)
  2335. if playerData != nil {
  2336. totalLevel := this.GetBuffTotalLevel(playerData)
  2337. if totalLevel > 0 {
  2338. brief.FightPower += brief.FightPower * totalLevel * model.GlobalGuildBattleFightPower / 100
  2339. }
  2340. }
  2341. tmpPosIdxData.OwnerBrief = &brief
  2342. }
  2343. ackMsg.PosDataList = append(ackMsg.PosDataList, &tmpPosIdxData)
  2344. }
  2345. }
  2346. func (this *GuildBattle) GetPosIdxLog(beginTime uint64, ackMsg *serverproto.SCGuildBattleLogAck) {
  2347. /*
  2348. if this.checkBattleFinish() {
  2349. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2350. return
  2351. }
  2352. */
  2353. //加载日志
  2354. if len(this.logList) <= 0 {
  2355. keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2356. retList, err := service.GetRedis().LRange(keyStr, 0, -1).Result()
  2357. if err != nil {
  2358. return
  2359. }
  2360. for idx := 0; idx < len(retList); idx++ {
  2361. logData := &serverproto.GuildBattleLog{}
  2362. err = model.GetDecodeMessage(logData, retList[idx])
  2363. if err != nil {
  2364. continue
  2365. }
  2366. this.logList = append(this.logList, logData)
  2367. }
  2368. sort.Slice(this.logList, func(i, j int) bool {
  2369. return this.logList[i].RecordTime < this.logList[j].RecordTime
  2370. })
  2371. }
  2372. //获取最新列表
  2373. if beginTime <= 0 {
  2374. starIdx := len(this.logList)
  2375. if starIdx > 0 {
  2376. for idx := starIdx - 1; idx >= 0; idx-- {
  2377. ackMsg.LogList = append(ackMsg.LogList, this.logList[idx])
  2378. if len(ackMsg.LogList) >= 10 {
  2379. break
  2380. }
  2381. }
  2382. }
  2383. } else {
  2384. for idx := len(this.logList) - 1; idx >= 0; idx-- {
  2385. if this.logList[idx].RecordTime < beginTime {
  2386. ackMsg.LogList = append(ackMsg.LogList, this.logList[idx])
  2387. if len(ackMsg.LogList) >= 10 {
  2388. break
  2389. }
  2390. }
  2391. }
  2392. }
  2393. if len(ackMsg.LogList) < 10 {
  2394. ackMsg.IsEnd = true
  2395. }
  2396. }
  2397. func (this *GuildBattle) outPosIdx(bFight bool, uid uint64, nowTime uint64) bool {
  2398. posIdxData := this.getPosInfoByUid(uid)
  2399. if posIdxData == nil {
  2400. return false
  2401. }
  2402. if posIdxData.OwnerBrief != nil && posIdxData.OwnerBrief.Uid == uid {
  2403. if bFight {
  2404. posIdxData.GuildId = 0
  2405. posIdxData.Uid = 0
  2406. //因为被打败离开,才要清,如果是主动离开。很可能这块地已经存在战斗了。不能清战斗数据
  2407. posIdxData.FightEndTime = 0
  2408. posIdxData.FightStartTime = 0
  2409. posIdxData.ChallengeUid = 0
  2410. posIdxData.FightPingTime = 0
  2411. }
  2412. posIdxData.OwnerBrief = nil
  2413. this.changePosIdxList.Add(posIdxData.PosIdx)
  2414. delete(this.posIdxDataUidList, uid)
  2415. return true
  2416. }
  2417. return false
  2418. }
  2419. func (this *GuildBattle) inPosIdx(posIdxData *serverproto.GuildPosIdxData, uid, selfGuildId uint64, nowTime uint64) bool {
  2420. posIdxData.OwnerBrief = &serverproto.CommonPlayerBriefInfo{Uid: uid} //需要重新获取
  2421. posIdxData.FightEndTime = 0
  2422. posIdxData.FightStartTime = 0
  2423. posIdxData.ChallengeUid = 0
  2424. posIdxData.FightPingTime = 0
  2425. posIdxData.GuildId = selfGuildId
  2426. posIdxData.Uid = uid //当前地块标记为该玩家
  2427. this.posIdxDataUidList[uid] = posIdxData
  2428. this.changePosIdxList.Add(posIdxData.PosIdx)
  2429. return true
  2430. }
  2431. func (this *GuildBattle) BattleChallengePre(posIdx int32, selfGuildId uint64, bfInfo *serverproto.CommonPlayerBriefInfo,
  2432. curRmb uint64, buyNum int32, ssAckMsg *serverproto.SSGuildBattleChallengeAck) {
  2433. ssAckMsg.PosIdx = posIdx
  2434. ssAckMsg.GuildBattleIdx = this.guildBattleIdx
  2435. if this.checkBattleFinish() {
  2436. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
  2437. return
  2438. }
  2439. posIdxData := this.getPosInfo(posIdx)
  2440. if posIdxData == nil {
  2441. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2442. return
  2443. }
  2444. nowTime := util.GetTimeMilliseconds()
  2445. playerData := this.getPlayerData(bfInfo.Uid, selfGuildId)
  2446. if playerData == nil {
  2447. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_DAOCHANG100_NO_CHALLENGE_COUNT)
  2448. return
  2449. }
  2450. var costRmb uint64 = 0
  2451. //判断是否处于虚弱状态
  2452. if playerData.RebornEndTime > 0 && playerData.RebornEndTime > nowTime {
  2453. if playerData.AutoReborn {
  2454. if curRmb >= costRmb+model.GlobalGuildBattleRebornCost {
  2455. costRmb += model.GlobalGuildBattleRebornCost
  2456. playerData.RebornEndTime = 0
  2457. this.changePlayerDataList.Add(bfInfo.Uid)
  2458. ssAckMsg.CostRmb = costRmb
  2459. ssAckMsg.SelfData = playerData
  2460. } else {
  2461. playerData.AutoReborn = false
  2462. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_CHALLENGE_NO_RMB)
  2463. return
  2464. }
  2465. } else {
  2466. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_REBORN_TIME)
  2467. return
  2468. }
  2469. }
  2470. //当前正在战斗
  2471. if posIdxData.FightEndTime > 0 && posIdxData.FightEndTime > nowTime {
  2472. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FIGHTING)
  2473. ssAckMsg.ChallengePosData = posIdxData
  2474. return
  2475. }
  2476. //该玩家是否已经占领该位置
  2477. if posIdxData.OwnerBrief != nil && posIdxData.OwnerBrief.Uid == bfInfo.Uid {
  2478. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_HAS)
  2479. ssAckMsg.ChallengePosData = posIdxData
  2480. return
  2481. }
  2482. //同公会无法进行挑战
  2483. if posIdxData.OwnerBrief != nil && posIdxData.GuildId == selfGuildId {
  2484. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_SAME_GUILD)
  2485. ssAckMsg.ChallengePosData = posIdxData
  2486. return
  2487. }
  2488. //直接占领
  2489. if posIdxData.GuildId == 0 || posIdxData.OwnerBrief == nil {
  2490. //处理地块数量
  2491. //占领着地块自加
  2492. guildId := GuildMag.GetPlayerGuildId(bfInfo.Uid)
  2493. if posIdxData.GuildId == 0 || posIdxData.GuildId != 0 && posIdxData.GuildId != guildId {
  2494. //抵款自家
  2495. for idx, data := range this.battleBaseData.ScoreList {
  2496. if data.Key == guildId {
  2497. this.battleBaseData.ScoreList[idx].Value2++
  2498. }
  2499. }
  2500. }
  2501. //原有地块持有者地块自减
  2502. if posIdxData.GuildId != 0 && posIdxData.GuildId != guildId {
  2503. for idx, data := range this.battleBaseData.ScoreList {
  2504. if data.Key == posIdxData.GuildId {
  2505. this.battleBaseData.ScoreList[idx].Value2--
  2506. }
  2507. }
  2508. }
  2509. //离开玩家当前战力的地块(不在位置但还是属于该玩家所在公会)
  2510. this.outPosIdx(false, bfInfo.Uid, nowTime)
  2511. this.inPosIdx(posIdxData, bfInfo.Uid, selfGuildId, nowTime)
  2512. ssAckMsg.SelfData = playerData
  2513. ssAckMsg.ChallengePosData = posIdxData
  2514. } else {
  2515. //挑战次数判断//优先用购买次数
  2516. if buyNum > 0 {
  2517. ssAckMsg.UseBuyNum = true
  2518. util.InfoF("BattleChallengePre fight use buy num ")
  2519. } else if playerData.ChallengeNum > 0 {
  2520. playerData.ChallengeNum--
  2521. this.changePlayerDataList.Add(bfInfo.Uid)
  2522. util.InfoF("BattleChallengePre fight use init num ")
  2523. } else {
  2524. if playerData.AutoChallenge {
  2525. if curRmb < costRmb+model.GlobalGuildBattleBuyChallengeCost {
  2526. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_CHALLENGE_NO_RMB)
  2527. //todo wangzhaocan 挑战次数不够,并且金币也不够,取消自动购买
  2528. playerData.AutoChallenge = false
  2529. this.changePlayerDataList.Add(bfInfo.Uid)
  2530. return
  2531. }
  2532. //挑战消耗的金币(自动消耗)
  2533. costRmb += model.GlobalGuildBattleBuyChallengeCost
  2534. util.InfoF("BattleChallengePre fight use gold ")
  2535. } else {
  2536. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_DAOCHANG100_NO_CHALLENGE_COUNT)
  2537. return
  2538. }
  2539. }
  2540. util.InfoF("BattleChallengePre fight use buy num cost %v", costRmb)
  2541. ssAckMsg.SelfData = playerData
  2542. ssAckMsg.CostRmb = costRmb
  2543. //挑战玩家
  2544. if posIdxData.OwnerBrief != nil {
  2545. fightInfo := model2.GetRoleGuildBattleFightInfoWithoutCache(posIdxData.OwnerBrief.Uid)
  2546. if fightInfo != nil {
  2547. posIdxData.FightEndTime = nowTime + PosIdxFightMaxTime
  2548. posIdxData.FightStartTime = nowTime
  2549. posIdxData.ChallengeUid = bfInfo.Uid
  2550. posIdxData.FightPingTime = nowTime
  2551. this.changePosIdxList.Add(posIdxData.PosIdx)
  2552. ssAckMsg.FightInfo = fightInfo
  2553. //bufflist
  2554. fightPlayerData := this.getPlayerData(posIdxData.OwnerBrief.Uid, posIdxData.GuildId)
  2555. if fightPlayerData != nil {
  2556. ssAckMsg.FightBuffList = fightPlayerData.BuffList
  2557. }
  2558. ssAckMsg.SelfBuffList = playerData.BuffList
  2559. return
  2560. }
  2561. }
  2562. posIdxData.OwnerBrief = nil
  2563. posIdxData.FightEndTime = 0
  2564. posIdxData.FightStartTime = 0
  2565. posIdxData.GuildId = 0
  2566. posIdxData.ChallengeUid = 0
  2567. posIdxData.FightPingTime = 0
  2568. }
  2569. }
  2570. func (this *GuildBattle) BattleChallengePing(challengeUid uint64, posIdx int32, ssAckMsg *serverproto.SCGuildBattleChallengePingAck) {
  2571. if this.checkBattleFinish() {
  2572. return
  2573. }
  2574. posIdxData := this.getPosInfo(posIdx)
  2575. if posIdxData == nil {
  2576. return
  2577. }
  2578. //当前玩家没有挑战该位置
  2579. if posIdxData.ChallengeUid != challengeUid {
  2580. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
  2581. return
  2582. }
  2583. nowTime := util.GetTimeMilliseconds()
  2584. if posIdxData.FightPingTime > 0 && posIdxData.FightPingTime+60*1000 < nowTime {
  2585. //战斗无效
  2586. posIdxData.ChallengeUid = 0
  2587. posIdxData.FightPingTime = 0
  2588. posIdxData.FightStartTime = 0
  2589. posIdxData.FightEndTime = 0
  2590. this.changePosIdxList.Add(posIdxData.PosIdx)
  2591. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
  2592. return
  2593. }
  2594. posIdxData.FightPingTime = nowTime
  2595. posIdxData.FightEndTime = nowTime + PosIdxFightMaxTime
  2596. }
  2597. func (this *GuildBattle) battleResultValidCheck(selfUid uint64, selfFightPower int32, selfGuildId uint64, beChallengeFighter *serverproto.GuildPosIdxData) bool {
  2598. if beChallengeFighter.Uid <= 0 {
  2599. return true
  2600. }
  2601. if len(model.GlobalDaoChang100BattleCheck) >= 3 {
  2602. playerData := this.getPlayerData(selfUid, selfGuildId)
  2603. beChallengePlayerData := this.getPlayerData(beChallengeFighter.Uid, beChallengeFighter.GuildId)
  2604. if playerData == nil || beChallengePlayerData == nil {
  2605. //找不到对应的玩家数据,这个应该不让过了。
  2606. return false
  2607. }
  2608. var challengePower int32 = 0
  2609. //拉取一下最新的战力算了。。
  2610. tmpBf := model2.GetPlayerBriefInfoWithoutCache(beChallengeFighter.Uid)
  2611. if tmpBf != nil {
  2612. challengePower = tmpBf.FightPower
  2613. }
  2614. //进攻方的战力
  2615. playerTotalLevel := this.GetBuffTotalLevel(playerData)
  2616. if playerTotalLevel > 0 {
  2617. selfFightPower += selfFightPower * playerTotalLevel * model.GlobalGuildBattleFightPower / 100
  2618. }
  2619. //防守方的战力
  2620. beChallengeTotalLevel := this.GetBuffTotalLevel(beChallengePlayerData)
  2621. if beChallengeTotalLevel > 0 {
  2622. challengePower += challengePower * beChallengeTotalLevel * model.GlobalGuildBattleFightPower / 100
  2623. }
  2624. p1 := (float64(challengePower) - model.GlobalDaoChang100BattleCheck[0]) * model.GlobalDaoChang100BattleCheck[1]
  2625. p2 := float64(challengePower) * model.GlobalDaoChang100BattleCheck[2]
  2626. p := int32(math.Max(p1, p2) + 0.5)
  2627. util.InfoF("uid=%v battleResultValidCheckDaoChang100 selfFightPower=%v bechallengePower=%v", selfUid, selfFightPower, challengePower)
  2628. if selfFightPower <= p {
  2629. util.ErrorF("uid=%v battleResultValidCheckDaoChang100 selfFightPower=%v bechallengePower=%v invalid", selfUid, selfFightPower, challengePower)
  2630. return false
  2631. }
  2632. }
  2633. return true
  2634. }
  2635. func (this *GuildBattle) BattleChallengeResult(posIdx int32, challengeGuildId uint64, bfInfo *serverproto.CommonPlayerBriefInfo,
  2636. battleResult bool, ssAckMsg *serverproto.SCGuildBattleChallengeResultAck, ev rocommon.ProcEvent) {
  2637. if this.checkBattleFinish() {
  2638. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2639. return
  2640. }
  2641. posIdxData := this.getPosInfo(posIdx)
  2642. if posIdxData == nil {
  2643. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  2644. return
  2645. }
  2646. //当前位置的挑战者不是该玩家
  2647. if posIdxData.ChallengeUid != bfInfo.Uid {
  2648. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
  2649. return
  2650. }
  2651. targetPosIdxDataBfInfo := &serverproto.CommonPlayerBriefInfo{}
  2652. if posIdxData.OwnerBrief != nil {
  2653. err := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.OwnerBrief.Uid, targetPosIdxDataBfInfo)
  2654. if err == nil {
  2655. posIdxData.OwnerBrief = targetPosIdxDataBfInfo
  2656. }
  2657. }
  2658. //战力校验
  2659. if posIdxData.Uid > 0 && battleResult {
  2660. if !this.battleResultValidCheck(bfInfo.Uid, bfInfo.FightPower, challengeGuildId, posIdxData) {
  2661. battleResult = false
  2662. }
  2663. }
  2664. nowTime := util.GetTimeMilliseconds()
  2665. posIdxData.FightEndTime = 0
  2666. posIdxData.FightStartTime = 0
  2667. posIdxData.FightPingTime = 0
  2668. //挑战失败处理
  2669. if !battleResult {
  2670. if posIdxData.OwnerBrief != nil {
  2671. //添加占领日志
  2672. tmpSt := posIdxLogSt{
  2673. logType: 2,
  2674. posIdx: posIdx,
  2675. challengeGuild: challengeGuildId,
  2676. challengeNickName: bfInfo.NickName,
  2677. challengeUid: bfInfo.Uid,
  2678. posIdxUid: posIdxData.OwnerBrief.Uid,
  2679. posIdxGuild: posIdxData.GuildId,
  2680. }
  2681. if posIdxData.OwnerBrief.NickName != "" {
  2682. tmpSt.posIdxNickName = posIdxData.OwnerBrief.NickName
  2683. }
  2684. this.addPosIdxLog(tmpSt)
  2685. } else if posIdxData.Uid != 0 {
  2686. //添加占领日志
  2687. ownerBrief := &serverproto.CommonPlayerBriefInfo{}
  2688. err2 := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.Uid, ownerBrief)
  2689. if err2 == nil {
  2690. tmpSt := posIdxLogSt{
  2691. logType: 2,
  2692. posIdx: posIdx,
  2693. challengeGuild: challengeGuildId,
  2694. challengeNickName: bfInfo.NickName,
  2695. challengeUid: bfInfo.Uid,
  2696. posIdxUid: posIdxData.Uid,
  2697. posIdxGuild: posIdxData.GuildId,
  2698. }
  2699. if ownerBrief.NickName != "" {
  2700. tmpSt.posIdxNickName = ownerBrief.NickName
  2701. }
  2702. this.addPosIdxLog(tmpSt)
  2703. }
  2704. }
  2705. //进攻失败处理//进攻玩家清除连胜,重置复活时间
  2706. playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
  2707. if playerData != nil {
  2708. playerData.WinStrak = 0
  2709. playerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
  2710. this.changePlayerDataList.Add(bfInfo.Uid)
  2711. }
  2712. return
  2713. }
  2714. if posIdxData.OwnerBrief != nil {
  2715. //原有地块持有者地块自减
  2716. for idx, data := range this.battleBaseData.ScoreList {
  2717. if data.Key == posIdxData.GuildId {
  2718. this.battleBaseData.ScoreList[idx].Value2--
  2719. } else if data.Key == challengeGuildId {
  2720. this.battleBaseData.ScoreList[idx].Value2++
  2721. }
  2722. }
  2723. //挑战玩家
  2724. //老玩家离开占位
  2725. oldUid := posIdxData.OwnerBrief.Uid
  2726. oldUidNIckName := posIdxData.OwnerBrief.NickName
  2727. oldUidGuildId := posIdxData.GuildId
  2728. this.outPosIdx(true, oldUid, nowTime)
  2729. oldPlayerData := this.getPlayerData(oldUid, oldUidGuildId)
  2730. if oldPlayerData != nil {
  2731. /* 策划说,防守失败,不清连胜
  2732. if oldPlayerData.WinStrak > 0 {
  2733. oldPlayerData.WinStrak = 0
  2734. this.changePlayerDataList.Add(oldUid)
  2735. }
  2736. */
  2737. //被击杀玩家虚弱状态通知
  2738. oldPlayerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
  2739. ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
  2740. GuildBattleIdx: this.guildBattleIdx,
  2741. NtfUid: oldUid,
  2742. SelfData: oldPlayerData,
  2743. }
  2744. SendSocial(ntfMsg)
  2745. this.changePlayerDataList.Add(oldUid)
  2746. // model.ServiceReplay(ev, ntfMsg)
  2747. }
  2748. //当前挑战的玩家离开之前的占位
  2749. this.outPosIdx(false, bfInfo.Uid, nowTime)
  2750. //当前挑战的玩家进入挑战成功的占位
  2751. this.inPosIdx(posIdxData, bfInfo.Uid, challengeGuildId, nowTime)
  2752. //击杀次数记录
  2753. playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
  2754. if playerData != nil {
  2755. playerData.WinNum++
  2756. playerData.WinStrak++
  2757. this.changePlayerDataList.Add(bfInfo.Uid)
  2758. //玩家击杀排行榜
  2759. UpdateGuildBattleRankList_Kill(this.mag.guildBattleId, this.guildBattleIdx,
  2760. RankKillListType, playerData.WinNum, bfInfo.Uid)
  2761. //通知玩家自己
  2762. ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
  2763. GuildBattleIdx: this.guildBattleIdx,
  2764. NtfUid: bfInfo.Uid,
  2765. SelfData: playerData,
  2766. PosData: posIdxData,
  2767. }
  2768. model.ServiceReplay(ev, ntfMsg)
  2769. }
  2770. //添加占领日志
  2771. tmpSt := posIdxLogSt{
  2772. logType: 1,
  2773. posIdx: posIdx,
  2774. battleResult: true,
  2775. challengeGuild: challengeGuildId,
  2776. challengeNickName: bfInfo.NickName,
  2777. challengeUid: bfInfo.Uid,
  2778. posIdxUid: oldUid,
  2779. posIdxGuild: oldUidGuildId,
  2780. posIdxNickName: oldUidNIckName,
  2781. }
  2782. if playerData != nil && playerData.WinStrak > BattleWinNumTips {
  2783. tmpSt.WinSteak = playerData.WinStrak
  2784. }
  2785. this.addPosIdxLog(tmpSt)
  2786. } else {
  2787. util.InfoF("BattleChallengeResult posIdxData.OwnerBrief is nil uid: %v", posIdxData.Uid)
  2788. playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
  2789. if posIdxData.Uid != 0 && playerData != nil {
  2790. for idx, data := range this.battleBaseData.ScoreList {
  2791. if data.Key == posIdxData.GuildId {
  2792. this.battleBaseData.ScoreList[idx].Value2--
  2793. } else if data.Key == challengeGuildId {
  2794. this.battleBaseData.ScoreList[idx].Value2++
  2795. }
  2796. }
  2797. //找不到老玩家数据,非法战斗
  2798. brief := &serverproto.CommonPlayerBriefInfo{}
  2799. err2 := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.Uid, brief)
  2800. if err2 != nil {
  2801. playerData.ChallengeNum++
  2802. //您在战斗时已被他人击败,本场战斗无效,本次挑战无消耗
  2803. this.changePlayerDataList.Add(bfInfo.Uid)
  2804. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_RESULT_INVALID)
  2805. return
  2806. }
  2807. //挑战玩家
  2808. //老玩家离开占位
  2809. oldUid := posIdxData.Uid
  2810. oldUidNIckName := brief.NickName
  2811. oldUidGuildId := posIdxData.GuildId
  2812. oldPlayerData := this.getPlayerData(oldUid, oldUidGuildId)
  2813. if oldPlayerData != nil {
  2814. //被击杀玩家虚弱状态通知
  2815. oldPlayerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
  2816. ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
  2817. GuildBattleIdx: this.guildBattleIdx,
  2818. NtfUid: oldUid,
  2819. SelfData: oldPlayerData,
  2820. }
  2821. SendSocial(ntfMsg)
  2822. this.changePlayerDataList.Add(oldUid)
  2823. }
  2824. //当前挑战的玩家离开之前的占位
  2825. this.outPosIdx(false, bfInfo.Uid, nowTime)
  2826. //当前挑战的玩家进入挑战成功的占位
  2827. this.inPosIdx(posIdxData, bfInfo.Uid, challengeGuildId, nowTime)
  2828. //击杀次数记录
  2829. playerData.WinNum++
  2830. playerData.WinStrak++
  2831. this.changePlayerDataList.Add(bfInfo.Uid)
  2832. //玩家击杀排行榜
  2833. UpdateGuildBattleRankList_Kill(this.mag.guildBattleId, this.guildBattleIdx,
  2834. RankKillListType, playerData.WinNum, bfInfo.Uid)
  2835. //通知玩家自己
  2836. ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
  2837. GuildBattleIdx: this.guildBattleIdx,
  2838. NtfUid: bfInfo.Uid,
  2839. SelfData: playerData,
  2840. PosData: posIdxData,
  2841. }
  2842. model.ServiceReplay(ev, ntfMsg)
  2843. //添加占领日志
  2844. tmpSt := posIdxLogSt{
  2845. logType: 1,
  2846. posIdx: posIdx,
  2847. battleResult: true,
  2848. challengeGuild: challengeGuildId,
  2849. challengeNickName: bfInfo.NickName,
  2850. challengeUid: bfInfo.Uid,
  2851. posIdxUid: oldUid,
  2852. posIdxGuild: oldUidGuildId,
  2853. posIdxNickName: oldUidNIckName,
  2854. }
  2855. if playerData.WinStrak > BattleWinNumTips {
  2856. tmpSt.WinSteak = playerData.WinStrak
  2857. }
  2858. this.addPosIdxLog(tmpSt)
  2859. return
  2860. }
  2861. //挑战成功但是该位置已经先被其他玩家占领
  2862. //本次挑战无效
  2863. if playerData != nil {
  2864. playerData.ChallengeNum++
  2865. //您在战斗时已被他人击败,本场战斗无效,本次挑战无消耗
  2866. this.changePlayerDataList.Add(bfInfo.Uid)
  2867. ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_RESULT_INVALID)
  2868. }
  2869. }
  2870. }
  2871. func (this *GuildBattle) BattlePKData(ssAckMsg *serverproto.SCGuildBattlePKDataAck) {
  2872. ssAckMsg.BattleDataList = this.battleBaseData.ScoreList
  2873. }
  2874. func (this *GuildBattle) GetBattleWinState() (uint64, uint64, bool) {
  2875. scoreList := this.battleBaseData.ScoreList
  2876. if len(scoreList) == 2 { //满编队伍
  2877. if scoreList[1].Key == 0 {
  2878. return scoreList[0].Key, 0, true
  2879. }
  2880. }
  2881. if !this.checkBattleFinish() {
  2882. //战斗未结束无法获取胜利/失败状态
  2883. return 0, 0, false
  2884. }
  2885. if len(scoreList) == 2 { //满编队伍
  2886. if scoreList[0].Value == scoreList[1].Value {
  2887. if scoreList[0].Value2 == scoreList[0].Value2 {
  2888. return scoreList[0].Key, scoreList[1].Key, true
  2889. } else if scoreList[0].Value2 < scoreList[0].Value2 {
  2890. return scoreList[0].Key, scoreList[1].Key, true
  2891. } else {
  2892. return scoreList[1].Key, scoreList[0].Key, true
  2893. }
  2894. } else if scoreList[0].Value > scoreList[1].Value {
  2895. return scoreList[0].Key, scoreList[1].Key, true
  2896. } else {
  2897. return scoreList[1].Key, scoreList[0].Key, true
  2898. }
  2899. }
  2900. return 0, 0, false
  2901. }
  2902. type posIdxLogSt struct {
  2903. logType int32
  2904. posIdx int32
  2905. battleResult bool
  2906. challengeUid uint64
  2907. challengeNickName string
  2908. challengeGuild uint64
  2909. posIdxUid uint64
  2910. posIdxNickName string
  2911. posIdxGuild uint64
  2912. WinSteak int32 //连杀次数
  2913. }
  2914. // =1、{0}公会{1}击败了{2}公会的{3}的防守。{4}连杀!
  2915. // =2、{0}公会{1}成功防守了{2}公会{3}的进攻。
  2916. func (this *GuildBattle) addPosIdxLog(st posIdxLogSt) {
  2917. logData := &serverproto.GuildBattleLog{
  2918. Type: st.logType,
  2919. PosIdx: st.posIdx,
  2920. }
  2921. if st.battleResult {
  2922. logData.State = true
  2923. }
  2924. logData.RecordTime = util.GetTimeMilliseconds()
  2925. logData.ChallengePlayerUid = st.challengeUid
  2926. logData.ChallengeGuildId = st.challengeGuild
  2927. logData.ChallengePlayerName = st.challengeNickName
  2928. logData.TargetPlayerUid = st.posIdxUid
  2929. logData.TargetGuildId = st.posIdxGuild
  2930. logData.TargetPlayerName = st.posIdxNickName
  2931. logData.WinSteak = st.WinSteak
  2932. //加载日志
  2933. keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2934. if len(this.logList) <= 0 {
  2935. //keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
  2936. retList, err := service.GetRedis().LRange(keyStr, 0, -1).Result()
  2937. if err != nil {
  2938. return
  2939. }
  2940. for idx := 0; idx < len(retList); idx++ {
  2941. logData := &serverproto.GuildBattleLog{}
  2942. err = model.GetDecodeMessage(logData, retList[idx])
  2943. if err != nil {
  2944. continue
  2945. }
  2946. this.logList = append(this.logList, logData)
  2947. }
  2948. sort.Slice(this.logList, func(i, j int) bool {
  2949. return this.logList[i].RecordTime < this.logList[j].RecordTime
  2950. })
  2951. }
  2952. this.logList = append(this.logList, logData)
  2953. if len(this.logList) > int(MaxUidLogCount) {
  2954. this.logList = append(this.logList[:0], this.logList[1:]...)
  2955. }
  2956. err, msgStr := model.GetEncodeMessage(logData)
  2957. if err != nil {
  2958. return
  2959. }
  2960. ret, err1 := service.GetRedis().LPush(keyStr, msgStr).Result()
  2961. if err1 == nil && ret > MaxUidLogCount {
  2962. service.GetRedis().LTrim(keyStr, 0, MaxUidLogCount-1)
  2963. }
  2964. }
  2965. func (this *GuildBattle) GetCountPartData(ackMsg *serverproto.SCGuildBattleRankListAck) {
  2966. if ackMsg == nil {
  2967. return
  2968. }
  2969. winGuild, _, ok := this.GetBattleWinState()
  2970. if ok {
  2971. if winGuild == this.battleBaseData.ScoreList[0].Key {
  2972. ackMsg.WinGuild = true
  2973. } else {
  2974. ackMsg.WinGuild = false
  2975. }
  2976. ackMsg.IsFinish = true
  2977. } else {
  2978. ackMsg.IsFinish = false
  2979. }
  2980. for _, countPart := range this.battleBaseData.ScoreList {
  2981. keyValue := &serverproto.KeyValueType64{
  2982. Key: countPart.Key,
  2983. Value: countPart.Value,
  2984. Value2: countPart.Value2,
  2985. Value3: countPart.Value3,
  2986. StrVal: countPart.StrVal,
  2987. }
  2988. ackMsg.BattleDataList = append(ackMsg.BattleDataList, keyValue)
  2989. }
  2990. }
  2991. func (this *GuildBattle) ExchangeGuildPre(oldPreUid, curPreUid, guildId uint64) {
  2992. oldPrePlayer := this.getPlayerData(oldPreUid, guildId)
  2993. curPrePlayer := this.getPlayerData(curPreUid, guildId)
  2994. if oldPrePlayer == nil || curPrePlayer == nil {
  2995. return
  2996. }
  2997. oldPrePlayer.GuildPre = false
  2998. curPrePlayer.GuildPre = true
  2999. this.changePlayerDataList.Add(oldPrePlayer.Uid)
  3000. this.changePlayerDataList.Add(curPrePlayer.Uid)
  3001. }