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

cask.gno

// Package cask is a Cellar challenge about fixed-width integer arithmetic.
package cask

import (
	"strconv"
	"strings"

	"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
	"gno.land/p/nt/avl/v0"
)

const (
	price int64 = 1_099_511_627_776 // shillings per cask of the 1811
	purse int64 = 100               // what every guest starts with
	goal  int64 = 1_000_000         // casks that make the cellar yours
)

type account struct {
	shillings int64
	casks     int64
}

var accounts = avl.NewTree() // address -> *account

// Buy purchases casks at the cooper's price.
func Buy(cur realm, casks int64) {
	player := flag.MustPlayer(cur.Previous())
	if casks <= 0 {
		panic("cask: buy at least one cask")
	}
	a := accountOf(player)
	cost := casks * price
	if cost > a.shillings {
		panic("cask: the cooper laughs; you cannot afford that")
	}
	a.shillings -= cost
	a.casks += casks
	if a.casks >= goal {
		captures.Record(player)
	}
}

// Holdings returns the purse and casks of addr.
func Holdings(addr address) (shillings, casks int64) {
	a := accountOf(addr)
	return a.shillings, a.casks
}

func accountOf(addr address) *account {
	if v := accounts.Get(addr.String()); v != nil {
		return v.(*account)
	}
	a := &account{shillings: purse}
	accounts.Set(addr.String(), a)
	return a
}

// Render shows the price list and the guests' books.
func Render(_ string) string {
	var b strings.Builder
	b.WriteString(flag.Card(info, captures))
	b.WriteString("\n**Price:** " + strconv.FormatInt(price, 10) + " shillings a cask · **Purse:** " +
		strconv.FormatInt(purse, 10) + " shillings · **Goal:** " + strconv.FormatInt(goal, 10) + " casks\n\n")
	b.WriteString("## The cooper's book\n\n| Guest | Shillings | Casks |\n|---|---:|---:|\n")
	accounts.Iterate("", "", func(addr string, v any) bool {
		a := v.(*account)
		b.WriteString("| `" + addr + "` | " + strconv.FormatInt(a.shillings, 10) + " | " + strconv.FormatInt(a.casks, 10) + " |\n")
		return false
	})
	b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
	return b.String()
}

cask_test.gno

package cask

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
)

const ash = address("g1zkzjmyveh7euq83h5zhnhj6mg0d7kr35nphzd2")

func TestNewGuestHasAPurse(t *testing.T) {
	shillings, casks := Holdings(ash)
	uassert.Equal(t, purse, shillings)
	uassert.Equal(t, int64(0), casks)
}

func TestHonestPurchasesFail(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewUserRealm(ash))
	uassert.AbortsContains(t, cur, "cannot afford", func() { Buy(cross(cur), 1) })
	uassert.AbortsContains(t, cur, "cannot afford", func() { Buy(cross(cur), goal) })
	uassert.AbortsContains(t, cur, "at least one", func() { Buy(cross(cur), 0) })
	uassert.AbortsContains(t, cur, "at least one", func() { Buy(cross(cur), -goal) })
	uassert.False(t, captures.Captured(ash))
}

challenge.gno

package cask

import (
	"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
	"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/hall"
)

var info = flag.Info{
	Slug:       "cask",
	Title:      "Cask Strength",
	Difficulty: flag.Easy,
	Category:   "integer arithmetic",
	Brief: "The cooper sells casks of the 1811 at a price no guest can pay, and every guest starts with " +
		"a purse of 100 shillings. He does the sums himself and he never checks his own arithmetic. " +
		"Own a million casks.",
}

var captures = flag.NewLedger()

type challenge struct{}

func (challenge) Info() flag.Info              { return info }
func (challenge) Captured(player address) bool { return captures.Captured(player) }

// Challenge describes this realm to a hall.
func Challenge() flag.Challenge { return challenge{} }

func init(cur realm) { hall.Enlist(cross(cur), challenge{}) }

gnomod.toml

module = "gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/cask"
gno = "0.9"

[addpkg]
  creator = "g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf"
  height = 1457730
  max_deposit = "100000000ugnot"