| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309 |
- 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
- }
- //发送奖励给
- 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)
- }
|