diff --git a/cmd/server/wire_gen.go b/cmd/server/wire_gen.go index 419a3f7..325767d 100644 --- a/cmd/server/wire_gen.go +++ b/cmd/server/wire_gen.go @@ -40,7 +40,8 @@ func Initialize() (*app.Application, error) { } appConfig := app.NewConfig(viper) mux := app.ProvideHttpServer(webWeb, apiApi, webSocket, appConfig) - createBattleCommand := usecase.NewCreateBattleCommand(matchRepository, streamRepository) + battleRepository := inmemory.NewBattleRepository() + createBattleCommand := usecase.NewCreateBattleCommand(battleRepository) v2 := subscriber.ProvideDatabaseSubscribers(createBattleCommand) v3 := app.ProvideEventSubscribers(database, v2) router, err := app.ProvideEventBus(v3...) diff --git a/features/api/match.feature b/features/api/match.feature index a2b5fcb..7b844a6 100644 --- a/features/api/match.feature +++ b/features/api/match.feature @@ -10,8 +10,6 @@ Feature: Match """ Then the response JSON should has "match_id" And the response status code should be 200 - And the websocket event "JoinMatchEvent" is received - And the websocket event "JoinMatchEvent" has "payload.player_id" with value "d7ae7356-2d91-47f7-81bd-428c40bf55c3" Scenario: The joined match will be returned Given the session id is "d7ae7356-2d91-47f7-81bd-428c40bf55c3" diff --git a/internal/entity/battle/battle.go b/internal/entity/battle/battle.go new file mode 100644 index 0000000..29635e8 --- /dev/null +++ b/internal/entity/battle/battle.go @@ -0,0 +1,13 @@ +package battle + +type Battle struct { + id string +} + +func New(id string) *Battle { + return &Battle{id: id} +} + +func (b *Battle) Id() string { + return b.id +} diff --git a/internal/repository/inmemory/battle_repository.go b/internal/repository/inmemory/battle_repository.go new file mode 100644 index 0000000..9c40ad4 --- /dev/null +++ b/internal/repository/inmemory/battle_repository.go @@ -0,0 +1,21 @@ +package inmemory + +import ( + "context" + + "github.com/elct9620/wvs/internal/entity/battle" + "github.com/elct9620/wvs/internal/usecase" +) + +var _ usecase.BattleRepository = &BattleRepository{} + +type BattleRepository struct { +} + +func NewBattleRepository() *BattleRepository { + return &BattleRepository{} +} + +func (r *BattleRepository) Save(ctx context.Context, entity *battle.Battle) error { + return nil +} diff --git a/internal/repository/inmemory/inmemory.go b/internal/repository/inmemory/inmemory.go index b0dc533..677c42f 100644 --- a/internal/repository/inmemory/inmemory.go +++ b/internal/repository/inmemory/inmemory.go @@ -8,4 +8,6 @@ import ( var DefaultSet = wire.NewSet( NewMatchRepository, wire.Bind(new(usecase.MatchRepository), new(*MatchRepository)), + NewBattleRepository, + wire.Bind(new(usecase.BattleRepository), new(*BattleRepository)), ) diff --git a/internal/usecase/create_battle_command.go b/internal/usecase/create_battle_command.go index ccce1bc..d895da3 100644 --- a/internal/usecase/create_battle_command.go +++ b/internal/usecase/create_battle_command.go @@ -3,7 +3,7 @@ package usecase import ( "context" - "github.com/elct9620/wvs/pkg/event" + "github.com/elct9620/wvs/internal/entity/battle" ) type CreateBattleInput struct { @@ -16,37 +16,22 @@ type CreateBattleOutput struct { var _ Command[*CreateBattleInput, *CreateBattleOutput] = &CreateBattleCommand{} type CreateBattleCommand struct { - matchs MatchRepository - streams StreamRepository + battles BattleRepository } func NewCreateBattleCommand( - matchs MatchRepository, - streams StreamRepository, + battles BattleRepository, ) *CreateBattleCommand { return &CreateBattleCommand{ - matchs: matchs, - streams: streams, + battles: battles, } } func (c *CreateBattleCommand) Execute(ctx context.Context, input *CreateBattleInput) (*CreateBattleOutput, error) { - match, err := c.matchs.Find(ctx, input.MatchId) - if err != nil { + entity := battle.New(input.MatchId) + if err := c.battles.Save(ctx, entity); err != nil { return nil, err } - for _, player := range match.Players() { - stream, err := c.streams.Find(ctx, player.Id()) - if err != nil { - continue - } - - event := event.NewJoinMatchEvent(match.Id(), player.Id()) - if err := stream.Publish(event); err != nil { - continue - } - } - return &CreateBattleOutput{}, nil } diff --git a/internal/usecase/repository.go b/internal/usecase/repository.go index 16eeef9..41eee3d 100644 --- a/internal/usecase/repository.go +++ b/internal/usecase/repository.go @@ -3,6 +3,7 @@ package usecase import ( "context" + "github.com/elct9620/wvs/internal/entity/battle" "github.com/elct9620/wvs/internal/entity/match" ) @@ -12,3 +13,7 @@ type MatchRepository interface { Waiting(context.Context) ([]*match.Match, error) Save(context.Context, *match.Match) error } + +type BattleRepository interface { + Save(context.Context, *battle.Battle) error +} diff --git a/pkg/event/match.go b/pkg/event/match.go deleted file mode 100644 index a16776b..0000000 --- a/pkg/event/match.go +++ /dev/null @@ -1,28 +0,0 @@ -package event - -import ( - "time" - - "github.com/google/uuid" -) - -const ( - EventJoinMatch = "JoinMatchEvent" -) - -type JoinMatchEvent struct { - AggregateId string `json:"aggregate_id"` - PlayerId string `json:"player_id"` -} - -func NewJoinMatchEvent(matchId string, playerId string) *Event { - return &Event{ - Id: uuid.NewString(), - Type: EventJoinMatch, - CreatedAt: time.Now(), - Payload: &JoinMatchEvent{ - AggregateId: matchId, - PlayerId: playerId, - }, - } -} diff --git a/wire_gen.go b/wire_gen.go index eb11b71..55be5b0 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -43,7 +43,8 @@ func InitializeTest() (*app.Application, error) { } appConfig := app.NewConfig(viper) mux := app.ProvideHttpTestServer(webWeb, apiApi, webSocket, testabilityTestability, appConfig) - createBattleCommand := usecase.NewCreateBattleCommand(matchRepository, streamRepository) + battleRepository := inmemory.NewBattleRepository() + createBattleCommand := usecase.NewCreateBattleCommand(battleRepository) v3 := subscriber.ProvideDatabaseSubscribers(createBattleCommand) v4 := app.ProvideEventSubscribers(database, v3) router, err := app.ProvideEventBus(v4...)