Skip to content

Commit

Permalink
Remove JoinMatch event and add BattleRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Mar 21, 2024
1 parent b2b58b2 commit 7a9b4f0
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 53 deletions.
3 changes: 2 additions & 1 deletion cmd/server/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions features/api/match.feature
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions 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
}
21 changes: 21 additions & 0 deletions 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
}
2 changes: 2 additions & 0 deletions internal/repository/inmemory/inmemory.go
Expand Up @@ -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)),
)
27 changes: 6 additions & 21 deletions internal/usecase/create_battle_command.go
Expand Up @@ -3,7 +3,7 @@ package usecase
import (
"context"

"github.com/elct9620/wvs/pkg/event"
"github.com/elct9620/wvs/internal/entity/battle"
)

type CreateBattleInput struct {
Expand All @@ -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
}
5 changes: 5 additions & 0 deletions internal/usecase/repository.go
Expand Up @@ -3,6 +3,7 @@ package usecase
import (
"context"

"github.com/elct9620/wvs/internal/entity/battle"
"github.com/elct9620/wvs/internal/entity/match"
)

Expand All @@ -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
}
28 changes: 0 additions & 28 deletions pkg/event/match.go

This file was deleted.

3 changes: 2 additions & 1 deletion wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a9b4f0

Please sign in to comment.