| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314 |
- package model
- import (
- "encoding/base64"
- "math"
- "rocommon"
- "rocommon/service"
- "rocommon/util"
- "roserver/baseserver/model"
- "roserver/baseserver/set"
- model2 "roserver/db/model"
- "roserver/serverproto"
- "sort"
- "strconv"
- )
- func getDaoChang100GuildScore(val uint64, passTime uint64) uint64 {
- //score
- var scoreStr = val
- if scoreStr >= (1 << 28) {
- scoreStr = (1 << 28) - 1
- }
- scoreStr <<= 36
- if 0xfffffffff > passTime {
- scoreStr |= 0xfffffffff - passTime
- } else {
- scoreStr |= 0xfffffffff - passTime/10000
- }
- return scoreStr
- }
- func getValByDaoChang100GuildScore(score float64) uint32 {
- val := uint32(uint64(score) >> 36)
- return val
- }
- func UpdateDaoChang100GuildRank(uid uint64, guildId uint64, score uint32) {
- if uid <= 0 || guildId <= 0 || score <= 0 {
- return
- }
- // guildIdStr := strconv.FormatUint(uid, 10)
- guildIdStr := strconv.FormatUint(guildId, 10)
- tmpScore, err := service.GetRedis().ZScore(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
- if err != nil && err != service.NIL {
- util.ErrorF("uid=%v UpdateDaoChang100GuildRank guildid=%v score=%v err=%v", uid, guildId, score, err)
- return
- }
- if err == service.NIL {
- scoreStr := getDaoChang100GuildScore(uint64(score), util.GetTimeMilliseconds())
- _, err = service.GetRedis().ZAdd(model.RoleDaoChang100GuildRankPrefix, service.BaseZ{Score: float64(scoreStr), Member: guildIdStr}).Result()
- return
- }
- oldScore := getValByDaoChang100GuildScore(tmpScore)
- var maxScore uint32 = 1<<28 - 1
- if oldScore > 0 && oldScore < maxScore {
- score += oldScore
- if score > maxScore {
- score = maxScore
- }
- scoreStr := getDaoChang100GuildScore(uint64(score), util.GetTimeMilliseconds())
- _, err = service.GetRedis().ZAdd(model.RoleDaoChang100GuildRankPrefix, service.BaseZ{Score: float64(scoreStr), Member: guildIdStr}).Result()
- if err != nil {
- util.ErrorF("uid=%v UpdateDaoChang100GuildRank guildid=%v score=%v err=%v", uid, guildId, score, err)
- }
- }
- }
- func backUpDaoChang100GuildRank(guildBattleId int32) {
- battleIdStr := strconv.Itoa(int(guildBattleId))
- prefixStr := model.RoleDaoChang100GuildRankPrefix + battleIdStr
- _, err := service.GetRedis().ZUnionStore(prefixStr, service.BaseStore{}, model.RoleDaoChang100GuildRankPrefix).Result()
- if err != nil {
- util.ErrorF("backUpArenaRankData store rank record err=%v prefixStr=%v", err, prefixStr)
- return
- }
- }
- // 清除掉排行版数据
- func ClearDaoChang100GuildRank() {
- tempCount, err := service.GetRedis().ZCard(model.RoleDaoChang100GuildRankPrefix).Result()
- if err != nil {
- util.ErrorF("ClearDaoChang100GuildRank get tempCount err:%v", err)
- return
- }
- if tempCount <= 0 {
- return
- }
- _, err2 := service.GetRedis().ZRemRangeByRank(model.RoleDaoChang100GuildRankPrefix, 0, tempCount-1).Result()
- if err2 != nil {
- util.ErrorF("[ClearDaoChang100GuildRank] remove RoleDaoChang100GuildRankPrefix err:%v uid:%v", err)
- return
- }
- }
- const (
- Guild_Battle_State_Prepare = 1
- Guild_Battle_State_Fight = 2
- Guild_Battle_State_Finish = 3
- )
- // 删除这个公会在公会对阵表中的排名
- func OnDisbandGuild(guildId uint64) {
- guildIdStr := strconv.FormatUint(guildId, 10)
- service.GetRedis().ZRem(model.RoleDaoChang100GuildRankPrefix, guildIdStr)
- }
- func GetSelfGuildBattleData(guildId uint64, ackMsg *serverproto.SCGetSelfGuildInfoAck) {
- guildIdStr := strconv.FormatUint(guildId, 10)
- tmpScore, err2 := service.GetRedis().ZScore(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
- if err2 == nil {
- oldScore := getValByDaoChang100GuildScore(tmpScore)
- ackMsg.CpNum = oldScore
- }
- selfRank, rankErr := service.GetRedis().ZRevRank(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
- if rankErr == nil {
- ackMsg.GuildBattleRank = uint32(selfRank + 1)
- }
- if GuildBattleMag.banType == Guild_Battle_Baned {
- ackMsg.NextStateBegin = GuildBattleMag.nextGuildBattleBegin
- ackMsg.GuildBattleState = Guild_Battle_State_Prepare
- return
- }
- curTime := util.GetCurrentTime()
- if curTime < GuildBattleMag.curGuildBattleBegin {
- //准备阶段
- ackMsg.NextStateBegin = GuildBattleMag.curGuildBattleBegin
- ackMsg.GuildBattleState = Guild_Battle_State_Prepare
- } else if curTime < GuildBattleMag.finalEnd {
- //战斗阶段
- ackMsg.NextStateBegin = GuildBattleMag.finalEnd
- ackMsg.GuildBattleState = Guild_Battle_State_Fight
- } else if curTime < GuildBattleMag.showEnd {
- //结束(展示阶段)
- ackMsg.NextStateBegin = GuildBattleMag.showEnd
- ackMsg.GuildBattleState = Guild_Battle_State_Finish
- } else if curTime > GuildBattleMag.showEnd {
- //准备阶段
- ackMsg.NextStateBegin = GuildBattleMag.nextGuildBattleBegin
- ackMsg.GuildBattleState = Guild_Battle_State_Prepare
- }
- }
- const (
- //公会战战场相关数据
- GuildBattlePrefix = "guildbattle:" //key=guildbattle:1-1 field=uid:xxxxx
- GuildBattleRankPrefix = "guildbattle:rank:"
- GuildBattleLogPrefix = "guildbattle:log:"
- GuildBattleBasePrefix = "guildbattle:base:" //公会管理器战当前战斗数据
- GuildBattleTablePrefix = "guildbattle:table:" //公会管理器当前对阵表
- RankScoreListType = 1 //积分排行榜
- RankKillListType = 2 //击杀排行榜
- PosIdxMaxNum = 55
- PosIdxFightMaxTime = 60*1000 + 10 //1分钟
- MaxUidLogCount int64 = 100
- BattleWinNumTips = 3 //击杀超过3次
- PrepareTime = 10 * 60 * 1000
- )
- func getguildBattleScore(val uint64, passTime uint64) uint64 {
- //score
- var scoreStr = val
- if scoreStr >= (1 << 28) {
- scoreStr = (1 << 28) - 1
- }
- scoreStr <<= 36
- if 0xfffffffff > passTime {
- scoreStr |= 0xfffffffff - passTime
- } else {
- scoreStr |= 0xfffffffff - passTime/10000
- }
- return scoreStr
- }
- func getValByGuildBattleScore(score float64) uint32 {
- val := uint32(uint64(score) >> 36)
- return val
- }
- func setGuildBattlePlayerDataToRedis(battleId, battleIdx int32, playerData *serverproto.GuildBattleSelfData) {
- err, msgStr := model.GetEncodeMessage(playerData)
- if err != nil {
- return
- }
- //uidStr := strconv.FormatUint(playerData.Uid, 10)
- keyStr := GuildBattlePrefix + strconv.Itoa(int(battleId)) + "-" + strconv.Itoa(int(battleIdx))
- fieldStr := "uid:" + strconv.FormatUint(playerData.Uid, 10)
- service.GetRedis().HSet(keyStr, fieldStr, msgStr)
- }
- func setGuildBattlePosIdxDataToRedis(battleId, battleIdx int32, posIdxData *serverproto.GuildPosIdxData) {
- err, msgStr := model.GetEncodeMessage(posIdxData)
- if err != nil {
- return
- }
- //[prefix][battleId]-[battleIdx]
- battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(battleId)) + "-" + strconv.Itoa(int(battleIdx))
- fieldStr := "idx:" + strconv.Itoa(int(posIdxData.PosIdx))
- service.GetRedis().HSet(battleKeyStr, fieldStr, msgStr)
- }
- func GetGuildBattleRankList(battleId, battleIdx int32, pagIdx, rankType int32, ackMsg *serverproto.SCGuildBattleRankListAck) {
- //[prefix][battleId]-[battleIdx]-[rankType]
- keyStr := GuildBattleRankPrefix + strconv.Itoa(int(battleId)) + "-" + strconv.Itoa(int(battleIdx)) + "-" + strconv.Itoa(int(rankType))
- if pagIdx <= 0 || pagIdx > 20 {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- if rankType != RankScoreListType && rankType != RankKillListType {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- endIdx := pagIdx * 5
- startIdx := endIdx - 5
- endIdx -= 1
- retList, err := service.GetRedis().ZRevRangeWithScores(keyStr, int64(startIdx), int64(endIdx)).Result()
- if err != nil {
- return
- }
- GuildBattleMag.initGuildBattleList()
- battleData := GuildBattleMag.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- battleData.GetCountPartData(ackMsg)
- for idx := 0; idx < len(retList); idx++ {
- uid, _ := model.Str2NumU64(retList[idx].Member.(string))
- score := getValByGuildBattleScore(retList[idx].Score)
- brief := &serverproto.CommonPlayerBriefInfo{}
- err2 := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, uid, brief)
- if err2 != nil {
- continue
- }
- guildId := GuildMag.GetPlayerGuildId(uid)
- ackMsg.RankDataList = append(ackMsg.RankDataList, &serverproto.GuildBattleRank{
- Score: int32(score),
- Rank: int32(idx) + (startIdx + 1),
- Info: brief,
- GuildId: guildId,
- })
- }
- }
- func UpdateGuildBattleRankList_Kill(battleId, battleIdx, rankType int32, score int32, uid uint64) {
- if uid == 0 || score == 0 {
- return
- }
- //[prefix][battleId]-[battleIdx]-[rankType]
- keyStr := GuildBattleRankPrefix + strconv.Itoa(int(battleId)) + "-" + strconv.Itoa(int(battleIdx)) + "-" + strconv.Itoa(int(rankType))
- uidStr := strconv.FormatUint(uid, 10)
- scoreStr := getguildBattleScore(uint64(score), util.GetTimeMilliseconds())
- _, err := service.GetRedis().ZAdd(keyStr, service.BaseZ{Score: float64(scoreStr), Member: uidStr}).Result()
- if err != nil {
- util.ErrorF("uid=%v UpdateGuildBattleRankList score=%v err=%v", uid, score, err)
- }
- }
- // 上线调用
- func (this *GuildBattleManager) IsInBattleTime() bool {
- if this.banType == 1 {
- return false
- }
- if Guild_Battle_Start <= this.guildBattleState && this.guildBattleState < Guild_Battle_End {
- return true
- }
- return false
- }
- // 公会相关判定 公会是否在公会战中
- func (this *GuildBattleManager) IsInGuildBattle(guildId uint64) (int32, bool) {
- if this.guildBattleId == 0 {
- return 0, false
- }
- //当前不在公会战时间内
- if this.guildBattleState < Guild_Battle_Start || this.guildBattleState > Guild_Battle_Final {
- return 0, false
- }
- for idx, data := range this.battleIdxDataList {
- if data == nil || len(this.battleIdxDataList[idx].battleBaseData.ScoreList) <= 0 {
- continue
- }
- //公会是否在公会战三个阶段
- if Guild_Battle_Start <= this.guildBattleState && this.guildBattleState < Guild_Battle_OffMatch_TS {
- if idx < Guild_Battle_Tournament_Begin_Match || idx > Guild_Battle_Tournament_End_Match {
- continue
- }
- for _, matchGuild := range this.battleIdxDataList[idx].battleBaseData.ScoreList {
- if matchGuild.Key == guildId {
- return idx, true
- }
- }
- } else if Guild_Battle_OffMatch_TS <= this.guildBattleState && this.guildBattleState < Guild_Battle_OffMatch_SF {
- if idx < Guild_Battle_Seminal_Begin_Match || idx > Guild_Battle_Seminal_End_Match {
- continue
- }
- for _, matchGuild := range this.battleIdxDataList[idx].battleBaseData.ScoreList {
- if matchGuild.Key == guildId {
- return idx, true
- }
- }
- } else if Guild_Battle_OffMatch_SF <= this.guildBattleState && this.guildBattleState < Guild_Battle_Result {
- if idx != Guild_Battle_Final_Match {
- continue
- }
- for _, matchGuild := range this.battleIdxDataList[idx].battleBaseData.ScoreList {
- if matchGuild.Key == guildId {
- return idx, true
- }
- }
- }
- }
- return 0, false
- }
- const (
- Guild_Battle_Final_Match = 1
- Guild_Battle_Seminal_Begin_Match = 2
- Guild_Battle_Seminal_End_Match = 3
- Guild_Battle_Tournament_Begin_Match = 4
- Guild_Battle_Tournament_End_Match = 7
- )
- const (
- Guild_Battle_Reward_Final = 1 // 决赛奖励
- Guild_Battle_Reward_Semifinals = 2 // 半决赛奖励
- Guild_Battle_Reward_Tournament = 3 // 四强赛奖励
- )
- const (
- Guild_Battle_None = 0 //周二五点-周六五点
- Guild_Battle_Start = 1 //公会战阶段 周六五点-四强赛准备时间前
- Guild_Battle_Tournament_Prepare = 2 //公会战阶段 五点-四强赛准备时间
- Guild_Battle_Tournament = 3 //公会战阶段 四强(8-4)战斗
- Guild_Battle_OffMatch_TS = 4 //公会战阶段 四强-半决赛 休赛
- Guild_Battle_Semifinals_Prepare = 5 //公会战阶段 半决赛赛准备时间
- Guild_Battle_Semifinals = 6 //公会战阶段 半决(4-2)赛战斗
- Guild_Battle_OffMatch_SF = 7 //公会战阶段 半决赛-决赛 休赛
- Guild_Battle_Final_Prepare = 8 //公会战阶段 决赛准备时间
- Guild_Battle_Final = 9 //公会战阶段 决赛战斗
- Guild_Battle_Result = 10 //榜单展示阶段 周天9.30-周二5.00
- Guild_Battle_End = 11 //公会战结束
- )
- type GuildBattleManager struct {
- bInit bool
- operator int32
- isOpen int32
- //当前公会战信息
- guildBattleId int32 //当前是第几次公会战
- curGuildBattleBegin uint64 //当前公会战开始时间
- guildBattleState int32 //公会战状态
- nextStateBegin uint64 //下一阶段开始时间
- rewardState int32 //奖励发送阶段
- banType int32 //封停类型
- //四强赛时间
- tournamentPrepare uint64
- tournamentBegin uint64
- tournamentEnd uint64
- //半决赛时间
- semifinalsPrepare uint64
- semifinalsBegin uint64
- semifinalsEnd uint64
- //决赛时间
- finalPrepare uint64
- finalBegin uint64
- finalEnd uint64
- showEnd uint64
- championTeam uint64
- nextGuildBattleBegin uint64 //获取下一轮公会战开始时间
- countPartTable *serverproto.GuildCountPartTable
- updateTimer util.ServerTimer //主循环定时器
- battleIdxDataList map[int32]*GuildBattle //当前对应的7场战斗
- }
- func newGuildBattleManager() *GuildBattleManager {
- mag := &GuildBattleManager{
- battleIdxDataList: map[int32]*GuildBattle{},
- countPartTable: &serverproto.GuildCountPartTable{},
- banType: 0,
- }
- mag.updateTimer = util.NewDurationTimer(util.GetCurrentTime(), 200)
- return mag
- }
- func (this *GuildBattleManager) Update(ms uint64) {
- if !this.updateTimer.IsStart() || !this.updateTimer.IsExpired(ms) {
- return
- }
- for _, val := range this.battleIdxDataList {
- val.update(ms)
- }
- //初始化做完才能走Tick流程
- if this.bInit == true {
- this.SwitchGuildBattleState()
- } else {
- this.initGuildBattleList()
- util.InfoF("initGuildBattleList init")
- }
- }
- // 服务器初始化操作
- func (this *GuildBattleManager) initGuildBattleList() {
- if this.bInit {
- return
- }
- this.bInit = true
- err := this.loadGuildBattleBaseFromRedis()
- if err != nil {
- //加载失败
- if err == service.NIL {
- util.InfoF("init loadGuildBattleBaseFromRedis empty")
- } else {
- util.InfoF("init loadGuildBattleBaseFromRedis failed")
- }
- }
- //初始化battleIdxDataList
- err2 := this.loadCountTableFromRedis()
- if err2 != nil {
- if err2 == service.NIL {
- util.InfoF("init loadCountTableFromRedis empty")
- } else {
- util.InfoF("init loadCountTableFromRedis failed")
- }
- }
- //初始对阵表结构
- if len(this.countPartTable.Data) > 0 {
- for _, data := range this.countPartTable.Data {
- this.battleIdxDataList[data.BattleIndex] = newGuildBattle(this, data.BattleIndex, data.Guild1Id, data.Guild2Id, 0, 0)
- }
- }
- }
- // 对阵表存储
- func (this *GuildBattleManager) saveCountTableToRedis() error {
- keyStr := GuildBattleTablePrefix + "-" + strconv.Itoa(int(this.guildBattleId))
- msgData, err := rocommon.GetCodec().Marshal(this.countPartTable)
- if err != nil {
- return err
- }
- msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
- ret, err2 := service.GetRedis().Set(keyStr, msgStr, 0).Result()
- if err2 != nil {
- util.InfoF("saveCountTableToRedis err=%v ret=%v", keyStr, ret)
- return err2
- }
- util.InfoF("saveCountTableToRedis save countPartTable success : %v", this.countPartTable)
- return nil
- }
- // 对阵表加载
- func (this *GuildBattleManager) loadCountTableFromRedis() error {
- keyStr := GuildBattleTablePrefix + "-" + strconv.Itoa(int(this.guildBattleId))
- ret, err := service.GetRedis().Get(keyStr).Result()
- if err != nil {
- util.InfoF("loadCountTableFromRedis get guildId key=%v err=%v\n", keyStr, err)
- return err
- }
- if ret == "" {
- util.InfoF("loadCountTableFromRedis key(%v)->val empty", keyStr)
- return err
- }
- str, err := base64.StdEncoding.DecodeString(ret)
- if err != nil {
- util.InfoF("loadCountTableFromRedis key(%v)->val DecodeString error", keyStr)
- return err
- }
- countTable := &serverproto.GuildCountPartTable{}
- err = rocommon.GetCodec().Unmarshal(str, countTable)
- if err == nil {
- this.countPartTable = countTable
- util.InfoF("loadCountTableFromRedis success:%v", countTable)
- return nil
- }
- util.InfoF("loadCountTableFromRedis failed")
- return err
- }
- // 公会战基础信息,只要有变动就保存下来
- func (this *GuildBattleManager) saveGuildBattleBaseToRedis() error {
- keyStr := GuildBattleBasePrefix
- guildBattleBase := &serverproto.GuildBattleBase{
- GuildBattleId: this.guildBattleId,
- CurGuildStart: this.curGuildBattleBegin,
- GuildBattleState: this.guildBattleState,
- NextStageBegin: this.nextStateBegin,
- NextBattleBegin: this.nextGuildBattleBegin,
- TournamentPrepare: this.tournamentPrepare,
- TournamentBegin: this.tournamentBegin,
- TournamentEnd: this.tournamentEnd,
- SemifinalsPrepare: this.semifinalsPrepare,
- SemifinalsBegin: this.semifinalsBegin,
- SemifinalsEnd: this.semifinalsEnd,
- FinalsPrepare: this.finalPrepare,
- FinalsBegin: this.finalBegin,
- FinalsEnd: this.finalEnd,
- ShowEnd: this.showEnd,
- IsOpen: this.isOpen,
- RewardState: this.rewardState,
- BanType: this.banType,
- }
- msgData, err := rocommon.GetCodec().Marshal(guildBattleBase)
- if err != nil {
- return err
- }
- msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
- ret, err2 := service.GetRedis().Set(keyStr, msgStr, 0).Result()
- if err2 != nil {
- util.InfoF("saveGuildBattleBase err=%v ret=%v", keyStr, ret)
- return err2
- }
- util.InfoF("saveGuildBattleBase save guildbase success : %v", guildBattleBase)
- return nil
- }
- // 服务器启动时加载
- func (this *GuildBattleManager) loadGuildBattleBaseFromRedis() error {
- keyStr := GuildBattleBasePrefix
- ret, err := service.GetRedis().Get(keyStr).Result()
- if err != nil {
- util.InfoF("loadGuildBattleBase get guildId key=%v err=%v\n", keyStr, err)
- return err
- }
- if ret == "" {
- util.InfoF("loadGuildBattleBase key(%v)->val empty", keyStr)
- return err
- }
- str, err := base64.StdEncoding.DecodeString(ret)
- if err != nil {
- util.InfoF("loadGuildBattleBase key(%v)->val DecodeString error", keyStr)
- return err
- }
- guildBase := &serverproto.GuildBattleBase{}
- err = rocommon.GetCodec().Unmarshal(str, guildBase)
- if err == nil {
- this.guildBattleId = guildBase.GuildBattleId
- this.curGuildBattleBegin = guildBase.CurGuildStart
- this.guildBattleState = guildBase.GuildBattleState
- this.nextStateBegin = guildBase.NextStageBegin
- this.nextGuildBattleBegin = guildBase.NextBattleBegin
- this.tournamentPrepare = guildBase.TournamentPrepare
- this.tournamentBegin = guildBase.TournamentBegin
- this.tournamentEnd = guildBase.TournamentEnd
- this.semifinalsPrepare = guildBase.SemifinalsPrepare
- this.semifinalsBegin = guildBase.SemifinalsBegin
- this.semifinalsEnd = guildBase.SemifinalsEnd
- this.finalPrepare = guildBase.FinalsPrepare
- this.finalBegin = guildBase.FinalsBegin
- this.finalEnd = guildBase.FinalsEnd
- this.showEnd = guildBase.ShowEnd
- this.isOpen = guildBase.IsOpen
- this.rewardState = guildBase.RewardState
- this.banType = guildBase.BanType
- util.InfoF("loadGuildBattleBase success:%v", guildBase)
- return nil
- }
- util.InfoF("loadGuildBattleBase failed")
- return err
- }
- const firstGuildBattleIdHour = 5 * 86400 * 1000
- // 第一次初始化公会战时间
- func (this *GuildBattleManager) InitFirstGuildBattleBegin() uint64 {
- nextWeekBegin := util.GetLatestWeek5Hour(0) + firstGuildBattleIdHour //下个周一早上五点。再加上5天整
- return nextWeekBegin
- }
- // 第一轮公会战之后,校验下一轮公会战时间
- const TwoDayMilliseconds = 2 * 24 * 3600 * 1000
- func (this *GuildBattleManager) GetCurWeekGuildBattleBeginTime(curWeekDay, curHour int32) uint64 {
- //如果当前是周六五点之后,或者周天的话,则初始化到下一周的周六五点 //6 :周六 0:周天
- if curWeekDay == 0 || (curWeekDay == 6 && curHour >= 5) {
- return this.InitFirstGuildBattleBegin()
- }
- //如果是已经到了周一之后,但是周六五点之前
- //则设定下一轮的公会战开始时间为,下周一凌晨5点的前两天整
- return util.GetLatestWeek5Hour(0) - TwoDayMilliseconds
- }
- func (this *GuildBattleManager) ResetNextGuildBattleBegin() uint64 {
- curMilSecond := util.GetCurrentTime()
- //如果当前时间还没有到 下一轮公会战 的8.30。则认为可以继续开启公会战
- if curMilSecond < this.nextGuildBattleBegin+Tournament_Begin_Hour+Tournament_End_Hour {
- return 0
- }
- //已经超过 this.nextGuildBattleBegin 时间对应的周六 8.30,则认为当前轮次的公会战已经过了,
- nowTime := util.GetCurrentTimeNow()
- return this.GetCurWeekGuildBattleBeginTime(int32(nowTime.Weekday()), int32(nowTime.Hour()))
- }
- func (this *GuildBattleManager) SetGuildBattleFinish() {
- this.guildBattleState = Guild_Battle_End
- }
- const (
- Tournament_Prepare_Hour = 60 * 10 * 1000
- Tournament_Begin_Hour = 15 * 3600 * 1000
- Tournament_End_Hour = 1800 * 1000
- Semifinals_Prepare_Hour = 60 * 10 * 1000
- Semifinals_Begin_Hour = 16 * 3600 * 1000
- Semifinals_End_Hour = 1800 * 1000
- Final_Prepare_Hour = 60 * 10 * 1000
- Final_Begin_Hour = 86400*1000 + 16*3600*1000
- Final_End_Hour = 1800 * 1000
- Show_End_Hour = 3 * 86400 * 1000
- Next_Guild_Battle_Begin = 7 * 86400 * 1000
- Guild_Battle_Baned = 1
- )
- // 公会战状态切换
- func (this *GuildBattleManager) SwitchGuildBattleState() {
- if this.banType == Guild_Battle_Baned {
- return
- }
- if this.nextGuildBattleBegin == 0 {
- nowTime := util.GetCurrentTimeNow()
- this.nextGuildBattleBegin = this.GetCurWeekGuildBattleBeginTime(int32(nowTime.Weekday()), int32(nowTime.Hour()))
- // this.nextGuildBattleBegin = this.InitFirstGuildBattleBegin()
- err := this.saveGuildBattleBaseToRedis()
- if err != nil {
- util.InfoF("saveGuildBattleBaseToRedis save nextGuildBattleBegin failed : %v", err)
- }
- } else {
- //如果公会战时间不为零。,则要考虑nextGuildBattleBegin已经过期,切当前不在公会战时间内
- //即当前时间是不是在周二5点到周六5点之间,如果是,则初始化下一轮公会战时间为本周六的凌晨五点
- nextGuildBattleBegin := this.ResetNextGuildBattleBegin()
- if nextGuildBattleBegin != 0 {
- this.nextGuildBattleBegin = nextGuildBattleBegin
- err := this.saveGuildBattleBaseToRedis()
- if err != nil {
- util.InfoF("saveGuildBattleBaseToRedis init save new nextGuildBattleBegin failed : %v", err)
- }
- }
- }
- curTime := util.GetCurrentTime()
- //公会战时间到
- if this.nextGuildBattleBegin <= curTime {
- //开启公会战//记录当前公会战开启时间
- this.guildBattleId += 1
- util.InfoF("SwitchGuildBattleState init begin : %v", this.guildBattleId)
- //备份这一轮的百人道场贡献点
- backUpDaoChang100GuildRank(this.guildBattleId)
- //设置下一次公会战的时间
- this.curGuildBattleBegin = this.nextGuildBattleBegin
- this.nextGuildBattleBegin += Next_Guild_Battle_Begin
- //初始化:周六8.00-8.30 周六9.00-9.30 周日9.00-9.00。
- //四强赛
- this.tournamentBegin = this.curGuildBattleBegin + Tournament_Begin_Hour
- this.tournamentPrepare = this.tournamentBegin - Tournament_Prepare_Hour
- this.tournamentEnd = this.tournamentBegin + Tournament_End_Hour
- //半决赛
- this.semifinalsBegin = this.curGuildBattleBegin + Semifinals_Begin_Hour
- this.semifinalsPrepare = this.semifinalsBegin - Semifinals_Prepare_Hour
- this.semifinalsEnd = this.semifinalsBegin + Semifinals_End_Hour
- //决赛
- this.finalBegin = this.curGuildBattleBegin + Final_Begin_Hour
- this.finalPrepare = this.finalBegin - Final_Prepare_Hour
- this.finalEnd = this.finalBegin + Final_End_Hour
- //初始化周二凌晨5定
- this.showEnd = this.curGuildBattleBegin + Show_End_Hour
- this.rewardState = Guild_Battle_Start
- //清空当前对阵表,和对阵数据
- this.countPartTable = &serverproto.GuildCountPartTable{}
- this.battleIdxDataList = make(map[int32]*GuildBattle) //
- //初始化对阵表
- this.initGuildBattleList()
- if this.curGuildBattleBegin <= curTime && curTime < this.tournamentBegin {
- this.initBattle()
- }
- this.GuildBattleStateChangeNtf(true)
- err := this.saveGuildBattleBaseToRedis()
- if err != nil {
- util.InfoF("saveGuildBattleBaseToRedis save failed err: %v", err)
- }
- err2 := this.saveCountTableToRedis()
- if err2 != nil {
- util.InfoF("saveCountTableToRedis save failed err2: %v", err2)
- }
- //百人道场贡献点清空,重新记录新的
- ClearDaoChang100GuildRank()
- }
- if this.guildBattleState == Guild_Battle_End {
- return
- }
- //战斗中
- var needSave bool = false
- oldState := this.guildBattleState
- switch this.guildBattleState {
- case Guild_Battle_Start:
- if this.tournamentPrepare <= curTime && curTime < this.tournamentBegin {
- this.guildBattleState = Guild_Battle_Tournament_Prepare
- } else if this.tournamentEnd <= curTime { //四强赛结束(表示四强塞没打过)
- //公会战结束
- this.SetGuildBattleFinish()
- } else if curTime > this.tournamentBegin {
- this.guildBattleState = Guild_Battle_Tournament_Prepare
- }
- case Guild_Battle_Tournament_Prepare:
- if this.tournamentBegin <= curTime && curTime < this.tournamentEnd {
- this.guildBattleState = Guild_Battle_Tournament
- } else if this.tournamentEnd <= curTime { //四强赛结束(表示四强塞没打过)
- //公会战结束
- this.SetGuildBattleFinish()
- }
- case Guild_Battle_Tournament:
- if this.tournamentEnd <= curTime && curTime < this.semifinalsPrepare { //四强赛结束
- this.guildBattleState = Guild_Battle_OffMatch_TS
- } else if this.semifinalsEnd <= curTime { //半决赛赛结束(表示半决赛没打过)
- //公会战结束
- this.SetGuildBattleFinish()
- } else if curTime > this.semifinalsPrepare {
- this.guildBattleState = Guild_Battle_OffMatch_TS
- }
- case Guild_Battle_OffMatch_TS:
- if this.rewardState < Guild_Battle_Tournament {
- this.CheckTournamentReward()
- needSave = true
- }
- if this.semifinalsPrepare <= curTime && curTime < this.semifinalsBegin { //四强赛结束
- this.guildBattleState = Guild_Battle_Semifinals_Prepare
- } else if this.semifinalsEnd <= curTime { //半决赛赛结束(表示半决赛没打过)
- //公会战结束
- this.SetGuildBattleFinish()
- } else if curTime > this.semifinalsBegin {
- this.guildBattleState = Guild_Battle_Semifinals_Prepare
- }
- case Guild_Battle_Semifinals_Prepare:
- if this.rewardState < Guild_Battle_Tournament {
- this.CheckTournamentReward()
- needSave = true
- }
- if this.semifinalsBegin <= curTime && curTime <= this.semifinalsEnd { //半决赛开始
- this.guildBattleState = Guild_Battle_Semifinals
- } else if this.semifinalsEnd <= curTime { //半决赛赛结束(表示半决赛没打过)
- //公会战结束
- this.SetGuildBattleFinish()
- }
- case Guild_Battle_Semifinals:
- if this.semifinalsEnd <= curTime && curTime < this.finalPrepare { //半决赛结束
- this.guildBattleState = Guild_Battle_OffMatch_SF
- } else if this.finalEnd <= curTime { //决赛赛结束(表示没有进入过决塞)
- //公会战结束
- this.SetGuildBattleFinish()
- } else if curTime > this.finalPrepare {
- this.guildBattleState = Guild_Battle_OffMatch_SF
- }
- if this.rewardState < Guild_Battle_Tournament {
- this.CheckTournamentReward()
- needSave = true
- }
- case Guild_Battle_OffMatch_SF:
- if this.rewardState < Guild_Battle_Semifinals {
- this.CheckSemiFinalReward()
- needSave = true
- }
- if this.finalPrepare <= curTime && curTime <= this.finalBegin { //四强赛结束
- this.guildBattleState = Guild_Battle_Final_Prepare
- } else if this.finalEnd <= curTime { //半决赛赛结束(表示半决赛没打过)
- //公会战结束
- this.SetGuildBattleFinish()
- } else if curTime > this.finalBegin {
- this.guildBattleState = Guild_Battle_Final_Prepare
- }
- case Guild_Battle_Final_Prepare:
- if this.rewardState < Guild_Battle_Semifinals {
- this.CheckSemiFinalReward()
- needSave = true
- }
- if this.finalBegin <= curTime && curTime <= this.finalEnd {
- this.guildBattleState = Guild_Battle_Final
- } else if this.finalEnd <= curTime { //决赛赛结束(表示没有进入过决塞)
- //公会战结束
- this.SetGuildBattleFinish()
- }
- case Guild_Battle_Final:
- if this.rewardState < Guild_Battle_Semifinals {
- this.CheckSemiFinalReward()
- needSave = true
- }
- if this.finalEnd < curTime && curTime <= this.showEnd { //决赛结束
- //结算决赛奖励,给胜负两个队伍发送奖励
- this.guildBattleState = Guild_Battle_Result
- this.rewardState = Guild_Battle_Result
- this.sendGuildBattleFinalsReward()
- this.sendGuildBattleMvpReward(Guild_Battle_Reward_Final, 1, 1)
- needSave = true
- } else if curTime > this.showEnd {
- this.guildBattleState = Guild_Battle_Result
- }
- case Guild_Battle_Result:
- if this.showEnd < curTime && curTime <= this.nextGuildBattleBegin {
- this.guildBattleState = Guild_Battle_End
- this.GuildBattleStateChangeNtf(false)
- }
- default:
- }
- if oldState != this.guildBattleState || needSave == true {
- err2 := this.saveGuildBattleBaseToRedis()
- if err2 != nil {
- util.InfoF("saveGuildBattleBaseToRedis save failed : %v", err2)
- }
- }
- }
- func (this *GuildBattleManager) GuildBattleStateChangeNtf(inBattle bool) {
- ntfMsg := &serverproto.SSGuildBattleStageChangeNtf{
- InGuildBattle: inBattle,
- }
- SendToAllGame(ntfMsg)
- }
- func (this *GuildBattleManager) CheckTournamentReward() bool {
- this.initSemifinalsBattle()
- this.sendGuildBattleReward(Guild_Battle_Reward_Tournament, 4, 7)
- this.sendGuildBattleMvpReward(Guild_Battle_Reward_Tournament, 4, 7)
- this.rewardState = Guild_Battle_Tournament
- return true
- }
- func (this *GuildBattleManager) CheckSemiFinalReward() bool {
- this.initFinalsBattle()
- this.sendGuildBattleReward(Guild_Battle_Reward_Semifinals, 2, 3)
- this.sendGuildBattleMvpReward(Guild_Battle_Reward_Semifinals, 2, 3)
- this.rewardState = Guild_Battle_Semifinals
- return true
- }
- func (this *GuildBattleManager) ExchangeGuildPre(oldPreUid uint64, curPreUid uint64, guildId uint64) {
- if this.guildBattleState <= Guild_Battle_Tournament {
- this.handleExchangeTitle(oldPreUid, curPreUid, guildId, 4, 7)
- } else if this.guildBattleState <= Guild_Battle_Semifinals {
- this.handleExchangeTitle(oldPreUid, curPreUid, guildId, 2, 3)
- } else if this.guildBattleState <= Guild_Battle_Final {
- this.handleExchangeTitle(oldPreUid, curPreUid, guildId, 1, 1)
- }
- }
- func (this *GuildBattleManager) handleExchangeTitle(oldPreUid, curPreUid, guildId uint64, begin, end int32) {
- for index, data := range this.battleIdxDataList {
- if begin <= index && end <= index {
- data.ExchangeGuildPre(oldPreUid, curPreUid, guildId)
- }
- }
- }
- const MaxBattleTeams = 7
- // 初始化四强赛队伍
- func (this *GuildBattleManager) initBattle() {
- topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model.RoleDaoChang100GuildRankPrefix, 0, 7).Result()
- if err != nil {
- util.ErrorF("UpdateDaoChang100GuildRank")
- }
- this.battleIdxDataList = make(map[int32]*GuildBattle)
- //获取八强的名单。塞入8进4的对阵表中
- if len(topListWithScore) >= 5 {
- //四强赛公会ID
- var seedTeams []uint64
- var randTeams = map[uint64]int32{}
- for index, _ := range topListWithScore {
- guildId, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
- if index < Guild_Battle_Tournament_Begin_Match {
- seedTeams = append(seedTeams, guildId)
- } else if index >= Guild_Battle_Tournament_Begin_Match {
- randTeams[guildId] = 1
- }
- }
- var randTeamsList []uint64
- for key, _ := range randTeams {
- randTeamsList = append(randTeamsList, key)
- }
- //填充四强赛的具体数据4-7 4个队伍
- var battleIndex int32 = Guild_Battle_Tournament_Begin_Match
- for idx, seedGuildId := range seedTeams {
- var guildId2 uint64 = 0
- if len(randTeamsList) >= idx+1 {
- guildId2 = randTeamsList[idx]
- }
- this.battleIdxDataList[battleIndex] = newGuildBattle(this, battleIndex, seedGuildId, guildId2, this.tournamentBegin, this.tournamentEnd)
- this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
- BattleIndex: battleIndex,
- Guild1Id: seedGuildId,
- Guild2Id: guildId2,
- })
- battleIndex++
- }
- this.guildBattleState = Guild_Battle_Start //开始状态
- } else if len(topListWithScore) >= Guild_Battle_Seminal_End_Match {
- //初始化半决赛
- var seedTeams []uint64
- var randTeams = map[uint64]int32{} //打乱顺序
- for index, _ := range topListWithScore {
- guildId, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
- if index < Guild_Battle_Seminal_Begin_Match {
- seedTeams = append(seedTeams, guildId)
- } else if index >= Guild_Battle_Seminal_Begin_Match {
- randTeams[guildId] = 1
- }
- }
- var randTeamsList []uint64 ///有序排放
- for key, _ := range randTeams {
- randTeamsList = append(randTeamsList, key)
- }
- //填充四强赛的具体数据4-7 4个队伍
- var battleIndex int32 = Guild_Battle_Seminal_Begin_Match
- for idx, seedGuildId := range seedTeams {
- var guildId2 uint64 = 0
- if len(randTeamsList) >= idx+1 {
- guildId2 = randTeamsList[idx]
- }
- this.battleIdxDataList[battleIndex] = newGuildBattle(this, battleIndex, seedGuildId, guildId2, this.semifinalsBegin, this.semifinalsEnd)
- this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
- BattleIndex: battleIndex,
- Guild1Id: seedGuildId,
- Guild2Id: guildId2,
- })
- battleIndex++
- }
- this.guildBattleState = Guild_Battle_OffMatch_TS //开始状态
- this.rewardState = Guild_Battle_Tournament
- } else if len(topListWithScore) == Guild_Battle_Seminal_Begin_Match {
- //初始化决赛
- guildId, _ := strconv.ParseUint(topListWithScore[0].Member.(string), 10, 64)
- guildId2, _ := strconv.ParseUint(topListWithScore[1].Member.(string), 10, 64)
- this.battleIdxDataList[1] = newGuildBattle(this, 1, guildId, guildId2, this.finalBegin, this.finalEnd)
- this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
- BattleIndex: 1,
- Guild1Id: guildId,
- Guild2Id: guildId2,
- })
- this.guildBattleState = Guild_Battle_OffMatch_SF //开始状态
- this.rewardState = Guild_Battle_Semifinals
- } else if len(topListWithScore) == Guild_Battle_Final_Match {
- //直接结算冠军
- guildId, _ := strconv.ParseUint(topListWithScore[0].Member.(string), 10, 64)
- this.battleIdxDataList[1] = newGuildBattle(this, 1, guildId, 0, this.finalBegin, this.finalEnd)
- // this.championTeam = guildId
- this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
- BattleIndex: 1,
- Guild1Id: guildId,
- Guild2Id: 0,
- })
- this.guildBattleState = Guild_Battle_Result
- this.rewardState = Guild_Battle_Result
- this.sendGuildBattleFinalsReward()
- //如果只有一个队伍。则当前所有阶段时间都设置为当前的时间
- this.ResetStageTime()
- } else {
- //直接结束
- this.guildBattleState = Guild_Battle_Result
- }
- }
- func (this *GuildBattleManager) ResetStageTime() {
- nowTime := util.GetCurrentTime()
- //四强赛
- this.tournamentBegin = nowTime
- this.tournamentPrepare = nowTime
- this.tournamentEnd = nowTime
- //半决赛
- this.semifinalsBegin = nowTime
- this.semifinalsPrepare = nowTime
- this.semifinalsEnd = nowTime
- //决赛
- this.finalBegin = nowTime
- this.finalPrepare = nowTime
- this.finalEnd = nowTime
- }
- // 初始化半决赛队伍 8-4必定有至少3个队伍(前面初始化规则)
- func (this *GuildBattleManager) initSemifinalsBattle() {
- _, ok := this.battleIdxDataList[3]
- if !ok {
- //获取八强的名单。塞入4进2的对阵表中
- team5, ok5 := this.battleIdxDataList[5]
- team6, ok6 := this.battleIdxDataList[6]
- if ok6 && ok5 {
- winGuildId6, _, bRet6 := team6.GetBattleWinState()
- winGuildId5, _, bRet5 := team5.GetBattleWinState()
- if bRet6 && bRet5 {
- this.battleIdxDataList[3] = newGuildBattle(this, 3, winGuildId5, winGuildId6, this.semifinalsBegin, this.semifinalsEnd)
- this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
- BattleIndex: 3,
- Guild1Id: winGuildId5,
- Guild2Id: winGuildId6,
- })
- }
- }
- }
- _, ok2 := this.battleIdxDataList[2]
- if !ok2 {
- team4, ok4 := this.battleIdxDataList[4]
- team7, ok7 := this.battleIdxDataList[7]
- if ok4 {
- winGuildId4, _, bRet4 := team4.GetBattleWinState()
- if bRet4 {
- var guildId7 uint64 = 0
- if ok7 {
- winGuildId7, _, bRet7 := team7.GetBattleWinState()
- if bRet7 {
- guildId7 = winGuildId7
- this.battleIdxDataList[2] = newGuildBattle(this, 2, winGuildId4, winGuildId7, this.semifinalsBegin, this.semifinalsEnd)
- }
- } else {
- this.battleIdxDataList[2] = newGuildBattle(this, 2, winGuildId4, 0, this.semifinalsBegin, this.semifinalsEnd)
- }
- this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
- BattleIndex: 2,
- Guild1Id: winGuildId4,
- Guild2Id: guildId7,
- })
- }
- }
- }
- err2 := this.saveCountTableToRedis()
- if err2 != nil {
- }
- }
- // 初始化决赛队伍 4-2必定有2个队伍(前面初始化规则)
- func (this *GuildBattleManager) initFinalsBattle() {
- _, ok := this.battleIdxDataList[1]
- if !ok {
- //获取八强的名单。塞入决赛的对阵表中
- team2, ok2 := this.battleIdxDataList[2]
- team3, ok3 := this.battleIdxDataList[3]
- if ok2 && ok3 {
- winGuildId2, _, bRet2 := team2.GetBattleWinState()
- winGuildId3, _, bRet3 := team3.GetBattleWinState()
- if bRet2 && bRet3 {
- this.battleIdxDataList[1] = newGuildBattle(this, 1, winGuildId2, winGuildId3, this.finalBegin, this.finalEnd)
- this.countPartTable.Data = append(this.countPartTable.Data, &serverproto.CountTableData{
- BattleIndex: 1,
- Guild1Id: winGuildId2,
- Guild2Id: winGuildId3,
- })
- }
- }
- }
- err2 := this.saveCountTableToRedis()
- if err2 != nil {
- }
- }
- const (
- GuileBattle_Reward_Rank_First = 1 //冠军奖励
- GuileBattle_Reward_Rank_Second = 2 //亚军奖励
- GuileBattle_Reward_Rank_Semifinals = 3 //4-2
- GuileBattle_Reward_Rank_Tournament = 4 //8-4
- )
- func (this *GuildBattleManager) SendGuildBattleReward(rewardType int32, guildId uint64) {
- //发送公会成员奖励
- guild := GuildMag.GetGuild(guildId)
- if guild == nil || guild.GuildMember == nil || guild.GuildBase == nil {
- util.ErrorF("SendGuildBattleReward, guild not found : %v", guildId)
- return
- }
- ntfMsg := &serverproto.SSGuildBattleRewardNtf{
- RewardType: rewardType,
- GuildRound: this.guildBattleId,
- }
- for _, data := range guild.GuildMember.MemberInfo {
- if data.Title == Guild_Title_Pre {
- ntfMsg.GuildPre = data.MemberId
- } else {
- ntfMsg.UidList = append(ntfMsg.UidList, data.MemberId)
- }
- }
- //发送公会会长特殊奖励
- SendToAllGame(ntfMsg)
- }
- // 发送四强赛失败奖励
- func (this *GuildBattleManager) sendGuildBattleReward(rewardType int32, beginIdx, endIdx int32) {
- for i := beginIdx; i <= endIdx; i++ {
- team, ok := this.battleIdxDataList[i]
- if !ok {
- //打印日志
- continue
- }
- winGuild, lostGuild, ok := team.GetBattleWinState()
- if !ok {
- continue
- }
- util.InfoF("SendGuildBattleReward, lost guildId:%v, winGuildId:%v, battleIndex:%v", lostGuild, winGuild, i)
- //发送奖励给LostGuild
- if rewardType == Guild_Battle_Reward_Tournament {
- this.SendGuildBattleReward(GuileBattle_Reward_Rank_Tournament, lostGuild)
- } else if rewardType == Guild_Battle_Reward_Semifinals {
- this.SendGuildBattleReward(GuileBattle_Reward_Rank_Semifinals, lostGuild)
- }
- }
- }
- // 发送决赛奖励
- func (this *GuildBattleManager) sendGuildBattleFinalsReward() {
- team, ok := this.battleIdxDataList[1]
- if !ok {
- //打印日志
- util.ErrorF("SendGuildBattleFinalsReward, team not found : %v", this.battleIdxDataList)
- return
- }
- winGuild, lostGuild, ok := team.GetBattleWinState()
- if !ok {
- util.ErrorF("SendGuildBattleFinalsReward, team not found2 : %v, %v", winGuild, lostGuild)
- return
- }
- if team != nil {
- if team.battleBaseData != nil {
- util.ErrorF("send battle final reward:%v,win:%v,lost:%v", team.battleBaseData, winGuild, lostGuild)
- }
- }
- //发送奖励给
- this.SendGuildBattleReward(GuileBattle_Reward_Rank_Second, lostGuild)
- this.SendGuildBattleReward(GuileBattle_Reward_Rank_First, winGuild)
- }
- const (
- Rank_Type_Score = 1
- Rank_Type_Killer = 2
- )
- func (this *GuildBattleManager) GuildBattleMvpReward(ackMsg *serverproto.SSGuildBattleSendMvpRewardAck) {
- if ackMsg == nil {
- return
- }
- if len(ackMsg.ScoreMvp) <= 0 && len(ackMsg.KillerMvp) <= 0 {
- return
- }
- //设置领奖标记
- for _, scoreData := range ackMsg.ScoreMvp {
- battle, ok := this.battleIdxDataList[scoreData.BattleIndex]
- if !ok {
- //打印日志,给了奖励,但是没有做记录
- continue
- }
- battle.battleBaseData.ScoreMvp = append(battle.battleBaseData.ScoreMvp, scoreData.Uid)
- battle.baseChange = true
- }
- for _, killerData := range ackMsg.KillerMvp {
- battle, ok := this.battleIdxDataList[killerData.BattleIndex]
- if !ok {
- //打印日志,给了奖励,但是没有做记录
- continue
- }
- battle.battleBaseData.KillerMvp = append(battle.battleBaseData.KillerMvp, killerData.Uid)
- battle.baseChange = true
- }
- }
- func (this *GuildBattleManager) checkRewardDraw(mvpType, battleIndex int32, uid uint64) bool {
- guildBattle := this.getBattleByIdx(battleIndex)
- if guildBattle == nil || guildBattle.battleBaseData == nil {
- return true //找不到这场战斗,不给奖励
- }
- if len(guildBattle.battleBaseData.ScoreMvp) > 0 && mvpType == Rank_Type_Score {
- for _, rewardUid := range guildBattle.battleBaseData.ScoreMvp {
- if rewardUid == uid {
- return true
- }
- }
- } else if len(guildBattle.battleBaseData.KillerMvp) > 0 && mvpType == Rank_Type_Killer {
- for _, rewardUid := range guildBattle.battleBaseData.KillerMvp {
- if rewardUid == uid {
- return true
- }
- }
- }
- return false
- }
- // 场次结束结束发送排名奖励
- func (this *GuildBattleManager) sendGuildBattleMvpReward(rewardType int32, startBattle, endBattle int32) {
- ntfMsg := &serverproto.SSGuildBattleSendMvpRewardReq{
- GuildBattleRound: this.guildBattleId,
- }
- for i := startBattle; i <= endBattle; i++ {
- //打包 积分Mvp
- scoreStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(i)) + "-" + strconv.Itoa(int(Rank_Type_Score))
- retList, err := service.GetRedis().ZRevRangeWithScores(scoreStr, int64(0), int64(0)).Result()
- if err == nil {
- for idx := 0; idx < len(retList); {
- uid, _ := model.Str2NumU64(retList[idx].Member.(string))
- bFind := this.checkRewardDraw(Rank_Type_Score, i, uid)
- if bFind {
- break
- }
- ntfMsg.ScoreMvp = append(ntfMsg.ScoreMvp, &serverproto.MvpRewardInfo{
- Uid: uid,
- RewardType: int32(rewardType),
- BattleIndex: int32(i),
- })
- break
- }
- }
- //打包 击杀Mvp
- killerStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(i)) + "-" + strconv.Itoa(int(Rank_Type_Killer))
- retList2, err2 := service.GetRedis().ZRevRangeWithScores(killerStr, int64(0), int64(0)).Result()
- if err2 == nil {
- for idx := 0; idx < len(retList2); {
- uid, _ := model.Str2NumU64(retList2[idx].Member.(string))
- bFind := this.checkRewardDraw(Rank_Type_Killer, i, uid)
- if bFind {
- break
- }
- ntfMsg.KillerMvp = append(ntfMsg.KillerMvp, &serverproto.MvpRewardInfo{
- Uid: uid,
- RewardType: int32(rewardType),
- BattleIndex: i,
- })
- break
- }
- }
- }
- SendToAllGame(ntfMsg)
- }
- // 对阵数据启动服务器加载
- func (this *GuildBattleManager) getBattleByIdx(battleIdx int32) *GuildBattle {
- if this.banType == Guild_Battle_Baned {
- return nil
- }
- if battle, ok := this.battleIdxDataList[battleIdx]; ok {
- return battle
- }
- //TODO 查看数据库中是否存在对阵表,存在则构建战斗数据
- // 并初始化战斗信息
- //guildBattleKey := GuildBattlePrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(battleIdx))
- return nil
- }
- func (this *GuildBattleManager) GMGuildBattleOperator(operator int32) {
- this.operator = operator
- }
- func (this *GuildBattleManager) GMAddGuildBattleScore(battleIdx int32, guild_index int32, score int32) {
- guildBattle := this.getBattleByIdx(battleIdx)
- if guildBattle != nil {
- if guild_index == 0 {
- guildBattle.battleBaseData.ScoreList[0].Value += score
- } else if guild_index == 1 {
- guildBattle.battleBaseData.ScoreList[1].Value += score
- }
- }
- }
- // 公会战开关
- func (this *GuildBattleManager) WebGMGuildBattle() {
- var inBattle bool
- if this.banType == 0 {
- //原先开着的,则关闭
- this.banType = 1
- inBattle = false
- util.InfoF("WebGMGuildBattle, guild battle current state close")
- } else {
- //原先关闭的,则开着
- this.banType = 0
- inBattle = true
- util.InfoF("WebGMGuildBattle, guild battle current state open")
- }
- this.GuildBattleStateChangeNtf(inBattle)
- err := this.saveGuildBattleBaseToRedis()
- if err != nil {
- }
- }
- // todo wangzhaocan 测试用 待删除
- func (this *GuildBattleManager) GMGuildBattleReward() {
- this.sendGuildBattleReward(Guild_Battle_Reward_Semifinals, 2, 3)
- }
- // 获取对阵双方信息(打开界面时的信息)
- func (this *GuildBattleManager) GuildBattleInfo(selfGuildId, uid uint64, battleIdx int32, ackMsg *serverproto.SCGuildBattleInfoAck) {
- if this.banType == Guild_Battle_Baned {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_GUILD_SPECIAL_STATE)
- return
- }
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- battleData.BattleInfoReq(uid, selfGuildId, ackMsg)
- }
- func (this *GuildBattleManager) GuildBattleSetting(battleIdx int32, uid, guildId uint64, autoChallenge, autoReborn bool, ackMsg *serverproto.SCGuildBattleSettingAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- battleData.BattleSetting(uid, guildId, autoChallenge, autoReborn, ackMsg)
- }
- // 是否在购买时间内//购买时间之前/之后,做区分
- func (this *GuildBattleManager) CheckInBuyTime(battleData *GuildBattle) serverproto.ErrorCode {
- if battleData == nil {
- return serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST
- }
- if Guild_Battle_Tournament_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Tournament_End_Match { //四强
- if this.guildBattleState < Guild_Battle_Tournament_Prepare {
- return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
- }
- } else if Guild_Battle_Seminal_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Seminal_End_Match { //半决赛
- if this.guildBattleState < Guild_Battle_Semifinals_Prepare {
- return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
- }
- } else if Guild_Battle_Tournament_Begin_Match <= battleData.guildBattleIdx && battleData.guildBattleIdx <= Guild_Battle_Tournament_End_Match { //决赛
- if this.guildBattleState < Guild_Battle_Final_Prepare {
- return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_NOT_BEGIN
- }
- }
- _, _, ok := battleData.GetBattleWinState()
- if ok {
- return serverproto.ErrorCode_ERROR_GUILDBATTLE_CUR_BATTLE_FINISH
- }
- return serverproto.ErrorCode_ERROR_OK
- }
- func (this *GuildBattleManager) BuyChallengeNum(battleIdx int32, uid, guildId uint64, ackMsg *serverproto.SCGuildBattleBuyChallengeAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- bRet := this.CheckInBuyTime(battleData)
- if bRet != serverproto.ErrorCode_ERROR_OK {
- ackMsg.Error = int32(bRet)
- return
- }
- battleData.BuyChallengeNum(uid, guildId, ackMsg)
- }
- func (this *GuildBattleManager) BuyBattleBuff(battleIdx int32, uid uint64, buffList []int32, ackMsg *serverproto.SSGuildBattleBuyBuffAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- bRet := this.CheckInBuyTime(battleData)
- if bRet != serverproto.ErrorCode_ERROR_OK {
- ackMsg.Error = int32(bRet)
- return
- }
- guildIdx := GuildMag.RoleToGuilds[uid]
- if guildIdx != nil {
- battleData.BuyBattleBuff(uid, guildIdx.GuildId, buffList, ackMsg)
- }
- }
- func (this *GuildBattleManager) ResetRebornCD(battleIdx int32, uid, guildId uint64, ackMsg *serverproto.SCGuildBattleRebornAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- bRet := this.CheckInBuyTime(battleData)
- if bRet != serverproto.ErrorCode_ERROR_OK {
- ackMsg.Error = int32(bRet)
- return
- }
- battleData.ResetRebornCD(uid, guildId, ackMsg)
- }
- func (this *GuildBattleManager) GetBattleRankList(battleIdx, pageIdx, rankType int32, ackMsg *serverproto.SCGuildBattleRankListAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- GetGuildBattleRankList(this.guildBattleId, battleIdx, pageIdx, rankType, ackMsg)
- }
- // limit 20 items
- func (this *GuildBattleManager) GetBattlePosIdxList(battleIdx int32, posIdxList []int32, ackMsg *serverproto.SCGuildBattlePosIdxListAck) {
- if len(posIdxList) <= 0 || len(posIdxList) > 20 {
- return
- }
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- bRet := this.CheckInBuyTime(battleData)
- if bRet != serverproto.ErrorCode_ERROR_OK {
- ackMsg.Error = int32(bRet)
- return
- }
- battleData.GetBattlePosIdxList(posIdxList, ackMsg)
- }
- func (this *GuildBattleManager) GetPosIdxLog(battleIdx int32, beginTime uint64, ackMsg *serverproto.SCGuildBattleLogAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- battleData.GetPosIdxLog(beginTime, ackMsg)
- }
- func (this *GuildBattleManager) BattleChallenge(battleIdx, posIdx int32, selfGuildId uint64, buyNum int32, bfInfo *serverproto.CommonPlayerBriefInfo,
- curRmb uint64, ackMsg *serverproto.SSGuildBattleChallengeAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- bRet := this.CheckInBuyTime(battleData)
- if bRet != serverproto.ErrorCode_ERROR_OK {
- ackMsg.Error = int32(bRet)
- return
- }
- battleData.BattleChallengePre(posIdx, selfGuildId, bfInfo, curRmb, buyNum, ackMsg)
- }
- func (this *GuildBattleManager) BattleChallengeResult(msg *serverproto.CSGuildBattleChallengeResultReq,
- ssAckMsg *serverproto.SCGuildBattleChallengeResultAck, ev rocommon.ProcEvent) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(msg.GuildBattleIdx)
- if battleData == nil {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- bRet := this.CheckInBuyTime(battleData)
- if bRet != serverproto.ErrorCode_ERROR_OK {
- ssAckMsg.Error = int32(bRet)
- return
- }
- battleData.BattleChallengeResult(msg.PosIdx, msg.SelfGuildId, msg.BfInfo, msg.BattleResult, ssAckMsg, ev)
- }
- func (this *GuildBattleManager) BattleChallengePing(challengeUid uint64, battleIdx, posIdx int32,
- ackMsg *serverproto.SCGuildBattleChallengePingAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- return
- }
- battleData.BattleChallengePing(challengeUid, posIdx, ackMsg)
- }
- func (this *GuildBattleManager) BattlePKData(battleIdx int32, ssAckMsg *serverproto.SCGuildBattlePKDataAck) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_NOT_EXIST)
- return
- }
- bRet := this.CheckInBuyTime(battleData)
- if bRet != serverproto.ErrorCode_ERROR_OK {
- ssAckMsg.Error = int32(bRet)
- // return
- }
- battleData.BattlePKData(ssAckMsg)
- }
- func (this *GuildBattleManager) GetGuildBattleCPRank(guildId uint64, ackMsg *serverproto.SSGuildBattleCPRankAck) {
- topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model.RoleDaoChang100GuildRankPrefix, 0, 19).Result()
- if err != nil {
- util.ErrorF("GetGuildBattleCPRank")
- return
- }
- util.InfoF("GetGuildBattleCPRank len:%v", len(topListWithScore))
- for index, _ := range topListWithScore {
- guildId, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
- guild := GuildMag.GetGuild(guildId)
- if guild == nil {
- continue
- }
- cpNum := getValByDaoChang100GuildScore(topListWithScore[index].Score)
- ackMsg.CpList = append(ackMsg.CpList, &serverproto.GuildBattleCP{
- Brief: guild.GuildBase.GuildBrief,
- MemCount: int32(len(guild.GuildMember.MemberInfo)),
- CpNum: uint32(cpNum),
- Rank: int32(index + 1),
- })
- }
- if guildId != 0 {
- guildIdStr := strconv.FormatUint(guildId, 10)
- tmpScore, err2 := service.GetRedis().ZScore(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
- if err2 == nil {
- oldScore := getValByDaoChang100GuildScore(tmpScore)
- ackMsg.SelfGuildCp = int32(oldScore)
- }
- selfRank, rankErr := service.GetRedis().ZRevRank(model.RoleDaoChang100GuildRankPrefix, guildIdStr).Result()
- if rankErr == nil {
- ackMsg.SelfGuildRank = int32(selfRank + 1)
- }
- }
- }
- func (this *GuildBattleManager) GetBattleState(guildId1, guildId2 uint64, battle *GuildBattle) (bool, int32) {
- if battle == nil {
- return false, 0
- }
- bRet := false
- retState := int32(0)
- winGuild, _, ok := battle.GetBattleWinState()
- if ok {
- if winGuild == guildId1 {
- bRet = true
- } else if winGuild == guildId2 {
- bRet = false
- }
- retState = Battle_State_End
- } else {
- nowTime := util.GetCurrentTime()
- if Guild_Battle_Tournament_Begin_Match <= battle.guildBattleIdx && battle.guildBattleIdx <= Guild_Battle_Tournament_End_Match {
- if this.tournamentPrepare <= nowTime && nowTime <= this.tournamentEnd {
- retState = Battle_State_Fight
- } else {
- retState = Battle_State_Prepare
- }
- } else if Guild_Battle_Seminal_Begin_Match <= battle.guildBattleIdx && battle.guildBattleIdx <= Guild_Battle_Seminal_End_Match {
- if this.semifinalsPrepare <= nowTime && nowTime <= this.semifinalsEnd {
- retState = Battle_State_Fight
- } else {
- retState = Battle_State_Prepare
- }
- } else if Guild_Battle_Final_Match == battle.guildBattleIdx {
- if this.finalPrepare <= nowTime && nowTime <= this.finalEnd {
- retState = Battle_State_Fight
- } else {
- retState = Battle_State_Prepare
- }
- }
- }
- return bRet, retState
- }
- const (
- Battle_State_Prepare = 1
- Battle_State_Fight = 2
- Battle_State_End = 3
- )
- func (this *GuildBattleManager) GetGuildBattleCountPart(ackMsg *serverproto.SSGuildBattleCountPartAck) {
- if this.banType == Guild_Battle_Baned {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_GUILD_SPECIAL_STATE)
- return
- }
- if this.guildBattleId == 0 || this.showEnd <= util.GetCurrentTime() {
- return
- }
- ackMsg.Table = &serverproto.GuildCountPartTable{}
- for _, data := range this.countPartTable.Data {
- battle := this.battleIdxDataList[data.BattleIndex]
- if battle == nil {
- continue
- }
- countTable := &serverproto.CountTableData{}
- countTable.BattleIndex = data.BattleIndex
- countTable.Guild1Id = data.Guild1Id
- countTable.Guild2Id = data.Guild2Id
- if len(battle.battleBaseData.ScoreList) >= 1 {
- countTable.Guild1Name = battle.battleBaseData.ScoreList[0].StrVal
- countTable.Guild1Badge = battle.battleBaseData.ScoreList[0].Value3
- }
- if len(battle.battleBaseData.ScoreList) >= 2 {
- countTable.Guild2Name = battle.battleBaseData.ScoreList[1].StrVal
- countTable.Guild2Badge = battle.battleBaseData.ScoreList[1].Value3
- }
- retWin, retState := this.GetBattleState(data.Guild1Id, data.Guild2Id, battle)
- countTable.WinGuild = retWin
- countTable.State = retState
- ackMsg.Table.Data = append(ackMsg.Table.Data, countTable)
- }
- // ackMsg.Table = this.countPartTable
- ackMsg.CurStge = this.guildBattleState
- ackMsg.TournamentPrepare = this.tournamentPrepare
- ackMsg.TournamentBegin = this.tournamentBegin
- ackMsg.TournamentEnd = this.tournamentEnd
- ackMsg.SemifinalsPrepare = this.semifinalsPrepare
- ackMsg.SemifinalsBegin = this.semifinalsBegin
- ackMsg.SemifinalsEnd = this.semifinalsEnd
- ackMsg.FinalsPrepare = this.finalPrepare
- ackMsg.FinalsBegin = this.finalBegin
- ackMsg.FinalsEnd = this.finalEnd
- }
- func (this *GuildBattleManager) GetGuildBattleMvpInfo(battleIdx int32, ackMsg *serverproto.SSGuildBattleMvpInfoAck) {
- if ackMsg == nil {
- return
- }
- data, ok := this.battleIdxDataList[battleIdx]
- if !ok {
- return
- }
- var guildId1 uint64 = 0
- for idx, scoreList := range data.battleBaseData.ScoreList {
- ackMsg.Detail = append(ackMsg.Detail, &serverproto.GuildBattleDetail{
- GuildId: scoreList.Key,
- SlotCount: 0,
- Score: scoreList.Value,
- })
- guild := GuildMag.GetGuild(scoreList.Key)
- if guild != nil {
- if idx == 1 {
- guildId1 = scoreList.Key
- ackMsg.CountPart.Guild1Id = scoreList.Key
- ackMsg.CountPart.Guild1Name = guild.GuildBase.GuildBrief.GuildName
- } else if idx == 2 {
- ackMsg.CountPart.Guild2Id = scoreList.Key
- ackMsg.CountPart.Guild2Name = guild.GuildBase.GuildBrief.GuildName
- }
- }
- }
- winGuild, _, bRet := this.GetBattleWinState(battleIdx)
- if bRet {
- if winGuild == guildId1 {
- ackMsg.CountPart.WinGuild = true
- } else {
- ackMsg.CountPart.WinGuild = false
- }
- }
- }
- func (this *GuildBattleManager) GetGuildBattleMvpDetail(battleIdx int32, mvpType int32, reqRank int32, ackMsg *serverproto.SSGuildBattleMvpDetailAck) {
- keyStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(battleIdx)) + "-" + strconv.Itoa(int(mvpType))
- topListWithScore, err := service.GetRedis().ZRevRangeWithScores(keyStr, int64(reqRank), int64(reqRank+9)).Result()
- if err != nil {
- util.ErrorF("UpdateDaoChang100GuildRank")
- }
- for index, data := range topListWithScore {
- tmpScore := getValByGuildBattleScore(data.Score)
- uid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
- mvp := &serverproto.GuildBattleMvp{
- Uid: uid,
- Score: int32(tmpScore),
- }
- mvp.GuildId = GuildMag.GetPlayerGuildId(uid)
- guild := GuildMag.GetGuild(mvp.GuildId)
- if guild != nil {
- mvp.Name = guild.GuildBase.GuildBrief.GuildName
- }
- ackMsg.Mvp = append(ackMsg.Mvp, mvp)
- }
- }
- func (this *GuildBattleManager) GetBattleWinStateByBaseData(baseData *serverproto.GuildBattleBaseData) (uint64, uint64, bool) {
- scoreList := baseData.ScoreList
- if len(scoreList) == 2 { //满编队伍
- if scoreList[0].Value == scoreList[1].Value {
- if scoreList[0].Value2 == scoreList[0].Value2 {
- return scoreList[0].Key, scoreList[1].Key, true
- } else if scoreList[0].Value2 < scoreList[0].Value2 {
- return scoreList[0].Key, scoreList[1].Key, true
- } else {
- return scoreList[1].Key, scoreList[0].Key, true
- }
- } else if scoreList[0].Value > scoreList[1].Value {
- return scoreList[0].Key, scoreList[1].Key, true
- } else {
- return scoreList[1].Key, scoreList[0].Key, true
- }
- } else if len(scoreList) == 1 { //轮空的情况下
- return scoreList[0].Key, scoreList[1].Key, true
- }
- return 0, 0, false
- }
- func (this *GuildBattleManager) CheckCurRewardRound(uid uint64) bool {
- for key, battle := range this.battleIdxDataList {
- _, lostGuild, ret := this.GetBattleWinState(key)
- if !ret {
- continue
- }
- playerData := battle.getPlayerData(uid, lostGuild)
- if playerData == nil {
- continue
- }
- //战斗结束//且有这个玩家,如果key是决赛,那就可以给奖励了,不是决赛且这场战斗输了也给奖励
- if key == 1 || key != 1 && playerData.GuildId == lostGuild {
- return true
- }
- }
- return false
- }
- func (this *GuildBattleManager) PackPlayerGuildBattleReward(uid uint64, round, index int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) serverproto.ErrorCode {
- keyStr := GuildBattlePrefix + strconv.Itoa(int(round)) + "-" + strconv.Itoa(int(index))
- fieldStr := "uid:" + strconv.FormatUint(uid, 10)
- resultData, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
- if err != nil {
- util.ErrorF("GuildBattleSelfData err uid:%v round: %v battleindex: %v, err3: %v", uid, round, index, err)
- return serverproto.ErrorCode_ERROR_FAIL
- }
- playerData := &serverproto.GuildBattleSelfData{}
- err2 := model.GetDecodeMessage(playerData, resultData)
- if err2 != nil {
- util.ErrorF("GuildBattleSelfData not decode uid:%v round: %v battleindex: %v, err3: %v", uid, round, index, err2)
- return serverproto.ErrorCode_ERROR_FAIL
- }
- isPre := false
- if playerData.GuildPre == true {
- isPre = true
- }
- rewardType := 0
- var winGuild uint64 = 0
- var lostGuild uint64 = 0
- //如果是本轮活动,则判定是否战斗结束://
- if round == this.guildBattleId {
- win, lost, ok := this.GetBattleWinState(int32(index))
- if !ok { //没结束就不管了
- return serverproto.ErrorCode_ERROR_FAIL
- }
- //不是冠军赛 则要看是否是战斗失败的公会
- if index != 1 && playerData.GuildId != lost {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- winGuild = win
- lostGuild = lost
- } else {
- //获取比赛的信息
- battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(round)) + "-" + strconv.Itoa(int(index))
- retStr, err3 := service.GetRedis().HGet(battleKeyStr, "base").Result()
- if err3 != nil {
- util.ErrorF("GuildBattleBaseData err round: %v battleindex: %v, err3: %v", round, index, err3)
- return serverproto.ErrorCode_ERROR_FAIL
- }
- //获取比赛的基础数据
- battleBaseData := &serverproto.GuildBattleBaseData{}
- err4 := model.GetDecodeMessage(battleBaseData, retStr)
- if err4 != nil {
- util.ErrorF("GuildBattleBaseData not decode round: %v battleindex:%v: err: %v", round, index, err4)
- return serverproto.ErrorCode_ERROR_FAIL
- }
- //比赛未结束,则后续不处理
- win2, lost2, ret := this.GetBattleWinStateByBaseData(battleBaseData)
- if !ret {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- winGuild = win2
- lostGuild = lost2
- }
- if index == 1 {
- if playerData.GuildId == winGuild {
- rewardType = GuileBattle_Reward_Rank_First
- } else if playerData.GuildId == lostGuild {
- rewardType = GuileBattle_Reward_Rank_Second
- }
- } else if index == 2 || index == 3 {
- rewardType = GuileBattle_Reward_Rank_Semifinals
- } else if 4 <= index && index <= 7 {
- rewardType = GuileBattle_Reward_Rank_Tournament
- }
- ackMsg.RoundReward = append(ackMsg.RoundReward, &serverproto.GuildBattleReward{
- IsPre: isPre,
- RewardType: int32(rewardType),
- })
- util.InfoF("uid=%v GuildBattleBaseData online get reward curround :%v, round: %v battleindex: %v guildid:%v", uid, this.guildBattleId, round, index, playerData.GuildId)
- return serverproto.ErrorCode_ERROR_OK
- }
- func (this *GuildBattleManager) PackPlayerGuildBattleMvpReward(uid uint64, mvpRound, index int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) {
- //获取本场战斗的base
- var battleBaseData *serverproto.GuildBattleBaseData = nil
- if mvpRound == this.guildBattleId {
- battle, ok := this.battleIdxDataList[int32(index)]
- if !ok {
- return
- }
- battleBaseData = battle.battleBaseData
- //公会战是否结束,未结束就不管
- _, _, ok2 := this.GetBattleWinState(int32(index))
- if !ok2 { //没结束就不管了
- return
- }
- } else {
- battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(mvpRound)) + "-" + strconv.Itoa(int(index))
- retStr, err3 := service.GetRedis().HGet(battleKeyStr, "base").Result()
- if err3 != nil {
- util.ErrorF("MvpReward GuildBattleBaseData err round: %v battleindex: %v, err3: %v", mvpRound, index, err3)
- return
- }
- if err3 == service.NIL {
- util.ErrorF("MvpReward GuildBattleBaseData not found round: %v battleindex: %v, err3: %v", mvpRound, index)
- return
- }
- battleBaseData = &serverproto.GuildBattleBaseData{}
- //获取比赛的基础数据
- err4 := model.GetDecodeMessage(battleBaseData, retStr)
- if err4 != nil {
- util.ErrorF("MvpReward GuildBattleBaseData not decode round: %v battleindex:%v: err: %v", mvpRound, index, err4)
- return
- }
- }
- if battleBaseData == nil {
- return
- }
- //遍历所有领奖记录,是否领取过奖励
- var bScoreReward bool = false
- var bKillerReward bool = false
- if len(battleBaseData.ScoreMvp) >= 0 {
- for _, rewardUid := range battleBaseData.ScoreMvp {
- if rewardUid == uid {
- bScoreReward = true
- break
- }
- }
- }
- if len(battleBaseData.KillerMvp) >= 0 {
- for _, rewardUid := range battleBaseData.KillerMvp {
- if rewardUid == uid {
- bKillerReward = true
- }
- break
- }
- }
- if bScoreReward == true && bKillerReward == true {
- return
- }
- //获取奖励类型
- rewardType := Guild_Battle_Reward_Tournament
- if index == 1 {
- rewardType = Guild_Battle_Reward_Final
- } else if index == 2 || index == 3 {
- rewardType = Guild_Battle_Reward_Semifinals
- }
- //打包 积分Mvp
- if bScoreReward == false {
- scoreStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(index)) + "-" + strconv.Itoa(int(Rank_Type_Score))
- retList, err3 := service.GetRedis().ZRevRangeWithScores(scoreStr, int64(0), int64(0)).Result()
- if err3 == nil && len(retList) > 0 {
- firstUid, _ := model.Str2NumU64(retList[0].Member.(string))
- if uid == firstUid {
- ackMsg.ScoreMvp = append(ackMsg.ScoreMvp, &serverproto.MvpRewardInfo{
- Uid: uid,
- RewardType: int32(rewardType),
- })
- battleBaseData.ScoreMvp = append(battleBaseData.ScoreMvp, uid)
- }
- }
- }
- //打包 击杀Mvp
- if bKillerReward == false {
- killerStr := GuildBattleRankPrefix + strconv.Itoa(int(this.guildBattleId)) + "-" + strconv.Itoa(int(index)) + "-" + strconv.Itoa(int(Rank_Type_Killer))
- retList2, err4 := service.GetRedis().ZRevRangeWithScores(killerStr, int64(0), int64(0)).Result()
- if err4 == nil && len(retList2) > 0 {
- firstUid, _ := model.Str2NumU64(retList2[0].Member.(string))
- if uid == firstUid {
- ackMsg.KillerMvp = append(ackMsg.KillerMvp, &serverproto.MvpRewardInfo{
- Uid: uid,
- RewardType: int32(rewardType),
- })
- battleBaseData.KillerMvp = append(battleBaseData.KillerMvp, uid)
- }
- }
- }
- //保存
- if mvpRound == this.guildBattleId {
- battle, ok := this.battleIdxDataList[int32(index)]
- if !ok {
- return
- }
- battle.saveBattleBaseData()
- } else {
- err, msgStr := model.GetEncodeMessage(battleBaseData)
- if err != nil {
- return
- }
- keyStr := GuildBattlePrefix + strconv.Itoa(int(mvpRound)) + "-" + strconv.Itoa(int(index))
- service.GetRedis().HSet(keyStr, "base", msgStr)
- }
- }
- // 玩家上线查看当前领奖//最多打包最近的5场
- func (this *GuildBattleManager) OnlineGetRoleGuildBattleReward(uid uint64, curRewardId, mvpRewardId int32, ackMsg *serverproto.SSGuildBattleOnlineGetRewardAck) {
- if ackMsg == nil || this.guildBattleState == 0 {
- return
- }
- var beginRound int32 = curRewardId + 1
- //计算领奖轮次//判定当前轮次是否有奖励可领取。有则领取,无则从(当前轮次-1)开始领取
- bRet := this.CheckCurRewardRound(uid)
- endRound := this.guildBattleId
- if !bRet {
- endRound = this.guildBattleId - 1
- }
- ackMsg.CurBattleRound = endRound
- //获取公会战奖励
- if endRound <= 0 || endRound < beginRound {
- //这里暂不处理,不需要打包数据
- } else {
- //curRewardId 已经领取轮次
- for round := endRound; beginRound <= round && (endRound-round <= 5); round-- {
- for index := 1; index <= MaxBattleTeams; index++ {
- //遍历七场战斗数据//查看有无该玩家。有则给奖励//从上到下,只要找到一场有该玩家的数据,则给这一档次的奖励
- bRet := this.PackPlayerGuildBattleReward(uid, round, int32(index), ackMsg)
- //找到奖励,当前轮就不在查找
- if bRet == serverproto.ErrorCode_ERROR_OK {
- break
- }
- }
- }
- }
- //设定领奖轮次
- mvpFinishRound := this.guildBattleId
- mvpBeginRound := mvpRewardId
- ackMsg.CurMvpRound = mvpFinishRound
- if mvpFinishRound < mvpBeginRound {
- return
- }
- //公会战MVP奖励
- for mvpRound := mvpFinishRound; mvpBeginRound <= mvpRound && (mvpFinishRound-mvpRound <= 5) && mvpRound > 0; mvpRound-- {
- for index := 1; index <= MaxBattleTeams; index++ {
- this.PackPlayerGuildBattleMvpReward(uid, mvpRound, int32(index), ackMsg)
- }
- }
- }
- // 获取战场当前的获胜状态
- func (this *GuildBattleManager) GetBattleWinState(battleIdx int32) (uint64, uint64, bool) {
- this.initGuildBattleList()
- battleData := this.getBattleByIdx(battleIdx)
- if battleData == nil {
- return 0, 0, false
- }
- return battleData.GetBattleWinState()
- }
- type GuildBattle struct {
- updateTimer util.ServerTimer //主循环定时器
- mag *GuildBattleManager
- guildBattleIdx int32 //1-7中的第几场战斗
- battleBaseData *serverproto.GuildBattleBaseData
- scoreUpdateMs uint64
- //参战玩家信息
- playerDataList map[uint64]*serverproto.GuildBattleSelfData
- //战斗日志
- logList []*serverproto.GuildBattleLog
- //占位信息
- posIdxDataList map[int32]*serverproto.GuildPosIdxData
- posIdxDataUidList map[uint64]*serverproto.GuildPosIdxData
- changePosIdxList set.Interface
- changePlayerDataList set.Interface
- baseChange bool //积分变化
- bPosInit bool
- }
- func newGuildBattle(mag *GuildBattleManager, battleIdx int32, guildId1, guildId2 uint64, startTime, endTime uint64) *GuildBattle {
- battle := &GuildBattle{
- playerDataList: map[uint64]*serverproto.GuildBattleSelfData{},
- posIdxDataList: map[int32]*serverproto.GuildPosIdxData{},
- posIdxDataUidList: map[uint64]*serverproto.GuildPosIdxData{},
- changePosIdxList: set.New(set.NonThreadSafe),
- changePlayerDataList: set.New(set.NonThreadSafe),
- battleBaseData: &serverproto.GuildBattleBaseData{},
- guildBattleIdx: battleIdx,
- mag: mag,
- }
- if startTime != 0 && endTime != 0 {
- battle.battleBaseData.BattleStartTime = startTime
- battle.battleBaseData.BattleEndTime = endTime
- }
- battle.updateTimer = util.NewDurationTimer(util.GetCurrentTime(), 1000)
- util.InfoF("GuildBattle battleIndex:%v guildId1=%v guildId2=%v startTime:%v, endTime:%v", battleIdx, guildId1, guildId2, startTime, endTime)
- battle.init(guildId1, guildId2, startTime, endTime)
- return battle
- }
- // 加载战斗数据(如果存在)
- func (this *GuildBattle) init(guildId1, guildId2 uint64, startTime, endTime uint64) {
- battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
- retStr, err := service.GetRedis().HGet(battleKeyStr, "base").Result()
- if err == nil {
- err2 := model.GetDecodeMessage(this.battleBaseData, retStr)
- if err2 != nil {
- util.ErrorF("GuildBattle id=%v idx=%v base err", this.mag.guildBattleId, this.guildBattleIdx)
- }
- }
- if len(this.battleBaseData.ScoreList) <= 0 {
- this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
- &serverproto.KeyValueType64{Key: guildId1}, &serverproto.KeyValueType64{Key: guildId2})
- //初始化公会数据
- for _, data := range this.battleBaseData.ScoreList {
- guild := GuildMag.GetGuild(data.Key)
- if guild != nil {
- data.Value3 = guild.GuildBase.GuildBrief.GuildBadge
- data.StrVal = guild.GuildBase.GuildBrief.GuildName
- }
- }
- this.initGuildPlayerData(guildId1, guildId2)
- this.saveBattleBaseData()
- util.ErrorF("GuildBattle id=%v idx=%v init battle data ", this.mag.guildBattleId, this.guildBattleIdx)
- } else {
- util.InfoF("GuildBattle battleIndex 2 :%v guildId1=%v guildId2=%v startTime:%v, endTime:%v", this.guildBattleIdx, guildId1, guildId2, this.battleBaseData.BattleStartTime, this.battleBaseData.BattleEndTime)
- }
- this.initPosListInfo()
- }
- func (this *GuildBattle) saveBattleBaseData() {
- err, msgStr := model.GetEncodeMessage(this.battleBaseData)
- if err != nil {
- return
- }
- keyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
- service.GetRedis().HSet(keyStr, "base", msgStr)
- }
- func (this *GuildBattle) saveBattlePlayerData() {
- for _, val := range this.changePlayerDataList.List() {
- uid := val.(uint64)
- if playerData, ok := this.playerDataList[uid]; ok {
- setGuildBattlePlayerDataToRedis(this.mag.guildBattleId, this.guildBattleIdx, playerData)
- }
- }
- this.changePlayerDataList.Clear()
- }
- func (this *GuildBattle) update(ms uint64) {
- if this.mag == nil || this.mag.banType == Guild_Battle_Baned {
- return
- }
- if !this.updateTimer.IsStart() || !this.updateTimer.IsExpired(ms) {
- return
- }
- //不在战斗中
- if ms < this.battleBaseData.BattleStartTime || ms > this.battleBaseData.BattleEndTime {
- return
- }
- //积分处理
- this.updateScore(ms)
- if this.baseChange {
- this.baseChange = false
- this.saveBattleBaseData()
- }
- this.saveBattlePlayerData()
- for _, val := range this.changePosIdxList.List() {
- posIdx := val.(int32)
- if posIdxData, ok := this.posIdxDataList[posIdx]; ok {
- setGuildBattlePosIdxDataToRedis(this.mag.guildBattleId, this.guildBattleIdx, posIdxData)
- }
- }
- this.changePosIdxList.Clear()
- }
- func (this *GuildBattle) updateScore(ms uint64) {
- if this.scoreUpdateMs < 0 {
- this.scoreUpdateMs = ms
- return
- }
- //5s
- if this.scoreUpdateMs+model.GlobalGuildBattleRewardTime > ms {
- return
- }
- this.scoreUpdateMs = ms
- var scoreMap = map[uint64]int32{}
- var playerScoreMap = map[uint64]uint32{}
- for _, v := range this.posIdxDataList {
- if warPoint, ok := model.ConvertGuildBattleList[v.PosIdx]; ok {
- if v.GuildId > 0 {
- scoreMap[v.GuildId] += warPoint
- }
- playerScoreMap[v.Uid] += uint32(warPoint)
- }
- }
- for k, v := range scoreMap {
- var scoreVal int32 = 0
- bFind := false
- for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
- if this.battleBaseData.ScoreList[idx].Key == k {
- this.battleBaseData.ScoreList[idx].Value += v
- bFind = true
- scoreVal = this.battleBaseData.ScoreList[idx].Value
- break
- }
- }
- if !bFind {
- this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
- &serverproto.KeyValueType64{Key: k, Value: v})
- scoreVal = v
- }
- this.baseChange = true
- //先到达胜利最大积分的为胜利队伍
- if scoreVal >= model.GlobalGuildBattleWinMaxScore {
- this.battleBaseData.BattleEndTime = ms
- util.InfoF("GuildBattle updateScore id=%v idx=%v guildid=%v score=%v",
- this.mag.guildBattleId, this.guildBattleIdx, k, scoreVal)
- break
- }
- }
- keyStr := GuildBattleRankPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" +
- strconv.Itoa(int(this.guildBattleIdx)) + "-" + strconv.Itoa(int(RankScoreListType))
- for k, v := range playerScoreMap {
- if k == 0 || v == 0 {
- continue
- }
- uidStr := strconv.FormatUint(k, 10)
- oldScore, err := service.GetRedis().ZScore(keyStr, uidStr).Result()
- if err == nil { //找到则变更数据
- tmpScore := getValByGuildBattleScore(oldScore)
- tmpScore += v
- scoreStr := getguildBattleScore(uint64(tmpScore), ms)
- util.InfoF("GuildBattle updateScore uid=%v addscore=%v score=%v", k, v, tmpScore)
- service.GetRedis().ZAdd(keyStr, service.BaseZ{Score: float64(scoreStr), Member: uidStr})
- } else if err == service.NIL { //未找到此人,则新插入一条记录
- scoreStr := getguildBattleScore(uint64(v), ms)
- service.GetRedis().ZAdd(keyStr, service.BaseZ{Score: float64(scoreStr), Member: uidStr})
- }
- }
- }
- func (this *GuildBattle) initGuildPlayerData(guildId1, guildId2 uint64) {
- guild1 := GuildMag.GetGuild(guildId1)
- if guild1 != nil {
- for _, member := range guild1.GuildMember.MemberInfo {
- playerData := &serverproto.GuildBattleSelfData{
- Uid: member.MemberId,
- GuildId: guildId1,
- ChallengeNum: model.GlobalGuildBattleChallengeNum,
- }
- if member.Title == Guild_Title_Pre {
- playerData.GuildPre = true
- }
- this.playerDataList[member.MemberId] = playerData
- this.changePlayerDataList.Add(member.MemberId)
- }
- }
- guild2 := GuildMag.GetGuild(guildId2)
- if guild2 != nil {
- for _, member := range guild2.GuildMember.MemberInfo {
- playerData := &serverproto.GuildBattleSelfData{
- Uid: member.MemberId,
- GuildId: guildId2,
- ChallengeNum: model.GlobalGuildBattleChallengeNum,
- }
- if member.Title == Guild_Title_Pre {
- playerData.GuildPre = true
- }
- this.playerDataList[member.MemberId] = playerData
- this.changePlayerDataList.Add(member.MemberId)
- }
- }
- this.saveBattlePlayerData()
- }
- func (this *GuildBattle) getPlayerData(uid, guildId uint64) *serverproto.GuildBattleSelfData {
- playerData, ok := this.playerDataList[uid]
- if !ok {
- //从数据库中加载
- keyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
- fieldStr := "uid:" + strconv.FormatUint(uid, 10)
- retData, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
- if err == nil {
- playerData = &serverproto.GuildBattleSelfData{}
- err2 := model.GetDecodeMessage(playerData, retData)
- if err2 == nil {
- this.playerDataList[uid] = playerData
- util.ErrorF("GuildBattle id=%v idx=%v base err", this.mag.guildBattleId, this.guildBattleIdx)
- } else {
- playerData = nil
- }
- }
- /*
- if playerData == nil {
- playerData = &serverproto.GuildBattleSelfData{
- Uid: uid,
- GuildId: guildId,
- ChallengeNum: model.GlobalGuildBattleChallengeNum,
- }
- }
- */
- }
- return playerData
- }
- func (this *GuildBattle) checkBattleFinish() bool {
- nowTime := util.GetTimeMilliseconds()
- //是否固定时间结束
- if Guild_Battle_Tournament_Begin_Match <= this.guildBattleIdx && this.guildBattleIdx <= Guild_Battle_Tournament_End_Match {
- if this.mag.tournamentEnd < nowTime {
- return true
- }
- } else if Guild_Battle_Seminal_Begin_Match == this.guildBattleIdx || this.guildBattleIdx == Guild_Battle_Seminal_End_Match {
- if this.mag.semifinalsEnd < nowTime {
- return true
- }
- } else if Guild_Battle_Final_Match == this.guildBattleIdx {
- if this.mag.finalEnd < nowTime {
- return true
- }
- }
- //是否固定时间结束
- if this.battleBaseData.BattleEndTime < nowTime && this.battleBaseData.BattleEndTime > 0 {
- return true
- }
- return false
- }
- func (this *GuildBattle) BattleInfoReq(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleInfoAck) {
- //参赛者和观战者区分
- bWatch := true
- for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
- if this.battleBaseData.ScoreList[idx].Key == guildId {
- bWatch = false
- break
- }
- }
- if !bWatch {
- playerData := this.getPlayerData(uid, guildId)
- if playerData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- ackMsg.SelfData = playerData
- }
- ackMsg.GuildBattleIdx = this.guildBattleIdx
- ackMsg.BattleStartTime = this.battleBaseData.BattleStartTime
- ackMsg.BattleEndTime = this.battleBaseData.BattleEndTime
- posIdxData := this.getPosInfoByUid(uid)
- if posIdxData != nil {
- ackMsg.ChallengePosData = posIdxData
- }
- for _, countPart := range this.battleBaseData.ScoreList {
- keyValue := &serverproto.KeyValueType64{
- Key: countPart.Key,
- Value: countPart.Value,
- Value2: countPart.Value2,
- Value3: countPart.Value3,
- StrVal: countPart.StrVal,
- }
- ackMsg.BattleDataList = append(ackMsg.BattleDataList, keyValue)
- }
- }
- func (this *GuildBattle) BattleSetting(uid, guildId uint64, autoChallenge, autoReborn bool, ackMsg *serverproto.SCGuildBattleSettingAck) {
- playerData := this.getPlayerData(uid, guildId)
- if playerData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- playerData.AutoChallenge = autoChallenge
- playerData.AutoReborn = autoReborn
- this.changePlayerDataList.Add(uid)
- ackMsg.SelfData = playerData
- }
- func (this *GuildBattle) BuyChallengeNum(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleBuyChallengeAck) {
- playerData := this.getPlayerData(uid, guildId)
- if playerData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- if this.checkBattleFinish() {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
- return
- }
- this.changePlayerDataList.Add(uid)
- ackMsg.SelfData = playerData
- }
- func (this *GuildBattle) BuyBattleBuff(uid, guildId uint64, buffList []int32, ackMsg *serverproto.SSGuildBattleBuyBuffAck) {
- playerData := this.getPlayerData(uid, guildId)
- if playerData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- if this.checkBattleFinish() {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
- return
- }
- for _, data := range buffList {
- bFind := false
- for index, playerBuff := range playerData.BuffList {
- if playerBuff.Key == data {
- bFind = true
- level := playerBuff.Value + 1
- //判定表中是否存在level这一级的buff
- buffData := model.GuildBattleBuffList[data]
- if buffData == nil {
- continue
- }
- //存在level级buff,判定金币够不够,不够直接回退
- buyInfo := buffData.BuyData[level]
- if buyInfo == nil {
- continue
- }
- ackMsg.NeedRes = append(ackMsg.NeedRes, &serverproto.KeyValueType{
- Key: buyInfo.Key,
- Value: buyInfo.Value,
- })
- //设置
- playerData.BuffList[index].Value = level
- break
- }
- }
- //没找到对应的BUFF,则新购买
- if bFind == false {
- buffData := model.GuildBattleBuffList[data]
- if buffData == nil {
- continue
- }
- //存在level级buff,判定金币够不够,不够直接回退
- buyInfo := buffData.BuyData[1]
- if buyInfo == nil {
- continue
- }
- //购买一级的
- playerData.BuffList = append(playerData.BuffList, &serverproto.KeyValueType{
- Key: data,
- Value: 1,
- })
- ackMsg.NeedRes = append(ackMsg.NeedRes, &serverproto.KeyValueType{
- Key: buyInfo.Key,
- Value: buyInfo.Value,
- })
- }
- }
- ackMsg.SelfData = playerData
- this.changePlayerDataList.Add(uid)
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_OK)
- }
- func (this *GuildBattle) ResetRebornCD(uid, guildId uint64, ackMsg *serverproto.SCGuildBattleRebornAck) {
- playerData := this.getPlayerData(uid, guildId)
- if playerData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- nowTime := util.GetTimeMilliseconds()
- if playerData.RebornEndTime <= nowTime {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_HAS_REBORN)
- return
- }
- playerData.RebornEndTime = 0
- this.changePlayerDataList.Add(uid)
- ackMsg.SelfData = playerData
- }
- func (this *GuildBattle) initPosListInfo() {
- if this.bPosInit {
- return
- }
- this.bPosInit = true
- var guildPosIdxNum = map[uint64]int32{}
- battleKeyStr := GuildBattlePrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
- for idx := 1; idx <= PosIdxMaxNum; idx++ {
- fieldStr := "idx:" + strconv.Itoa(idx)
- retData, err := service.GetRedis().HGet(battleKeyStr, fieldStr).Result()
- if err != nil {
- continue
- }
- posIdxData := &serverproto.GuildPosIdxData{}
- err = model.GetDecodeMessage(posIdxData, retData)
- if err != nil {
- continue
- }
- this.posIdxDataList[int32(posIdxData.PosIdx)] = posIdxData
- if posIdxData.OwnerBrief != nil {
- this.posIdxDataUidList[posIdxData.OwnerBrief.Uid] = posIdxData
- }
- if posIdxData.GuildId > 0 {
- guildPosIdxNum[posIdxData.GuildId]++
- }
- }
- for k, v := range guildPosIdxNum {
- bFind := false
- for idx := 0; idx < len(this.battleBaseData.ScoreList); idx++ {
- if this.battleBaseData.ScoreList[idx].Key == k {
- //value=积分数量
- //valu2=道场数量
- this.battleBaseData.ScoreList[idx].Value2 = v
- this.baseChange = true
- bFind = true
- break
- }
- }
- if !bFind {
- this.battleBaseData.ScoreList = append(this.battleBaseData.ScoreList,
- &serverproto.KeyValueType64{Key: k, Value2: v})
- this.baseChange = true
- }
- }
- }
- func (this *GuildBattle) getPosInfo(posIdx int32) *serverproto.GuildPosIdxData {
- this.initPosListInfo()
- posIdxData, ok := this.posIdxDataList[posIdx]
- if ok {
- return posIdxData
- } else {
- //无公会占领地块
- posIdxData := &serverproto.GuildPosIdxData{
- PosIdx: posIdx,
- }
- this.posIdxDataList[posIdx] = posIdxData
- return posIdxData
- }
- }
- func (this *GuildBattle) getPosInfoByUid(uid uint64) *serverproto.GuildPosIdxData {
- this.initPosListInfo()
- posIdxData, ok := this.posIdxDataUidList[uid]
- if ok {
- return posIdxData
- }
- return nil
- }
- func (this *GuildBattle) GetBuffTotalLevel(playerData *serverproto.GuildBattleSelfData) int32 {
- if playerData == nil || len(playerData.BuffList) <= 0 {
- return 0
- }
- totalLevel := int32(0)
- for _, data := range playerData.BuffList {
- totalLevel += data.Value
- }
- return totalLevel
- }
- func (this *GuildBattle) GetBattlePosIdxList(posIdxList []int32, ackMsg *serverproto.SCGuildBattlePosIdxListAck) {
- if this.checkBattleFinish() {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
- return
- }
- for idx := 0; idx < len(posIdxList); idx++ {
- if posIdxList[idx] <= 0 || posIdxList[idx] > int32(len(model.ConvertGuildBattleList)) {
- continue
- }
- tmpPosIdxData := serverproto.GuildPosIdxData{}
- posIdxData := this.getPosInfo(posIdxList[idx])
- if posIdxData == nil {
- continue
- }
- tmpPosIdxData = *posIdxData
- //玩家不在该位置但是也也能属于该玩家
- if posIdxData.OwnerBrief != nil {
- posIdxData.OwnerBrief = model2.GetPlayerBriefInfoWithoutCache(posIdxData.OwnerBrief.Uid)
- }
- if posIdxData.OwnerBrief != nil {
- var brief serverproto.CommonPlayerBriefInfo
- brief = *posIdxData.OwnerBrief
- playerData := this.getPlayerData(posIdxData.OwnerBrief.Uid, posIdxData.GuildId)
- if playerData != nil {
- totalLevel := this.GetBuffTotalLevel(playerData)
- if totalLevel > 0 {
- brief.FightPower += brief.FightPower * totalLevel * model.GlobalGuildBattleFightPower / 100
- }
- }
- tmpPosIdxData.OwnerBrief = &brief
- }
- ackMsg.PosDataList = append(ackMsg.PosDataList, &tmpPosIdxData)
- }
- }
- func (this *GuildBattle) GetPosIdxLog(beginTime uint64, ackMsg *serverproto.SCGuildBattleLogAck) {
- /*
- if this.checkBattleFinish() {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
- return
- }
- */
- //加载日志
- if len(this.logList) <= 0 {
- keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
- retList, err := service.GetRedis().LRange(keyStr, 0, -1).Result()
- if err != nil {
- return
- }
- for idx := 0; idx < len(retList); idx++ {
- logData := &serverproto.GuildBattleLog{}
- err = model.GetDecodeMessage(logData, retList[idx])
- if err != nil {
- continue
- }
- this.logList = append(this.logList, logData)
- }
- sort.Slice(this.logList, func(i, j int) bool {
- return this.logList[i].RecordTime < this.logList[j].RecordTime
- })
- }
- //获取最新列表
- if beginTime <= 0 {
- starIdx := len(this.logList)
- if starIdx > 0 {
- for idx := starIdx - 1; idx >= 0; idx-- {
- ackMsg.LogList = append(ackMsg.LogList, this.logList[idx])
- if len(ackMsg.LogList) >= 10 {
- break
- }
- }
- }
- } else {
- for idx := len(this.logList) - 1; idx >= 0; idx-- {
- if this.logList[idx].RecordTime < beginTime {
- ackMsg.LogList = append(ackMsg.LogList, this.logList[idx])
- if len(ackMsg.LogList) >= 10 {
- break
- }
- }
- }
- }
- if len(ackMsg.LogList) < 10 {
- ackMsg.IsEnd = true
- }
- }
- func (this *GuildBattle) outPosIdx(bFight bool, uid uint64, nowTime uint64) bool {
- posIdxData := this.getPosInfoByUid(uid)
- if posIdxData == nil {
- return false
- }
- if posIdxData.OwnerBrief != nil && posIdxData.OwnerBrief.Uid == uid {
- if bFight {
- posIdxData.GuildId = 0
- posIdxData.Uid = 0
- //因为被打败离开,才要清,如果是主动离开。很可能这块地已经存在战斗了。不能清战斗数据
- posIdxData.FightEndTime = 0
- posIdxData.FightStartTime = 0
- posIdxData.ChallengeUid = 0
- posIdxData.FightPingTime = 0
- }
- posIdxData.OwnerBrief = nil
- this.changePosIdxList.Add(posIdxData.PosIdx)
- delete(this.posIdxDataUidList, uid)
- return true
- }
- return false
- }
- func (this *GuildBattle) inPosIdx(posIdxData *serverproto.GuildPosIdxData, uid, selfGuildId uint64, nowTime uint64) bool {
- posIdxData.OwnerBrief = &serverproto.CommonPlayerBriefInfo{Uid: uid} //需要重新获取
- posIdxData.FightEndTime = 0
- posIdxData.FightStartTime = 0
- posIdxData.ChallengeUid = 0
- posIdxData.FightPingTime = 0
- posIdxData.GuildId = selfGuildId
- posIdxData.Uid = uid //当前地块标记为该玩家
- this.posIdxDataUidList[uid] = posIdxData
- this.changePosIdxList.Add(posIdxData.PosIdx)
- return true
- }
- func (this *GuildBattle) BattleChallengePre(posIdx int32, selfGuildId uint64, bfInfo *serverproto.CommonPlayerBriefInfo,
- curRmb uint64, buyNum int32, ssAckMsg *serverproto.SSGuildBattleChallengeAck) {
- ssAckMsg.PosIdx = posIdx
- ssAckMsg.GuildBattleIdx = this.guildBattleIdx
- if this.checkBattleFinish() {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FINISH)
- return
- }
- posIdxData := this.getPosInfo(posIdx)
- if posIdxData == nil {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- nowTime := util.GetTimeMilliseconds()
- playerData := this.getPlayerData(bfInfo.Uid, selfGuildId)
- if playerData == nil {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_DAOCHANG100_NO_CHALLENGE_COUNT)
- return
- }
- var costRmb uint64 = 0
- //判断是否处于虚弱状态
- if playerData.RebornEndTime > 0 && playerData.RebornEndTime > nowTime {
- if playerData.AutoReborn {
- if curRmb >= costRmb+model.GlobalGuildBattleRebornCost {
- costRmb += model.GlobalGuildBattleRebornCost
- playerData.RebornEndTime = 0
- this.changePlayerDataList.Add(bfInfo.Uid)
- ssAckMsg.CostRmb = costRmb
- ssAckMsg.SelfData = playerData
- } else {
- playerData.AutoReborn = false
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_CHALLENGE_NO_RMB)
- return
- }
- } else {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_REBORN_TIME)
- return
- }
- }
- //当前正在战斗
- if posIdxData.FightEndTime > 0 && posIdxData.FightEndTime > nowTime {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FIGHTING)
- ssAckMsg.ChallengePosData = posIdxData
- return
- }
- //该玩家是否已经占领该位置
- if posIdxData.OwnerBrief != nil && posIdxData.OwnerBrief.Uid == bfInfo.Uid {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_HAS)
- ssAckMsg.ChallengePosData = posIdxData
- return
- }
- //同公会无法进行挑战
- if posIdxData.OwnerBrief != nil && posIdxData.GuildId == selfGuildId {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_SAME_GUILD)
- ssAckMsg.ChallengePosData = posIdxData
- return
- }
- //直接占领
- if posIdxData.GuildId == 0 || posIdxData.OwnerBrief == nil {
- //处理地块数量
- //占领着地块自加
- guildId := GuildMag.GetPlayerGuildId(bfInfo.Uid)
- if posIdxData.GuildId == 0 || posIdxData.GuildId != 0 && posIdxData.GuildId != guildId {
- //抵款自家
- for idx, data := range this.battleBaseData.ScoreList {
- if data.Key == guildId {
- this.battleBaseData.ScoreList[idx].Value2++
- }
- }
- }
- //原有地块持有者地块自减
- if posIdxData.GuildId != 0 && posIdxData.GuildId != guildId {
- for idx, data := range this.battleBaseData.ScoreList {
- if data.Key == posIdxData.GuildId {
- this.battleBaseData.ScoreList[idx].Value2--
- }
- }
- }
- //离开玩家当前战力的地块(不在位置但还是属于该玩家所在公会)
- this.outPosIdx(false, bfInfo.Uid, nowTime)
- this.inPosIdx(posIdxData, bfInfo.Uid, selfGuildId, nowTime)
- ssAckMsg.SelfData = playerData
- ssAckMsg.ChallengePosData = posIdxData
- } else {
- //挑战次数判断//优先用购买次数
- if buyNum > 0 {
- ssAckMsg.UseBuyNum = true
- util.InfoF("BattleChallengePre fight use buy num ")
- } else if playerData.ChallengeNum > 0 {
- playerData.ChallengeNum--
- this.changePlayerDataList.Add(bfInfo.Uid)
- util.InfoF("BattleChallengePre fight use init num ")
- } else {
- if playerData.AutoChallenge {
- if curRmb < costRmb+model.GlobalGuildBattleBuyChallengeCost {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_CHALLENGE_NO_RMB)
- //todo wangzhaocan 挑战次数不够,并且金币也不够,取消自动购买
- playerData.AutoChallenge = false
- this.changePlayerDataList.Add(bfInfo.Uid)
- return
- }
- //挑战消耗的金币(自动消耗)
- costRmb += model.GlobalGuildBattleBuyChallengeCost
- util.InfoF("BattleChallengePre fight use gold ")
- } else {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_DAOCHANG100_NO_CHALLENGE_COUNT)
- return
- }
- }
- util.InfoF("BattleChallengePre fight use buy num cost %v", costRmb)
- ssAckMsg.SelfData = playerData
- ssAckMsg.CostRmb = costRmb
- //挑战玩家
- if posIdxData.OwnerBrief != nil {
- fightInfo := model2.GetRoleGuildBattleFightInfoWithoutCache(posIdxData.OwnerBrief.Uid)
- if fightInfo != nil {
- posIdxData.FightEndTime = nowTime + PosIdxFightMaxTime
- posIdxData.FightStartTime = nowTime
- posIdxData.ChallengeUid = bfInfo.Uid
- posIdxData.FightPingTime = nowTime
- this.changePosIdxList.Add(posIdxData.PosIdx)
- ssAckMsg.FightInfo = fightInfo
- //bufflist
- fightPlayerData := this.getPlayerData(posIdxData.OwnerBrief.Uid, posIdxData.GuildId)
- if fightPlayerData != nil {
- ssAckMsg.FightBuffList = fightPlayerData.BuffList
- }
- ssAckMsg.SelfBuffList = playerData.BuffList
- return
- }
- }
- posIdxData.OwnerBrief = nil
- posIdxData.FightEndTime = 0
- posIdxData.FightStartTime = 0
- posIdxData.GuildId = 0
- posIdxData.ChallengeUid = 0
- posIdxData.FightPingTime = 0
- }
- }
- func (this *GuildBattle) BattleChallengePing(challengeUid uint64, posIdx int32, ssAckMsg *serverproto.SCGuildBattleChallengePingAck) {
- if this.checkBattleFinish() {
- return
- }
- posIdxData := this.getPosInfo(posIdx)
- if posIdxData == nil {
- return
- }
- //当前玩家没有挑战该位置
- if posIdxData.ChallengeUid != challengeUid {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
- return
- }
- nowTime := util.GetTimeMilliseconds()
- if posIdxData.FightPingTime > 0 && posIdxData.FightPingTime+60*1000 < nowTime {
- //战斗无效
- posIdxData.ChallengeUid = 0
- posIdxData.FightPingTime = 0
- posIdxData.FightStartTime = 0
- posIdxData.FightEndTime = 0
- this.changePosIdxList.Add(posIdxData.PosIdx)
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
- return
- }
- posIdxData.FightPingTime = nowTime
- posIdxData.FightEndTime = nowTime + PosIdxFightMaxTime
- }
- func (this *GuildBattle) battleResultValidCheck(selfUid uint64, selfFightPower int32, selfGuildId uint64, beChallengeFighter *serverproto.GuildPosIdxData) bool {
- if beChallengeFighter.Uid <= 0 {
- return true
- }
- if len(model.GlobalDaoChang100BattleCheck) >= 3 {
- playerData := this.getPlayerData(selfUid, selfGuildId)
- beChallengePlayerData := this.getPlayerData(beChallengeFighter.Uid, beChallengeFighter.GuildId)
- if playerData == nil || beChallengePlayerData == nil {
- //找不到对应的玩家数据,这个应该不让过了。
- return false
- }
- var challengePower int32 = 0
- //拉取一下最新的战力算了。。
- tmpBf := model2.GetPlayerBriefInfoWithoutCache(beChallengeFighter.Uid)
- if tmpBf != nil {
- challengePower = tmpBf.FightPower
- }
- //进攻方的战力
- playerTotalLevel := this.GetBuffTotalLevel(playerData)
- if playerTotalLevel > 0 {
- selfFightPower += selfFightPower * playerTotalLevel * model.GlobalGuildBattleFightPower / 100
- }
- //防守方的战力
- beChallengeTotalLevel := this.GetBuffTotalLevel(beChallengePlayerData)
- if beChallengeTotalLevel > 0 {
- challengePower += challengePower * beChallengeTotalLevel * model.GlobalGuildBattleFightPower / 100
- }
- p1 := (float64(challengePower) - model.GlobalDaoChang100BattleCheck[0]) * model.GlobalDaoChang100BattleCheck[1]
- p2 := float64(challengePower) * model.GlobalDaoChang100BattleCheck[2]
- p := int32(math.Max(p1, p2) + 0.5)
- util.InfoF("uid=%v battleResultValidCheckDaoChang100 selfFightPower=%v bechallengePower=%v", selfUid, selfFightPower, challengePower)
- if selfFightPower <= p {
- util.ErrorF("uid=%v battleResultValidCheckDaoChang100 selfFightPower=%v bechallengePower=%v invalid", selfUid, selfFightPower, challengePower)
- return false
- }
- }
- return true
- }
- func (this *GuildBattle) BattleChallengeResult(posIdx int32, challengeGuildId uint64, bfInfo *serverproto.CommonPlayerBriefInfo,
- battleResult bool, ssAckMsg *serverproto.SCGuildBattleChallengeResultAck, ev rocommon.ProcEvent) {
- if this.checkBattleFinish() {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- posIdxData := this.getPosInfo(posIdx)
- if posIdxData == nil {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- //当前位置的挑战者不是该玩家
- if posIdxData.ChallengeUid != bfInfo.Uid {
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_INVALID)
- return
- }
- targetPosIdxDataBfInfo := &serverproto.CommonPlayerBriefInfo{}
- if posIdxData.OwnerBrief != nil {
- err := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.OwnerBrief.Uid, targetPosIdxDataBfInfo)
- if err == nil {
- posIdxData.OwnerBrief = targetPosIdxDataBfInfo
- }
- }
- //战力校验
- if posIdxData.Uid > 0 && battleResult {
- if !this.battleResultValidCheck(bfInfo.Uid, bfInfo.FightPower, challengeGuildId, posIdxData) {
- battleResult = false
- }
- }
- nowTime := util.GetTimeMilliseconds()
- posIdxData.FightEndTime = 0
- posIdxData.FightStartTime = 0
- posIdxData.FightPingTime = 0
- //挑战失败处理
- if !battleResult {
- if posIdxData.OwnerBrief != nil {
- //添加占领日志
- tmpSt := posIdxLogSt{
- logType: 2,
- posIdx: posIdx,
- challengeGuild: challengeGuildId,
- challengeNickName: bfInfo.NickName,
- challengeUid: bfInfo.Uid,
- posIdxUid: posIdxData.OwnerBrief.Uid,
- posIdxGuild: posIdxData.GuildId,
- }
- if posIdxData.OwnerBrief.NickName != "" {
- tmpSt.posIdxNickName = posIdxData.OwnerBrief.NickName
- }
- this.addPosIdxLog(tmpSt)
- } else if posIdxData.Uid != 0 {
- //添加占领日志
- ownerBrief := &serverproto.CommonPlayerBriefInfo{}
- err2 := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.Uid, ownerBrief)
- if err2 == nil {
- tmpSt := posIdxLogSt{
- logType: 2,
- posIdx: posIdx,
- challengeGuild: challengeGuildId,
- challengeNickName: bfInfo.NickName,
- challengeUid: bfInfo.Uid,
- posIdxUid: posIdxData.Uid,
- posIdxGuild: posIdxData.GuildId,
- }
- if ownerBrief.NickName != "" {
- tmpSt.posIdxNickName = ownerBrief.NickName
- }
- this.addPosIdxLog(tmpSt)
- }
- }
- //进攻失败处理//进攻玩家清除连胜,重置复活时间
- playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
- if playerData != nil {
- playerData.WinStrak = 0
- playerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
- this.changePlayerDataList.Add(bfInfo.Uid)
- }
- return
- }
- if posIdxData.OwnerBrief != nil {
- //原有地块持有者地块自减
- for idx, data := range this.battleBaseData.ScoreList {
- if data.Key == posIdxData.GuildId {
- this.battleBaseData.ScoreList[idx].Value2--
- } else if data.Key == challengeGuildId {
- this.battleBaseData.ScoreList[idx].Value2++
- }
- }
- //挑战玩家
- //老玩家离开占位
- oldUid := posIdxData.OwnerBrief.Uid
- oldUidNIckName := posIdxData.OwnerBrief.NickName
- oldUidGuildId := posIdxData.GuildId
- this.outPosIdx(true, oldUid, nowTime)
- oldPlayerData := this.getPlayerData(oldUid, oldUidGuildId)
- if oldPlayerData != nil {
- /* 策划说,防守失败,不清连胜
- if oldPlayerData.WinStrak > 0 {
- oldPlayerData.WinStrak = 0
- this.changePlayerDataList.Add(oldUid)
- }
- */
- //被击杀玩家虚弱状态通知
- oldPlayerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
- ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
- GuildBattleIdx: this.guildBattleIdx,
- NtfUid: oldUid,
- SelfData: oldPlayerData,
- }
- SendSocial(ntfMsg)
- this.changePlayerDataList.Add(oldUid)
- // model.ServiceReplay(ev, ntfMsg)
- }
- //当前挑战的玩家离开之前的占位
- this.outPosIdx(false, bfInfo.Uid, nowTime)
- //当前挑战的玩家进入挑战成功的占位
- this.inPosIdx(posIdxData, bfInfo.Uid, challengeGuildId, nowTime)
- //击杀次数记录
- playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
- if playerData != nil {
- playerData.WinNum++
- playerData.WinStrak++
- this.changePlayerDataList.Add(bfInfo.Uid)
- //玩家击杀排行榜
- UpdateGuildBattleRankList_Kill(this.mag.guildBattleId, this.guildBattleIdx,
- RankKillListType, playerData.WinNum, bfInfo.Uid)
- //通知玩家自己
- ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
- GuildBattleIdx: this.guildBattleIdx,
- NtfUid: bfInfo.Uid,
- SelfData: playerData,
- PosData: posIdxData,
- }
- model.ServiceReplay(ev, ntfMsg)
- }
- //添加占领日志
- tmpSt := posIdxLogSt{
- logType: 1,
- posIdx: posIdx,
- battleResult: true,
- challengeGuild: challengeGuildId,
- challengeNickName: bfInfo.NickName,
- challengeUid: bfInfo.Uid,
- posIdxUid: oldUid,
- posIdxGuild: oldUidGuildId,
- posIdxNickName: oldUidNIckName,
- }
- if playerData != nil && playerData.WinStrak > BattleWinNumTips {
- tmpSt.WinSteak = playerData.WinStrak
- }
- this.addPosIdxLog(tmpSt)
- } else {
- util.InfoF("BattleChallengeResult posIdxData.OwnerBrief is nil uid: %v", posIdxData.Uid)
- playerData := this.getPlayerData(bfInfo.Uid, challengeGuildId)
- if posIdxData.Uid != 0 && playerData != nil {
- for idx, data := range this.battleBaseData.ScoreList {
- if data.Key == posIdxData.GuildId {
- this.battleBaseData.ScoreList[idx].Value2--
- } else if data.Key == challengeGuildId {
- this.battleBaseData.ScoreList[idx].Value2++
- }
- }
- //找不到老玩家数据,非法战斗
- brief := &serverproto.CommonPlayerBriefInfo{}
- err2 := model2.GetSystemDataFromRedis(model2.RolePlayerBriefPrefix, posIdxData.Uid, brief)
- if err2 != nil {
- playerData.ChallengeNum++
- //您在战斗时已被他人击败,本场战斗无效,本次挑战无消耗
- this.changePlayerDataList.Add(bfInfo.Uid)
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_RESULT_INVALID)
- return
- }
- //挑战玩家
- //老玩家离开占位
- oldUid := posIdxData.Uid
- oldUidNIckName := brief.NickName
- oldUidGuildId := posIdxData.GuildId
- oldPlayerData := this.getPlayerData(oldUid, oldUidGuildId)
- if oldPlayerData != nil {
- //被击杀玩家虚弱状态通知
- oldPlayerData.RebornEndTime = nowTime + model.GlobalGuildBattleRebornCdTime
- ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
- GuildBattleIdx: this.guildBattleIdx,
- NtfUid: oldUid,
- SelfData: oldPlayerData,
- }
- SendSocial(ntfMsg)
- this.changePlayerDataList.Add(oldUid)
- }
- //当前挑战的玩家离开之前的占位
- this.outPosIdx(false, bfInfo.Uid, nowTime)
- //当前挑战的玩家进入挑战成功的占位
- this.inPosIdx(posIdxData, bfInfo.Uid, challengeGuildId, nowTime)
- //击杀次数记录
- playerData.WinNum++
- playerData.WinStrak++
- this.changePlayerDataList.Add(bfInfo.Uid)
- //玩家击杀排行榜
- UpdateGuildBattleRankList_Kill(this.mag.guildBattleId, this.guildBattleIdx,
- RankKillListType, playerData.WinNum, bfInfo.Uid)
- //通知玩家自己
- ntfMsg := &serverproto.SCGuildBattleDataChangeNtf{
- GuildBattleIdx: this.guildBattleIdx,
- NtfUid: bfInfo.Uid,
- SelfData: playerData,
- PosData: posIdxData,
- }
- model.ServiceReplay(ev, ntfMsg)
- //添加占领日志
- tmpSt := posIdxLogSt{
- logType: 1,
- posIdx: posIdx,
- battleResult: true,
- challengeGuild: challengeGuildId,
- challengeNickName: bfInfo.NickName,
- challengeUid: bfInfo.Uid,
- posIdxUid: oldUid,
- posIdxGuild: oldUidGuildId,
- posIdxNickName: oldUidNIckName,
- }
- if playerData.WinStrak > BattleWinNumTips {
- tmpSt.WinSteak = playerData.WinStrak
- }
- this.addPosIdxLog(tmpSt)
- return
- }
- //挑战成功但是该位置已经先被其他玩家占领
- //本次挑战无效
- if playerData != nil {
- playerData.ChallengeNum++
- //您在战斗时已被他人击败,本场战斗无效,本次挑战无消耗
- this.changePlayerDataList.Add(bfInfo.Uid)
- ssAckMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_BATTLE_RESULT_INVALID)
- }
- }
- }
- func (this *GuildBattle) BattlePKData(ssAckMsg *serverproto.SCGuildBattlePKDataAck) {
- ssAckMsg.BattleDataList = this.battleBaseData.ScoreList
- }
- func (this *GuildBattle) GetBattleWinState() (uint64, uint64, bool) {
- scoreList := this.battleBaseData.ScoreList
- if len(scoreList) == 2 { //满编队伍
- if scoreList[1].Key == 0 {
- return scoreList[0].Key, 0, true
- }
- }
- if !this.checkBattleFinish() {
- //战斗未结束无法获取胜利/失败状态
- return 0, 0, false
- }
- if len(scoreList) == 2 { //满编队伍
- if scoreList[0].Value == scoreList[1].Value {
- if scoreList[0].Value2 == scoreList[0].Value2 {
- return scoreList[0].Key, scoreList[1].Key, true
- } else if scoreList[0].Value2 < scoreList[0].Value2 {
- return scoreList[0].Key, scoreList[1].Key, true
- } else {
- return scoreList[1].Key, scoreList[0].Key, true
- }
- } else if scoreList[0].Value > scoreList[1].Value {
- return scoreList[0].Key, scoreList[1].Key, true
- } else {
- return scoreList[1].Key, scoreList[0].Key, true
- }
- }
- return 0, 0, false
- }
- type posIdxLogSt struct {
- logType int32
- posIdx int32
- battleResult bool
- challengeUid uint64
- challengeNickName string
- challengeGuild uint64
- posIdxUid uint64
- posIdxNickName string
- posIdxGuild uint64
- WinSteak int32 //连杀次数
- }
- // =1、{0}公会{1}击败了{2}公会的{3}的防守。{4}连杀!
- // =2、{0}公会{1}成功防守了{2}公会{3}的进攻。
- func (this *GuildBattle) addPosIdxLog(st posIdxLogSt) {
- logData := &serverproto.GuildBattleLog{
- Type: st.logType,
- PosIdx: st.posIdx,
- }
- if st.battleResult {
- logData.State = true
- }
- logData.RecordTime = util.GetTimeMilliseconds()
- logData.ChallengePlayerUid = st.challengeUid
- logData.ChallengeGuildId = st.challengeGuild
- logData.ChallengePlayerName = st.challengeNickName
- logData.TargetPlayerUid = st.posIdxUid
- logData.TargetGuildId = st.posIdxGuild
- logData.TargetPlayerName = st.posIdxNickName
- logData.WinSteak = st.WinSteak
- //加载日志
- keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
- if len(this.logList) <= 0 {
- //keyStr := GuildBattleLogPrefix + strconv.Itoa(int(this.mag.guildBattleId)) + "-" + strconv.Itoa(int(this.guildBattleIdx))
- retList, err := service.GetRedis().LRange(keyStr, 0, -1).Result()
- if err != nil {
- return
- }
- for idx := 0; idx < len(retList); idx++ {
- logData := &serverproto.GuildBattleLog{}
- err = model.GetDecodeMessage(logData, retList[idx])
- if err != nil {
- continue
- }
- this.logList = append(this.logList, logData)
- }
- sort.Slice(this.logList, func(i, j int) bool {
- return this.logList[i].RecordTime < this.logList[j].RecordTime
- })
- }
- this.logList = append(this.logList, logData)
- if len(this.logList) > int(MaxUidLogCount) {
- this.logList = append(this.logList[:0], this.logList[1:]...)
- }
- err, msgStr := model.GetEncodeMessage(logData)
- if err != nil {
- return
- }
- ret, err1 := service.GetRedis().LPush(keyStr, msgStr).Result()
- if err1 == nil && ret > MaxUidLogCount {
- service.GetRedis().LTrim(keyStr, 0, MaxUidLogCount-1)
- }
- }
- func (this *GuildBattle) GetCountPartData(ackMsg *serverproto.SCGuildBattleRankListAck) {
- if ackMsg == nil {
- return
- }
- winGuild, _, ok := this.GetBattleWinState()
- if ok {
- if winGuild == this.battleBaseData.ScoreList[0].Key {
- ackMsg.WinGuild = true
- } else {
- ackMsg.WinGuild = false
- }
- ackMsg.IsFinish = true
- } else {
- ackMsg.IsFinish = false
- }
- for _, countPart := range this.battleBaseData.ScoreList {
- keyValue := &serverproto.KeyValueType64{
- Key: countPart.Key,
- Value: countPart.Value,
- Value2: countPart.Value2,
- Value3: countPart.Value3,
- StrVal: countPart.StrVal,
- }
- ackMsg.BattleDataList = append(ackMsg.BattleDataList, keyValue)
- }
- }
- func (this *GuildBattle) ExchangeGuildPre(oldPreUid, curPreUid, guildId uint64) {
- oldPrePlayer := this.getPlayerData(oldPreUid, guildId)
- curPrePlayer := this.getPlayerData(curPreUid, guildId)
- if oldPrePlayer == nil || curPrePlayer == nil {
- return
- }
- oldPrePlayer.GuildPre = false
- curPrePlayer.GuildPre = true
- this.changePlayerDataList.Add(oldPrePlayer.Uid)
- this.changePlayerDataList.Add(curPrePlayer.Uid)
- }
|