Skip to content

Commit

Permalink
added make move functionality;
Browse files Browse the repository at this point in the history
  • Loading branch information
olimdzhon committed Jul 31, 2024
1 parent 9399780 commit 7fe3ab5
Show file tree
Hide file tree
Showing 23 changed files with 2,850 additions and 175 deletions.
1,435 changes: 1,402 additions & 33 deletions api/checkers/checkers/tx.pulsar.go

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions api/checkers/checkers/tx_grpc.pb.go

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

2 changes: 1 addition & 1 deletion docs/static/openapi.yml

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ deps:
- remote: buf.build
owner: cosmos
repository: ibc
commit: 7f7c07e11c014b1f848fa5728330e31d
digest: shake256:0d08fc2ad790a120d894f28ccd554d3060bfd602b61aa754614e3366f13da7cff7cb075ecfa7043e74609c054211472adddb37aa0d7f861334c35c0693b02f2e
commit: ef1dd1634b6c429bb2b52d983a683ce3
digest: shake256:8325b0de8e8a2e027a8d33e0585a13273ce34b510cbedf7b785882a785baa62d42405ab2a836ea42fb1ddf098580f5cfc2414b1851dfcdd0b7c84851e96afeb9
- remote: buf.build
owner: cosmos
repository: ics23
Expand All @@ -29,8 +29,8 @@ deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 2bbd25900cb34c79bae97d85c948d3cf
digest: shake256:a6446e23f4408160217c22738a0c8c370ff3ff966f601d678960b803829cf53b5cf5998d20fcb3f9c9be944b24337843e7beb6e681ddbf9d036fb6491c0e47e7
commit: 8bc2c51e08c447cd8886cdea48a73e14
digest: shake256:a969155953a5cedc5b2df5b42c368f2bc66ff8ce1804bc96e0f14ff2ee8a893687963058909df844d1643cdbc98ff099d2daa6bc9f9f5b8886c49afdc60e19af
- remote: buf.build
owner: protocolbuffers
repository: wellknowntypes
Expand Down
17 changes: 17 additions & 0 deletions proto/checkers/checkers/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ service Msg {
// parameters. The authority defaults to the x/gov module account.
rpc UpdateParams (MsgUpdateParams) returns (MsgUpdateParamsResponse);
rpc CreateGame (MsgCreateGame ) returns (MsgCreateGameResponse );
rpc PlayMove (MsgPlayMove ) returns (MsgPlayMoveResponse );
}
// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
Expand Down Expand Up @@ -48,3 +49,19 @@ message MsgCreateGameResponse {
string gameIndex = 1;
}

message MsgPlayMove {
option (cosmos.msg.v1.signer) = "creator";
string creator = 1;
string gameIndex = 2;
uint64 fromX = 3;
uint64 fromY = 4;
uint64 toX = 5;
uint64 toY = 6;
}

message MsgPlayMoveResponse {
int32 capturedX = 1;
int32 capturedY = 2;
string winner = 3;
}

8 changes: 4 additions & 4 deletions x/checkers/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package keeper_test
import "github.com/olimdzhon/checkers/x/checkers/testutil"

const (
alice = testutil.Alice
bob = testutil.Bob
carol = testutil.Carol
)
alice = testutil.Alice
bob = testutil.Bob
carol = testutil.Carol
)
5 changes: 1 addition & 4 deletions x/checkers/keeper/msg_server_create_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ func (k msgServer) CreateGame(goCtx context.Context, msg *types.MsgCreateGame) (
systemInfo.NextId++
k.Keeper.SetSystemInfo(ctx, systemInfo)




return &types.MsgCreateGameResponse{
GameIndex: newIndex,
}, nil
}, nil
}
64 changes: 32 additions & 32 deletions x/checkers/keeper/msg_server_create_game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,42 @@ import (

func setupMsgServerCreateGame(t testing.TB) (types.MsgServer, keeper.Keeper, context.Context) {
k, ctx := keepertest.CheckersKeeper(t)
checkers.InitGenesis(ctx, k, *types.DefaultGenesis())
return keeper.NewMsgServerImpl(k), k , sdk.WrapSDKContext(ctx)
checkers.InitGenesis(ctx, k, *types.DefaultGenesis())
return keeper.NewMsgServerImpl(k), k, sdk.WrapSDKContext(ctx)
}

func TestCreateGame(t *testing.T) {
msgServer, _, context := setupMsgServerCreateGame(t)
createResponse, err := msgServer.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
})
require.Nil(t, err)
require.EqualValues(t, types.MsgCreateGameResponse{
GameIndex: "1", // TODO: update with a proper value when updated
}, *createResponse)
msgServer, _, context := setupMsgServerCreateGame(t)
createResponse, err := msgServer.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
})
require.Nil(t, err)
require.EqualValues(t, types.MsgCreateGameResponse{
GameIndex: "1", // TODO: update with a proper value when updated
}, *createResponse)
}

