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.

challenge.gno

2.10 Kb · 88 lines
 1package flag
 2
 3import (
 4	"errors"
 5	"strings"
 6)
 7
 8// Difficulty grades a challenge. It sets the starting value of the
 9// challenge on the scoring curve.
10type Difficulty int
11
12// Difficulty levels, from a first evening in the cellar to a long night.
13const (
14	Easy Difficulty = iota + 1
15	Medium
16	Hard
17)
18
19// String returns the human name of d.
20func (d Difficulty) String() string {
21	switch d {
22	case Easy:
23		return "Easy"
24	case Medium:
25		return "Medium"
26	case Hard:
27		return "Hard"
28	}
29	return "Unknown"
30}
31
32// Valid reports whether d is one of the declared levels.
33func (d Difficulty) Valid() bool { return d >= Easy && d <= Hard }
34
35// Info describes a challenge to players and to the hall.
36type Info struct {
37	Slug       string     // unique, lowercase, 3 to 24 chars of [a-z0-9-]
38	Title      string     // display name
39	Difficulty Difficulty // starting value on the scoring curve
40	Category   string     // the Gno semantic it teaches, e.g. "render"
41	Brief      string     // the story a player reads first (markdown)
42}
43
44// Challenge is what a challenge realm hands to a hall.
45//
46// Implementations live in the challenge realm, so their methods run with
47// that realm's storage. Captured must be a pure read.
48type Challenge interface {
49	Info() Info
50	Captured(player address) bool
51}
52
53// Errors returned by Validate.
54var (
55	ErrBadSlug       = errors.New("flag: slug must be 3-24 chars of [a-z0-9-]")
56	ErrNoTitle       = errors.New("flag: title is required")
57	ErrBadDifficulty = errors.New("flag: unknown difficulty")
58	ErrNoBrief       = errors.New("flag: brief is required")
59)
60
61// Validate checks that info can be listed in a hall.
62func (info Info) Validate() error {
63	if !validSlug(info.Slug) {
64		return ErrBadSlug
65	}
66	if strings.TrimSpace(info.Title) == "" {
67		return ErrNoTitle
68	}
69	if !info.Difficulty.Valid() {
70		return ErrBadDifficulty
71	}
72	if strings.TrimSpace(info.Brief) == "" {
73		return ErrNoBrief
74	}
75	return nil
76}
77
78func validSlug(s string) bool {
79	if len(s) < 3 || len(s) > 24 {
80		return false
81	}
82	for _, c := range s {
83		if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-') {
84			return false
85		}
86	}
87	return s[0] != '-' && s[len(s)-1] != '-'
88}