-
Notifications
You must be signed in to change notification settings - Fork 40
/
wager_handler.go
79 lines (74 loc) · 2.53 KB
/
wager_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
rules "github.com/xavierlepretre/checkers/x/checkers/rules"
"github.com/xavierlepretre/checkers/x/checkers/types"
)
// Returns an error if the player has not enough funds.
func (k *Keeper) CollectWager(ctx sdk.Context, storedGame *types.StoredGame) error {
// Make the player pay the wager at the beginning
if storedGame.MoveCount == 0 {
// Black plays first
black, err := storedGame.GetBlackAddress()
if err != nil {
panic(err.Error())
}
err = k.bank.SendCoinsFromAccountToModule(ctx, black, types.ModuleName, sdk.NewCoins(storedGame.GetWagerCoin()))
if err != nil {
return sdkerrors.Wrapf(err, types.ErrBlackCannotPay.Error())
}
} else if storedGame.MoveCount == 1 {
// Red plays second
red, err := storedGame.GetBlackAddress()
if err != nil {
panic(err.Error())
}
err = k.bank.SendCoinsFromAccountToModule(ctx, red, types.ModuleName, sdk.NewCoins(storedGame.GetWagerCoin()))
if err != nil {
return sdkerrors.Wrapf(err, types.ErrRedCannotPay.Error())
}
}
return nil
}
// Game must have a valid winner.
func (k *Keeper) MustPayWinnings(ctx sdk.Context, storedGame *types.StoredGame) {
// Pay the winnings to the winner
winnerAddress, found, err := storedGame.GetWinnerAddress()
if err != nil {
panic(err.Error())
}
if !found {
panic(fmt.Sprintf(types.ErrCannotFindWinnerByColor.Error(), storedGame.Winner))
}
winnings := storedGame.GetWagerCoin()
if storedGame.MoveCount == 0 {
panic(types.ErrNothingToPay.Error())
} else if 1 < storedGame.MoveCount {
winnings = winnings.Add(winnings)
}
err = k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, winnerAddress, sdk.NewCoins(winnings))
if err != nil {
panic(types.ErrCannotPayWinnings.Error())
}
}
// Game must be in a state where it can be refunded.
func (k *Keeper) MustRefundWager(ctx sdk.Context, storedGame *types.StoredGame) {
// Refund wager to black player if red rejects after black played
if storedGame.MoveCount == 1 {
black, err := storedGame.GetBlackAddress()
if err != nil {
panic(err.Error())
}
err = k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, black, sdk.NewCoins(storedGame.GetWagerCoin()))
if err != nil {
panic(fmt.Sprintf(types.ErrCannotRefundWager.Error(), rules.BLACK_PLAYER.Color))
}
} else if storedGame.MoveCount == 0 {
// Do nothing
} else {
// TODO Implement a draw mechanism.
panic(fmt.Sprintf(types.ErrNotInRefundState.Error(), storedGame.MoveCount))
}
}