role_guild.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. package model
  2. import (
  3. "rocommon/util"
  4. "roserver/baseserver/model"
  5. "roserver/serverproto"
  6. )
  7. const (
  8. MAX_APPLY_COUNT = 5
  9. Req_Gap_Time = 500
  10. )
  11. type RoleGuild struct {
  12. SaveObject
  13. roleGuild *serverproto.RoleGuild
  14. recommendID int32 //客户端请求次数
  15. //公会战临时数据
  16. guildBattlePosIdx int32
  17. guildBattleFightUid uint64
  18. lastReqTime uint64
  19. challengePosIdxTime uint64 //以免端时间内发起多次挑战
  20. }
  21. func newRoleGuild(r *Role) *RoleGuild {
  22. roleGuild := &RoleGuild{
  23. SaveObject: SaveObject{
  24. role: r,
  25. },
  26. roleGuild: &serverproto.RoleGuild{
  27. GuildActive: &serverproto.GuildActiveInfo{},
  28. GuildApply: &serverproto.RoleApplyInfo{},
  29. GuildBattle: &serverproto.GuildBattle{},
  30. DemonInfo: &serverproto.DemonInfo{},
  31. },
  32. recommendID: 0,
  33. lastReqTime: 0,
  34. }
  35. return roleGuild
  36. }
  37. //
  38. func (this *RoleGuild) Load(msg interface{}) bool {
  39. proRole := msg.(*serverproto.Role)
  40. this.roleGuild = proRole.RoleGuild
  41. if this.roleGuild.GuildActive == nil {
  42. this.roleGuild.GuildActive = &serverproto.GuildActiveInfo{}
  43. }
  44. if this.roleGuild.GuildApply == nil {
  45. this.roleGuild.GuildApply = &serverproto.RoleApplyInfo{}
  46. }
  47. if this.roleGuild.GuildBattle == nil {
  48. this.roleGuild.GuildBattle = &serverproto.GuildBattle{}
  49. }
  50. if this.roleGuild.DemonInfo == nil {
  51. this.roleGuild.DemonInfo = &serverproto.DemonInfo{}
  52. }
  53. // this.GuildBattleOnline()
  54. return true
  55. }
  56. func (this *RoleGuild) GetGuildInfo() *serverproto.RoleGuild {
  57. return this.roleGuild
  58. }
  59. func (this *RoleGuild) Save() {
  60. this.SetDirty(false)
  61. util.DebugF("uid=%v RoleGuild save...", this.role.GetUUid())
  62. saveMsg := &serverproto.SSGuildDataSaveReq{
  63. Guild: &serverproto.RoleGuild{},
  64. }
  65. saveMsg.Guild = this.roleGuild
  66. this.role.SendDb(saveMsg)
  67. }
  68. func (this *RoleGuild) DailyReset(notify bool) {
  69. globalData, ok := serverproto.GlobalCfgLoader[int32(serverproto.GlobalType_Global_Guild_Online_GuildActive)]
  70. if !ok {
  71. util.ErrorF("uid=%v RoleGuild DailyReset...", this.role.GetUUid())
  72. return
  73. }
  74. if this.roleGuild.DemonInfo != nil {
  75. this.roleGuild.DemonInfo.UseFightCount = 0
  76. this.roleGuild.DemonInfo.FreeFightCount = 0
  77. this.roleGuild.DemonInfo.BuyFightCount = 0
  78. this.SetDirty(true)
  79. }
  80. if this.roleGuild.GuildActive.DayActiveValue > 0 || this.roleGuild.QuitNum > 0 {
  81. //清除昨日公会活跃度获取值
  82. this.roleGuild.GuildActive.DayActiveValue = 0
  83. //每日重置退出公会次数
  84. this.roleGuild.QuitNum = 0
  85. this.SetDirty(true)
  86. }
  87. //每日活跃值
  88. if this.AddGuildActivity(globalData.IVal) {
  89. this.SetDirty(true)
  90. }
  91. if this.ClearBossFightCount() {
  92. this.SetDirty(true)
  93. }
  94. //通知客户端
  95. if notify == true {
  96. this.OnQuitGuildInfoChange()
  97. }
  98. }
  99. //普通boss单独清除
  100. func (this *RoleGuild) ClearBossFightCount() bool {
  101. ret := false
  102. if this.roleGuild.BossFight == nil || len(this.roleGuild.BossFight) <= 0 {
  103. return ret
  104. }
  105. for _, data := range this.roleGuild.BossFight {
  106. bossType := this.GetBossType(data.BossId)
  107. if bossType == 1 {
  108. // data.BossId = 0
  109. data.FightCount = 0
  110. ret = true
  111. }
  112. }
  113. return ret
  114. }
  115. func (this *RoleGuild) GetRecommendCount() int32 {
  116. count := this.recommendID%3 + 1
  117. this.recommendID++
  118. return count
  119. }
  120. func (this *RoleGuild) GetNextJoin() int64 {
  121. return this.roleGuild.NextJoin
  122. }
  123. func (this *RoleGuild) OnQuitGuildInfoChange() {
  124. ntfMsg := &serverproto.SCQuitGuildCDNtf{}
  125. ntfMsg.QuitNum = this.roleGuild.QuitNum
  126. ntfMsg.NextJoin = this.roleGuild.NextJoin
  127. this.role.ReplayGate(ntfMsg, true)
  128. }
  129. func (this *RoleGuild) CheckGuildUnlock() serverproto.ErrorCode {
  130. globalData, ok := serverproto.GlobalCfgLoader[int32(serverproto.GlobalType_Global_Guild_System_Unlock)]
  131. if !ok {
  132. return serverproto.ErrorCode_ERROR_GUILD_CONFIG_NOT_FOUND
  133. }
  134. if this.role.GetRoleBattle().GetLevelId() < globalData.IVal {
  135. return serverproto.ErrorCode_ERROR_GUILD_SYSTEM_LOCKED
  136. }
  137. return serverproto.ErrorCode_ERROR_OK
  138. }
  139. //所有申请全部撤销
  140. func (this *RoleGuild) CanBuildGuild(guildName string) serverproto.ErrorCode {
  141. if this.roleGuild.GuildId != 0 {
  142. return serverproto.ErrorCode_ERROR_GUILD_IN_GUILD
  143. }
  144. bSensitive := SensitiveUtil.IsMatch(guildName)
  145. if bSensitive == true {
  146. return serverproto.ErrorCode_ERROR_GUILD_NAME_SENSITIVE
  147. }
  148. bRet := this.CheckGuildUnlock()
  149. if bRet != serverproto.ErrorCode_ERROR_OK {
  150. return bRet
  151. }
  152. //检查道具是否足够
  153. var costItemList = map[int32]int32{}
  154. globalData, ok := serverproto.GlobalCfgLoader[int32(serverproto.GlobalType_Global_Guild_Build_Cost)]
  155. if !ok {
  156. return serverproto.ErrorCode_ERROR_GUILD_BUILD_ITEM_NOT_ENOUGH
  157. }
  158. itemId, itemNum := model.Str2Res(globalData.SVal)
  159. costItemList[itemId] = itemNum
  160. if this.role.CheckResLitNum(costItemList) == false {
  161. util.DebugF("uid=%v build_guild error resource not enough!! costItemList=%v", this.role.GetUUid(), costItemList)
  162. return serverproto.ErrorCode_ERROR_GUILD_BUILD_ITEM_NOT_ENOUGH
  163. }
  164. return serverproto.ErrorCode_ERROR_OK
  165. }
  166. func (this *RoleGuild) DisbandGuild() {
  167. this.roleGuild.GuildId = 0
  168. this.roleGuild.GuildName = ""
  169. this.SetDirty(true)
  170. }
  171. func (this *RoleGuild) CheckApply(guildId uint64) serverproto.ErrorCode {
  172. if guildId <= 0 {
  173. return serverproto.ErrorCode_ERROR_FAIL
  174. }
  175. bRet := this.CheckGuildUnlock()
  176. if bRet != serverproto.ErrorCode_ERROR_OK {
  177. return bRet
  178. }
  179. //判定加入公会冷却时间
  180. if this.roleGuild.NextJoin > int64(util.GetCurrentTime()) {
  181. return serverproto.ErrorCode_ERROR_GUILD_LEAVE_GUILD_CD
  182. }
  183. return serverproto.ErrorCode_ERROR_OK
  184. }
  185. func (this *RoleGuild) CanQuitGuild() serverproto.ErrorCode {
  186. return serverproto.ErrorCode_ERROR_OK
  187. }
  188. //离开公会
  189. func (this *RoleGuild) QuitGuild() {
  190. this.roleGuild.GuildId = 0
  191. this.roleGuild.GuildName = ""
  192. this.roleGuild.QuitNum += 1
  193. configCount := len(model.ConvertLeaveGuildTime)
  194. if this.roleGuild.QuitNum >= int32(configCount) {
  195. cdCount := model.ConvertLeaveGuildTime[int32(configCount)]
  196. this.roleGuild.NextJoin = int64(util.GetCurrentTime()) + int64(cdCount)*3600*1000
  197. } else {
  198. cdCount := model.ConvertLeaveGuildTime[int32(this.roleGuild.QuitNum)]
  199. this.roleGuild.NextJoin = int64(util.GetCurrentTime()) + int64(cdCount)*3600*1000
  200. }
  201. this.OnQuitGuildInfoChange()
  202. this.SetDirty(true)
  203. }
  204. func (this *RoleGuild) BuildGuildSuccess(guildBrief *serverproto.GuildBrief) {
  205. if guildBrief == nil {
  206. return
  207. }
  208. globalData, ok := serverproto.GlobalCfgLoader[int32(serverproto.GlobalType_Global_Guild_Build_Cost)]
  209. if !ok {
  210. return
  211. }
  212. var costItemList = map[int32]int32{}
  213. itemId, itemNum := model.Str2Res(globalData.SVal)
  214. costItemList[itemId] = itemNum
  215. if this.role.CheckResLitNum(costItemList) == false {
  216. util.DebugF("uid=%v build_guild error resource not enough!! costItemList=%v", this.role.GetUUid(), costItemList)
  217. return
  218. }
  219. this.role.DelItemList(costItemList, AddItemST{AddFrom: AddFrom_Guild})
  220. //扣除道具
  221. this.roleGuild.GuildId = guildBrief.GuildId
  222. this.roleGuild.GuildName = guildBrief.GuildName
  223. this.SetDirty(true)
  224. //Task
  225. TaskMagCheck(this.role, serverproto.TaskType_Guild_Join_Count, 1)
  226. }
  227. func (this *RoleGuild) SetOnlineGuildInfo(guildId uint64, name string) {
  228. //Task
  229. if guildId > 0 && this.roleGuild.GuildId != guildId {
  230. TaskMagCheck(this.role, serverproto.TaskType_Guild_Join_Count, 1)
  231. }
  232. if guildId != 0 && this.roleGuild.GuildId != guildId {
  233. globalData, ok := serverproto.GlobalCfgLoader[int32(serverproto.GlobalType_Global_Guild_Elite_Boss_FightCD)]
  234. if ok {
  235. this.roleGuild.EliteBossCd = util.GetCurrentTime() + uint64(globalData.IVal)*60*1000
  236. }
  237. }
  238. if guildId != this.roleGuild.GuildId ||
  239. (this.roleGuild.GuildId == guildId && this.roleGuild.GuildName != name) {
  240. this.roleGuild.GuildId = guildId
  241. this.roleGuild.GuildName = name
  242. this.SetDirty(true)
  243. }
  244. }
  245. func (this *RoleGuild) AddGuildActivity(activePoint int32) bool {
  246. if activePoint == 0 || this.roleGuild.GuildId == 0 {
  247. return false
  248. }
  249. globalData, ok := serverproto.GlobalCfgLoader[int32(serverproto.GlobalType_Global_Guild_Personal_Active)]
  250. if !ok {
  251. return false
  252. }
  253. if this.roleGuild.GuildActive == nil {
  254. this.roleGuild.GuildActive = &serverproto.GuildActiveInfo{}
  255. }
  256. if this.roleGuild.GuildActive.DayActiveValue >= globalData.IVal {
  257. return false
  258. }
  259. oldActive := this.roleGuild.GuildActive.DayActiveValue
  260. this.roleGuild.GuildActive.DayActiveValue += activePoint
  261. if this.roleGuild.GuildActive.DayActiveValue > globalData.IVal {
  262. this.roleGuild.GuildActive.DayActiveValue = globalData.IVal
  263. }
  264. addActive := this.roleGuild.GuildActive.DayActiveValue - oldActive
  265. this.roleGuild.GuildActive.ActiveValue += uint32(addActive)
  266. //判定活跃度上限
  267. ntfMsg := &serverproto.SSGuildOnAddActivityNtf{}
  268. ntfMsg.Uid = this.role.GetUUid()
  269. ntfMsg.GuildId = this.role.GetRoleGuildId()
  270. ntfMsg.Activity = activePoint
  271. this.role.SendGuild(ntfMsg)
  272. this.SetDirty(true)
  273. return true
  274. }
  275. func (this *RoleGuild) GMAddGuildActivity(activePoint int32) {
  276. if activePoint == 0 {
  277. return
  278. }
  279. this.roleGuild.GuildActive.DayActiveValue += activePoint
  280. this.roleGuild.GuildActive.ActiveValue += uint32(activePoint)
  281. //判定活跃度上限
  282. ntfMsg := &serverproto.SSGuildOnAddActivityNtf{}
  283. ntfMsg.Uid = this.role.GetUUid()
  284. ntfMsg.GuildId = this.role.GetRoleGuildId()
  285. ntfMsg.Activity = activePoint
  286. this.role.SendGuild(ntfMsg)
  287. }
  288. func (this *RoleGuild) ClearGuildApplyCD() {
  289. this.roleGuild.QuitNum = 0
  290. this.roleGuild.NextJoin = 0
  291. this.OnQuitGuildInfoChange()
  292. this.SetDirty(true)
  293. }
  294. func (this *RoleGuild) CheckBossFight(bossId uint32, damage uint32, fightTime uint32) serverproto.ErrorCode {
  295. //校验战斗时长
  296. cfg, ok := serverproto.GuildBossCfgLoader[int32(bossId)]
  297. if !ok {
  298. return serverproto.ErrorCode_ERROR_GUILD_CONFIG_NOT_FOUND
  299. }
  300. if fightTime > uint32(cfg.BattleTime) {
  301. return serverproto.ErrorCode_ERROR_FAIL
  302. }
  303. for _, data := range this.roleGuild.BossFight {
  304. if data.BossId == bossId {
  305. bossType := this.GetBossType(data.BossId)
  306. bossChallenge := cfg.Challenge
  307. if bossType == 1 {
  308. vipGuildBoss := this.role.GetRoleBase().GetVipData(model.Vip_System_GuildHunt)
  309. if vipGuildBoss > 0 {
  310. bossChallenge += vipGuildBoss
  311. }
  312. } else if bossType == 2 {
  313. if this.roleGuild.EliteBossCd > util.GetCurrentTime() {
  314. return serverproto.ErrorCode_ERROR_GUILD_BOSS_FIGHT_CD
  315. }
  316. }
  317. if data.FightCount >= bossChallenge {
  318. return serverproto.ErrorCode_ERROR_GUILD_BOSS_CHALLENGE_MAX
  319. }
  320. break
  321. }
  322. }
  323. return serverproto.ErrorCode_ERROR_OK
  324. }
  325. func (this *RoleGuild) GetGuildBossReward(bossId uint32, damage int32) map[int32]int32 {
  326. rewardRank, ok := model.ConvertGuildBossRewardRank[int32(bossId)]
  327. if !ok {
  328. return nil
  329. }
  330. for _, data := range rewardRank.Reward {
  331. if data.MinDam <= damage && damage <= data.MaxDam {
  332. bossReward, ok2 := model.ConvertGuildBossReward[data.RewardID]
  333. if !ok2 {
  334. return nil
  335. }
  336. return bossReward.RewardList
  337. }
  338. }
  339. return nil
  340. }
  341. func (this *RoleGuild) GuildBossFightSuccess(msg *serverproto.SCGuildBossChallengeAck) {
  342. //校验战斗次数,合法则自增
  343. var bFind = false
  344. for _, data := range this.roleGuild.BossFight {
  345. if data.BossId == msg.BossId {
  346. data.FightCount++
  347. bFind = true
  348. break
  349. }
  350. }
  351. if bFind == false {
  352. this.roleGuild.BossFight = append(this.roleGuild.BossFight, &serverproto.GuildBossTickTime{
  353. BossId: msg.BossId,
  354. FightCount: 1,
  355. })
  356. }
  357. //给奖励
  358. var addItemList = map[int32]int32{}
  359. addItemList = this.GetGuildBossReward(msg.BossId, int32(msg.Damage))
  360. if addItemList != nil && len(addItemList) > 0 {
  361. this.role.AddItemList(addItemList, AddFrom_GuildBoss, true)
  362. for itemId, itemNum := range addItemList {
  363. msg.Reward = append(msg.Reward, &serverproto.KeyValueType{
  364. Key: itemId,
  365. Value: itemNum,
  366. })
  367. }
  368. }
  369. this.SetDirty(true)
  370. TaskMagCheck(this.role, serverproto.TaskType_Guild_Boss_Normal_Count, 1)
  371. }
  372. func (this *RoleGuild) SetFightCount(msg *serverproto.SCGuildBossInfoAck) {
  373. if len(this.roleGuild.BossFight) <= 0 {
  374. return
  375. }
  376. msg.EliteBossCd = this.roleGuild.EliteBossCd
  377. for _, data := range this.roleGuild.BossFight {
  378. msg.BossFight = append(msg.BossFight, &serverproto.KeyValueType{
  379. Key: int32(data.BossId),
  380. Value: data.FightCount,
  381. })
  382. }
  383. this.SetDirty(true)
  384. }
  385. func (this *RoleGuild) GetBossType(bossId uint32) int32 {
  386. cfg, ok := serverproto.GuildBossCfgLoader[int32(bossId)]
  387. if !ok {
  388. return -1
  389. }
  390. return cfg.BossType
  391. }
  392. func (this *RoleGuild) BossRefreshData(ntfMsg *serverproto.SSGuildBossRefreshNtf) {
  393. if len(ntfMsg.RefTime) <= 0 || len(ntfMsg.RefTime) > 20 {
  394. return
  395. }
  396. var bChanged = true
  397. for _, ntfRef := range ntfMsg.RefTime {
  398. if this.GetBossType(ntfRef.BossId) != 2 {
  399. continue
  400. }
  401. var bFind = false
  402. for _, roleRef := range this.roleGuild.BossFight {
  403. if ntfRef.BossId == roleRef.BossId {
  404. bFind = true
  405. if ntfRef.RefreshTime != roleRef.RefreshTime {
  406. roleRef.RefreshTime = ntfRef.RefreshTime
  407. roleRef.FightCount = 0
  408. bChanged = true
  409. }
  410. }
  411. }
  412. if bFind == false {
  413. this.roleGuild.BossFight = append(this.roleGuild.BossFight, &serverproto.GuildBossTickTime{
  414. BossId: ntfRef.BossId,
  415. FightCount: 0,
  416. RefreshTime: ntfRef.RefreshTime,
  417. })
  418. bChanged = true
  419. }
  420. }
  421. if bChanged == true {
  422. this.SetDirty(true)
  423. }
  424. }
  425. func (this *RoleGuild) GetBossFightCount(bossId uint32) int32 {
  426. for _, roleRef := range this.roleGuild.BossFight {
  427. if roleRef.BossId == bossId {
  428. return roleRef.FightCount
  429. }
  430. }
  431. return 0
  432. }
  433. func (this *RoleGuild) GuildNameChange(guildId uint64, name string) {
  434. if guildId == this.roleGuild.GuildId {
  435. this.roleGuild.GuildName = name
  436. this.SetDirty(true)
  437. }
  438. }
  439. func (this *RoleGuild) HasBattleCount(msg *serverproto.SSOnlineGuildInfoAck) bool {
  440. if msg == nil || len(msg.Boss) <= 0 {
  441. return false
  442. }
  443. for _, bossId := range msg.Boss {
  444. bFind := false
  445. for _, data := range this.roleGuild.BossFight {
  446. if data.BossId == bossId {
  447. bFind = true
  448. cfg, ok := serverproto.GuildBossCfgLoader[int32(bossId)]
  449. if !ok {
  450. break
  451. }
  452. bossType := this.GetBossType(data.BossId)
  453. bossChallenge := cfg.Challenge
  454. if bossType == 1 {
  455. vipGuildBoss := this.role.GetRoleBase().GetVipData(model.Vip_System_GuildHunt)
  456. if vipGuildBoss > 0 {
  457. bossChallenge += vipGuildBoss
  458. }
  459. }
  460. if data.FightCount >= bossChallenge {
  461. break
  462. }
  463. //只要有1个可以挑战,就返回TRUE
  464. return true
  465. }
  466. }
  467. //有可以战斗的boss,但是没有战斗记录,则返回true
  468. if bFind == false {
  469. return true
  470. }
  471. }
  472. return false
  473. }
  474. func (this *RoleGuild) GuildBattleBuyChallengeNum(msg *serverproto.CSGuildBattleBuyChallengeReq) {
  475. ackMsg := &serverproto.SCGuildBattleBuyChallengeAck{}
  476. if msg.GuildBattleIdx > 7 || msg.GuildBattleIdx <= 0 {
  477. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  478. this.role.ReplayGate(ackMsg, true)
  479. return
  480. }
  481. var buyCost uint64 = 50
  482. if this.role.GetResNum(int32(serverproto.ResType_Res_Rmb)) < buyCost {
  483. ackMsg.Error = int32(this.role.GetResNotice(int32(serverproto.ResType_Res_Rmb)))
  484. this.role.ReplayGate(ackMsg, true)
  485. return
  486. }
  487. this.role.DelItem(int32(serverproto.ResType_Res_Rmb), int32(buyCost), AddItemST{AddFrom: AddFrom_GuildBattle})
  488. msg.SelfGuildId = uint64(this.roleGuild.GuildId)
  489. this.role.SendGuild(msg)
  490. }
  491. func (this *RoleGuild) GuildBattleResetReborn(msg *serverproto.CSGuildBattleRebornReq) {
  492. ackMsg := &serverproto.SCGuildBattleRebornAck{}
  493. if msg.GuildBattleIdx > 7 || msg.GuildBattleIdx <= 0 {
  494. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  495. this.role.ReplayGate(ackMsg, true)
  496. return
  497. }
  498. if this.role.GetResNum(int32(serverproto.ResType_Res_Rmb)) < model.GlobalGuildBattleRebornCost {
  499. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_RMB_NOT_ENOUGH)
  500. this.role.ReplayGate(ackMsg, true)
  501. return
  502. }
  503. msg.SelfGuildId = uint64(this.roleGuild.GuildId)
  504. this.role.SendGuild(msg)
  505. }
  506. func (this *RoleGuild) OnGuildBattleResetReborn(ackMsg *serverproto.SCGuildBattleRebornAck) {
  507. if ackMsg.Error == int32(serverproto.ErrorCode_ERROR_OK) {
  508. //扣除资源
  509. this.role.DelItem(int32(serverproto.ResType_Res_Rmb), int32(model.GlobalGuildBattleRebornCost),
  510. AddItemST{AddFrom: AddFrom_GuildBattle})
  511. }
  512. this.role.ReplayGate(ackMsg, true)
  513. }
  514. func (this *RoleGuild) GuildBattleChallengePre(battleIdx, posIdx int32) serverproto.ErrorCode {
  515. if this.roleGuild.GuildId <= 0 {
  516. return serverproto.ErrorCode_ERROR_FAIL
  517. }
  518. nowTime := util.GetTimeMilliseconds()
  519. if this.challengePosIdxTime > 0 && (nowTime-this.challengePosIdxTime) < 1*60*1000 {
  520. return serverproto.ErrorCode_ERROR_GUILDBATTLE_FIGHTING
  521. }
  522. this.challengePosIdxTime = nowTime
  523. ssMsg := &serverproto.SSGuildBattleChallengeReq{
  524. GuildBattleIdx: battleIdx,
  525. PosIdx: posIdx,
  526. SelfGuildId: uint64(this.roleGuild.GuildId),
  527. }
  528. if this.roleGuild.GuildBattle != nil {
  529. ssMsg.BuyChallengeNum = this.roleGuild.GuildBattle.ChallengeNum
  530. }
  531. ssMsg.BfInfo = &serverproto.CommonPlayerBriefInfo{}
  532. this.role.GetRoleBriefInfo(ssMsg.BfInfo)
  533. ssMsg.CurRmb = this.role.GetResNum(int32(serverproto.ResType_Res_Rmb))
  534. this.role.SendGuild(ssMsg)
  535. return serverproto.ErrorCode_ERROR_OK
  536. }
  537. func (this *RoleGuild) OnGuildBattleChallengePre(ssAckMsg *serverproto.SSGuildBattleChallengeAck) {
  538. ackMsg := &serverproto.SCGuildBattleChallengeAck{
  539. Error: ssAckMsg.Error,
  540. GuildBattleIdx: ssAckMsg.GuildBattleIdx,
  541. PosIdx: ssAckMsg.PosIdx,
  542. SelfData: ssAckMsg.SelfData,
  543. }
  544. if ssAckMsg.UseBuyNum == true {
  545. if this.roleGuild.GuildBattle != nil && this.roleGuild.GuildBattle.ChallengeNum > 0 {
  546. this.roleGuild.GuildBattle.ChallengeNum--
  547. } else {
  548. //打印日志
  549. }
  550. }
  551. //先购买虚弱,但是无法挑战
  552. //扣除资源(花费金币方式来进行挑战/重置复活虚弱)
  553. if ssAckMsg.CostRmb > 0 {
  554. this.role.DelItem(int32(serverproto.ResType_Res_Rmb), int32(ssAckMsg.CostRmb),
  555. AddItemST{AddFrom: AddFrom_GuildBattle})
  556. }
  557. if ackMsg.SelfData != nil {
  558. ntfMsg := &serverproto.SCGuildBattleChallengeNtf{
  559. GuildBattleIdx: ssAckMsg.GuildBattleIdx,
  560. PosIdx: ssAckMsg.PosIdx,
  561. SelfData: ssAckMsg.SelfData,
  562. }
  563. if this.roleGuild.GuildBattle != nil {
  564. ntfMsg.BuyChallengeNum = this.roleGuild.GuildBattle.ChallengeNum
  565. }
  566. this.role.ReplayGate(ntfMsg, true)
  567. }
  568. if ssAckMsg.Error == int32(serverproto.ErrorCode_ERROR_OK) {
  569. ackMsg.FightInfo = ssAckMsg.FightInfo
  570. ackMsg.SelfBuffList = ssAckMsg.SelfBuffList
  571. ackMsg.FightBuffList = ssAckMsg.FightBuffList
  572. ackMsg.ChallengePosData = ssAckMsg.ChallengePosData
  573. this.guildBattlePosIdx = ssAckMsg.PosIdx
  574. if ssAckMsg.FightInfo != nil {
  575. this.guildBattleFightUid = ssAckMsg.FightInfo.BriefInfo.Uid
  576. } else {
  577. //重置挑战时间
  578. this.challengePosIdxTime = 0
  579. }
  580. } else {
  581. //重置挑战时间
  582. this.challengePosIdxTime = 0
  583. }
  584. if this.roleGuild.GuildBattle != nil {
  585. ackMsg.BuyChallengeNum = this.roleGuild.GuildBattle.ChallengeNum
  586. }
  587. this.role.ReplayGate(ackMsg, true)
  588. }
  589. func (this *RoleGuild) GuildBattleChallengeResult(msg *serverproto.CSGuildBattleChallengeResultReq) {
  590. msg.SelfGuildId = uint64(this.roleGuild.GuildId)
  591. msg.BfInfo = &serverproto.CommonPlayerBriefInfo{}
  592. this.role.GetRoleBriefInfo(msg.BfInfo)
  593. //reset challenge data
  594. this.guildBattlePosIdx = 0
  595. this.guildBattleFightUid = 0
  596. //重置挑战时间
  597. this.challengePosIdxTime = 0
  598. this.role.SendGuild(msg)
  599. }
  600. func (this *RoleGuild) GuildBattleChallengePint(msg *serverproto.CSGuildBattleChallengePingReq) {
  601. if msg.GuildBattleIdx <= 0 || msg.GuildBattleIdx > 7 || this.guildBattlePosIdx != msg.PosIdx {
  602. ackMsg := &serverproto.SCGuildBattleChallengePingAck{
  603. Error: int32(serverproto.ErrorCode_ERROR_FAIL),
  604. }
  605. if this.guildBattlePosIdx != msg.PosIdx {
  606. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_FIGHTING)
  607. }
  608. this.role.ReplayGate(ackMsg, true)
  609. return
  610. }
  611. this.role.SendGuild(msg)
  612. }
  613. func (this *RoleGuild) CheckReqTime() bool {
  614. curTime := util.GetCurrentTime()
  615. if this.lastReqTime != 0 && this.lastReqTime+Req_Gap_Time > curTime {
  616. ntfMsg := &serverproto.SCGuildBattleCPRankAck{
  617. Error: int32(serverproto.ErrorCode_ERROR_GUILDBATTLE_OPERATOR_TO_FAST),
  618. }
  619. this.role.ReplayGate(ntfMsg, true)
  620. return false
  621. }
  622. this.lastReqTime = curTime
  623. return true
  624. }
  625. func (this *RoleGuild) GetGuildBattleRewardRound() int32 {
  626. if this.roleGuild != nil && this.roleGuild.GuildBattle != nil {
  627. return this.roleGuild.GuildBattle.RewardRound
  628. }
  629. return 0
  630. }
  631. func (this *RoleGuild) SetGuildBattleRewardRound(round int32) {
  632. if this.roleGuild != nil && this.roleGuild.GuildBattle != nil {
  633. if this.roleGuild.GuildBattle.RewardRound >= round {
  634. return
  635. }
  636. this.roleGuild.GuildBattle.RewardRound = round
  637. this.SetDirty(true)
  638. }
  639. }
  640. func (this *RoleGuild) GetGuildBattleMvpRound() int32 {
  641. if this.roleGuild != nil && this.roleGuild.GuildBattle != nil {
  642. return this.roleGuild.GuildBattle.MvpRewardRound
  643. }
  644. return 0
  645. }
  646. //MvpRewardRound只是用作,减少公会服务器遍历
  647. func (this *RoleGuild) SetGuildBattleMvpRound(round int32) {
  648. if this.roleGuild != nil && this.roleGuild.GuildBattle != nil {
  649. if this.roleGuild.GuildBattle.MvpRewardRound >= round {
  650. return
  651. }
  652. this.roleGuild.GuildBattle.MvpRewardRound = round
  653. this.SetDirty(true)
  654. }
  655. }
  656. func (this *RoleGuild) AddBuyChallengeCount(add bool) {
  657. if this.roleGuild != nil && this.roleGuild.GuildBattle != nil {
  658. if add == true {
  659. this.roleGuild.GuildBattle.ChallengeNum++
  660. } else {
  661. this.roleGuild.GuildBattle.ChallengeNum--
  662. }
  663. this.SetDirty(true)
  664. }
  665. }
  666. func (this *RoleGuild) RemoveBuyBuffGold(needRes []*serverproto.KeyValueType) {
  667. if len(needRes) <= 0 {
  668. return
  669. }
  670. var costItemList = map[int32]int32{}
  671. for _, data := range needRes {
  672. costItemList[data.Key] = data.Value
  673. }
  674. if len(costItemList) > 0 {
  675. this.role.DelItemList(costItemList, AddItemST{AddFrom: AddFrom_GuildBattle})
  676. }
  677. }
  678. func (this *RoleGuild) GuildBattleOnline() {
  679. if this.roleGuild == nil || this.roleGuild.GuildBattle == nil {
  680. return
  681. }
  682. reqMsg := &serverproto.SSGuildBattleOnlineGetRewardReq{
  683. Uid: this.role.GetUUid(),
  684. CurRewardId: this.roleGuild.GuildBattle.RewardRound,
  685. MvpRewardId: this.roleGuild.GuildBattle.MvpRewardRound,
  686. }
  687. this.role.SendGuild(reqMsg)
  688. }
  689. func (this *RoleGuild) GetBuyChallengeNum() int32 {
  690. if this.roleGuild.GuildBattle != nil {
  691. return this.roleGuild.GuildBattle.ChallengeNum
  692. }
  693. return 0
  694. }
  695. func (this *RoleGuild) GMSendReward() {
  696. reqMsg := &serverproto.SSGuildBattleRewardReq{}
  697. this.role.SendGuild(reqMsg)
  698. }
  699. func (this *RoleGuild) BuyDemonFightCount(buyCount int32) (serverproto.ErrorCode, int32, int32) {
  700. if this.roleGuild.DemonInfo == nil {
  701. this.roleGuild.DemonInfo = &serverproto.DemonInfo{}
  702. }
  703. bRet := this.IsInDemonFightTime()
  704. if bRet != serverproto.ErrorCode_ERROR_OK {
  705. return bRet, 0, 0
  706. }
  707. //获取当前公会魔王ID
  708. nowTime := util.GetCurrentTimeNow()
  709. curWeekDay := nowTime.Weekday()
  710. if curWeekDay == 0 {
  711. curWeekDay = 7
  712. }
  713. demonData, _ := model.GetDemonInfo(int32(curWeekDay), nowTime)
  714. if demonData == nil {
  715. return serverproto.ErrorCode_ERROR_FAIL, 0, 0
  716. }
  717. //检查总购买次数
  718. totalFightCount := this.roleGuild.DemonInfo.BuyFightCount + buyCount
  719. if totalFightCount > demonData.BuyChallengeCount {
  720. return serverproto.ErrorCode_ERROR_FAIL, 0, 0
  721. }
  722. //计算金币消耗
  723. var costItemList = map[int32]int32{}
  724. if this.roleGuild.DemonInfo.BuyFightCount >= int32(len(demonData.ChallengePrice))- 1 {
  725. data, ok := demonData.ChallengePrice[int32(len(demonData.ChallengePrice)-1)]
  726. if !ok {
  727. util.DebugF("uid=%v config not Found", this.role.GetUUid())
  728. return serverproto.ErrorCode_ERROR_FAIL, 0, 0
  729. }
  730. costItemList[data.Key] += data.Value * buyCount
  731. } else {
  732. payCount := int32(0)
  733. for i := int32(1); i <= buyCount; i++ {
  734. curCount := this.roleGuild.DemonInfo.BuyFightCount + i
  735. if curCount >= int32(len(demonData.ChallengePrice))-1 {
  736. break
  737. }
  738. payCount = i
  739. data, ok := demonData.ChallengePrice[curCount]
  740. if !ok {
  741. util.DebugF("uid=%v config not Found, buyCount %v", this.role.GetUUid(), curCount)
  742. return serverproto.ErrorCode_ERROR_FAIL, 0, 0
  743. }
  744. costItemList[data.Key] += data.Value
  745. }
  746. if payCount < buyCount {
  747. leftCount := buyCount - payCount
  748. data, ok := demonData.ChallengePrice[int32(len(demonData.ChallengePrice)-1)]
  749. if !ok {
  750. util.DebugF("uid=%v config not Found", this.role.GetUUid())
  751. return serverproto.ErrorCode_ERROR_FAIL, 0, 0
  752. }
  753. costItemList[data.Key] += data.Value * leftCount
  754. }
  755. }
  756. if len(costItemList) <= 0 {
  757. return serverproto.ErrorCode_ERROR_FAIL, 0, 0
  758. }
  759. if this.role.CheckResLitNum(costItemList) == false {
  760. util.DebugF("uid=%v build_guild error resource not enough!! costItemList=%v", this.role.GetUUid(), costItemList)
  761. return serverproto.ErrorCode_ERROR_FAIL, 0, 0
  762. }
  763. this.role.DelItemList(costItemList, AddItemST{AddFrom: AddFrom_GuildDemon})
  764. this.roleGuild.DemonInfo.BuyFightCount += buyCount
  765. this.SetDirty(true)
  766. leftUseCount := this.roleGuild.DemonInfo.BuyFightCount + demonData.ChallengeCount - this.roleGuild.DemonInfo.UseFightCount
  767. leftBuyCont := demonData.BuyChallengeCount - this.roleGuild.DemonInfo.BuyFightCount
  768. if leftBuyCont < 0 {
  769. leftBuyCont = 0
  770. }
  771. if leftUseCount < 0 {
  772. leftUseCount = 0
  773. }
  774. return serverproto.ErrorCode_ERROR_OK, leftUseCount, leftBuyCont
  775. }
  776. func (this *RoleGuild) GetGuildDemonInfo(ackMsg *serverproto.SCGuildDemonInfoAck) {
  777. if ackMsg == nil {
  778. return
  779. }
  780. nowTime := util.GetCurrentTimeNow()
  781. curWeekDay := nowTime.Weekday()
  782. if curWeekDay == 0 {
  783. curWeekDay = 7
  784. }
  785. demonData, _ := model.GetDemonInfo(int32(curWeekDay), nowTime)
  786. if demonData == nil {
  787. return
  788. }
  789. ackMsg.LeftFightCount = demonData.ChallengeCount + this.roleGuild.DemonInfo.BuyFightCount - this.roleGuild.DemonInfo.UseFightCount
  790. ackMsg.LeftBuyCount = demonData.BuyChallengeCount - this.roleGuild.DemonInfo.BuyFightCount
  791. }
  792. func (this *RoleGuild) OnGuildDemonFight(refreshTime uint64, freeFightCount int32, ackMsg *serverproto.SCGuildDemonFightAck) {
  793. if ackMsg == nil || this.roleGuild.DemonInfo == nil {
  794. return
  795. }
  796. if ackMsg.Error != int32(serverproto.ErrorCode_ERROR_OK) {
  797. if this.roleGuild.DemonInfo.UseFightCount > 0 {
  798. this.roleGuild.DemonInfo.UseFightCount--
  799. }
  800. return
  801. }
  802. this.roleGuild.DemonInfo.RefreshTime = refreshTime
  803. this.roleGuild.DemonInfo.LastFightTime =util.GetCurrentTime()
  804. this.SetDirty(true)
  805. demonData := model.GetDemonDataByConfigId(ackMsg.DemonId)
  806. if demonData == nil || len(demonData.BaseReward) <= 0 {
  807. util.ErrorF("get demon reward config not found uid:%v, demon:%v", this.role.GetUUid(), ackMsg.DemonId)
  808. return
  809. }
  810. //总共战斗次数
  811. totalFightCount := this.roleGuild.DemonInfo.BuyFightCount + demonData.ChallengeCount
  812. ackMsg.FightCount = totalFightCount - this.roleGuild.DemonInfo.UseFightCount
  813. this.role.AddItemList(demonData.BaseReward, AddFrom_GuildDemon, true)
  814. for itemId, itemNum := range demonData.BaseReward {
  815. ackMsg.BaseReward = append(ackMsg.BaseReward, &serverproto.KeyValueType{
  816. Key: itemId,
  817. Value: itemNum,
  818. })
  819. }
  820. }
  821. func (this *RoleGuild) OnlineAddGuildDemonReward(ntfMsg *serverproto.SSGuildDemonOnlineGetRewardNtf) {
  822. if ntfMsg == nil || this.roleGuild.DemonInfo == nil{
  823. return
  824. }
  825. if len(ntfMsg.RewardList) > 0 {
  826. this.role.AddMail1(model.GlobalMailGuildDemonReward, int32(serverproto.MailType_MailType_GuildDemon),
  827. ntfMsg.RewardList, []int32{ntfMsg.DemonId}, "", "")
  828. }
  829. this.roleGuild.DemonInfo = &serverproto.DemonInfo{}
  830. this.SetDirty(true)
  831. }
  832. func (this *RoleGuild) GetDemonFightTime () uint64 {
  833. if this.roleGuild.DemonInfo == nil {
  834. return 0
  835. }
  836. if this.roleGuild.DemonInfo.LastFightTime != 0 {
  837. isReset := model.IsDailyResetHour5(this.roleGuild.DemonInfo.LastFightTime)
  838. if isReset == false { //表示同一天
  839. demonData := model.GetDemonInfoByTimeStamp(this.roleGuild.DemonInfo.LastFightTime)
  840. if demonData == nil {
  841. return 0
  842. }
  843. //同一天,ID不一样,说明做过更新
  844. if demonData.DemonId == int32(this.roleGuild.DemonInfo.RefreshTime) {
  845. this.roleGuild.DemonInfo.RefreshTime = uint64(demonData.DemonId)
  846. }
  847. return 0
  848. }
  849. } else {
  850. nowTime := util.GetCurrentTimeNow()
  851. curWeekDay := nowTime.Weekday()
  852. if curWeekDay == 0 {
  853. curWeekDay = 7
  854. }
  855. demonData, _ := model.GetDemonInfo(int32(curWeekDay), nowTime)
  856. if demonData == nil {
  857. return 0
  858. }
  859. if demonData.DemonId == int32(this.roleGuild.DemonInfo.RefreshTime) {
  860. return 0
  861. }
  862. }
  863. return this.roleGuild.DemonInfo.RefreshTime
  864. }
  865. func (this *RoleGuild) CheckDemonChallenge() serverproto.ErrorCode{
  866. if this.roleGuild.DemonInfo == nil {
  867. return serverproto.ErrorCode_ERROR_FAIL
  868. }
  869. bRet := this.IsInDemonFightTime()
  870. if bRet != serverproto.ErrorCode_ERROR_OK {
  871. return bRet
  872. }
  873. nowTime := util.GetCurrentTimeNow()
  874. curWeekDay := nowTime.Weekday()
  875. if curWeekDay == 0 {
  876. curWeekDay = 7
  877. }
  878. demonData, _ := model.GetDemonInfo(int32(curWeekDay), nowTime)
  879. if demonData == nil {
  880. return serverproto.ErrorCode_ERROR_FAIL
  881. }
  882. //挑战次数不够
  883. if this.roleGuild.DemonInfo.BuyFightCount + demonData.ChallengeCount <= this.roleGuild.DemonInfo.UseFightCount {
  884. return serverproto.ErrorCode_ERROR_FAIL
  885. }
  886. this.roleGuild.DemonInfo.UseFightCount += 1
  887. this.SetDirty(true)
  888. return serverproto.ErrorCode_ERROR_OK
  889. }
  890. //是否在魔王战斗时间
  891. func (this *RoleGuild) IsInDemonFightTime() serverproto.ErrorCode {
  892. nowTime := util.GetCurrentTimeNow()
  893. //凌晨4.30 到 5点前
  894. nowMinut := nowTime.Minute()
  895. if nowTime.Hour() == 4 && 30 <= nowMinut && nowMinut<= 59 {
  896. return serverproto.ErrorCode_ERROR_GUILDDEMON_NOT_IN_CHANLLENGE_TIME
  897. }
  898. return serverproto.ErrorCode_ERROR_OK
  899. }
  900. func (this *RoleGuild) AddGuildDemonReward(rewardList []*serverproto.KeyValueType, msg *serverproto.SSGuildDemonRewardNtf) {
  901. if msg == nil || this.roleGuild == nil || this.roleGuild.DemonInfo == nil{
  902. return
  903. }
  904. if this.roleGuild.DemonInfo.RefreshTime == 0 {
  905. util.ErrorF("[AddGuildDemonReward]guild demon reward error")
  906. return
  907. }
  908. this.role.AddMail1(model.GlobalMailGuildDemonReward, int32(serverproto.MailType_MailType_GuildDemon),
  909. rewardList, []int32{msg.DemonId}, "", "")
  910. this.roleGuild.DemonInfo.RefreshTime = 0
  911. this.SetDirty(true)
  912. }
  913. func (this *RoleGuild) SendGuildDemonBroadCast(demonId int32, guildName string, rewardLevel int32) {
  914. this.role.AddSystemMessage(SystemmessageType_GuildDemon,
  915. AddSystemMsg{ParamId: demonId, ParaStr: guildName, ParamList: []int32{rewardLevel}})
  916. }