Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

coinflipduel.gno

// Package coinflipduel is a one-on-one coin-flip duel: challenge another
// address, they call heads or tails, and the block settles it. No stakes,
// just bragging rights tracked in an on-chain win/loss record.
package coinflipduel

import (
	"strconv"
	"strings"

	"chain"
	"chain/runtime"

	"gno.land/p/nym-moultest123/kit/ui/v0"
	"gno.land/p/nt/avl/v0"
)

// duelStatus tracks the lifecycle of a duel.
type duelStatus int

const (
	statusPending duelStatus = iota
	statusResolved
	statusCancelled
)

func (s duelStatus) String() string {
	switch s {
	case statusPending:
		return "pending"
	case statusResolved:
		return "resolved"
	case statusCancelled:
		return "cancelled"
	default:
		return "?"
	}
}

// duel is one challenge from Challenger to Opponent over a single coin flip.
// The Challenger calls a side up front; the Opponent's only move is to
// accept, which triggers the flip.
type duel struct {
	ID         string
	Challenger address
	Opponent   address
	Call       string // "heads" or "tails" — the challenger's pick
	Status     duelStatus
	Result     string // "heads" or "tails" once resolved
	Winner     address
	CreatedAt  int64
	ResolvedAt int64
}

// playerState is the persisted win/loss record for one address.
type playerState struct {
	Wins   int
	Losses int
	Duels  int
}

var (
	duels   avl.Tree // duel ID -> *duel
	players avl.Tree // address string -> *playerState

	nextID    int
	flipNonce int
	totalDone int

	champion     address
	championWins int
)

func parseCall(s string) (string, bool) {
	switch strings.ToLower(strings.TrimSpace(s)) {
	case "heads", "h":
		return "heads", true
	case "tails", "t":
		return "tails", true
	default:
		return "", false
	}
}

// flipCoin derives a deterministic pseudo-random side from the current chain
// height, a monotonic per-realm nonce, and both duelists' addresses, so two
// duels resolved in the same block still diverge.
func flipCoin(d *duel) string {
	flipNonce++
	seed := runtime.ChainHeight() + int64(flipNonce)
	s := d.Challenger.String() + d.Opponent.String() + d.ID
	for i := 0; i < len(s); i++ {
		seed += int64(s[i])
	}
	if seed < 0 {
		seed = -seed
	}
	if seed%2 == 0 {
		return "heads"
	}
	return "tails"
}

func getOrCreate(addr address) *playerState {
	key := addr.String()
	if v := players.Get(key); v != nil {
		return v.(*playerState)
	}
	ps := &playerState{}
	players.Set(key, ps)
	return ps
}

func recordResult(winner, loser address) {
	w := getOrCreate(winner)
	w.Wins++
	w.Duels++
	l := getOrCreate(loser)
	l.Losses++
	l.Duels++

	if w.Wins > championWins {
		championWins = w.Wins
		champion = winner
	}
}

// Challenge opens a new duel against opponentAddr, locking in the
// challenger's call of "heads"/"tails" ("h"/"t" also accepted). The
// opponent settles it by calling Accept with the returned duel ID.
func Challenge(cur realm, opponentAddr string, call string) string {
	challenger := cur.Previous().Address()

	opponent := address(strings.TrimSpace(opponentAddr))
	if !opponent.IsValid() {
		panic("invalid opponent address")
	}
	if opponent == challenger {
		panic("cannot duel yourself")
	}

	pick, ok := parseCall(call)
	if !ok {
		panic("call must be heads or tails (h/t)")
	}

	nextID++
	id := strconv.Itoa(nextID)
	d := &duel{
		ID:         id,
		Challenger: challenger,
		Opponent:   opponent,
		Call:       pick,
		Status:     statusPending,
		CreatedAt:  runtime.ChainHeight(),
	}
	duels.Set(id, d)

	chain.Emit("DuelChallenged",
		"id", id,
		"challenger", challenger.String(),
		"opponent", opponent.String(),
		"call", pick,
	)

	return "duel #" + id + " created: you called " + pick +
		", waiting for " + opponent.String() + " to accept"
}

// Accept settles a pending duel. Only the challenged opponent may call it;
// the flip happens immediately and the result is final.
func Accept(cur realm, id string) string {
	caller := cur.Previous().Address()

	v := duels.Get(id)
	if v == nil {
		panic("no such duel")
	}
	d := v.(*duel)

	if d.Status != statusPending {
		panic("duel already " + d.Status.String())
	}
	if caller != d.Opponent {
		panic("only the challenged opponent can accept this duel")
	}

	result := flipCoin(d)
	d.Result = result
	d.Status = statusResolved
	d.ResolvedAt = runtime.ChainHeight()

	if result == d.Call {
		d.Winner = d.Challenger
		recordResult(d.Challenger, d.Opponent)
	} else {
		d.Winner = d.Opponent
		recordResult(d.Opponent, d.Challenger)
	}

	totalDone++

	chain.Emit("DuelResolved",
		"id", id,
		"result", result,
		"winner", d.Winner.String(),
	)

	return "the coin landed on " + result + " -- " + d.Winner.String() + " wins duel #" + id
}

