Skip to content

Commit

Permalink
Add player info handling and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierlepretre committed Feb 12, 2024
1 parent aa5e8ba commit cf1df87
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 0 deletions.
1 change: 1 addition & 0 deletions x/checkers/keeper/end_block_server_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (k Keeper) ForfeitExpiredGames(goCtx context.Context) {
panic(fmt.Sprintf(types.ErrCannotFindWinnerByColor.Error(), storedGame.Turn))
}
k.MustPayWinnings(ctx, &storedGame)
k.MustRegisterPlayerForfeit(ctx, &storedGame)
storedGame.Board = ""
k.SetStoredGame(ctx, storedGame)
}
Expand Down
103 changes: 103 additions & 0 deletions x/checkers/keeper/end_block_server_game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,106 @@ func TestForfeit2OldestPlayedTwiceIn1CallCalledBank(t *testing.T) {
keeper.SetStoredGame(ctx, game2)
keeper.ForfeitExpiredGames(context)
}

func TestForfeitGameAddPlayerInfo(t *testing.T) {
msgServer, keeper, 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,
})
game1, found := keeper.GetStoredGame(ctx, "1")
require.True(t, found)
oldDeadline := types.FormatDeadline(ctx.BlockTime().Add(time.Duration(-1)))
game1.Deadline = oldDeadline
keeper.SetStoredGame(ctx, game1)
keeper.ForfeitExpiredGames(context)

bobInfo, found := keeper.GetPlayerInfo(ctx, bob)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: bob,
WonCount: 0,
LostCount: 0,
ForfeitedCount: 1,
}, bobInfo)
carolInfo, found := keeper.GetPlayerInfo(ctx, carol)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: carol,
WonCount: 1,
LostCount: 0,
ForfeitedCount: 0,
}, carolInfo)
}

func TestForfeiGameUpdatePlayerInfo(t *testing.T) {
msgServer, keeper, context := setupMsgServerWithOneGameForPlayMove(t)
ctx := sdk.UnwrapSDKContext(context)

keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: bob,
WonCount: 1,
LostCount: 2,
ForfeitedCount: 3,
})
keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: carol,
WonCount: 4,
LostCount: 5,
ForfeitedCount: 6,
})

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,
})
game1, found := keeper.GetStoredGame(ctx, "1")
require.True(t, found)
oldDeadline := types.FormatDeadline(ctx.BlockTime().Add(time.Duration(-1)))
game1.Deadline = oldDeadline
keeper.SetStoredGame(ctx, game1)
keeper.ForfeitExpiredGames(context)

bobInfo, found := keeper.GetPlayerInfo(ctx, bob)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: bob,
WonCount: 1,
LostCount: 2,
ForfeitedCount: 4,
}, bobInfo)
carolInfo, found := keeper.GetPlayerInfo(ctx, carol)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: carol,
WonCount: 5,
LostCount: 5,
ForfeitedCount: 6,
}, carolInfo)
}
81 changes: 81 additions & 0 deletions x/checkers/keeper/msg_server_create_game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,84 @@ func TestSavedCreatedDeadlineIsParseable(t *testing.T) {
_, err := game.GetDeadlineAsTime()
require.Nil(t, err)
}

func TestCreatePlayerInfoNotAdded(t *testing.T) {
msgServer, keeper, context := setupMsgServerCreateGame(t)
ctx := sdk.UnwrapSDKContext(context)
msgServer.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
Wager: 45,
Denom: "stake",
})
aliceInfo, found := keeper.GetPlayerInfo(ctx, alice)
require.False(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: "",
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, aliceInfo)
bobInfo, found := keeper.GetPlayerInfo(ctx, bob)
require.False(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: "",
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, bobInfo)
carolInfo, found := keeper.GetPlayerInfo(ctx, carol)
require.False(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: "",
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, carolInfo)
}

