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.

flag source pure

Package flag is the shared vocabulary of Cellar, a capture-the-flag wargame played on gno.land.

Overview

Package flag is the shared vocabulary of Cellar, a capture-the-flag wargame played on gno.land.

A challenge is a realm that holds a deliberate weakness. When a player exploits it, the realm records a capture in a Ledger. A hall (a scoreboard realm) learns about the challenge through the Challenge interface and asks it, on claim, whether a given player captured it.

Anyone can author a challenge by importing this package:

Example
 1package riddle
 2
 3import (
 4	"gno.land/p/<ns>/cellar/flag"
 5	"gno.land/r/<ns>/cellar/hall"
 6)
 7
 8var captures = flag.NewLedger()
 9
10type challenge struct{}
11
12func (challenge) Info() flag.Info { return info }
13func (challenge) Captured(p address) bool { return captures.Captured(p) }
14
15func init(cur realm) { hall.Enlist(cross(cur), challenge{}) }

The package also carries the scoring curve (Value) and the rule that maps a caller to the player who gets the credit (Player).

Constants 3

const DecaySolves

1const DecaySolves = 20
source

DecaySolves is the number of solves after which a challenge settles at its floor value.

const Easy, Medium, Hard

1const (
2	Easy Difficulty = iota + 1
3	Medium
4	Hard
5)
source

Difficulty levels, from a first evening in the cellar to a long night.

Variables 2

var ErrBadSlug, ErrNoTitle, ErrBadDifficulty, ErrNoBrief

1var (
2	ErrBadSlug       = errors.New("flag: slug must be 3-24 chars of [a-z0-9-]")
3	ErrNoTitle       = errors.New("flag: title is required")
4	ErrBadDifficulty = errors.New("flag: unknown difficulty")
5	ErrNoBrief       = errors.New("flag: brief is required")
6)
source

Errors returned by Validate.

var ErrNoPlayer

1var ErrNoPlayer = errors.New("flag: deploy your solution under gno.land/r/<your-g1-address>/ so the hall knows who you are")
source

ErrNoPlayer is returned when a caller cannot be tied to a player.

Functions 4

func Captures

1func Captures(l *Ledger, n int) string
source

Captures renders the n most recent captures of l as a markdown list.

func Card

1func Card(info Info, l *Ledger) string
source

Card renders the standard header of a challenge page: title, grade, category, number of captures and the brief. Authors use it at the top of their Render so every challenge in the cellar reads the same way.

func Value

1func Value(d Difficulty, solves int) int
source

Value returns what a challenge of difficulty d is worth to every one of its solvers once it has solves solvers. It follows the CTFd dynamic curve: the value falls with the square of the solve count and settles at the floor after DecaySolves solves. A lone solver keeps the full value, and the value applies retroactively to all solvers.

func NewLedger

1func NewLedger() *Ledger
source

NewLedger returns an empty Ledger.

Types 6

type Caller

interface
1type Caller interface {
2	Address() address
3	PkgPath() string
4}
source

Caller is the part of a realm value Player needs. Pass cur.Previous().

type Capture

struct
1type Capture struct {
2	Player address
3	Height int64 // block height of the capture
4	Order  int   // 1 for the first capture of this challenge
5}
source

Capture is the proof that a player broke a challenge.

type Challenge

interface
1type Challenge interface {
2	Info() Info
3	Captured(player address) bool
4}
source

Challenge is what a challenge realm hands to a hall.

Implementations live in the challenge realm, so their methods run with that realm's storage. Captured must be a pure read.

type Difficulty

ident
1type Difficulty int
source

Difficulty grades a challenge. It sets the starting value of the challenge on the scoring curve.

Methods on Difficulty

func FirstBloodBonus

method on Difficulty
1func (d Difficulty) FirstBloodBonus() int
source

FirstBloodBonus returns the one-time bonus for the first solver.

func Floor

method on Difficulty
1func (d Difficulty) Floor() int
source

Floor returns the lowest value a challenge of difficulty d decays to: two fifths of its initial value.

func Initial

method on Difficulty
1func (d Difficulty) Initial() int
source

Initial returns the value of a challenge nobody has solved yet.

func String

method on Difficulty
1func (d Difficulty) String() string
source

String returns the human name of d.

func Valid

method on Difficulty
1func (d Difficulty) Valid() bool
source

Valid reports whether d is one of the declared levels.

type Info

struct
1type Info struct {
2	Slug       string     // unique, lowercase, 3 to 24 chars of [a-z0-9-]
3	Title      string     // display name
4	Difficulty Difficulty // starting value on the scoring curve
5	Category   string     // the Gno semantic it teaches, e.g. "render"
6	Brief      string     // the story a player reads first (markdown)
7}
source

Info describes a challenge to players and to the hall.

Methods on Info

func Validate

method on Info
1func (info Info) Validate() error
source

Validate checks that info can be listed in a hall.

type Ledger

struct
1type Ledger struct {
2	captures *avl.Tree // address -> Capture
3	order    []address // players in capture order
4}
source

Ledger records captures for one challenge.

Keep a Ledger in an unexported variable of your realm and never return a pointer to it: its methods mutate, and a method call on a leaked pointer commits under your realm's authority. (Challenge "rack" is about exactly that.)

Methods on Ledger

func Captured

method on Ledger
1func (l *Ledger) Captured(player address) bool
source

Captured reports whether player has captured.

func Count

method on Ledger
1func (l *Ledger) Count() int
source

Count returns the number of distinct players who captured.

func Get

method on Ledger
1func (l *Ledger) Get(player address) (Capture, bool)
source

Get returns the capture of player, if any.

func Recent

method on Ledger
1func (l *Ledger) Recent(n int) []Capture
source

Recent returns up to n captures, newest first.

func Record

method on Ledger
1func (l *Ledger) Record(player address) (Capture, bool)
source

Record stores a capture for player at the current height. It returns the stored capture and true, or the earlier capture and false if the player had already captured.

Dependencies

Source Files 11