// Cancel withdraws a still-pending duel. Only the challenger may cancel,
// and only before the opponent accepts.
func Cancel(cur realm, id string) string {
	caller := cur.Previous().Address()

	v := duels.Get(id)
	if v == nil {
		panic("no such duel")
	}
	d := v.(*duel)

	if d.Status != statusPending {
		panic("duel already " + d.Status.String())
	}
	if caller != d.Challenger {
		panic("only the challenger can cancel this duel")
	}

	d.Status = statusCancelled
	return "duel #" + id + " cancelled"
}

func renderHome() string {
	var b strings.Builder
	b.WriteString("# Coin-Flip Duel\n\n")
	b.WriteString("Challenge another address to a coin flip. You call heads or tails " +
		"up front; they accept and the block decides. No stakes -- just a record.\n\n")
	b.WriteString("- Total duels resolved: " + strconv.Itoa(totalDone) + "\n")

	if champion.IsValid() {
		b.WriteString("- Reigning champion: `" + champion.String() +
			"` (" + strconv.Itoa(championWins) + " wins)\n")
	} else {
		b.WriteString("- No champion yet -- be the first to win a duel.\n")
	}

	b.WriteString("\n## How to play\n\n")
	b.WriteString("1. `Challenge(opponentAddr, \"heads\"|\"tails\")` -- opens a duel, returns its ID.\n")
	b.WriteString("2. The opponent calls `Accept(id)` -- flips the coin and settles it on the spot.\n")
	b.WriteString("3. The challenger may `Cancel(id)` while it's still pending.\n\n")
	b.WriteString("View a duel at this realm's path plus its ID (e.g. `.../coinflipduel:3`), " +
		"or a player's record plus their address (e.g. `.../coinflipduel:g1youraddress...`).\n\n")

	b.WriteString("## Pending duels\n\n")
	pending := 0
	duels.Iterate("", "", func(key string, value interface{}) bool {
		d := value.(*duel)
		if d.Status == statusPending {
			pending++
			b.WriteString("- #" + d.ID + ": `" + d.Challenger.String() + "` called " +
				d.Call + ", waiting on `" + d.Opponent.String() + "`\n")
		}
		return false
	})
	if pending == 0 {
		b.WriteString("_none right now_\n")
	}

	return b.String()
}

func renderDuel(id string) string {
	v := duels.Get(id)
	if v == nil {
		return "# Duel #" + ui.Inline(id) + "\n\nNo such duel.\n"
	}
	d := v.(*duel)

	var b strings.Builder
	b.WriteString("# Duel #" + d.ID + "\n\n")
	b.WriteString("- Challenger: `" + d.Challenger.String() + "` called **" + d.Call + "**\n")
	b.WriteString("- Opponent: `" + d.Opponent.String() + "`\n")
	b.WriteString("- Status: " + d.Status.String() + "\n")

	if d.Status == statusResolved {
		b.WriteString("- Coin landed on: **" + d.Result + "**\n")
		b.WriteString("- Winner: `" + d.Winner.String() + "`\n")
	}
	return b.String()
}

func renderPlayer(rawAddr string) string {
	addr := strings.TrimSpace(rawAddr)
	safe := ui.Inline(addr)

	v := players.Get(addr)
	if v == nil {
		return "# Player " + safe + "\n\nNo recorded duels yet.\n"
	}
	ps := v.(*playerState)

	var b strings.Builder
	b.WriteString("# Player " + safe + "\n\n")
	b.WriteString("- Wins: " + strconv.Itoa(ps.Wins) + "\n")
	b.WriteString("- Losses: " + strconv.Itoa(ps.Losses) + "\n")
	b.WriteString("- Duels played: " + strconv.Itoa(ps.Duels) + "\n")
	return b.String()
}

// Render shows the duel arena at "", a specific duel when path is a numeric
// ID, or one player's record when path is their bech32 address.
func Render(path string) string {
	path = strings.TrimPrefix(strings.TrimSpace(path), "/")
	if path == "" {
		return renderHome()
	}
	if _, err := strconv.Atoi(path); err == nil {
		return renderDuel(path)
	}
	return renderPlayer(path)
}

coinflipduel_test.gno

package coinflipduel

