Skip to content

Commit

Permalink
refactor: scrappy to be more new instance safe
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Oct 30, 2023
1 parent 5a2cdca commit 9399675
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 74 deletions.
5 changes: 1 addition & 4 deletions management/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}

Expand Down
7 changes: 7 additions & 0 deletions scrappy/base/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package base

import "darkbot/scrappy/shared/api"

func (s *BaseStorage) FixtureSetAPI(base_api api.APIinterface) {
s.api = base_api
}
9 changes: 5 additions & 4 deletions scrappy/base/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
4 changes: 1 addition & 3 deletions scrappy/base/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
)

func FixtureBaseStorageMockified() *BaseStorage {
storage := (&BaseStorage{}).New()
storage.Api = NewBaseApiMock()
return storage
return NewBaseStorage(NewBaseApiMock())
}

func TestGetBases(t *testing.T) {
Expand Down
20 changes: 13 additions & 7 deletions scrappy/baseattack/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
2 changes: 1 addition & 1 deletion scrappy/baseattack/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestAPI(t *testing.T) {
api := basesattackAPI{}.New()
api := NewBaseAttackAPI()
result, _ := api.GetData()
data := string(result)
fmt.Println(data)
Expand Down
6 changes: 3 additions & 3 deletions scrappy/baseattack/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"darkbot/scrappy/shared/api"
"darkbot/scrappy/tests"
"darkbot/settings/utils/logger"
"io/ioutil"
"os"
"path"
)

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion scrappy/baseattack/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions scrappy/fixtures.go
Original file line number Diff line number Diff line change
@@ -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)
}
43 changes: 28 additions & 15 deletions scrappy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion scrappy/player/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions scrappy/player/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
4 changes: 1 addition & 3 deletions scrappy/player/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
)

func FixturePlayerStorageMockified() *PlayerStorage {
storage := (&PlayerStorage{}).New()
storage.Api = NewPlayerMockAPI()
return storage
return NewPlayerStorage(NewPlayerMockAPI())
}

func TestGetPlayers(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion settings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 2 additions & 2 deletions viewer/templ/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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{
Expand Down
Loading

0 comments on commit 9399675

Please sign in to comment.