Skip to content

Commit

Permalink
feat: update proto specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Sep 5, 2024
1 parent 455669f commit 997581a
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module game-player-data
go 1.23

require (
github.com/emortalmc/proto-specs/gen/go v0.0.0-20230916202652-8975e470ce34
github.com/emortalmc/proto-specs/gen/go v0.0.0-20240905105617-8c6a55ee2bae
github.com/gogo/protobuf v1.3.2
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ github.com/emortalmc/proto-specs/gen/go v0.0.0-20230916193435-6abf652a5c2f h1:Dv
github.com/emortalmc/proto-specs/gen/go v0.0.0-20230916193435-6abf652a5c2f/go.mod h1:se+tHcK9FWxeadkxLF5uj+SPauEye0X+Iq6cGczXGJY=
github.com/emortalmc/proto-specs/gen/go v0.0.0-20230916202652-8975e470ce34 h1:q095naqNtgsBaI8o1TgkIo3U1pkSbIEnRW1KqWWu8Ww=
github.com/emortalmc/proto-specs/gen/go v0.0.0-20230916202652-8975e470ce34/go.mod h1:se+tHcK9FWxeadkxLF5uj+SPauEye0X+Iq6cGczXGJY=
github.com/emortalmc/proto-specs/gen/go v0.0.0-20240905105043-b1abec6eb8cf h1:IdMv84dOIWFMLeIYifQKxc1EmQybknwtiyTbBDrUnvc=
github.com/emortalmc/proto-specs/gen/go v0.0.0-20240905105043-b1abec6eb8cf/go.mod h1:se+tHcK9FWxeadkxLF5uj+SPauEye0X+Iq6cGczXGJY=
github.com/emortalmc/proto-specs/gen/go v0.0.0-20240905105617-8c6a55ee2bae h1:Y9ofcDfHuJbXUjTP/5+jMKOPpIysHhDWibpG0nYhy98=
github.com/emortalmc/proto-specs/gen/go v0.0.0-20240905105617-8c6a55ee2bae/go.mod h1:se+tHcK9FWxeadkxLF5uj+SPauEye0X+Iq6cGczXGJY=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
2 changes: 1 addition & 1 deletion internal/kafka/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *consumer) handleBlockSumoUpdate(ctx context.Context, pId uuid.UUID, msg
player = config.CreateDefaultBlockSumoData(pId)
}

msgData := &pbmodel.BlockSumoPlayerData{}
msgData := &pbmodel.V1BlockSumoPlayerData{}

if err := anypb.UnmarshalTo(msg.Data, msgData, proto.UnmarshalOptions{}); err != nil {
return fmt.Errorf("failed to unmarshal data: %w", err)
Expand Down
12 changes: 6 additions & 6 deletions internal/repository/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ type BlockSumoData struct {
}

func (d *BlockSumoData) ToAnyProto() (*anypb.Any, error) {
return anypb.New(&gameplayerdata.BlockSumoPlayerData{
return anypb.New(&gameplayerdata.V1BlockSumoPlayerData{
BlockSlot: d.BlockSlot,
ShearsSlot: d.ShearsSlot,
})
}

func (d *BlockSumoData) FromProto(pId uuid.UUID, data *gameplayerdata.BlockSumoPlayerData) {
func (d *BlockSumoData) FromProto(pId uuid.UUID, data *gameplayerdata.V1BlockSumoPlayerData) {
d.PlayerId = pId
d.BlockSlot = data.BlockSlot
d.ShearsSlot = data.ShearsSlot
Expand All @@ -36,8 +36,8 @@ type MinesweeperData struct {
}

// TODO
func (d *MinesweeperData) ToProto() *gameplayerdata.MinesweeperPlayerData {
return &gameplayerdata.MinesweeperPlayerData{}
func (d *MinesweeperData) ToProto() *gameplayerdata.V1MinesweeperPlayerData {
return &gameplayerdata.V1MinesweeperPlayerData{}
}

// TowerDefenceData TODO
Expand All @@ -46,6 +46,6 @@ type TowerDefenceData struct {
}

// TODO
func (d *TowerDefenceData) ToProto() *gameplayerdata.TowerDefencePlayerData {
return &gameplayerdata.TowerDefencePlayerData{}
func (d *TowerDefenceData) ToProto() *gameplayerdata.V1TowerDefencePlayerData {
return &gameplayerdata.V1TowerDefencePlayerData{}
}
17 changes: 17 additions & 0 deletions internal/repository/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ func (m *mongoRepository) GetBlockSumoData(ctx context.Context, playerId uuid.UU
return &data, nil
}

func (m *mongoRepository) GetBlockSumoDataForPlayers(ctx context.Context, playerIds []uuid.UUID) ([]*model.BlockSumoData, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

cursor, err := m.blockSumoCollection.Find(ctx, bson.M{"_id": bson.M{"$in": playerIds}})
if err != nil {
return nil, err
}

var data []*model.BlockSumoData
if err := cursor.All(ctx, &data); err != nil {
return nil, err
}

return data, nil
}

func (m *mongoRepository) SaveBlockSumoPlayer(ctx context.Context, data *model.BlockSumoData) error {
return m.saveData(ctx, data.PlayerId, data, m.blockSumoCollection)
}
Expand Down
1 change: 1 addition & 0 deletions internal/repository/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type Repository interface {
GetBlockSumoData(ctx context.Context, playerId uuid.UUID) (*model.BlockSumoData, error)
GetBlockSumoDataForPlayers(ctx context.Context, playerIds []uuid.UUID) ([]*model.BlockSumoData, error)
SaveBlockSumoPlayer(ctx context.Context, data *model.BlockSumoData) error

GetTowerDefencePlayer(ctx context.Context, playerId uuid.UUID) (*model.TowerDefenceData, error)
Expand Down
36 changes: 36 additions & 0 deletions internal/service/game_player_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/anypb"
)

type gamePlayerDataService struct {
Expand Down Expand Up @@ -52,6 +53,41 @@ func (s *gamePlayerDataService) GetGamePlayerData(ctx context.Context, req *pb.G
}, nil
}

func (s *gamePlayerDataService) GetMultipleGamePlayerData(ctx context.Context, req *pb.GetMultipleGamePlayerDataRequest) (*pb.GetMultipleGamePlayerDataResponse, error) {
pIds := make([]uuid.UUID, len(req.PlayerIds))
for i, pIdStr := range req.PlayerIds {
pId, err := uuid.Parse(pIdStr)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid player id")
}
pIds[i] = pId
}

data := make(map[string]*anypb.Any, len(pIds))

switch req.GameMode {
case gameplayerdata.GameDataGameMode_BLOCK_SUMO:
bsData, err := s.repo.GetBlockSumoDataForPlayers(ctx, pIds)
if err != nil {
return nil, createDbErr(err)
}

for _, d := range bsData {
anypb, err := d.ToAnyProto()
if err != nil {
return nil, status.Error(codes.Internal, "failed to convert data to proto")
}

data[d.PlayerId.String()] = anypb
}
}

return &pb.GetMultipleGamePlayerDataResponse{
Data: data,
}, nil

}

func createDbErr(err error) error {
if errors.Is(err, mongo.ErrNoDocuments) {
return status.Error(codes.NotFound, "player not found")
Expand Down
2 changes: 1 addition & 1 deletion tests/publish_update_game_player_data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
Async: false,
}

data := &pbmodel.BlockSumoPlayerData{
data := &pbmodel.V1BlockSumoPlayerData{
BlockSlot: 12,
ShearsSlot: 13,
}
Expand Down

0 comments on commit 997581a

Please sign in to comment.