Skip to content

Commit

Permalink
auto expiring games;
Browse files Browse the repository at this point in the history
  • Loading branch information
olimdzhon committed Aug 1, 2024
1 parent f625e8f commit eb3776e
Show file tree
Hide file tree
Showing 7 changed files with 1,306 additions and 4 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ require (
google.golang.org/protobuf v1.34.2
)

require github.com/btcsuite/btcd v0.22.1 // indirect

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.2-20240508200655-46a4cf4ba109.2 // indirect
buf.build/gen/go/bufbuild/registry/connectrpc/go v1.16.2-20240610164129-660609bc46d3.1 // indirect
Expand Down Expand Up @@ -219,6 +221,7 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tendermint/tendermint v0.35.9
github.com/tidwall/btree v1.7.0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
Expand Down
696 changes: 696 additions & 0 deletions go.sum

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions x/checkers/keeper/end_block_server_game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package keeper

import (
"context"
"fmt"

rules "github.com/olimdzhon/checkers/x/checkers/rules"
"github.com/olimdzhon/checkers/x/checkers/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) ForfeitExpiredGames(goCtx context.Context) {
ctx := sdk.UnwrapSDKContext(goCtx)

opponents := map[string]string{
rules.PieceStrings[rules.BLACK_PLAYER]: rules.PieceStrings[rules.RED_PLAYER],
rules.PieceStrings[rules.RED_PLAYER]: rules.PieceStrings[rules.BLACK_PLAYER],
}

// Get FIFO information
systemInfo, found := k.GetSystemInfo(ctx)
if !found {
panic("SystemInfo not found")
}

gameIndex := systemInfo.FifoHeadIndex
var storedGame types.StoredGame
for {
// Finished moving along
if gameIndex == types.NoFifoIndex {
break
}
storedGame, found = k.GetStoredGame(ctx, gameIndex)
if !found {
panic("Fifo head game not found " + systemInfo.FifoHeadIndex)
}
deadline, err := storedGame.GetDeadlineAsTime()
if err != nil {
panic(err)
}
if deadline.Before(ctx.BlockTime()) {
// Game is past deadline
k.RemoveFromFifo(ctx, &storedGame, &systemInfo)
lastBoard := storedGame.Board
if storedGame.MoveCount <= 1 {
// No point in keeping a game that was never really played
k.RemoveStoredGame(ctx, gameIndex)
} else {
storedGame.Winner, found = opponents[storedGame.Turn]
if !found {
panic(fmt.Sprintf(types.ErrCannotFindWinnerByColor.Error(), storedGame.Turn))
}
storedGame.Board = ""
k.SetStoredGame(ctx, storedGame)
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.GameForfeitedEventType,
sdk.NewAttribute(types.GameForfeitedEventGameIndex, gameIndex),
sdk.NewAttribute(types.GameForfeitedEventWinner, storedGame.Winner),
sdk.NewAttribute(types.GameForfeitedEventBoard, lastBoard),
),
)
// Move along FIFO
gameIndex = systemInfo.FifoHeadIndex
} else {
// All other games after are active anyway
break
}
}

k.SetSystemInfo(ctx, systemInfo)
}
Loading

0 comments on commit eb3776e

Please sign in to comment.