import (
	"testing"

	"gno.land/p/nt/avl/v0"
	"gno.land/p/nt/testutils/v0"
)

func resetState() {
	duels = avl.Tree{}
	players = avl.Tree{}
	nextID = 0
	flipNonce = 0
	totalDone = 0
	var zero address
	champion = zero
	championWins = 0
}

func TestParseCall(t *testing.T) {
	for _, s := range []string{"heads", "Heads", " h ", "tails", "T"} {
		if _, ok := parseCall(s); !ok {
			t.Fatalf("parseCall(%q) should be valid", s)
		}
	}
	if _, ok := parseCall("sideways"); ok {
		t.Fatal(`parseCall("sideways") should be invalid`)
	}
}

// Crossing: Challenge and Accept mutate realm state, so the test needs `cur realm`.
func TestChallengeAndAccept(cur realm, t *testing.T) {
	resetState()
	alice := testutils.TestAddress("alice")
	bob := testutils.TestAddress("bob")

	testing.SetRealm(testing.NewUserRealm(alice))
	Challenge(cross(cur), bob.String(), "heads")

	if !duels.Has("1") {
		t.Fatal("expected duel #1 to exist")
	}

	testing.SetRealm(testing.NewUserRealm(bob))
	Accept(cross(cur), "1")

	v := duels.Get("1")
	d := v.(*duel)
	if d.Status != statusResolved {
		t.Fatalf("Status = %v, want resolved", d.Status)
	}
	if d.Result != "heads" && d.Result != "tails" {
		t.Fatalf("Result = %q, want heads or tails", d.Result)
	}
	if totalDone != 1 {
		t.Fatalf("totalDone = %d, want 1", totalDone)
	}

	winnerStats := getOrCreate(d.Winner)
	if winnerStats.Wins != 1 {
		t.Fatalf("winner Wins = %d, want 1", winnerStats.Wins)
	}
	if !champion.IsValid() || champion != d.Winner {
		t.Fatal("champion should be the duel winner")
	}
}

func TestChallengeSelfRejected(cur realm, t *testing.T) {
	resetState()
	alice := testutils.TestAddress("alice")
	testing.SetRealm(testing.NewUserRealm(alice))

	// Challenge panics inside a cross-realm call; a plain defer/recover in
	// this test does not catch a cross-boundary panic, so use revive().
	rec := revive(func() { Challenge(cross(cur), alice.String(), "heads") })
	if rec == nil {
		t.Fatal("expected panic when challenging yourself")
	}
}

func TestOnlyOpponentCanAccept(cur realm, t *testing.T) {
	resetState()
	alice := testutils.TestAddress("alice")
	bob := testutils.TestAddress("bob")
	mallory := testutils.TestAddress("mallory")

	testing.SetRealm(testing.NewUserRealm(alice))
	Challenge(cross(cur), bob.String(), "tails")

	testing.SetRealm(testing.NewUserRealm(mallory))
	rec := revive(func() { Accept(cross(cur), "1") })
	if rec == nil {
		t.Fatal("expected panic when a non-opponent accepts")
	}
}

func TestCancel(cur realm, t *testing.T) {
	resetState()
	alice := testutils.TestAddress("alice")
	bob := testutils.TestAddress("bob")

	testing.SetRealm(testing.NewUserRealm(alice))
	Challenge(cross(cur), bob.String(), "heads")
	Cancel(cross(cur), "1")

	v := duels.Get("1")
	d := v.(*duel)
	if d.Status != statusCancelled {
		t.Fatalf("Status = %v, want cancelled", d.Status)
	}
}

// Non-crossing: Render is a plain read.
func TestRenderHome(t *testing.T) {
	resetState()
	out := Render("")
	if out == "" {
		t.Fatal(`Render("") should not be empty`)
	}
}

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/coinflipduel/v1"
gno = "0.9"
private = true

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186413
  max_deposit = "7000000ugnot"

render_example_test.gno

package coinflipduel

// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # Coin-Flip Duel
	//
	// Challenge another address to a coin flip. You call heads or tails up front; they accept and the block decides. No stakes -- just a record.
	//
	// - Total duels resolved: 0
	// - No champion yet -- be the first to win a duel.
	//
	// ## How to play
	//
	// 1. `Challenge(opponentAddr, "heads"|"tails")` -- opens a duel, returns its ID.
	// 2. The opponent calls `Accept(id)` -- flips the coin and settles it on the spot.
	// 3. The challenger may `Cancel(id)` while it's still pending.
	//
	// View a duel at this realm's path plus its ID (e.g. `.../coinflipduel:3`), or a player's record plus their address (e.g. `.../coinflipduel:g1youraddress...`).
	//
	// ## Pending duels
	//
	// _none right now_
}