func TestCreate1GameHasSaved(t *testing.T) {
msgSrvr, keeper, context := setupMsgServerCreateGame(t)
msgSrvr.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
})
systemInfo, found := keeper.GetSystemInfo(sdk.UnwrapSDKContext(context))
require.True(t, found)
require.EqualValues(t, types.SystemInfo{
NextId: 2,
}, systemInfo)
game1, found1 := keeper.GetStoredGame(sdk.UnwrapSDKContext(context), "1")
require.True(t, found1)
require.EqualValues(t, types.StoredGame{
Index: "1",
Board: "*b*b*b*b|b*b*b*b*|*b*b*b*b|********|********|r*r*r*r*|*r*r*r*r|r*r*r*r*",
Turn: "b",
Black: bob,
Red: carol,
}, game1)
msgSrvr, keeper, context := setupMsgServerCreateGame(t)
msgSrvr.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
})
systemInfo, found := keeper.GetSystemInfo(sdk.UnwrapSDKContext(context))
require.True(t, found)
require.EqualValues(t, types.SystemInfo{
NextId: 2,
}, systemInfo)
game1, found1 := keeper.GetStoredGame(sdk.UnwrapSDKContext(context), "1")
require.True(t, found1)
require.EqualValues(t, types.StoredGame{
Index: "1",
Board: "*b*b*b*b|b*b*b*b*|*b*b*b*b|********|********|r*r*r*r*|*r*r*r*r|r*r*r*r*",
Turn: "b",
Black: bob,
Red: carol,
}, game1)
}
65 changes: 65 additions & 0 deletions x/checkers/keeper/msg_server_play_move.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package keeper

import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/olimdzhon/checkers/x/checkers/rules"
"github.com/olimdzhon/checkers/x/checkers/types"
)

func (k msgServer) PlayMove(goCtx context.Context, msg *types.MsgPlayMove) (*types.MsgPlayMoveResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

storedGame, found := k.Keeper.GetStoredGame(ctx, msg.GameIndex)
if !found {
return nil, errorsmod.Wrapf(types.ErrGameNotFound, "%s", msg.GameIndex)
}

isBlack := storedGame.Black == msg.Creator
isRed := storedGame.Red == msg.Creator
var player rules.Player
if !isBlack && !isRed {
return nil, errorsmod.Wrapf(types.ErrCreatorNotPlayer, "%s", msg.Creator)
} else if isBlack && isRed {
player = rules.StringPieces[storedGame.Turn].Player
} else if isBlack {
player = rules.BLACK_PLAYER
} else {
player = rules.RED_PLAYER
}

game, err := storedGame.ParseGame()
if err != nil {
panic(err.Error())
}

if !game.TurnIs(player) {
return nil, errorsmod.Wrapf(types.ErrNotPlayerTurn, "%s", player)
}

captured, moveErr := game.Move(
rules.Pos{
X: int(msg.FromX),
Y: int(msg.FromY),
},
rules.Pos{
X: int(msg.ToX),
Y: int(msg.ToY),
},
)
if moveErr != nil {
return nil, errorsmod.Wrapf(types.ErrWrongMove, moveErr.Error())
}

storedGame.Board = game.String()
storedGame.Turn = rules.PieceStrings[game.Turn]
k.Keeper.SetStoredGame(ctx, storedGame)

return &types.MsgPlayMoveResponse{
CapturedX: int32(captured.X),
CapturedY: int32(captured.Y),
Winner: rules.PieceStrings[game.Winner()],
}, nil
}
Loading

0 comments on commit 7fe3ab5

Please sign in to comment.