From 939967500e7bcdb690c63e6cd5ddd4292229fce2 Mon Sep 17 00:00:00 2001 From: dd84ai Date: Mon, 30 Oct 2023 02:05:44 +0100 Subject: [PATCH] refactor: scrappy to be more new instance safe --- management/run.go | 5 +--- scrappy/base/fixtures.go | 7 ++++++ scrappy/base/storage.go | 9 ++++--- scrappy/base/storage_test.go | 4 +-- scrappy/baseattack/api.go | 20 +++++++++------ scrappy/baseattack/api_test.go | 2 +- scrappy/baseattack/mock.go | 6 ++--- scrappy/baseattack/mock_test.go | 2 +- scrappy/fixtures.go | 20 +++++++++++++++ scrappy/main.go | 43 +++++++++++++++++++++------------ scrappy/player/api.go | 2 +- scrappy/player/storage.go | 9 ++++--- scrappy/player/storage_test.go | 4 +-- settings/main.go | 2 +- viewer/templ/base.go | 4 +-- viewer/templ/base_test.go | 35 ++++++++++----------------- viewer/templ/derivatives.go | 2 +- viewer/templ/player.go | 2 +- viewer/templ/player_test.go | 3 ++- 19 files changed, 107 insertions(+), 74 deletions(-) create mode 100644 scrappy/base/fixtures.go create mode 100644 scrappy/fixtures.go diff --git a/management/run.go b/management/run.go index 346135b..6fb97d2 100644 --- a/management/run.go +++ b/management/run.go @@ -7,8 +7,6 @@ import ( "darkbot/configurator" "darkbot/listener" "darkbot/scrappy" - "darkbot/scrappy/base" - "darkbot/scrappy/player" "darkbot/settings" "darkbot/settings/utils" "darkbot/settings/utils/logger" @@ -33,8 +31,7 @@ var runCmd = &cobra.Command{ configurator.NewConfigurator(settings.Dbpath).Migrate() if settings.Config.DevEnvMockApi == "true" { - scrappy.Storage.BaseStorage.Api = base.NewBaseApiMock() - scrappy.Storage.PlayerStorage.Api = player.NewPlayerAPI() + scrappy.Storage = scrappy.FixtureMockedStorage() scrappy.Storage.Update() } diff --git a/scrappy/base/fixtures.go b/scrappy/base/fixtures.go new file mode 100644 index 0000000..0e7aed8 --- /dev/null +++ b/scrappy/base/fixtures.go @@ -0,0 +1,7 @@ +package base + +import "darkbot/scrappy/shared/api" + +func (s *BaseStorage) FixtureSetAPI(base_api api.APIinterface) { + s.api = base_api +} diff --git a/scrappy/base/storage.go b/scrappy/base/storage.go index 4de465b..3c27185 100644 --- a/scrappy/base/storage.go +++ b/scrappy/base/storage.go @@ -17,13 +17,13 @@ type Base struct { type BaseStorage struct { records.Records[records.StampedObjects[Base]] - Api api.APIinterface + api api.APIinterface parser parser.Parser[records.StampedObjects[Base]] } // Conveniently born some factory func (b *BaseStorage) Update() { - data, err := b.Api.GetData() + data, err := b.api.GetData() if err != nil { logger.CheckWarn(err, "quering API with error in BaseStorage") return @@ -37,8 +37,9 @@ func (b *BaseStorage) Update() { logger.Info("updated base storage") } -func (b *BaseStorage) New() *BaseStorage { +func NewBaseStorage(api api.APIinterface) *BaseStorage { + b := &BaseStorage{} b.parser = baseParser{} - b.Api = NewBaseApi() + b.api = api return b } diff --git a/scrappy/base/storage_test.go b/scrappy/base/storage_test.go index ec8d068..8a7ce0a 100644 --- a/scrappy/base/storage_test.go +++ b/scrappy/base/storage_test.go @@ -9,9 +9,7 @@ import ( ) func FixtureBaseStorageMockified() *BaseStorage { - storage := (&BaseStorage{}).New() - storage.Api = NewBaseApiMock() - return storage + return NewBaseStorage(NewBaseApiMock()) } func TestGetBases(t *testing.T) { diff --git a/scrappy/baseattack/api.go b/scrappy/baseattack/api.go index 5bc264e..3dd784c 100644 --- a/scrappy/baseattack/api.go +++ b/scrappy/baseattack/api.go @@ -10,26 +10,32 @@ type basesattackAPI struct { api.APIrequest } -func (a basesattackAPI) New() api.APIinterface { +func NewBaseAttackAPI() api.APIinterface { + a := basesattackAPI{} a.Init(settings.Config.ScrappyBaseAttackUrl) return a } type BaseAttackStorage struct { - Data string - Api api.APIinterface + data BaseAttackData + api api.APIinterface } -func (b *BaseAttackStorage) New() *BaseAttackStorage { - b.Api = basesattackAPI{}.New() +func NewBaseAttackStorage(api api.APIinterface) *BaseAttackStorage { + b := &BaseAttackStorage{} + b.api = api return b } +type BaseAttackData string + +func (b *BaseAttackStorage) GetData() BaseAttackData { return BaseAttackData(b.data) } + func (b *BaseAttackStorage) Update() { - data, err := b.Api.GetData() + data, err := b.api.GetData() if err != nil { logger.CheckWarn(err, "quering API with error in BaseAttackStorage") return } - b.Data = string(data) + b.data = BaseAttackData(string((data))) } diff --git a/scrappy/baseattack/api_test.go b/scrappy/baseattack/api_test.go index 9c582c9..692cc5e 100644 --- a/scrappy/baseattack/api_test.go +++ b/scrappy/baseattack/api_test.go @@ -9,7 +9,7 @@ import ( ) func TestAPI(t *testing.T) { - api := basesattackAPI{}.New() + api := NewBaseAttackAPI() result, _ := api.GetData() data := string(result) fmt.Println(data) diff --git a/scrappy/baseattack/mock.go b/scrappy/baseattack/mock.go index bb069e0..b8958ce 100644 --- a/scrappy/baseattack/mock.go +++ b/scrappy/baseattack/mock.go @@ -4,7 +4,7 @@ import ( "darkbot/scrappy/shared/api" "darkbot/scrappy/tests" "darkbot/settings/utils/logger" - "io/ioutil" + "os" "path" ) @@ -22,14 +22,14 @@ func NewMock(filename string) api.APIinterface { return BaseAttackAPISpy{APIbasis{Filename: filename}} } -func (a BaseAttackAPISpy) New() api.APIinterface { +func NewBaseAttackAPIMock() api.APIinterface { return NewMock("data.json") } func (a BaseAttackAPISpy) GetData() ([]byte, error) { path_testdata := tests.FixtureCreateTestDataFolder() path_testfile := path.Join(path_testdata, a.Filename) - data, err := ioutil.ReadFile(path_testfile) + data, err := os.ReadFile(path_testfile) logger.CheckPanic(err, "unable to read file") return data, nil } diff --git a/scrappy/baseattack/mock_test.go b/scrappy/baseattack/mock_test.go index 8cbeed1..2bb6464 100644 --- a/scrappy/baseattack/mock_test.go +++ b/scrappy/baseattack/mock_test.go @@ -13,7 +13,7 @@ import ( func TestRegenerateBaseData(t *testing.T) { utils.RegenerativeTest( func() error { - data, _ := basesattackAPI{}.New().GetData() + data, _ := NewBaseAttackAPI().GetData() path_testdata := tests.FixtureCreateTestDataFolder() path_testfile := path.Join(path_testdata, "data.json") err := ioutil.WriteFile(path_testfile, data, os.ModePerm) diff --git a/scrappy/fixtures.go b/scrappy/fixtures.go new file mode 100644 index 0000000..66ddc8f --- /dev/null +++ b/scrappy/fixtures.go @@ -0,0 +1,20 @@ +package scrappy + +import ( + "darkbot/scrappy/base" + "darkbot/scrappy/baseattack" + "darkbot/scrappy/player" + "darkbot/scrappy/shared/api" +) + +func FixtureNewStorage(players *player.PlayerStorage) *ScrappyStorage { + return &ScrappyStorage{playerStorage: players} +} + +func FixtureMockedStorage() *ScrappyStorage { + return NewScrapyStorage(base.NewBaseApi(), player.NewPlayerMockAPI(), baseattack.NewBaseAttackAPIMock()) +} + +func FixtureSetBaseStorageAPI(base_api api.APIinterface) { + Storage.baseStorage.FixtureSetAPI(base_api) +} diff --git a/scrappy/main.go b/scrappy/main.go index 6315de9..351a149 100644 --- a/scrappy/main.go +++ b/scrappy/main.go @@ -4,42 +4,55 @@ import ( "darkbot/scrappy/base" "darkbot/scrappy/baseattack" "darkbot/scrappy/player" + "darkbot/scrappy/shared/api" "darkbot/settings" - "darkbot/settings/utils/logger" + "darkbot/settings/logus" "time" ) type ScrappyStorage struct { - BaseStorage *base.BaseStorage - PlayerStorage *player.PlayerStorage - BaseAttackStorage *baseattack.BaseAttackStorage + baseStorage *base.BaseStorage + playerStorage *player.PlayerStorage + baseAttackStorage *baseattack.BaseAttackStorage } -func (s *ScrappyStorage) New() *ScrappyStorage { - s.BaseStorage = (&base.BaseStorage{}).New() - s.PlayerStorage = (&player.PlayerStorage{}).New() - s.BaseAttackStorage = (&baseattack.BaseAttackStorage{}).New() +func NewScrapyStorage(base_api api.APIinterface, player_api api.APIinterface, base_attack api.APIinterface) *ScrappyStorage { + s := &ScrappyStorage{} + s.baseStorage = base.NewBaseStorage(base_api) + s.playerStorage = player.NewPlayerStorage(player_api) + s.baseAttackStorage = baseattack.NewBaseAttackStorage(base_attack) return s } func (s *ScrappyStorage) Update() { - s.BaseStorage.Update() - s.PlayerStorage.Update() - s.BaseAttackStorage.Update() + s.baseStorage.Update() + s.playerStorage.Update() + s.baseAttackStorage.Update() +} + +func (s *ScrappyStorage) GetBaseStorage() *base.BaseStorage { + return s.baseStorage +} +func (s *ScrappyStorage) GetPlayerStorage() *player.PlayerStorage { + return s.playerStorage +} + +func (s *ScrappyStorage) GetBaseAttackStorage() *baseattack.BaseAttackStorage { + return s.baseAttackStorage } var Storage *ScrappyStorage func init() { - logger.Info("initialized scrappy") - Storage = (&ScrappyStorage{}).New() + logus.Info("initialized scrappy") + Storage = NewScrapyStorage(base.NewBaseApi(), player.NewPlayerAPI(), base.NewBaseApi()) } func Run() { - logger.Info("starting scrappy infinity update loop") + logus.Info("starting scrappy infinity update loop") for { Storage.Update() time.Sleep(time.Duration(settings.LoopDelay) * time.Second) } - logger.Info("gracefully shutdown scrappy infinity loop") + logus.Info("gracefully shutdown scrappy infinity loop") } diff --git a/scrappy/player/api.go b/scrappy/player/api.go index cf07abd..f2889ad 100644 --- a/scrappy/player/api.go +++ b/scrappy/player/api.go @@ -9,7 +9,7 @@ type PlayerAPI struct { api.APIrequest } -func NewPlayerAPI() api.APIinterface { +func NewPlayerAPI() PlayerAPI { a := PlayerAPI{} a.Init(settings.Config.ScrappyPlayerUrl) return a diff --git a/scrappy/player/storage.go b/scrappy/player/storage.go index b812446..c1a9cde 100644 --- a/scrappy/player/storage.go +++ b/scrappy/player/storage.go @@ -16,13 +16,13 @@ type Player struct { type PlayerStorage struct { records.Records[records.StampedObjects[Player]] - Api api.APIinterface + api api.APIinterface parser parser.Parser[records.StampedObjects[Player]] } // Conveniently born some factory func (b *PlayerStorage) Update() { - data, err := b.Api.GetData() + data, err := b.api.GetData() if err != nil { logger.CheckWarn(err, "quering API with error in PlayerStorage") return @@ -36,8 +36,9 @@ func (b *PlayerStorage) Update() { logger.Info("updated player storage") } -func (b *PlayerStorage) New() *PlayerStorage { +func NewPlayerStorage(api api.APIinterface) *PlayerStorage { + b := &PlayerStorage{} b.parser = playerParser{} - b.Api = NewPlayerAPI() + b.api = api return b } diff --git a/scrappy/player/storage_test.go b/scrappy/player/storage_test.go index 65eaa37..abf7840 100644 --- a/scrappy/player/storage_test.go +++ b/scrappy/player/storage_test.go @@ -9,9 +9,7 @@ import ( ) func FixturePlayerStorageMockified() *PlayerStorage { - storage := (&PlayerStorage{}).New() - storage.Api = NewPlayerMockAPI() - return storage + return NewPlayerStorage(NewPlayerMockAPI()) } func TestGetPlayers(t *testing.T) { diff --git a/settings/main.go b/settings/main.go index 6003f29..fc4daaa 100644 --- a/settings/main.go +++ b/settings/main.go @@ -25,7 +25,7 @@ type ConfigScheme struct { ConfiguratorDbname string `env:"CONFIGURATOR_DBNAME" envDefault:"dev"` - ConsolerPrefix string `env:"CONSOLER_PREFIX" envDefault:","` + ConsolerPrefix string `env:"CONSOLER_PREFIX" envDefault:";"` ProfilingEnabled string `env:"PROFILING" envDefault:"false"` LoopDelay string `env:"LOOP_DELAY" envDefault:"10"` diff --git a/viewer/templ/base.go b/viewer/templ/base.go index 05bd0a4..d0e8a9a 100644 --- a/viewer/templ/base.go +++ b/viewer/templ/base.go @@ -104,7 +104,7 @@ func (b *TemplateBase) Render() { UnderAttackPhrase: "\n@underAttack;", } - record, err := b.API.Scrappy.BaseStorage.GetLatestRecord() + record, err := b.API.Scrappy.GetBaseStorage().GetLatestRecord() if err != nil { return } @@ -127,7 +127,7 @@ func (b *TemplateBase) Render() { healthDeritive = "initializing" } else { HealthDecreasing = healthDeritiveNumber < 0 - UnderAttack = healthDeritiveNumber < HealthRateDecreasingThreshold || strings.Contains(b.API.Scrappy.BaseAttackStorage.Data, base.Name) + UnderAttack = healthDeritiveNumber < HealthRateDecreasingThreshold || strings.Contains(string(b.API.Scrappy.GetBaseAttackStorage().GetData()), base.Name) } input.Bases = append(input.Bases, AugmentedBase{ diff --git a/viewer/templ/base_test.go b/viewer/templ/base_test.go index f68ccc3..4fbbc15 100644 --- a/viewer/templ/base_test.go +++ b/viewer/templ/base_test.go @@ -25,15 +25,12 @@ func TestBaseViewerMocked(t *testing.T) { cg := configurator.ConfiguratorBase{Configurator: configurator.NewConfigurator(dbpath)} cg.TagsAdd(channelID, []string{"Station"}...) - bases := (&base.BaseStorage{}).New() - baseAttack := (&baseattack.BaseAttackStorage{}).New() - baseAttack.Api = baseattack.BaseAttackAPISpy{}.New() - baseAttack.Update() - scrappy.Storage = &scrappy.ScrappyStorage{BaseStorage: bases, BaseAttackStorage: baseAttack} + scrappy.Storage = scrappy.FixtureMockedStorage() + scrappy.Storage.Update() record := records.StampedObjects[base.Base]{}.New() record.Add(base.Base{Name: "Station1", Affiliation: "Abc", Health: 100}) record.Add(base.Base{Name: "Station2", Affiliation: "Qwe", Health: 100}) - bases.Add(record) + scrappy.Storage.GetBaseStorage().Add(record) render := NewTemplateBase(channelID, dbpath) render.Render() @@ -52,7 +49,7 @@ func TestBaseViewerMocked(t *testing.T) { record = records.StampedObjects[base.Base]{}.New() record.Add(base.Base{Name: "Station1", Affiliation: "Abc", Health: 100}) record.Add(base.Base{Name: "Station2", Affiliation: "Qwe", Health: 50}) - bases.Add(record) + scrappy.Storage.GetBaseStorage().Add(record) baseAlertDecreasing.Enable(channelID) isEnabled, _ = baseAlertDecreasing.Status(channelID) @@ -90,7 +87,7 @@ func TestBaseViewerMocked(t *testing.T) { record = records.StampedObjects[base.Base]{}.New() record.Add(base.Base{Name: "Bank of Bretonia", Affiliation: "Abc", Health: 100}) - bases.Add(record) + scrappy.Storage.GetBaseStorage().Add(record) cg.TagsAdd(channelID, []string{"Bank"}...) render = NewTemplateBase(channelID, dbpath) render.Render() @@ -138,17 +135,15 @@ func TestGetDerivative(t *testing.T) { tags := []string{""} logger.Debug("2") - scrappy.Storage.BaseStorage = (&base.BaseStorage{}).New() - scrappy.Storage.PlayerStorage = (&player.PlayerStorage{}).New() - scrappy.Storage.BaseStorage.Api = base.NewMock("basedata.json") + scrappy.Storage = scrappy.NewScrapyStorage(base.NewMock("basedata.json"), player.NewPlayerMockAPI(), baseattack.NewBaseAttackAPIMock()) logger.Debug("2.1") - scrappy.Storage.PlayerStorage.Api = player.NewPlayerMockAPI() logger.Debug("2.2") scrappy.Storage.Update() logger.Debug("2.3") - scrappy.Storage.BaseStorage.Api = base.NewMock("basedata2.json") + + scrappy.FixtureSetBaseStorageAPI(base.NewMock("basedata2.json")) scrappy.Storage.Update() - scrappy.Storage.BaseStorage.Records.List(func(values []records.StampedObjects[base.Base]) { + scrappy.Storage.GetBaseStorage().Records.List(func(values []records.StampedObjects[base.Base]) { values[1].Timestamp = values[0].Timestamp.Add(time.Minute * 15) }) @@ -156,7 +151,7 @@ func TestGetDerivative(t *testing.T) { result1 := make(map[string]base.Base) result2 := make(map[string]base.Base) - scrappy.Storage.BaseStorage.Records.List(func(values []records.StampedObjects[base.Base]) { + scrappy.Storage.GetBaseStorage().Records.List(func(values []records.StampedObjects[base.Base]) { for _, base := range values[0].List { result1[base.Name] = base } @@ -188,16 +183,12 @@ func TestDetectAttackOnLPBase(t *testing.T) { cg := configurator.ConfiguratorBase{Configurator: configurator.NewConfigurator(dbpath)} cg.TagsAdd(channelID, []string{"LP-7743"}...) - scrappy.Storage = (&scrappy.ScrappyStorage{}).New() - - scrappy.Storage.BaseStorage.Api = base.NewBaseApiMock() - scrappy.Storage.PlayerStorage.Api = player.NewPlayerMockAPI() - scrappy.Storage.BaseAttackStorage.Api = baseattack.NewMock("data_lp.json") + scrappy.Storage = scrappy.NewScrapyStorage(base.NewBaseApiMock(), player.NewPlayerMockAPI(), baseattack.NewMock("data_lp.json")) scrappy.Storage.Update() - assert.True(t, strings.Contains(scrappy.Storage.BaseAttackStorage.Data, "LP-7743")) + assert.True(t, strings.Contains(string(scrappy.Storage.GetBaseAttackStorage().GetData()), "LP-7743")) - bases := scrappy.Storage.BaseStorage + bases := scrappy.Storage.GetBaseStorage() record := records.StampedObjects[base.Base]{}.New() record.Add(base.Base{Name: "LP-7743", Affiliation: "Abc", Health: 5}) bases.Add(record) diff --git a/viewer/templ/derivatives.go b/viewer/templ/derivatives.go index 9e64c8a..1524ac1 100644 --- a/viewer/templ/derivatives.go +++ b/viewer/templ/derivatives.go @@ -29,7 +29,7 @@ func (t NoNonZeroDerivativesWarning) Error() string { func CalculateDerivates(tags []string, api apis.API) (map[string]float64, error) { baseHealths := make(map[string][]float64) var res_records []records.StampedObjects[base.Base] - api.Scrappy.BaseStorage.Records.List(func(records2 []records.StampedObjects[base.Base]) { + api.Scrappy.GetBaseStorage().Records.List(func(records2 []records.StampedObjects[base.Base]) { res_records = records2 }) diff --git a/viewer/templ/player.go b/viewer/templ/player.go index 2a6b8df..745547f 100644 --- a/viewer/templ/player.go +++ b/viewer/templ/player.go @@ -80,7 +80,7 @@ func TagContains(name string, tags []string) bool { } func (t *PlayersTemplates) Render() { - record, err := t.API.Scrappy.PlayerStorage.GetLatestRecord() + record, err := t.API.Scrappy.GetPlayerStorage().GetLatestRecord() if err != nil { return } diff --git a/viewer/templ/player_test.go b/viewer/templ/player_test.go index 6bd2f45..f298ff1 100644 --- a/viewer/templ/player_test.go +++ b/viewer/templ/player_test.go @@ -22,7 +22,8 @@ func TestPlayerViewerMadeUpData(t *testing.T) { configurator.ConfiguratorPlayerFriend{Configurator: configurator.NewConfigurator(dbpath)}.TagsAdd(channelID, []string{"player4"}...) players := player.PlayerStorage{} - scrappy.Storage = &scrappy.ScrappyStorage{PlayerStorage: &players} + + scrappy.Storage = scrappy.FixtureNewStorage(&players) record := records.StampedObjects[player.Player]{}.New() record.Add(player.Player{Name: "player1", System: "system1", Region: "region1"}) record.Add(player.Player{Name: "player2", System: "system2", Region: "region2"})