Skip to content

Commit

Permalink
emit game information;
Browse files Browse the repository at this point in the history
  • Loading branch information
olimdzhon committed Jul 31, 2024
1 parent 7fe3ab5 commit 49bdad5
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
9 changes: 9 additions & 0 deletions x/checkers/keeper/msg_server_create_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func (k msgServer) CreateGame(goCtx context.Context, msg *types.MsgCreateGame) (
systemInfo.NextId++
k.Keeper.SetSystemInfo(ctx, systemInfo)

ctx.EventManager().EmitEvent(
sdk.NewEvent(types.GameCreatedEventType,
sdk.NewAttribute(types.GameCreatedEventCreator, msg.Creator),
sdk.NewAttribute(types.GameCreatedEventGameIndex, newIndex),
sdk.NewAttribute(types.GameCreatedEventBlack, msg.Black),
sdk.NewAttribute(types.GameCreatedEventRed, msg.Red),
),
)

return &types.MsgCreateGameResponse{
GameIndex: newIndex,
}, nil
Expand Down
41 changes: 41 additions & 0 deletions x/checkers/keeper/msg_server_create_game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,44 @@ func TestCreate1GameHasSaved(t *testing.T) {
Red: carol,
}, game1)
}

func TestCreate1GameGetAll(t *testing.T) {
msgSrvr, keeper, context := setupMsgServerCreateGame(t)
msgSrvr.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
})
games := keeper.GetAllStoredGame(sdk.UnwrapSDKContext(context))
require.Len(t, games, 1)
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,
}, games[0])
}

func TestCreate1GameEmitted(t *testing.T) {
msgSrvr, _, context := setupMsgServerCreateGame(t)
ctx := sdk.UnwrapSDKContext(context)
msgSrvr.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
})
require.NotNil(t, ctx)
events := sdk.StringifyEvents(ctx.EventManager().ABCIEvents())
require.Len(t, events, 1)
event := events[0]
require.EqualValues(t, sdk.StringEvent{
Type: "new-game-created",
Attributes: []sdk.Attribute{
{Key: "creator", Value: alice},
{Key: "game-index", Value: "1"},
{Key: "black", Value: bob},
{Key: "red", Value: carol},
},
}, event)
}
11 changes: 11 additions & 0 deletions x/checkers/keeper/msg_server_play_move.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"strconv"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -57,6 +58,16 @@ func (k msgServer) PlayMove(goCtx context.Context, msg *types.MsgPlayMove) (*typ
storedGame.Turn = rules.PieceStrings[game.Turn]
k.Keeper.SetStoredGame(ctx, storedGame)

ctx.EventManager().EmitEvent(
sdk.NewEvent(types.MovePlayedEventType,
sdk.NewAttribute(types.MovePlayedEventCreator, msg.Creator),
sdk.NewAttribute(types.MovePlayedEventGameIndex, msg.GameIndex),
sdk.NewAttribute(types.MovePlayedEventCapturedX, strconv.FormatInt(int64(captured.X), 10)),
sdk.NewAttribute(types.MovePlayedEventCapturedY, strconv.FormatInt(int64(captured.Y), 10)),
sdk.NewAttribute(types.MovePlayedEventWinner, rules.PieceStrings[game.Winner()]),
),
)

return &types.MsgPlayMoveResponse{
CapturedX: int32(captured.X),
CapturedY: int32(captured.Y),
Expand Down
60 changes: 60 additions & 0 deletions x/checkers/keeper/msg_server_play_move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ func TestPlayMoveSavedGame(t *testing.T) {
}, game1)
}

func TestPlayMoveEmitted(t *testing.T) {
msgServer, _, context := setupMsgServerWithOneGameForPlayMove(t)
ctx := sdk.UnwrapSDKContext(context)
msgServer.PlayMove(context, &types.MsgPlayMove{
Creator: bob,
GameIndex: "1",
FromX: 1,
FromY: 2,
ToX: 2,
ToY: 3,
})
require.NotNil(t, ctx)
events := sdk.StringifyEvents(ctx.EventManager().ABCIEvents())
require.Len(t, events, 2)
event := events[0]
require.EqualValues(t, sdk.StringEvent{
Type: "move-played",
Attributes: []sdk.Attribute{
{Key: "creator", Value: bob},
{Key: "game-index", Value: "1"},
{Key: "captured-x", Value: "-1"},
{Key: "captured-y", Value: "-1"},
{Key: "winner", Value: "*"},
},
}, event)
}

func TestPlayMoveNotPlayer(t *testing.T) {
msgServer, _, context := setupMsgServerWithOneGameForPlayMove(t)
playMoveResponse, err := msgServer.PlayMove(context, &types.MsgPlayMove{
Expand Down Expand Up @@ -194,6 +221,39 @@ func TestPlayMove2SavedGame(t *testing.T) {
}, game1)
}

func TestPlayMove2Emitted(t *testing.T) {
msgServer, _, context := setupMsgServerWithOneGameForPlayMove(t)
ctx := sdk.UnwrapSDKContext(context)
msgServer.PlayMove(context, &types.MsgPlayMove{
Creator: bob,
GameIndex: "1",
FromX: 1,
FromY: 2,
ToX: 2,
ToY: 3,
})
msgServer.PlayMove(context, &types.MsgPlayMove{
Creator: carol,
GameIndex: "1",
FromX: 0,
FromY: 5,
ToX: 1,
ToY: 4,
})
require.NotNil(t, ctx)
events := sdk.StringifyEvents(ctx.EventManager().ABCIEvents())
require.Len(t, events, 2)
event := events[0]
require.Equal(t, "move-played", event.Type)
require.EqualValues(t, []sdk.Attribute{
{Key: "creator", Value: carol},
{Key: "game-index", Value: "1"},
{Key: "captured-x", Value: "-1"},
{Key: "captured-y", Value: "-1"},
{Key: "winner", Value: "*"},
}, event.Attributes[5:])
}

func TestPlayMove3(t *testing.T) {
msgServer, _, context := setupMsgServerWithOneGameForPlayMove(t)
msgServer.PlayMove(context, &types.MsgPlayMove{
Expand Down
17 changes: 17 additions & 0 deletions x/checkers/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ func KeyPrefix(p string) []byte {
const (
SystemInfoKey = "SystemInfo/value/"
)

const (
GameCreatedEventType = "new-game-created" // Indicates what event type to listen to
GameCreatedEventCreator = "creator" // Subsidiary information
GameCreatedEventGameIndex = "game-index" // What game is relevant
GameCreatedEventBlack = "black" // Is it relevant to me?
GameCreatedEventRed = "red" // Is it relevant to me?
)

const (
MovePlayedEventType = "move-played"
MovePlayedEventCreator = "creator"
MovePlayedEventGameIndex = "game-index"
MovePlayedEventCapturedX = "captured-x"
MovePlayedEventCapturedY = "captured-y"
MovePlayedEventWinner = "winner"
)

0 comments on commit 49bdad5

Please sign in to comment.