connect4_v3 source realm
package connect4\_v3 is a staked Connect 4 game. A creator posts an offer with a ugnot stake; anyone (or a named oppo...
package connect4_v3 is a staked Connect 4 game. A creator posts an offer with a ugnot stake; anyone (or a named opponent) accepts with the same stake; the winner takes the pot minus a flat house fee.
The first mover is picked by commit-reveal: the creator offers with sha256(passphrase) and must Reveal the passphrase within 90s of Accept, or forfeit. The draw also mixes in the block height of Accept, which the creator cannot know when committing (so it cannot grind a passphrase for a known opponent), while the acceptor cannot use it without the passphrase. Randomness computed in Accept alone could be re-rolled by an acceptor who bundles [Accept, Play] in one atomic tx and lets it revert.
Calls signed by an account session key (Quick play) may only Play, Reveal, ClaimTimeout and Cancel: a stolen session key must not resign games, move stakes or act as the owner.
Every move has a 90s clock measured in block time. Nothing on-chain fires by itself, so an expired clock is settled by anyone calling ClaimTimeout: an unrevealed game is won by the acceptor, a game whose first mover never moved is void and both stakes are refunded, otherwise the player who ran out of time forfeits.
1
13
func Accept
crossing ActionAccept joins an open offer by sending exactly its stake. The creator then has 90s to Reveal, which picks the first mover.
func ActiveJSON
ActionActiveJSON returns {"now":<unix>,"games":[...]}: Open and Playing games in id order, skipping offset and returning at most limit (clamped to 1-100).
func Cancel
crossing ActionCancel withdraws an open offer and refunds the creator. The creator may cancel at any time; anyone may once the offer has expired.
func ClaimTimeout
crossing ActionClaimTimeout settles a game whose move clock ran out. Anyone may call it.
func GameJSON
ActionGameJSON returns {"now":<unix>,"game":<game>|null}. It is a read-only accessor for clients querying over vm/qeval; now is the block time.
func Offer
crossing ActionOffer posts a game open for validFor minutes. The stake is the ugnot sent with the call. A non-empty opponent makes the offer private. commitment is the lowercase hex sha256 of a secret passphrase, revealed after Accept.
func Play
crossing ActionPlay drops a piece in column (1-7). move is the game's move count the player saw (Moves before this piece): a resent or delayed move that the game has moved past is refused instead of being played on a later turn.
func Render
Render serves "" (lobby and leaderboards), "new" (offer form) and "game/<id>" (one game with every action its state allows). Everything written comes from realm state or from query values that have been validated and re-formatted; raw path input is never echoed.
func Resign
crossing ActionResign concedes a game in progress.
func Reveal
crossing ActionReveal discloses the creator's passphrase, which picks the first mover. It must come within 90s of Accept.
func SetFee
crossing ActionSetFee changes the flat house fee, in ugnot, for new offers. Games already offered keep the fee they were created with.
func TransferOwnership
crossing Actionfunc WithdrawFees
crossing ActionWithdrawFees sends all collected fees to to.
3
type Game
struct 1type Game struct {
2 ID int
3 Creator address
4 Opponent address // optional; if set, only this address may accept
5 Acceptor address
6 Stake int64 // ugnot, per player
7 Fee int64 // ugnot house fee, fixed at offer time
8 ExpiresAt time.Time
9 // AcceptHeight is the block height of Accept, mixed into the first-mover draw.
10 AcceptHeight int64
11 // Commitment is the lowercase hex sha256 of the creator's passphrase.
12 Commitment string
13
14 Board board
15 Turn byte // 0 = awaiting Reveal, 1 = creator, 2 = acceptor
16 Moves int
17 LastCol int // column and row of the last piece dropped, valid if Moves > 0
18 LastRow int
19 LastMove time.Time
20 Status Status
21 Winner address
22}