service.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package leagueStorage
  2. import (
  3. "f1-game/internal/data"
  4. "f1-game/internal/enum"
  5. "f1-game/internal/types"
  6. "f1-game/nodes/map/internal/db"
  7. dbLeague "f1-game/nodes/map/internal/db/data/league"
  8. mapFacade "f1-game/nodes/map/internal/facade"
  9. clog "github.com/cherry-game/cherry/logger"
  10. )
  11. func Service() *service {
  12. return &service{}
  13. }
  14. type service struct {
  15. mapFacade.LeagueServiceBase[*db.MapLeagueTable]
  16. }
  17. func (p *service) OnInit(leagueTable *db.MapLeagueTable) {
  18. // 初始化税率
  19. p.InitStorageRatio(leagueTable)
  20. // 初始化资源容量
  21. p.CheckAndInitCapacity(leagueTable)
  22. // 添加初始资源
  23. p.AddAssets(leagueTable, data.Const.LeagueInitItems)
  24. }
  25. // 初始化联盟仓库初始税率
  26. func (p *service) InitStorageRatio(leagueTable *db.MapLeagueTable) {
  27. mapLeagueStorageTable := db.GetMapLeagueStorageTable(leagueTable.LeagueID)
  28. if len(mapLeagueStorageTable.Storage.RatioMap) > 0 {
  29. return
  30. }
  31. // 初始化税率
  32. for _, leagueStorageRow := range data.LeagueStorage.List() {
  33. if leagueStorageRow.InitRate > 0 {
  34. mapLeagueStorageTable.Storage.RatioMap.Put(leagueStorageRow.ID, dbLeague.NewStorageRatio(leagueStorageRow.ID, leagueStorageRow.InitRate))
  35. }
  36. }
  37. mapLeagueStorageTable.Save2Queue()
  38. }
  39. // 联盟资源容量上限检查和初始化
  40. func (p *service) CheckAndInitCapacity(leagueTable *db.MapLeagueTable) {
  41. mapLeagueStorageTable := db.GetMapLeagueStorageTable(leagueTable.LeagueID)
  42. isChange := mapLeagueStorageTable.Storage.CurrencyMap.Check(leagueTable.LeagueInfo.Attrs.GetI64(enum.LeagueAttrResCapacity), true)
  43. if isChange {
  44. mapLeagueStorageTable.Save2Queue()
  45. }
  46. }
  47. func (p *service) AddAssets(leagueTable *db.MapLeagueTable, assets types.Assets) {
  48. if len(assets) <= 0 {
  49. clog.Warnf("addAssets empty. leagueID:%d", leagueTable.LeagueID)
  50. return
  51. }
  52. mapLeagueStorageTable := db.GetMapLeagueStorageTable(leagueTable.LeagueID)
  53. for _, asset := range assets {
  54. if !enum.IsLeagueCurrency(asset.ID) {
  55. clog.Warnf("addAssets not league currency : %v", asset)
  56. continue
  57. }
  58. mapLeagueStorageTable.Storage.CurrencyMap.Increase(asset.ID, asset.Num)
  59. }
  60. mapLeagueStorageTable.Save2Queue()
  61. }
  62. // CheckAutoAdd 检查每几秒资源发放情况
  63. func (p *service) CheckAutoAdd(leagueID int64, nowSecond int64) {
  64. var (
  65. save bool
  66. addSencond = data.Const.ItemAddSecond
  67. mapLeagueStorageTable = db.GetMapLeagueStorageTable(leagueID)
  68. mapLeagueTable, _ = db.GetMapLeagueTable(leagueID)
  69. )
  70. wood := mapLeagueStorageTable.Storage.CurrencyMap.Get(enum.LeagueItemIDWood)
  71. if wood.CheckAutoAdd(nowSecond, mapLeagueTable.LeagueInfo.Attrs.GetI64(enum.LeagueAttrWoodYield), addSencond) {
  72. save = true
  73. }
  74. iron := mapLeagueStorageTable.Storage.CurrencyMap.Get(enum.LeagueItemIDIron)
  75. if iron.CheckAutoAdd(nowSecond, mapLeagueTable.LeagueInfo.Attrs.GetI64(enum.LeagueAttrIronYield), addSencond) {
  76. save = true
  77. }
  78. stone := mapLeagueStorageTable.Storage.CurrencyMap.Get(enum.LeagueItemIDStone)
  79. if stone.CheckAutoAdd(nowSecond, mapLeagueTable.LeagueInfo.Attrs.GetI64(enum.LeagueAttrStoneYield), addSencond) {
  80. save = true
  81. }
  82. grain := mapLeagueStorageTable.Storage.CurrencyMap.Get(enum.LeagueItemIDGrain)
  83. if grain.CheckAutoAdd(nowSecond, mapLeagueTable.LeagueInfo.Attrs.GetI64(enum.LeagueAttrGrainYield), addSencond) {
  84. save = true
  85. }
  86. if save {
  87. mapLeagueStorageTable.Save2Queue()
  88. }
  89. }