func TestCreatePlayerInfoNotUpdated(t *testing.T) {
msgServer, keeper, context := setupMsgServerCreateGame(t)
ctx := sdk.UnwrapSDKContext(context)
keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: alice,
})
keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: bob,
})
keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: carol,
})
msgServer.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
Wager: 45,
Denom: "stake",
})
aliceInfo, found := keeper.GetPlayerInfo(ctx, alice)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: alice,
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, aliceInfo)
bobInfo, found := keeper.GetPlayerInfo(ctx, bob)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: bob,
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, bobInfo)
carolInfo, found := keeper.GetPlayerInfo(ctx, carol)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: carol,
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, carolInfo)
}
1 change: 1 addition & 0 deletions x/checkers/keeper/msg_server_play_move.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (k msgServer) PlayMove(goCtx context.Context, msg *types.MsgPlayMove) (*typ
storedGame.Board = ""
k.Keeper.RemoveFromFifo(ctx, &storedGame, &systemInfo)
k.Keeper.MustPayWinnings(ctx, &storedGame)
k.Keeper.MustRegisterPlayerWin(ctx, &storedGame)
}

storedGame.Deadline = types.FormatDeadline(types.GetNextDeadline(ctx))
Expand Down
56 changes: 56 additions & 0 deletions x/checkers/keeper/msg_server_play_move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,59 @@ func TestSavedPlayedDeadlineIsParseable(t *testing.T) {
_, err := game.GetDeadlineAsTime()
require.Nil(t, err)
}

func TestPlayerInfoNoAdditionOnNoWinner(t *testing.T) {
msgServer, keeper, context := setupMsgServerWithOneGameForPlayMove(t)
ctx := sdk.UnwrapSDKContext(context)
msgServer.PlayMove(context, &types.MsgPlayMove{
Creator: bob,
GameIndex: "1",
FromX: 1,
FromY: 2,
ToX: 2,
ToY: 3,
})
bobInfo, found := keeper.GetPlayerInfo(ctx, bob)
require.False(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: "",
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, bobInfo)
}

func TestPlayerInfoNoUpdatedOnNoWinner(t *testing.T) {
msgServer, keeper, context := setupMsgServerWithOneGameForPlayMove(t)
ctx := sdk.UnwrapSDKContext(context)
keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: bob,
})
keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: carol,
})
msgServer.PlayMove(context, &types.MsgPlayMove{
Creator: bob,
GameIndex: "1",
FromX: 1,
FromY: 2,
ToX: 2,
ToY: 3,
})
bobInfo, found := keeper.GetPlayerInfo(ctx, bob)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: bob,
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, bobInfo)
carolInfo, found := keeper.GetPlayerInfo(ctx, carol)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: carol,
WonCount: 0,
LostCount: 0,
ForfeitedCount: 0,
}, carolInfo)
}
61 changes: 61 additions & 0 deletions x/checkers/keeper/msg_server_play_move_winner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,64 @@ func TestPlayMoveUpToWinnerCalledBank(t *testing.T) {

testutil.PlayAllMoves(t, msgServer, context, "1", bob, carol, testutil.Game1Moves)
}

func TestCompleteGameAddPlayerInfo(t *testing.T) {
msgServer, keeper, context := setupMsgServerWithOneGameForPlayMove(t)
ctx := sdk.UnwrapSDKContext(context)

testutil.PlayAllMoves(t, msgServer, context, "1", bob, carol, testutil.Game1Moves)

bobInfo, found := keeper.GetPlayerInfo(ctx, bob)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: bob,
WonCount: 1,
LostCount: 0,
ForfeitedCount: 0,
}, bobInfo)
carolInfo, found := keeper.GetPlayerInfo(ctx, carol)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: carol,
WonCount: 0,
LostCount: 1,
ForfeitedCount: 0,
}, carolInfo)
}

func TestCompleteGameUpdatePlayerInfo(t *testing.T) {
msgServer, keeper, context := setupMsgServerWithOneGameForPlayMove(t)
ctx := sdk.UnwrapSDKContext(context)

keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: bob,
WonCount: 1,
LostCount: 2,
ForfeitedCount: 3,
})
keeper.SetPlayerInfo(ctx, types.PlayerInfo{
Index: carol,
WonCount: 4,
LostCount: 5,
ForfeitedCount: 6,
})

testutil.PlayAllMoves(t, msgServer, context, "1", bob, carol, testutil.Game1Moves)

bobInfo, found := keeper.GetPlayerInfo(ctx, bob)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: bob,
WonCount: 2,
LostCount: 2,
ForfeitedCount: 3,
}, bobInfo)
carolInfo, found := keeper.GetPlayerInfo(ctx, carol)
require.True(t, found)
require.EqualValues(t, types.PlayerInfo{
Index: carol,
WonCount: 4,
LostCount: 6,
ForfeitedCount: 6,
}, carolInfo)
}
Loading

0 comments on commit cf1df87

Please sign in to comment.